From dcbff85fd431773bd313043e20bf13c48be7b1ae Mon Sep 17 00:00:00 2001 From: Boyan Asenov Date: Thu, 18 Jul 2019 12:17:21 +0100 Subject: [PATCH 1/8] Refactor util functions --- .../Private/SpatialGDKEditorCloudLauncher.cpp | 3 +- .../Private/SpatialGDKEditorSettings.cpp | 29 ------ .../Public/SpatialGDKEditorSettings.h | 9 -- .../SpatialGDKSimulatedPlayerDeployment.cpp | 43 ++++----- .../SpatialGDKSimulatedPlayerDeployment.h | 3 - .../Private/LocalDeploymentManager.cpp | 93 ++----------------- .../Private/SpatialGDKServicesModule.cpp | 75 +++++++++++++++ .../Public/LocalDeploymentManager.h | 4 - .../Public/SpatialGDKServicesModule.h | 11 +++ 9 files changed, 116 insertions(+), 154 deletions(-) diff --git a/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorCloudLauncher.cpp b/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorCloudLauncher.cpp index 87ab7fc1b6..678dd4059b 100644 --- a/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorCloudLauncher.cpp +++ b/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorCloudLauncher.cpp @@ -3,6 +3,7 @@ #include "SpatialGDKEditorCloudLauncher.h" #include "Interfaces/IPluginManager.h" +#include "SpatialGDKServicesModule.h" #include "SpatialGDKEditorSettings.h" bool SpatialGDKCloudLaunch() @@ -13,7 +14,7 @@ bool SpatialGDKCloudLaunch() FString LauncherCmdArguments = FString::Printf( TEXT("/c cmd.exe /c DeploymentLauncher.exe create %s %s %s \"%s\" \"%s\" %s"), - *SpatialGDKSettings->GetProjectName(), + *FSpatialGDKServicesModule::GetProjectName(), *SpatialGDKSettings->GetAssemblyName(), *SpatialGDKSettings->GetPrimaryDeploymentName(), *SpatialGDKSettings->GetPrimaryLanchConfigPath(), diff --git a/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorSettings.cpp b/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorSettings.cpp index dc3b465adc..33b8959ec0 100644 --- a/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorSettings.cpp +++ b/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorSettings.cpp @@ -30,7 +30,6 @@ USpatialGDKEditorSettings::USpatialGDKEditorSettings(const FObjectInitializer& O { SpatialOSLaunchConfig.FilePath = GetSpatialOSLaunchConfig(); SpatialOSSnapshotFile = GetSpatialOSSnapshotFile(); - ProjectName = GetProjectNameFromSpatial(); } void USpatialGDKEditorSettings::PostEditChangeProperty(struct FPropertyChangedEvent& PropertyChangedEvent) @@ -101,27 +100,6 @@ void USpatialGDKEditorSettings::SetLevelEditorPlaySettingsWorkerTypes() } } -FString USpatialGDKEditorSettings::GetProjectNameFromSpatial() const -{ - FString FileContents; - const FString SpatialOSFile = FSpatialGDKServicesModule::GetSpatialOSDirectory().Append(TEXT("/spatialos.json")); - - if (!FFileHelper::LoadFileToString(FileContents, *SpatialOSFile)) - { - return TEXT(""); - } - - TSharedRef> Reader = TJsonReaderFactory<>::Create(FileContents); - TSharedPtr JsonObject; - - if (FJsonSerializer::Deserialize(Reader, JsonObject)) - { - return JsonObject->GetStringField("name"); - } - - return FString(); -} - bool USpatialGDKEditorSettings::IsAssemblyNameValid(const FString& Name) { const FRegexPattern AssemblyPatternRegex(SpatialConstants::AssemblyPattern); @@ -165,12 +143,6 @@ void USpatialGDKEditorSettings::SetAssemblyName(const FString& Name) SaveConfig(); } -void USpatialGDKEditorSettings::SetProjectName(const FString& Name) -{ - ProjectName = Name; - SaveConfig(); -} - void USpatialGDKEditorSettings::SetPrimaryLaunchConfigPath(const FString& Path) { PrimaryLaunchConfigPath.FilePath = FPaths::ConvertRelativePathToFull(Path); @@ -215,7 +187,6 @@ bool USpatialGDKEditorSettings::IsDeploymentConfigurationValid() const { bool result = IsAssemblyNameValid(AssemblyName) && IsDeploymentNameValid(PrimaryDeploymentName) && - IsProjectNameValid(ProjectName) && !SnapshotPath.FilePath.IsEmpty() && !PrimaryLaunchConfigPath.FilePath.IsEmpty() && IsRegionCodeValid(PrimaryDeploymentRegionCode); diff --git a/SpatialGDK/Source/SpatialGDKEditor/Public/SpatialGDKEditorSettings.h b/SpatialGDK/Source/SpatialGDKEditor/Public/SpatialGDKEditorSettings.h index 35de44d74e..860f3fd961 100644 --- a/SpatialGDK/Source/SpatialGDKEditor/Public/SpatialGDKEditorSettings.h +++ b/SpatialGDK/Source/SpatialGDKEditor/Public/SpatialGDKEditorSettings.h @@ -266,9 +266,6 @@ class SPATIALGDKEDITOR_API USpatialGDKEditorSettings : public UObject TArray SpatialOSCommandLineLaunchFlags; private: - UPROPERTY(EditAnywhere, config, Category = "Cloud", meta = (ConfigRestartRequired = false, DisplayName = "SpatialOS project")) - FString ProjectName; - UPROPERTY(EditAnywhere, config, Category = "Cloud", meta = (ConfigRestartRequired = false, DisplayName = "Assembly name")) FString AssemblyName; @@ -373,12 +370,6 @@ class SPATIALGDKEDITOR_API USpatialGDKEditorSettings : public UObject return AssemblyName; } - void SetProjectName(const FString& Name); - FORCEINLINE FString GetProjectName() const - { - return ProjectName; - } - void SetPrimaryLaunchConfigPath(const FString& Path); FORCEINLINE FString GetPrimaryLanchConfigPath() const { diff --git a/SpatialGDK/Source/SpatialGDKEditorToolbar/Private/SpatialGDKSimulatedPlayerDeployment.cpp b/SpatialGDK/Source/SpatialGDKEditorToolbar/Private/SpatialGDKSimulatedPlayerDeployment.cpp index aa4af941f8..648d6e03c4 100644 --- a/SpatialGDK/Source/SpatialGDKEditorToolbar/Private/SpatialGDKSimulatedPlayerDeployment.cpp +++ b/SpatialGDK/Source/SpatialGDKEditorToolbar/Private/SpatialGDKSimulatedPlayerDeployment.cpp @@ -10,6 +10,7 @@ #include "Framework/Notifications/NotificationManager.h" #include "Templates/SharedPointer.h" #include "SpatialGDKEditorSettings.h" +#include "SpatialGDKServicesModule.h" #include "Textures/SlateIcon.h" #include "Widgets/Input/SButton.h" #include "Widgets/Input/SComboButton.h" @@ -28,7 +29,8 @@ void SSpatialGDKSimulatedPlayerDeployment::Construct(const FArguments& InArgs) { const USpatialGDKEditorSettings* SpatialGDKSettings = GetDefault(); - + FString ProjectName = FSpatialGDKServicesModule::GetProjectName(); + ParentWindowPtr = InArgs._ParentWindow; SpatialGDKEditorPtr = InArgs._SpatialGDKEditor; @@ -89,23 +91,22 @@ void SSpatialGDKSimulatedPlayerDeployment::Construct(const FArguments& InArgs) .AutoHeight() .Padding(2.0f) [ - SNew(SHorizontalBox) - + SHorizontalBox::Slot() - .FillWidth(1.0f) - [ - SNew(STextBlock) - .Text(FText::FromString(FString(TEXT("Project Name")))) - .ToolTipText(FText::FromString(FString(TEXT("The name of the SpatialOS project.")))) - ] - + SHorizontalBox::Slot() - .FillWidth(1.0f) - [ - SNew(SEditableTextBox) - .Text(FText::FromString(SpatialGDKSettings->GetProjectName())) - .ToolTipText(FText::FromString(FString(TEXT("The name of the SpatialOS project.")))) - .OnTextCommitted(this, &SSpatialGDKSimulatedPlayerDeployment::OnProjectNameCommited) - .OnTextChanged(this, &SSpatialGDKSimulatedPlayerDeployment::OnProjectNameCommited, ETextCommit::Default) - ] + SNew(SHorizontalBox) + + SHorizontalBox::Slot() + .FillWidth(1.0f) + [ + SNew(STextBlock) + .Text(FText::FromString(FString(TEXT("Project Name")))) + .ToolTipText(FText::FromString(FString(TEXT("The name of the SpatialOS project.")))) + ] + + SHorizontalBox::Slot() + .FillWidth(1.0f) + [ + SNew(SEditableTextBox) + .Text(FText::FromString(ProjectName)) + .ToolTipText(FText::FromString(FString(TEXT("The name of the SpatialOS project.")))) + .IsEnabled(false) + ] ] // Assembly Name + SVerticalBox::Slot() @@ -397,12 +398,6 @@ void SSpatialGDKSimulatedPlayerDeployment::OnDeploymentAssemblyCommited(const FT SpatialGDKSettings->SetAssemblyName(InText.ToString()); } -void SSpatialGDKSimulatedPlayerDeployment::OnProjectNameCommited(const FText& InText, ETextCommit::Type InCommitType) -{ - USpatialGDKEditorSettings* SpatialGDKSettings = GetMutableDefault(); - SpatialGDKSettings->SetProjectName(InText.ToString()); -} - void SSpatialGDKSimulatedPlayerDeployment::OnPrimaryDeploymentNameCommited(const FText& InText, ETextCommit::Type InCommitType) { USpatialGDKEditorSettings* SpatialGDKSettings = GetMutableDefault(); diff --git a/SpatialGDK/Source/SpatialGDKEditorToolbar/Public/SpatialGDKSimulatedPlayerDeployment.h b/SpatialGDK/Source/SpatialGDKEditorToolbar/Public/SpatialGDKSimulatedPlayerDeployment.h index 2f7cad9f10..1abfea0f6b 100644 --- a/SpatialGDK/Source/SpatialGDKEditorToolbar/Public/SpatialGDKSimulatedPlayerDeployment.h +++ b/SpatialGDK/Source/SpatialGDKEditorToolbar/Public/SpatialGDKSimulatedPlayerDeployment.h @@ -43,9 +43,6 @@ class SSpatialGDKSimulatedPlayerDeployment : public SCompoundWidget /** Delegate to commit assembly name */ void OnDeploymentAssemblyCommited(const FText& InText, ETextCommit::Type InCommitType); - /** Delegate to commit project name */ - void OnProjectNameCommited(const FText& InText, ETextCommit::Type InCommitType); - /** Delegate to commit primary deployment name */ void OnPrimaryDeploymentNameCommited(const FText& InText, ETextCommit::Type InCommitType); diff --git a/SpatialGDK/Source/SpatialGDKServices/Private/LocalDeploymentManager.cpp b/SpatialGDK/Source/SpatialGDKServices/Private/LocalDeploymentManager.cpp index 52b4d642f9..5d3ed7f887 100644 --- a/SpatialGDK/Source/SpatialGDKServices/Private/LocalDeploymentManager.cpp +++ b/SpatialGDK/Source/SpatialGDKServices/Private/LocalDeploymentManager.cpp @@ -31,9 +31,6 @@ FLocalDeploymentManager::FLocalDeploymentManager() , bStartingSpatialService(false) , bStoppingSpatialService(false) { - // Get the project name from the spatialos.json. - ProjectName = GetProjectName(); - #if PLATFORM_WINDOWS // Don't kick off background processes when running commandlets if (IsRunningCommandlet() == false) @@ -89,35 +86,6 @@ void FLocalDeploymentManager::OnWorkerConfigDirectoryChanged(const TArray JsonParsedSpatialFile; - if (ParseJson(SpatialFileResult, JsonParsedSpatialFile)) - { - if (JsonParsedSpatialFile->TryGetStringField(TEXT("name"), ProjectName)) - { - return ProjectName; - } - else - { - UE_LOG(LogSpatialDeploymentManager, Error, TEXT("'name' does not exist in spatialos.json. Can't read project name.")); - } - } - else - { - UE_LOG(LogSpatialDeploymentManager, Error, TEXT("Json parsing of spatialos.json failed. Can't get project name.")); - } - - ProjectName.Empty(); - return ProjectName; -} - void FLocalDeploymentManager::WorkerBuildConfigAsync() { AsyncTask(ENamedThreads::AnyBackgroundThreadNormalTask, [this] @@ -125,7 +93,7 @@ void FLocalDeploymentManager::WorkerBuildConfigAsync() FString BuildConfigArgs = TEXT("worker build build-config"); FString WorkerBuildConfigResult; int32 ExitCode; - ExecuteAndReadOutput(SpatialExe, BuildConfigArgs, FSpatialGDKServicesModule::GetSpatialOSDirectory(), WorkerBuildConfigResult, ExitCode); + FSpatialGDKServicesModule::ExecuteAndReadOutput(SpatialExe, BuildConfigArgs, FSpatialGDKServicesModule::GetSpatialOSDirectory(), WorkerBuildConfigResult, ExitCode); if (ExitCode == ExitCodeSuccess) { @@ -138,49 +106,6 @@ void FLocalDeploymentManager::WorkerBuildConfigAsync() }); } -bool FLocalDeploymentManager::ParseJson(const FString& RawJsonString, TSharedPtr& JsonParsed) -{ - TSharedRef> JsonReader = TJsonReaderFactory::Create(RawJsonString); - return FJsonSerializer::Deserialize(JsonReader, JsonParsed); -} - -// ExecuteAndReadOutput exists so that a spatial command window does not spawn when using 'spatial.exe'. It does not however allow reading from StdErr. -// For other processes which do not spawn cmd windows, use ExecProcess instead. -void FLocalDeploymentManager::ExecuteAndReadOutput(const FString& Executable, const FString& Arguments, const FString& DirectoryToRun, FString& OutResult, int32& ExitCode) -{ -#if PLATFORM_WINDOWS - UE_LOG(LogSpatialDeploymentManager, Verbose, TEXT("Executing '%s' with arguments '%s' in directory '%s'"), *Executable, *Arguments, *DirectoryToRun); - - void* ReadPipe = nullptr; - void* WritePipe = nullptr; - ensure(FPlatformProcess::CreatePipe(ReadPipe, WritePipe)); - - FProcHandle ProcHandle = FPlatformProcess::CreateProc(*Executable, *Arguments, false, true, true, nullptr, 1 /*PriorityModifer*/, *DirectoryToRun, WritePipe); - - if (ProcHandle.IsValid()) - { - for (bool bProcessFinished = false; !bProcessFinished; ) - { - bProcessFinished = FPlatformProcess::GetProcReturnCode(ProcHandle, &ExitCode); - - OutResult = OutResult.Append(FPlatformProcess::ReadPipe(ReadPipe)); - FPlatformProcess::Sleep(0.01f); - } - - FPlatformProcess::CloseProc(ProcHandle); - } - else - { - UE_LOG(LogSpatialDeploymentManager, Error, TEXT("Execution failed. '%s' with arguments '%s' in directory '%s' "), *Executable, *Arguments, *DirectoryToRun); - } - - FPlatformProcess::ClosePipe(0, ReadPipe); - FPlatformProcess::ClosePipe(0, WritePipe); -#else - ExitCode = 1; -#endif -} - void FLocalDeploymentManager::RefreshServiceStatus() { AsyncTask(ENamedThreads::AnyBackgroundThreadNormalTask, [this] @@ -234,7 +159,7 @@ bool FLocalDeploymentManager::TryStartLocalDeployment(FString LaunchConfig, FStr TryStartSpatialService(); } - FString SpotCreateArgs = FString::Printf(TEXT("alpha deployment create --launch-config=\"%s\" --name=localdeployment --project-name=%s --json %s"), *LaunchConfig, *ProjectName, *LaunchArgs); + FString SpotCreateArgs = FString::Printf(TEXT("alpha deployment create --launch-config=\"%s\" --name=localdeployment --project-name=%s --json %s"), *LaunchConfig, *FSpatialGDKServicesModule::GetProjectName(), *LaunchArgs); FDateTime SpotCreateStart = FDateTime::Now(); @@ -253,7 +178,7 @@ bool FLocalDeploymentManager::TryStartLocalDeployment(FString LaunchConfig, FStr bool bSuccess = false; TSharedPtr SpotJsonResult; - bool bParsingSuccess = ParseJson(SpotCreateResult, SpotJsonResult); + bool bParsingSuccess = FSpatialGDKServicesModule::ParseJson(SpotCreateResult, SpotJsonResult); if (!bParsingSuccess) { UE_LOG(LogSpatialDeploymentManager, Error, TEXT("Json parsing of spot create result failed. Result: %s"), *SpotCreateResult); @@ -322,7 +247,7 @@ bool FLocalDeploymentManager::TryStopLocalDeployment() bool bSuccess = false; TSharedPtr SpotJsonResult; - bool bPasingSuccess = ParseJson(SpotDeleteResult, SpotJsonResult); + bool bPasingSuccess = FSpatialGDKServicesModule::ParseJson(SpotDeleteResult, SpotJsonResult); if (!bPasingSuccess) { UE_LOG(LogSpatialDeploymentManager, Error, TEXT("Json parsing of spot delete result failed. Result: %s"), *SpotDeleteResult); @@ -371,7 +296,7 @@ bool FLocalDeploymentManager::TryStartSpatialService() FString SpatialServiceStartArgs = FString::Printf(TEXT("service start --version=%s"), *SpatialServiceVersion); FString ServiceStartResult; int32 ExitCode; - ExecuteAndReadOutput(SpatialExe, SpatialServiceStartArgs, FSpatialGDKServicesModule::GetSpatialOSDirectory(), ServiceStartResult, ExitCode); + FSpatialGDKServicesModule::ExecuteAndReadOutput(SpatialExe, SpatialServiceStartArgs, FSpatialGDKServicesModule::GetSpatialOSDirectory(), ServiceStartResult, ExitCode); bStartingSpatialService = false; @@ -403,7 +328,7 @@ bool FLocalDeploymentManager::TryStopSpatialService() FString SpatialServiceStartArgs = TEXT("service stop"); FString ServiceStopResult; int32 ExitCode; - ExecuteAndReadOutput(SpatialExe, SpatialServiceStartArgs, FSpatialGDKServicesModule::GetSpatialOSDirectory(), ServiceStopResult, ExitCode); + FSpatialGDKServicesModule::ExecuteAndReadOutput(SpatialExe, SpatialServiceStartArgs, FSpatialGDKServicesModule::GetSpatialOSDirectory(), ServiceStopResult, ExitCode); bStoppingSpatialService = false; if (ExitCode == ExitCodeSuccess) @@ -430,7 +355,7 @@ bool FLocalDeploymentManager::GetLocalDeploymentStatus() return bLocalDeploymentRunning; } - FString SpotListArgs = FString::Printf(TEXT("alpha deployment list --project-name=%s --json --view BASIC --status-filter NOT_STOPPED_DEPLOYMENTS"), *ProjectName); + FString SpotListArgs = FString::Printf(TEXT("alpha deployment list --project-name=%s --json --view BASIC --status-filter NOT_STOPPED_DEPLOYMENTS"), *FSpatialGDKServicesModule::GetProjectName()); FString SpotListResult; FString StdErr; @@ -444,7 +369,7 @@ bool FLocalDeploymentManager::GetLocalDeploymentStatus() } TSharedPtr SpotJsonResult; - bool bPasingSuccess = ParseJson(SpotListResult, SpotJsonResult); + bool bPasingSuccess = FSpatialGDKServicesModule::ParseJson(SpotListResult, SpotJsonResult); if (!bPasingSuccess) { UE_LOG(LogSpatialDeploymentManager, Error, TEXT("Json parsing of spot list result failed. Result: %s"), *SpotListResult); @@ -493,7 +418,7 @@ bool FLocalDeploymentManager::GetServiceStatus() FString SpatialServiceStatusArgs = TEXT("service status"); FString ServiceStatusResult; int32 ExitCode; - ExecuteAndReadOutput(SpatialExe, SpatialServiceStatusArgs, FSpatialGDKServicesModule::GetSpatialOSDirectory(), ServiceStatusResult, ExitCode); + FSpatialGDKServicesModule::ExecuteAndReadOutput(SpatialExe, SpatialServiceStatusArgs, FSpatialGDKServicesModule::GetSpatialOSDirectory(), ServiceStatusResult, ExitCode); if (ExitCode != ExitCodeSuccess) { diff --git a/SpatialGDK/Source/SpatialGDKServices/Private/SpatialGDKServicesModule.cpp b/SpatialGDK/Source/SpatialGDKServices/Private/SpatialGDKServicesModule.cpp index c1478c869e..795c23ba35 100644 --- a/SpatialGDK/Source/SpatialGDKServices/Private/SpatialGDKServicesModule.cpp +++ b/SpatialGDK/Source/SpatialGDKServices/Private/SpatialGDKServicesModule.cpp @@ -2,6 +2,9 @@ #include "SpatialGDKServicesModule.h" +#include "Misc/FileHelper.h" +#include "Serialization/JsonReader.h" +#include "Serialization/JsonSerializer.h" #include "SpatialGDKServicesPrivate.h" #define LOCTEXT_NAMESPACE "FSpatialGDKServicesModule" @@ -10,6 +13,7 @@ DEFINE_LOG_CATEGORY(LogSpatialGDKServices); IMPLEMENT_MODULE(FSpatialGDKServicesModule, SpatialGDKServices); + void FSpatialGDKServicesModule::StartupModule() { } @@ -18,6 +22,8 @@ void FSpatialGDKServicesModule::ShutdownModule() { } +FString FSpatialGDKServicesModule::ProjectName = FSpatialGDKServicesModule::ParseProjectName(); + FLocalDeploymentManager* FSpatialGDKServicesModule::GetLocalDeploymentManager() { return &LocalDeploymentManager; @@ -42,4 +48,73 @@ FString FSpatialGDKServicesModule::GetSpatialGDKPluginDirectory(const FString& A return FPaths::ConvertRelativePathToFull(FPaths::Combine(PluginDir, AppendPath)); } +bool FSpatialGDKServicesModule::ParseJson(const FString& RawJsonString, TSharedPtr& JsonParsed) +{ + TSharedRef> JsonReader = TJsonReaderFactory::Create(RawJsonString); + return FJsonSerializer::Deserialize(JsonReader, JsonParsed); +} + +// ExecuteAndReadOutput exists so that a spatial command window does not spawn when using 'spatial.exe'. It does not however allow reading from StdErr. +// For other processes which do not spawn cmd windows, use ExecProcess instead. +void FSpatialGDKServicesModule::ExecuteAndReadOutput(const FString& Executable, const FString& Arguments, const FString& DirectoryToRun, FString& OutResult, int32& ExitCode) +{ + UE_LOG(LogSpatialDeploymentManager, Verbose, TEXT("Executing '%s' with arguments '%s' in directory '%s'"), *Executable, *Arguments, *DirectoryToRun); + + void* ReadPipe = nullptr; + void* WritePipe = nullptr; + ensure(FPlatformProcess::CreatePipe(ReadPipe, WritePipe)); + + FProcHandle ProcHandle = FPlatformProcess::CreateProc(*Executable, *Arguments, false, true, true, nullptr, 1 /*PriorityModifer*/, *DirectoryToRun, WritePipe); + + if (ProcHandle.IsValid()) + { + for (bool bProcessFinished = false; !bProcessFinished; ) + { + bProcessFinished = FPlatformProcess::GetProcReturnCode(ProcHandle, &ExitCode); + + OutResult = OutResult.Append(FPlatformProcess::ReadPipe(ReadPipe)); + FPlatformProcess::Sleep(0.01f); + } + + FPlatformProcess::CloseProc(ProcHandle); + } + else + { + UE_LOG(LogSpatialDeploymentManager, Error, TEXT("Execution failed. '%s' with arguments '%s' in directory '%s' "), *Executable, *Arguments, *DirectoryToRun); + } + + FPlatformProcess::ClosePipe(0, ReadPipe); + FPlatformProcess::ClosePipe(0, WritePipe); +} + +FString FSpatialGDKServicesModule::ParseProjectName() +{ + FString ProjectNameParsed; + const FString SpatialDirectory = FSpatialGDKServicesModule::GetSpatialOSDirectory(); + + FString SpatialFileName = TEXT("spatialos.json"); + FString SpatialFileResult; + FFileHelper::LoadFileToString(SpatialFileResult, *FPaths::Combine(SpatialDirectory, SpatialFileName)); + + TSharedPtr JsonParsedSpatialFile; + if (ParseJson(SpatialFileResult, JsonParsedSpatialFile)) + { + if (JsonParsedSpatialFile->TryGetStringField(TEXT("name"), ProjectNameParsed)) + { + return ProjectNameParsed; + } + else + { + UE_LOG(LogSpatialDeploymentManager, Error, TEXT("'name' does not exist in spatialos.json. Can't read project name.")); + } + } + else + { + UE_LOG(LogSpatialDeploymentManager, Error, TEXT("Json parsing of spatialos.json failed. Can't get project name.")); + } + + ProjectNameParsed.Empty(); + return ProjectNameParsed; +} + #undef LOCTEXT_NAMESPACE diff --git a/SpatialGDK/Source/SpatialGDKServices/Public/LocalDeploymentManager.h b/SpatialGDK/Source/SpatialGDKServices/Public/LocalDeploymentManager.h index 936838b2da..9ebcf5e3a7 100644 --- a/SpatialGDK/Source/SpatialGDKServices/Public/LocalDeploymentManager.h +++ b/SpatialGDK/Source/SpatialGDKServices/Public/LocalDeploymentManager.h @@ -45,11 +45,7 @@ class FLocalDeploymentManager void SPATIALGDKSERVICES_API SetAutoDeploy(bool bAutoDeploy); - // TODO: Refactor these into Utils - FString GetProjectName(); void WorkerBuildConfigAsync(); - bool ParseJson(const FString& RawJsonString, TSharedPtr& JsonParsed); - void ExecuteAndReadOutput(const FString& Executable, const FString& Arguments, const FString& DirectoryToRun, FString& OutResult, int32& ExitCode); const FString GetSpotExe(); FSimpleMulticastDelegate OnSpatialShutdown; diff --git a/SpatialGDK/Source/SpatialGDKServices/Public/SpatialGDKServicesModule.h b/SpatialGDK/Source/SpatialGDKServices/Public/SpatialGDKServicesModule.h index bc35e103fe..bc970eb681 100644 --- a/SpatialGDK/Source/SpatialGDKServices/Public/SpatialGDKServicesModule.h +++ b/SpatialGDK/Source/SpatialGDKServices/Public/SpatialGDKServicesModule.h @@ -8,6 +8,8 @@ class SPATIALGDKSERVICES_API FSpatialGDKServicesModule : public IModuleInterface { public: + static FString ProjectName; + virtual void StartupModule() override; virtual void ShutdownModule() override; @@ -21,6 +23,15 @@ class SPATIALGDKSERVICES_API FSpatialGDKServicesModule : public IModuleInterface static FString GetSpatialOSDirectory(const FString& AppendPath = TEXT("")); static FString GetSpatialGDKPluginDirectory(const FString& AppendPath = TEXT("")); + FORCEINLINE static FString GetProjectName() + { + return ProjectName; + } + + static bool ParseJson(const FString& RawJsonString, TSharedPtr& JsonParsed); + static void ExecuteAndReadOutput(const FString& Executable, const FString& Arguments, const FString& DirectoryToRun, FString& OutResult, int32& ExitCode); + private: FLocalDeploymentManager LocalDeploymentManager; + static FString ParseProjectName(); }; From c8afb3af008f74d3539cc94dd7c5624f09c39c74 Mon Sep 17 00:00:00 2001 From: Boyan Asenov Date: Thu, 18 Jul 2019 13:24:59 +0100 Subject: [PATCH 2/8] Order imporrts --- .../SpatialGDKEditor/Private/SpatialGDKEditorCloudLauncher.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorCloudLauncher.cpp b/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorCloudLauncher.cpp index 678dd4059b..04f2308971 100644 --- a/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorCloudLauncher.cpp +++ b/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorCloudLauncher.cpp @@ -3,8 +3,8 @@ #include "SpatialGDKEditorCloudLauncher.h" #include "Interfaces/IPluginManager.h" -#include "SpatialGDKServicesModule.h" #include "SpatialGDKEditorSettings.h" +#include "SpatialGDKServicesModule.h" bool SpatialGDKCloudLaunch() { From d1200c60aed92e60f54eb8ac342dedee19c00c62 Mon Sep 17 00:00:00 2001 From: Boyan Asenov Date: Thu, 18 Jul 2019 14:29:07 +0100 Subject: [PATCH 3/8] Fix tabs --- .../SpatialGDKSimulatedPlayerDeployment.cpp | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/SpatialGDK/Source/SpatialGDKEditorToolbar/Private/SpatialGDKSimulatedPlayerDeployment.cpp b/SpatialGDK/Source/SpatialGDKEditorToolbar/Private/SpatialGDKSimulatedPlayerDeployment.cpp index 648d6e03c4..70f76629fb 100644 --- a/SpatialGDK/Source/SpatialGDKEditorToolbar/Private/SpatialGDKSimulatedPlayerDeployment.cpp +++ b/SpatialGDK/Source/SpatialGDKEditorToolbar/Private/SpatialGDKSimulatedPlayerDeployment.cpp @@ -91,22 +91,21 @@ void SSpatialGDKSimulatedPlayerDeployment::Construct(const FArguments& InArgs) .AutoHeight() .Padding(2.0f) [ - SNew(SHorizontalBox) - + SHorizontalBox::Slot() - .FillWidth(1.0f) - [ - SNew(STextBlock) - .Text(FText::FromString(FString(TEXT("Project Name")))) - .ToolTipText(FText::FromString(FString(TEXT("The name of the SpatialOS project.")))) - ] - + SHorizontalBox::Slot() - .FillWidth(1.0f) - [ - SNew(SEditableTextBox) - .Text(FText::FromString(ProjectName)) - .ToolTipText(FText::FromString(FString(TEXT("The name of the SpatialOS project.")))) - .IsEnabled(false) - ] + SNew(SHorizontalBox) + + SHorizontalBox::Slot() + .FillWidth(1.0f) + [ + SNew(STextBlock) + .Text(FText::FromString(FString(TEXT("Project Name")))) + .ToolTipText(FText::FromString(FString(TEXT("The name of the SpatialOS project.")))) + ] + + SHorizontalBox::Slot() + .FillWidth(1.0f) + [ SNew(SEditableTextBox) + .Text(FText::FromString(ProjectName)) + .ToolTipText(FText::FromString(FString(TEXT("The name of the SpatialOS project.")))) + .IsEnabled(false) + ] ] // Assembly Name + SVerticalBox::Slot() From ad2009a74eb9c82b57c2b4d228311a6861a3230f Mon Sep 17 00:00:00 2001 From: Boyan Asenov Date: Tue, 23 Jul 2019 15:22:20 +0100 Subject: [PATCH 4/8] Fix typo in bPasingSuccess --- .../SpatialGDKServices/Private/LocalDeploymentManager.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SpatialGDK/Source/SpatialGDKServices/Private/LocalDeploymentManager.cpp b/SpatialGDK/Source/SpatialGDKServices/Private/LocalDeploymentManager.cpp index 5d3ed7f887..14d444fb5d 100644 --- a/SpatialGDK/Source/SpatialGDKServices/Private/LocalDeploymentManager.cpp +++ b/SpatialGDK/Source/SpatialGDKServices/Private/LocalDeploymentManager.cpp @@ -369,14 +369,14 @@ bool FLocalDeploymentManager::GetLocalDeploymentStatus() } TSharedPtr SpotJsonResult; - bool bPasingSuccess = FSpatialGDKServicesModule::ParseJson(SpotListResult, SpotJsonResult); - if (!bPasingSuccess) + bool bParsingSuccess = FSpatialGDKServicesModule::ParseJson(SpotListResult, SpotJsonResult); + if (!bParsingSuccess) { UE_LOG(LogSpatialDeploymentManager, Error, TEXT("Json parsing of spot list result failed. Result: %s"), *SpotListResult); } const TSharedPtr* SpotJsonContent = nullptr; - if (bPasingSuccess && SpotJsonResult->TryGetObjectField(TEXT("content"), SpotJsonContent)) + if (bParsingSuccess && SpotJsonResult->TryGetObjectField(TEXT("content"), SpotJsonContent)) { const TArray>* JsonDeployments; if (!SpotJsonContent->Get()->TryGetArrayField(TEXT("deployments"), JsonDeployments)) From e8ac8f322c60b769a76f96e7af9653a4c3eab8ac Mon Sep 17 00:00:00 2001 From: Boyan Asenov Date: Tue, 23 Jul 2019 15:24:00 +0100 Subject: [PATCH 5/8] Fix weird spacing --- .../Private/SpatialGDKSimulatedPlayerDeployment.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SpatialGDK/Source/SpatialGDKEditorToolbar/Private/SpatialGDKSimulatedPlayerDeployment.cpp b/SpatialGDK/Source/SpatialGDKEditorToolbar/Private/SpatialGDKSimulatedPlayerDeployment.cpp index 70f76629fb..0b20335b7c 100644 --- a/SpatialGDK/Source/SpatialGDKEditorToolbar/Private/SpatialGDKSimulatedPlayerDeployment.cpp +++ b/SpatialGDK/Source/SpatialGDKEditorToolbar/Private/SpatialGDKSimulatedPlayerDeployment.cpp @@ -101,7 +101,8 @@ void SSpatialGDKSimulatedPlayerDeployment::Construct(const FArguments& InArgs) ] + SHorizontalBox::Slot() .FillWidth(1.0f) - [ SNew(SEditableTextBox) + [ + SNew(SEditableTextBox) .Text(FText::FromString(ProjectName)) .ToolTipText(FText::FromString(FString(TEXT("The name of the SpatialOS project.")))) .IsEnabled(false) From f904c0057f4546f134392f80af38fd1437832b2c Mon Sep 17 00:00:00 2001 From: Boyan Asenov Date: Tue, 23 Jul 2019 15:27:36 +0100 Subject: [PATCH 6/8] Fix log category --- .../Private/SpatialGDKServicesModule.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SpatialGDK/Source/SpatialGDKServices/Private/SpatialGDKServicesModule.cpp b/SpatialGDK/Source/SpatialGDKServices/Private/SpatialGDKServicesModule.cpp index 795c23ba35..36758c0e48 100644 --- a/SpatialGDK/Source/SpatialGDKServices/Private/SpatialGDKServicesModule.cpp +++ b/SpatialGDK/Source/SpatialGDKServices/Private/SpatialGDKServicesModule.cpp @@ -58,7 +58,7 @@ bool FSpatialGDKServicesModule::ParseJson(const FString& RawJsonString, TSharedP // For other processes which do not spawn cmd windows, use ExecProcess instead. void FSpatialGDKServicesModule::ExecuteAndReadOutput(const FString& Executable, const FString& Arguments, const FString& DirectoryToRun, FString& OutResult, int32& ExitCode) { - UE_LOG(LogSpatialDeploymentManager, Verbose, TEXT("Executing '%s' with arguments '%s' in directory '%s'"), *Executable, *Arguments, *DirectoryToRun); + UE_LOG(LogSpatialGDKServices, Verbose, TEXT("Executing '%s' with arguments '%s' in directory '%s'"), *Executable, *Arguments, *DirectoryToRun); void* ReadPipe = nullptr; void* WritePipe = nullptr; @@ -80,7 +80,7 @@ void FSpatialGDKServicesModule::ExecuteAndReadOutput(const FString& Executable, } else { - UE_LOG(LogSpatialDeploymentManager, Error, TEXT("Execution failed. '%s' with arguments '%s' in directory '%s' "), *Executable, *Arguments, *DirectoryToRun); + UE_LOG(LogSpatialGDKServices, Error, TEXT("Execution failed. '%s' with arguments '%s' in directory '%s' "), *Executable, *Arguments, *DirectoryToRun); } FPlatformProcess::ClosePipe(0, ReadPipe); @@ -105,12 +105,12 @@ FString FSpatialGDKServicesModule::ParseProjectName() } else { - UE_LOG(LogSpatialDeploymentManager, Error, TEXT("'name' does not exist in spatialos.json. Can't read project name.")); + UE_LOG(LogSpatialGDKServices, Error, TEXT("'name' does not exist in spatialos.json. Can't read project name.")); } } else { - UE_LOG(LogSpatialDeploymentManager, Error, TEXT("Json parsing of spatialos.json failed. Can't get project name.")); + UE_LOG(LogSpatialGDKServices, Error, TEXT("Json parsing of spatialos.json failed. Can't get project name.")); } ProjectNameParsed.Empty(); From e9519bf3f9e66962659d370fe141e2419fc6a27c Mon Sep 17 00:00:00 2001 From: Boyan Asenov Date: Tue, 23 Jul 2019 15:43:41 +0100 Subject: [PATCH 7/8] Remove unused imports --- .../SpatialGDKEditor/Private/SpatialGDKEditorSettings.cpp | 4 ---- .../SpatialGDKServices/Private/LocalDeploymentManager.cpp | 7 +------ 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorSettings.cpp b/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorSettings.cpp index 33b8959ec0..498f7e751e 100644 --- a/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorSettings.cpp +++ b/SpatialGDK/Source/SpatialGDKEditor/Private/SpatialGDKEditorSettings.cpp @@ -2,14 +2,10 @@ #include "SpatialGDKEditorSettings.h" -#include "Dom/JsonObject.h" #include "Internationalization/Regex.h" #include "ISettingsModule.h" -#include "Misc/FileHelper.h" #include "Misc/MessageDialog.h" #include "Modules/ModuleManager.h" -#include "Serialization/JsonReader.h" -#include "Serialization/JsonSerializer.h" #include "Settings/LevelEditorPlaySettings.h" #include "Templates/SharedPointer.h" #include "SpatialConstants.h" diff --git a/SpatialGDK/Source/SpatialGDKServices/Private/LocalDeploymentManager.cpp b/SpatialGDK/Source/SpatialGDKServices/Private/LocalDeploymentManager.cpp index 14d444fb5d..db9976e0bd 100644 --- a/SpatialGDK/Source/SpatialGDKServices/Private/LocalDeploymentManager.cpp +++ b/SpatialGDK/Source/SpatialGDKServices/Private/LocalDeploymentManager.cpp @@ -8,13 +8,8 @@ #include "Editor.h" #include "FileCache.h" #include "GeneralProjectSettings.h" -#include "HAL/FileManager.h" -#include "HAL/PlatformFilemanager.h" #include "Interop/Connection/EditorWorkerController.h" -#include "Misc/FileHelper.h" -#include "Serialization/JsonReader.h" -#include "Serialization/JsonSerializer.h" -#include "Serialization/JsonWriter.h" +#include "Json/Public/Dom/JsonObject.h" #include "SpatialGDKServicesModule.h" DEFINE_LOG_CATEGORY(LogSpatialDeploymentManager); From 8820c5a46b6fb4906e2629418df693dbdee5f1da Mon Sep 17 00:00:00 2001 From: Boyan Asenov Date: Tue, 30 Jul 2019 15:52:39 +0100 Subject: [PATCH 8/8] Remove unused project name --- .../Source/SpatialGDKServices/Public/LocalDeploymentManager.h | 1 - 1 file changed, 1 deletion(-) diff --git a/SpatialGDK/Source/SpatialGDKServices/Public/LocalDeploymentManager.h b/SpatialGDK/Source/SpatialGDKServices/Public/LocalDeploymentManager.h index 9ebcf5e3a7..267a2e510b 100644 --- a/SpatialGDK/Source/SpatialGDKServices/Public/LocalDeploymentManager.h +++ b/SpatialGDK/Source/SpatialGDKServices/Public/LocalDeploymentManager.h @@ -75,7 +75,6 @@ class FLocalDeploymentManager bool bStoppingSpatialService; FString LocalRunningDeploymentID; - FString ProjectName; bool bRedeployRequired = false; bool bAutoDeploy = false;