diff --git a/EcsactNet.uplugin b/EcsactNet.uplugin index 2bae7ad..1b01b18 100644 --- a/EcsactNet.uplugin +++ b/EcsactNet.uplugin @@ -28,6 +28,11 @@ }, { "Name": "EcsactNetWasm", + "Type": "Runtime", + "LoadingPhase": "Default" + }, + { + "Name": "EcsactNetWasmEditor", "Type": "Editor", "LoadingPhase": "Default" } diff --git a/Source/EcsactNetWasm/EcsactNetWasm.Build.cs b/Source/EcsactNetWasm/EcsactNetWasm.Build.cs index 25dc9db..2e375a9 100644 --- a/Source/EcsactNetWasm/EcsactNetWasm.Build.cs +++ b/Source/EcsactNetWasm/EcsactNetWasm.Build.cs @@ -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[] {}); diff --git a/Source/EcsactNetWasm/Public/EcsactNetUnreal/Wasm/EcsactNetWasm.cpp b/Source/EcsactNetWasm/Public/EcsactNetUnreal/Wasm/EcsactNetWasm.cpp index b7b9ab8..989238d 100644 --- a/Source/EcsactNetWasm/Public/EcsactNetUnreal/Wasm/EcsactNetWasm.cpp +++ b/Source/EcsactNetWasm/Public/EcsactNetUnreal/Wasm/EcsactNetWasm.cpp @@ -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{}; - 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("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() - ); - 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(); - - auto client = FEcsactNetEditorModule::Get().GetHttpClient(); - auto requests = TArray{}; - - 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)>::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(); - - UE_LOG(LogTemp, Warning, TEXT("TODO: watch for wasm file changes")); - // UploadSystemImpls(); - - return true; -} - -IMPLEMENT_MODULE(FEcsactNetWasmModule, EcsactNet) +IMPLEMENT_MODULE(FEcsactNetWasmModule, EcsactNetWasm) diff --git a/Source/EcsactNetWasm/Public/EcsactNetUnreal/Wasm/EcsactNetWasm.h b/Source/EcsactNetWasm/Public/EcsactNetUnreal/Wasm/EcsactNetWasm.h index 829fd50..1b59f92 100644 --- a/Source/EcsactNetWasm/Public/EcsactNetUnreal/Wasm/EcsactNetWasm.h +++ b/Source/EcsactNetWasm/Public/EcsactNetUnreal/Wasm/EcsactNetWasm.h @@ -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; diff --git a/Source/EcsactNetWasmEditor/EcsactNetWasmEditor.Build.cs b/Source/EcsactNetWasmEditor/EcsactNetWasmEditor.Build.cs new file mode 100644 index 0000000..5ebaf8e --- /dev/null +++ b/Source/EcsactNetWasmEditor/EcsactNetWasmEditor.Build.cs @@ -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[] {}); + } +} diff --git a/Source/EcsactNetWasmEditor/Public/EcsactNetUnreal/Wasm/EcsactNetWasmEditor.cpp b/Source/EcsactNetWasmEditor/Public/EcsactNetUnreal/Wasm/EcsactNetWasmEditor.cpp new file mode 100644 index 0000000..22c6724 --- /dev/null +++ b/Source/EcsactNetWasmEditor/Public/EcsactNetUnreal/Wasm/EcsactNetWasmEditor.cpp @@ -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{}; + 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("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() + ); + + check(settings_section.IsValid()); + auto& ecsact_net_editor = FEcsactNetEditorModule::Get(); + const auto* settings = GetDefault(); + + settings_section->OnModified().BindRaw( + this, + &FEcsactNetWasmEditorModule::OnSettingsModified + ); + + ecsact_net_editor.AddEcsactNetToolsMenuExtension( + FMenuExtensionDelegate::CreateRaw( + this, + &FEcsactNetWasmEditorModule::AddMenuEntry + ) + ); +} + +auto FEcsactNetWasmEditorModule::UploadSystemImpls() -> void { + const auto* settings = GetDefault(); + + auto client = FEcsactNetEditorModule::Get().GetHttpClient(); + auto requests = TArray{}; + + 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)>::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(); + + UE_LOG(LogTemp, Warning, TEXT("TODO: watch for wasm file changes")); + // UploadSystemImpls(); + + return true; +} + +auto FEcsactNetWasmEditorModule::ShutdownModule() -> void { +} + +IMPLEMENT_MODULE(FEcsactNetWasmEditorModule, EcsactNetWasmEdior) diff --git a/Source/EcsactNetWasmEditor/Public/EcsactNetUnreal/Wasm/EcsactNetWasmEditor.h b/Source/EcsactNetWasmEditor/Public/EcsactNetUnreal/Wasm/EcsactNetWasmEditor.h new file mode 100644 index 0000000..03285c5 --- /dev/null +++ b/Source/EcsactNetWasmEditor/Public/EcsactNetUnreal/Wasm/EcsactNetWasmEditor.h @@ -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; +};