From 3ec3868ae6298a6852d0b525993e51fba7352f38 Mon Sep 17 00:00:00 2001 From: getnamo Date: Fri, 26 Jul 2024 02:05:39 -0700 Subject: [PATCH 1/3] refactor and add struct to bytes and bytes to struct (json variant) for c++ --- Source/SIOJson/Private/SIOJConvert.cpp | 42 ++++++++++++++++++-------- Source/SIOJson/Public/SIOJConvert.h | 3 ++ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Source/SIOJson/Private/SIOJConvert.cpp b/Source/SIOJson/Private/SIOJConvert.cpp index 712c212..0678981 100644 --- a/Source/SIOJson/Private/SIOJConvert.cpp +++ b/Source/SIOJson/Private/SIOJConvert.cpp @@ -835,36 +835,52 @@ bool USIOJConvert::JsonObjectToUStruct(TSharedPtr JsonObject, UStru bool USIOJConvert::JsonFileToUStruct(const FString& FilePath, UStruct* Struct, void* StructPtr, bool IsBlueprintStruct /*= false*/) { //Read bytes from file - TArray OutBytes; - if (!FFileHelper::LoadFileToArray(OutBytes, *FilePath)) + TArray ReadBytes; + if (!FFileHelper::LoadFileToArray(ReadBytes, *FilePath)) { return false; } - //Convert to json string - FString JsonString; - FFileHelper::BufferToString(JsonString, OutBytes.GetData(), OutBytes.Num()); - - //Read into struct - return JsonObjectToUStruct(ToJsonObject(JsonString), Struct, StructPtr, IsBlueprintStruct); + return BytesToStruct(ReadBytes, Struct, StructPtr, IsBlueprintStruct); } bool USIOJConvert::ToJsonFile(const FString& FilePath, UStruct* Struct, void* StructPtr, bool IsBlueprintStruct /*= false*/) +{ + TArray Bytes; + StructToBytes(Struct, StructPtr, Bytes, IsBlueprintStruct); + + //flush to disk + return FFileHelper::SaveArrayToFile(Bytes, *FilePath); +} + +bool USIOJConvert::StructToBytes(UStruct* Struct, void* StructPtr, TArray& OutBytes, bool IsBlueprintStruct) { //Get json object with trimmed values TSharedPtr JsonObject = ToJsonObject(Struct, StructPtr, IsBlueprintStruct); TSharedPtr TrimmedValue = MakeShareable(new FJsonValueObject(JsonObject)); - TrimValueKeyNames(TrimmedValue); + + if (IsBlueprintStruct) + { + TrimValueKeyNames(TrimmedValue); + } //Convert to string FString JsonString = ToJsonString(TrimmedValue); FTCHARToUTF8 Utf8String(*JsonString); - TArray Bytes; - Bytes.Append((uint8*)Utf8String.Get(), Utf8String.Length()); + OutBytes.Append((uint8*)Utf8String.Get(), Utf8String.Length()); - //flush to disk - return FFileHelper::SaveArrayToFile(Bytes, *FilePath); + return true; +} + +bool USIOJConvert::BytesToStruct(const TArray& InBytes, UStruct* Struct, void* StructPtr, bool IsBlueprintStruct) +{ + //Convert to json string + FString JsonString; + FFileHelper::BufferToString(JsonString, InBytes.GetData(), InBytes.Num()); + + //Read into struct + return JsonObjectToUStruct(ToJsonObject(JsonString), Struct, StructPtr, IsBlueprintStruct); } void USIOJConvert::TrimValueKeyNames(const TSharedPtr& JsonValue) diff --git a/Source/SIOJson/Public/SIOJConvert.h b/Source/SIOJson/Public/SIOJConvert.h index dbcfd50..3836a9d 100644 --- a/Source/SIOJson/Public/SIOJConvert.h +++ b/Source/SIOJson/Public/SIOJConvert.h @@ -48,6 +48,9 @@ class SIOJSON_API USIOJConvert : public UObject static bool JsonFileToUStruct(const FString& FilePath, UStruct* Struct, void* StructPtr, bool IsBlueprintStruct = false); static bool ToJsonFile(const FString& FilePath, UStruct* Struct, void* StructPtr, bool IsBlueprintStruct = false); + static bool StructToBytes(UStruct* Struct, void* StructPtr, TArray& OutBytes, bool IsBlueprintStruct = false); + static bool BytesToStruct(const TArray& InBytes, UStruct* Struct, void* StructPtr, bool IsBlueprintStruct = false); + //typically from callbacks static class USIOJsonValue* ToSIOJsonValue(const TArray>& JsonValueArray); From 37212d28ea0e865e1cb82528839d98a42944a1cf Mon Sep 17 00:00:00 2001 From: getnamo Date: Wed, 23 Oct 2024 22:12:54 -0700 Subject: [PATCH 2/3] potential fix for #441 --- SocketIOClient.uplugin | 2 +- Source/CoreUtility/Public/CUOpusCoder.h | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/SocketIOClient.uplugin b/SocketIOClient.uplugin index 039205b..cb7dabc 100644 --- a/SocketIOClient.uplugin +++ b/SocketIOClient.uplugin @@ -1,7 +1,7 @@ { "FileVersion": 3, "Version": 1, - "VersionName": "2.8.2", + "VersionName": "2.8.3", "EngineVersion": "5.4", "FriendlyName": "Socket.IO Client", "Description": "Real-time WebSocket networking via Socket.IO protocol usable from blueprints and c++.", diff --git a/Source/CoreUtility/Public/CUOpusCoder.h b/Source/CoreUtility/Public/CUOpusCoder.h index d6ce5f5..4ba47ff 100644 --- a/Source/CoreUtility/Public/CUOpusCoder.h +++ b/Source/CoreUtility/Public/CUOpusCoder.h @@ -4,7 +4,9 @@ #include "CoreMinimal.h" -#define WITH_OPUS (PLATFORM_WINDOWS || PLATFORM_UNIX || PLATFORM_ANDROID) +#ifndef WITH_OPUS + #define WITH_OPUS (PLATFORM_WINDOWS || PLATFORM_UNIX || PLATFORM_ANDROID) +#endif #if WITH_OPUS #include "ThirdParty/libOpus/opus-1.1/include/opus.h" From 512fa6614b09f36311229be08b38de1286bc43d8 Mon Sep 17 00:00:00 2001 From: getnamo Date: Wed, 23 Oct 2024 22:57:39 -0700 Subject: [PATCH 3/3] bump for serialization addition --- SocketIOClient.uplugin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SocketIOClient.uplugin b/SocketIOClient.uplugin index cb7dabc..ed46728 100644 --- a/SocketIOClient.uplugin +++ b/SocketIOClient.uplugin @@ -1,7 +1,7 @@ { "FileVersion": 3, "Version": 1, - "VersionName": "2.8.3", + "VersionName": "2.8.4", "EngineVersion": "5.4", "FriendlyName": "Socket.IO Client", "Description": "Real-time WebSocket networking via Socket.IO protocol usable from blueprints and c++.",