33#include " CheckAppInstanceBPLibrary.h"
44#include " CheckAppInstance.h"
55
6+ DEFINE_LOG_CATEGORY (LogCheckAppInstanceBPLibrary)
7+
68UCheckAppInstanceBPLibrary::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
0 commit comments