Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions EcsactNet.uplugin
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
},
{
"Name": "EcsactNetWasm",
"Type": "Runtime",
"LoadingPhase": "Default"
},
{
"Name": "EcsactNetWasmEditor",
"Type": "Editor",
"LoadingPhase": "Default"
}
Expand Down
13 changes: 0 additions & 13 deletions Source/EcsactNetWasm/EcsactNetWasm.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,10 @@ public EcsactNetWasm(ReadOnlyTargetRules Target) : base(Target) {
PublicDependencyModuleNames.AddRange(new string[] {
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
"Json",
"Core",
"Engine",
"UnrealEd",
"Ecsact",
"Settings",
"PropertyEditor",
"HTTP",
"Json",
"JsonUtilities",
});

PrivateDependencyModuleNames.AddRange(new string[] {
"EcsactNetEditor",
"EcsactEditor",
});

DynamicallyLoadedModuleNames.AddRange(new string[] {});
Expand Down
132 changes: 1 addition & 131 deletions Source/EcsactNetWasm/Public/EcsactNetUnreal/Wasm/EcsactNetWasm.cpp
Original file line number Diff line number Diff line change
@@ -1,143 +1,13 @@
#include "EcsactNetWasm.h"
#include "UObject/NoExportTypes.h"
#include "ISettingsModule.h"
#include "ISettingsSection.h"
#include "Editor.h"
#include "LevelEditor.h"
#include "ISettingsContainer.h"
#include "Modules/ModuleManager.h"
#include "EcsactNetWasmSettings.h"
#include "HttpModule.h"
#include "Interfaces/IHttpRequest.h"
#include "Interfaces/IHttpResponse.h"
#include "Dom/JsonObject.h"
#include "Serialization/JsonSerializer.h"
#include "JsonObjectConverter.h"
#include "Misc/Base64.h"
#include "HAL/FileManager.h"
#include "Misc/FileHelper.h"
#include "EcsactNetEditor/EcsactNetEditor.h"
#include "EcsactNetEditor/EcsactNetHttpClient.h"
#include "Framework/MultiBox/MultiBoxBuilder.h"
#include "Framework/MultiBox/MultiBoxExtender.h"

#define LOCTEXT_NAMESPACE "FEcsactNetWasmModule"

static auto ReadFileAsBase64(const FString& FilePath) -> FString {
auto file_contents = TArray<uint8>{};
if(!FFileHelper::LoadFileToArray(file_contents, *FilePath)) {
UE_LOG(LogTemp, Error, TEXT("Failed to read file: %s"), *FilePath);
return {};
}

return FBase64::Encode(file_contents);
}

auto FEcsactNetWasmModule::StartupModule() -> void {
auto& settings_module =
FModuleManager::GetModuleChecked<ISettingsModule>("Settings");
auto settings_container = settings_module.GetContainer("Project");
auto settings_section = settings_module.RegisterSettings(
"Project",
"Plugins",
"EcsactNetWasm",
LOCTEXT("SettingsName", "Ecsact Net Wasm Settings"),
LOCTEXT(
"SettingsDescription",
"Configuration settings for Ecsact Net Wasm System Implementations"
),
GetMutableDefault<UEcsactNetWasmSettings>()
);
check(settings_section.IsValid());

settings_section->OnModified().BindRaw(
this,
&FEcsactNetWasmModule::OnSettingsModified
);

auto& ecsact_net_editor = FEcsactNetEditorModule::Get();

ecsact_net_editor.AddEcsactNetToolsMenuExtension(
FMenuExtensionDelegate::CreateRaw(this, &FEcsactNetWasmModule::AddMenuEntry)
);
}

auto FEcsactNetWasmModule::AddMenuEntry( //
class FMenuBuilder& MenuBuilder
) -> void {
MenuBuilder.AddMenuEntry(
LOCTEXT("EcsactNetUploadWasm", "Upload System Impls"),
LOCTEXT("EcsactNetUploadWasm", "Manually trigger a wasm system upload"),
FSlateIcon(),
FUIAction(FExecuteAction::CreateLambda([this] { UploadSystemImpls(); }))
);
}

auto FEcsactNetWasmModule::ShutdownModule() -> void {
}

auto FEcsactNetWasmModule::UploadSystemImpls() -> void {
const auto* settings = GetDefault<UEcsactNetWasmSettings>();

auto client = FEcsactNetEditorModule::Get().GetHttpClient();
auto requests = TArray<FSystemImplsReplaceRequest>{};

for(auto wasm_file : settings->SystemImplWasmFiles) {
auto file_path = wasm_file.FilePath;
if(FPaths::IsRelative(wasm_file.FilePath)) {
file_path = FPaths::Combine(FPaths::ProjectDir(), file_path);
}

auto req = FSystemImplsReplaceRequest{};
req.fileContentsType = "SYSTEM_IMPL_WASM_BASE64";
req.fileContents = ReadFileAsBase64(file_path);

if(req.fileContents.IsEmpty()) {
return;
}

auto req_json = FString{};

if(!FJsonObjectConverter::UStructToJsonObjectString(req, req_json)) {
UE_LOG(LogTemp, Error, TEXT("Failed to serialize request payload"));
return;
}

requests.Add(req);
}

client->UploadSystemImpls(
std::move(requests),
TDelegate<void(TArray<FSystemImplsReplaceResponse>)>::CreateLambda(
[](auto response) {
for(auto item : response) {
if(item.status == "SIS_OK") {
UE_LOG(LogTemp, Log, TEXT("Successfully uploaded wasm!"));
UE_LOG(LogTemp, Log, TEXT("Uploaded System Names:"));
for(auto name : item.systemNames) {
UE_LOG(LogTemp, Log, TEXT(" - %s"), *name);
}
} else {
UE_LOG(
LogTemp,
Log,
TEXT("Failed to upload wasm. Status is %s"),
*item.status
);
}
}
}
)
);
}

auto FEcsactNetWasmModule::OnSettingsModified() -> bool {
const auto* settings = GetDefault<UEcsactNetWasmSettings>();

UE_LOG(LogTemp, Warning, TEXT("TODO: watch for wasm file changes"));
// UploadSystemImpls();

return true;
}

IMPLEMENT_MODULE(FEcsactNetWasmModule, EcsactNet)
IMPLEMENT_MODULE(FEcsactNetWasmModule, EcsactNetWasm)
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
DECLARE_LOG_CATEGORY_EXTERN(EcsactNetWasm, Log, All);

class FEcsactNetWasmModule : public IModuleInterface {
auto OnSettingsModified() -> bool;
auto UploadSystemImpls() -> void;
auto AddMenuEntry(class FMenuBuilder&) -> void;

public:
auto StartupModule() -> void override;
auto ShutdownModule() -> void override;
Expand Down
38 changes: 38 additions & 0 deletions Source/EcsactNetWasmEditor/EcsactNetWasmEditor.Build.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

using UnrealBuildTool;
using System.IO;

public class EcsactNetWasmEditor : ModuleRules {
public EcsactNetWasmEditor(ReadOnlyTargetRules Target) : base(Target) {
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;

PublicIncludePaths.AddRange(new string[] {});

PrivateIncludePaths.AddRange(new string[] {});

PublicDependencyModuleNames.AddRange(new string[] {
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
"Json",
"Core",
"Engine",
"UnrealEd",
"Ecsact",
"Settings",
"PropertyEditor",
"HTTP",
"Json",
"JsonUtilities",
"EcsactNetWasm",
});

PrivateDependencyModuleNames.AddRange(new string[] {
"EcsactNetEditor",
"EcsactEditor",
});

DynamicallyLoadedModuleNames.AddRange(new string[] {});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include "EcsactNetWasmEditor.h"
#include "EcsactNetEditor/Public/EcsactNetEditor/EcsactNetEditor.h"
#include "EcsactNetEditor/EcsactNetHttpClient.h"
#include "EcsactNetWasm/Public/EcsactNetUnreal/Wasm/EcsactNetWasmSettings.h"
#include "EcsactNetWasm/Public/EcsactNetUnreal/Wasm/EcsactNetWasm.h"
#include "UObject/NoExportTypes.h"
#include "ISettingsModule.h"
#include "ISettingsSection.h"
#include "Editor.h"
#include "LevelEditor.h"
#include "ISettingsContainer.h"
#include "Modules/ModuleManager.h"
#include "HttpModule.h"
#include "Interfaces/IHttpRequest.h"
#include "Interfaces/IHttpResponse.h"
#include "Dom/JsonObject.h"
#include "Serialization/JsonSerializer.h"
#include "JsonObjectConverter.h"
#include "Misc/Base64.h"
#include "HAL/FileManager.h"
#include "Misc/FileHelper.h"
#include "Framework/MultiBox/MultiBoxBuilder.h"
#include "Framework/MultiBox/MultiBoxExtender.h"

#define LOCTEXT_NAMESPACE "FEcsactNetWasmEditorModule"

static auto ReadFileAsBase64(const FString& FilePath) -> FString {
auto file_contents = TArray<uint8>{};
if(!FFileHelper::LoadFileToArray(file_contents, *FilePath)) {
UE_LOG(LogTemp, Error, TEXT("Failed to read file: %s"), *FilePath);
return {};
}

return FBase64::Encode(file_contents);
}

auto FEcsactNetWasmEditorModule::StartupModule() -> void {
auto& settings_module =
FModuleManager::GetModuleChecked<ISettingsModule>("Settings");
auto settings_container = settings_module.GetContainer("Project");
auto settings_section = settings_module.RegisterSettings(
"Project",
"Plugins",
"EcsactNetWasm",
LOCTEXT("SettingsName", "Ecsact Net Wasm Settings"),
LOCTEXT(
"SettingsDescription",
"Configuration settings for Ecsact Net Wasm System Implementations"
),
GetMutableDefault<UEcsactNetWasmSettings>()
);

check(settings_section.IsValid());
auto& ecsact_net_editor = FEcsactNetEditorModule::Get();
const auto* settings = GetDefault<UEcsactNetWasmSettings>();

settings_section->OnModified().BindRaw(
this,
&FEcsactNetWasmEditorModule::OnSettingsModified
);

ecsact_net_editor.AddEcsactNetToolsMenuExtension(
FMenuExtensionDelegate::CreateRaw(
this,
&FEcsactNetWasmEditorModule::AddMenuEntry
)
);
}

auto FEcsactNetWasmEditorModule::UploadSystemImpls() -> void {
const auto* settings = GetDefault<UEcsactNetWasmSettings>();

auto client = FEcsactNetEditorModule::Get().GetHttpClient();
auto requests = TArray<FSystemImplsReplaceRequest>{};

for(auto wasm_file : settings->SystemImplWasmFiles) {
auto file_path = wasm_file.FilePath;
if(FPaths::IsRelative(wasm_file.FilePath)) {
file_path = FPaths::Combine(FPaths::ProjectDir(), file_path);
}

auto req = FSystemImplsReplaceRequest{};
req.fileContentsType = "SYSTEM_IMPL_WASM_BASE64";
req.fileContents = ReadFileAsBase64(file_path);

if(req.fileContents.IsEmpty()) {
return;
}

auto req_json = FString{};

if(!FJsonObjectConverter::UStructToJsonObjectString(req, req_json)) {
UE_LOG(LogTemp, Error, TEXT("Failed to serialize request payload"));
return;
}

requests.Add(req);
}

client->UploadSystemImpls(
std::move(requests),
TDelegate<void(TArray<FSystemImplsReplaceResponse>)>::CreateLambda(
[](auto response) {
for(auto item : response) {
if(item.status == "SIS_OK") {
UE_LOG(LogTemp, Log, TEXT("Successfully uploaded wasm!"));
UE_LOG(LogTemp, Log, TEXT("Uploaded System Names:"));
for(auto name : item.systemNames) {
UE_LOG(LogTemp, Log, TEXT(" - %s"), *name);
}
} else {
UE_LOG(
LogTemp,
Log,
TEXT("Failed to upload wasm. Status is %s"),
*item.status
);
}
}
}
)
);
}

auto FEcsactNetWasmEditorModule::AddMenuEntry( //
class FMenuBuilder& MenuBuilder
) -> void {
MenuBuilder.AddMenuEntry(
LOCTEXT("EcsactNetUploadWasm", "Upload System Impls"),
LOCTEXT("EcsactNetUploadWasm", "Manually trigger a wasm system upload"),
FSlateIcon(),
FUIAction(FExecuteAction::CreateLambda([this] { UploadSystemImpls(); }))
);
}

auto FEcsactNetWasmEditorModule::OnSettingsModified() -> bool {
const auto* settings = GetDefault<UEcsactNetWasmSettings>();

UE_LOG(LogTemp, Warning, TEXT("TODO: watch for wasm file changes"));
// UploadSystemImpls();

return true;
}

auto FEcsactNetWasmEditorModule::ShutdownModule() -> void {
}

IMPLEMENT_MODULE(FEcsactNetWasmEditorModule, EcsactNetWasmEdior)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "CoreMinimal.h"
#include "Modules/ModuleManager.h"

DECLARE_LOG_CATEGORY_EXTERN(EcsactNetWasmEditor, Log, All);

class FEcsactNetWasmEditorModule : public IModuleInterface {
auto OnSettingsModified() -> bool;
auto UploadSystemImpls() -> void;

public:
auto StartupModule() -> void override;
auto AddMenuEntry( //
class FMenuBuilder& MenuBuilder
) -> void;
auto ShutdownModule() -> void override;
};