Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Feature/refactor spatialos project settings merge (#1279)
Browse files Browse the repository at this point in the history
* Refactor util functions

* Order imporrts

* Fix tabs

* Fix typo in bPasingSuccess

* Fix weird spacing

* Fix log category

* Remove unused imports

* Remove unused project name
  • Loading branch information
joshuahuburn authored Aug 12, 2019
1 parent 3fc0050 commit 288e1ce
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "Interfaces/IPluginManager.h"
#include "SpatialGDKEditorSettings.h"
#include "SpatialGDKServicesModule.h"

bool SpatialGDKCloudLaunch()
{
Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,7 +26,6 @@ USpatialGDKEditorSettings::USpatialGDKEditorSettings(const FObjectInitializer& O
{
SpatialOSLaunchConfig.FilePath = GetSpatialOSLaunchConfig();
SpatialOSSnapshotFile = GetSpatialOSSnapshotFile();
ProjectName = GetProjectNameFromSpatial();
}

void USpatialGDKEditorSettings::PostEditChangeProperty(struct FPropertyChangedEvent& PropertyChangedEvent)
Expand Down Expand Up @@ -101,27 +96,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<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(FileContents);
TSharedPtr<FJsonObject> JsonObject;

if (FJsonSerializer::Deserialize(Reader, JsonObject))
{
return JsonObject->GetStringField("name");
}

return FString();
}

bool USpatialGDKEditorSettings::IsAssemblyNameValid(const FString& Name)
{
const FRegexPattern AssemblyPatternRegex(SpatialConstants::AssemblyPattern);
Expand Down Expand Up @@ -165,12 +139,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);
Expand Down Expand Up @@ -215,7 +183,6 @@ bool USpatialGDKEditorSettings::IsDeploymentConfigurationValid() const
{
bool result = IsAssemblyNameValid(AssemblyName) &&
IsDeploymentNameValid(PrimaryDeploymentName) &&
IsProjectNameValid(ProjectName) &&
!SnapshotPath.FilePath.IsEmpty() &&
!PrimaryLaunchConfigPath.FilePath.IsEmpty() &&
IsRegionCodeValid(PrimaryDeploymentRegionCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,6 @@ class SPATIALGDKEDITOR_API USpatialGDKEditorSettings : public UObject
TArray<FString> 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;

Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -29,7 +30,8 @@
void SSpatialGDKSimulatedPlayerDeployment::Construct(const FArguments& InArgs)
{
const USpatialGDKEditorSettings* SpatialGDKSettings = GetDefault<USpatialGDKEditorSettings>();

FString ProjectName = FSpatialGDKServicesModule::GetProjectName();

ParentWindowPtr = InArgs._ParentWindow;
SpatialGDKEditorPtr = InArgs._SpatialGDKEditor;

Expand Down Expand Up @@ -109,10 +111,9 @@ void SSpatialGDKSimulatedPlayerDeployment::Construct(const FArguments& InArgs)
.FillWidth(1.0f)
[
SNew(SEditableTextBox)
.Text(FText::FromString(SpatialGDKSettings->GetProjectName()))
.Text(FText::FromString(ProjectName))
.ToolTipText(FText::FromString(FString(TEXT("The name of the SpatialOS project."))))
.OnTextCommitted(this, &SSpatialGDKSimulatedPlayerDeployment::OnProjectNameCommited)
.OnTextChanged(this, &SSpatialGDKSimulatedPlayerDeployment::OnProjectNameCommited, ETextCommit::Default)
.IsEnabled(false)
]
]
// Assembly Name
Expand Down Expand Up @@ -405,12 +406,6 @@ void SSpatialGDKSimulatedPlayerDeployment::OnDeploymentAssemblyCommited(const FT
SpatialGDKSettings->SetAssemblyName(InText.ToString());
}

void SSpatialGDKSimulatedPlayerDeployment::OnProjectNameCommited(const FText& InText, ETextCommit::Type InCommitType)
{
USpatialGDKEditorSettings* SpatialGDKSettings = GetMutableDefault<USpatialGDKEditorSettings>();
SpatialGDKSettings->SetProjectName(InText.ToString());
}

void SSpatialGDKSimulatedPlayerDeployment::OnPrimaryDeploymentNameCommited(const FText& InText, ETextCommit::Type InCommitType)
{
USpatialGDKEditorSettings* SpatialGDKSettings = GetMutableDefault<USpatialGDKEditorSettings>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -31,9 +26,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)
Expand Down Expand Up @@ -89,43 +81,14 @@ void FLocalDeploymentManager::OnWorkerConfigDirectoryChanged(const TArray<FFileC
WorkerBuildConfigAsync();
}

FString FLocalDeploymentManager::GetProjectName()
{
const FString SpatialDirectory = FSpatialGDKServicesModule::GetSpatialOSDirectory();

FString SpatialFileName = TEXT("spatialos.json");
FString SpatialFileResult;
FFileHelper::LoadFileToString(SpatialFileResult, *FPaths::Combine(SpatialDirectory, SpatialFileName));

TSharedPtr<FJsonObject> 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]
{
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)
{
Expand All @@ -138,49 +101,6 @@ void FLocalDeploymentManager::WorkerBuildConfigAsync()
});
}

bool FLocalDeploymentManager::ParseJson(const FString& RawJsonString, TSharedPtr<FJsonObject>& JsonParsed)
{
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::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]
Expand Down Expand Up @@ -234,7 +154,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();

Expand All @@ -253,7 +173,7 @@ bool FLocalDeploymentManager::TryStartLocalDeployment(FString LaunchConfig, FStr
bool bSuccess = false;

TSharedPtr<FJsonObject> 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);
Expand Down Expand Up @@ -322,7 +242,7 @@ bool FLocalDeploymentManager::TryStopLocalDeployment()
bool bSuccess = false;

TSharedPtr<FJsonObject> 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);
Expand Down Expand Up @@ -371,7 +291,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;

Expand Down Expand Up @@ -403,7 +323,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)
Expand All @@ -430,7 +350,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;
Expand All @@ -444,14 +364,14 @@ bool FLocalDeploymentManager::GetLocalDeploymentStatus()
}

TSharedPtr<FJsonObject> SpotJsonResult;
bool bPasingSuccess = 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<FJsonObject>* SpotJsonContent = nullptr;
if (bPasingSuccess && SpotJsonResult->TryGetObjectField(TEXT("content"), SpotJsonContent))
if (bParsingSuccess && SpotJsonResult->TryGetObjectField(TEXT("content"), SpotJsonContent))
{
const TArray<TSharedPtr<FJsonValue>>* JsonDeployments;
if (!SpotJsonContent->Get()->TryGetArrayField(TEXT("deployments"), JsonDeployments))
Expand Down Expand Up @@ -493,7 +413,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)
{
Expand Down
Loading

0 comments on commit 288e1ce

Please sign in to comment.