Skip to content

Commit 2b897ff

Browse files
committed
Fixed lock file for Mac
1 parent d7b63a4 commit 2b897ff

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Source/CheckAppInstance/Private/CheckAppInstanceBPLibrary.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "CheckAppInstanceBPLibrary.h"
44
#include "CheckAppInstance.h"
55

6+
DEFINE_LOG_CATEGORY(LogCheckAppInstanceBPLibrary)
7+
68
UCheckAppInstanceBPLibrary::UCheckAppInstanceBPLibrary(const FObjectInitializer& ObjectInitializer)
79
: Super(ObjectInitializer)
810
{
@@ -18,17 +20,41 @@ void UCheckAppInstanceBPLibrary::CheckAnotherAppInstance(bool bIsEnabled)
1820
bIsEnabled = ParseValue.ToBool();
1921
}
2022

23+
if(!bIsEnabled)
24+
{
25+
return;
26+
}
27+
2128
FCoreDelegates::OnAllModuleLoadingPhasesComplete.AddLambda([bIsEnabled]()
2229
{
2330
// Only one instance of the game can be initialized!
2431
const UGeneralProjectSettings& ProjectSettings = *GetDefault<UGeneralProjectSettings>();
2532
const FString LockFilePath = FPlatformProcess::UserTempDir() + ProjectSettings.ProjectID.ToString();
26-
if (!IFileManager::Get().CreateFileWriter(*LockFilePath, 0) && bIsEnabled)
33+
bool bIsAppInstLaunched = false;
34+
35+
#if PLATFORM_WINDOWS
36+
bIsAppInstLaunched = (IFileManager::Get().CreateFileWriter(*LockFilePath, 0) == nullptr ? true : false);
37+
#else
38+
int FileDescriptor = open(TCHAR_TO_UTF8(*LockFilePath), O_CREAT | O_RDWR, 0666); // 0644
39+
if(FileDescriptor >= 0)
40+
{
41+
int CodeFlock = flock(FileDescriptor, LOCK_EX | LOCK_NB);
42+
if (CodeFlock && errno == EWOULDBLOCK)
43+
{
44+
flock(FileDescriptor, LOCK_UN);
45+
close(FileDescriptor);
46+
47+
bIsAppInstLaunched = true;
48+
}
49+
}
50+
#endif
51+
UE_LOG(LogCheckAppInstanceBPLibrary, Log, TEXT("LockFilePath: %s, bIsAppInstLaunched: %s"), *LockFilePath, bIsAppInstLaunched ? TEXT("true") : TEXT("false"));
52+
if (bIsAppInstLaunched)
2753
{
2854
FPlatformApplicationMisc::RequestMinimize();
2955
FPlatformMisc::RequestExit(0);
3056
}
3157
});
32-
#endif
58+
#endif // !UE_EDITOR
3359
}
3460

Source/CheckAppInstance/Public/CheckAppInstanceBPLibrary.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@
88
#include "GeneralProjectSettings.h"
99
#include "Misc/CoreDelegates.h"
1010
#include "Misc/CommandLine.h"
11+
12+
#if PLATFORM_UNIX
13+
#include <unistd.h> // close
14+
#include <fcntl.h> // open
15+
#include <sys/file.h> // flock
16+
#include <errno.h>
17+
#endif
18+
1119
#include "CheckAppInstanceBPLibrary.generated.h"
1220

21+
DECLARE_LOG_CATEGORY_EXTERN(LogCheckAppInstanceBPLibrary, Log, All);
22+
1323
UCLASS()
1424
class UCheckAppInstanceBPLibrary : public UBlueprintFunctionLibrary
1525
{

0 commit comments

Comments
 (0)