UE4/C++: JSON/XML/portable-binary çã®æ±ç¨ã·ãªã¢ã©ã¤ãºã©ã¤ãã©ãªã¼ cereal ã« UE4 ã®å種åãéä¾µå ¥åã¢ããã¿ã¼ã§å¯¾å¿ãããã©ã¤ãã©ãªã¼ cereal-UE4 ãå ¬éãã¾ãã
cereal-UE4
cereal ã«ã¤ãã¦
è¿å¹´ã¢ãã³ãª C++er ã«ãã°ãã°ä½¿ããã¦ããã¨æããã JSON / XML / Binary ( platform native ã® endian ã«ä¾åããªã portable ãå¯ ) ã«å¯¾å¿ããã·ãªã¢ã©ã¤ã¶ã¼ã®ã©ã¤ãã©ãªã¼ã§ãããããã¼ãªã³ãªã¼ã©ã¤ãã©ãªã¼ãªã®ã§ UE4 ã®ããã¸ã§ã¯ãã¸åãè¾¼ãéã«ãæéãã»ã¼ãããã¾ããã
cereal-UE4 ãä½ã£ãçµç·¯
- UE4 æ¨æºã® JSON ã·ãªã¢ã©ã¤ã¶ã¼ãããã©ãã
- FJsonSerializable é«ã¬ãã« JSON ã·ãªã¢ã©ã¤ãºAPI 㯠historia - FJsonSerializebleãã¯ãã使ã£ã¦ã¿ã ã§ç´¹ä»ããã¦ããããã«ä½¿ç¨å¯è½ãªç¶æ³ã§ã¯ã¨ã¦ã楽ã便å©ã ãâ¦
- æ¨æºæä¾ããã¦ããå®è£
ã®ç¯å²ã§å¯¾å¿ãã¦ããåãé常ã«éå®çãä¾ãã° UE4 ã§ã¯é »åºãã
FVector
ãFLinearColor
ãªã©ã®ã·ãªã¢ã©ã¤ãºã対å¿ã§ããªãã FJsonSerializable
ãç¶æ¿ãã¦åã®å¯¾å¿ãå¢ããäºã¯ã§ããããããã¨æéããããã- ç¶æ¿ã®ä½¿ç¨ãåæã¨ããä¾µå
¥åã·ãªã¢ã©ã¤ã¶ã¼ãªã®ã§ UE4 ã®ä»çµã¿ä¸
USTRUCT
åã«é©ç¨ã§ããªãï¼è´å½çã«ãã¡ãã®ï¼ð ï¼ã - ä»®æ³é¢æ°ãæã¤
FJsonSerializable
ã®ç¶æ¿ãåæã¨ããè¨è¨ã®ããããã®æ¹æ³ã§ã·ãªã¢ã©ã¤ãºå¯¾å¿ããåã¯ä»®æ³é¢æ°ãã¼ãã«ãå¿ è¦ã¨ãªãé POD åï¼å³å¯ã«ã¯éæ¨æºã¬ã¤ã¢ã¦ãåï¼ã¨ãªããããTArray<FVector>
ã®ããã«é£ç¶ã§ãã¼ã¿ã¡ã³ãã¼"ã ã"ãã¢ã©ã¤ã¡ã³ããã"å¯"ã«ä¸¦ãã§æ¬²ãããã¼ã¿æ§é ã«é©ç¨ã§ããªãï¼è´å½çã«ãã¡ãã®ï¼ð ï¼
- æ¨æºæä¾ããã¦ããå®è£
ã®ç¯å²ã§å¯¾å¿ãã¦ããåãé常ã«éå®çãä¾ãã° UE4 ã§ã¯é »åºãã
- UE4/JSON/DOM ä½ã¬ãã«API ã¯ã¹ãã¼ããã¤ã³ã¿ã¼ã¨åç §ãå ¥ãä¹±ããå®è£ ãå¿ è¦ã¨ãªããããææ¸ãã¯è¤éã§ç²ããããã³ã¼ãéã大ãããªãä¿å®æ§ãæªããªãã
- FJsonSerializable é«ã¬ãã« JSON ã·ãªã¢ã©ã¤ãºAPI 㯠historia - FJsonSerializebleãã¯ãã使ã£ã¦ã¿ã ã§ç´¹ä»ããã¦ããããã«ä½¿ç¨å¯è½ãªç¶æ³ã§ã¯ã¨ã¦ã楽ã便å©ã ãâ¦
ã¨ãè¨ãããã§ã UE4 ã® JSON ã·ãªã¢ã©ã¤ã¶ã¼ã¯ç¾ç¶ããã¨æ®å¿µãªã®ã§ã UE4 ã®å¤ã®ä¸çã§ã¯ C++er ã«ããã¨äººæ°ã®é«ãï¼â楽ã«ä½¿ãã¦ã¨ã¦ã便å©ï¼ãª cereal ã UE4 ã«å¯¾å¿ããè£å©çãªã©ã¤ãã©ãªã¼ãå®è£ ããäºã«ãã¾ããã
cereal-UE4 ã®ä»çµã¿
cereal ã«ã¯å¤§ããåãã¦ä¾µå
¥åã¨éä¾µå
¥åã®ï¼ã¤ã®æ¹æ³ã§ä»»æã®ã¦ã¼ã¶ã¼å®ç¾©åã¸ã®ã·ãªã¢ã©ã¤ã¶ã¼å¯¾å¿ã追å ã§ãã¾ããããã§ã cereal-UE4 ã§ã¯éä¾µå
¥å㧠UE4 ã§ä½¿ãããä¸è¬çãªå¤ãã®åã¸å¯¾å¿ããã·ãªã¢ã©ã¤ã¶ã¼ãããããæ¸ãã¦ã cereal
+ cereal-UE4
ã UE4 ã®ããã¸ã§ã¯ãã¸è¿½å ããã°ç°¡åã« cereal ã«ãã UE4 åã«å¯¾å¿ãã JSON / XML / Binary ã®ã·ãªã¢ã©ã¤ãºãå¯è½ãªããå®è£
ãã¾ãããå
¨ã¦ã®å®è£
㯠template
é¢æ°ãªã®ã§ãå®éã«ä½¿ç¨ããåã®ã·ãªã¢ã©ã¤ã¶ã¼ã®ã¿ãããããªã³ãã«å½±é¿ãã¾ãã
ãã¡ããã cereal-UE4 ããããã¼ãªã³ãªã¼ã©ã¤ãã©ãªã¼ã§ãã cereal ã¨ä½µãã¦ç°¡åã« UE4 ããã¸ã§ã¯ãã¸å°å ¥ã§ãã¾ãã
使ãæ¹
UE4 ã®ããã¸ã§ã¯ãã®ãã£ã¬ã¯ããªã¼ã®é©å½ãªã¨ãã㸠Thirdparty
ãªã©é©å½ãªãã£ã¬ã¯ããªã¼ãç¨æãã
cereal
cereal-UE4
ã GitHub ãã clone ãªã zip ãªãã§èª¿éãé ç½®ãã¾ãã
<MyProject> |-Thirdparty | |-cereal | |-cereal-UE4 |- MyProject.uproject |- ...
ããããã MyProject.Build.cs ã«ã
using System.IO;
ãåé ã«è¿½å ãã¤ã¤ã
public class MyProject
å
ã® public MyProject( ReadOnlyTargetRules Target )
ã³ã³ã¹ãã©ã¯ã¿ã¼ã®ä¸ã«ã
{ var base_path = Path.GetDirectoryName( RulesCompiler.GetFileNameFromType( GetType() ) ); string third_party_path = Path.Combine( base_path, "..", "..", "Thirdparty"); PublicIncludePaths.Add( Path.Combine( third_party_path, "cereal", "include") ); PublicIncludePaths.Add( Path.Combine( third_party_path, "cereal-UE4", "include") ); }
ãããªå
·å㧠Thirdparty/cereal/include
㨠Thirdparty/cereal-UE4/include
ã UE4 ããã¸ã§ã¯ãã® C++ ã³ã¼ããã¡ã¤ã«ã®ã³ã³ãã¤ã«ã®éã«ã¤ã³ã¯ã«ã¼ããã¹ã«è¿½å ãããè¨å®ã追å ãã¾ãã
ãã¨ã¯ããã ã® struct
ã§ã class
ã§ã USTRUCT
ã§ã UCLASS
ã§ãæ°ã«ãããã·ãªã¢ã©ã¤ãºãããã¯ã©ã¹ãä¾ãã°ä»¥ä¸ã®ãã㪠FMySomething
åã«å¯¾ãã¦ã
// ããã¯ããªãã®ã好ããªããã«ç¨æãã USTRUCT( BlueprintType ) struct FMySomething { GENERATED_BODY() UPROPERTY( BlueprintReadWrite ) FString String; UPROPERTY( BlueprintReadWrite ) FDateTime DateTime; UPROPERTY( BlueprintReadWrite ) FVector Vector; UPROPERTY( BlueprintReadWrite ) TArray< int32 > ArrayI32; };
// ã好ããªããã« cereal ã®éä¾µå ¥åã·ãªã¢ã©ã¤ã¶ã¼ã追å ããã // åã®å®ç¾©ã®ç´å¾ã« .h ã¸æ¸ãã¦ãããããã·ãªã¢ã©ã¤ãºãç¹å®ã® .cpp ã§ããè¡ããªãã®ãªããã® .cpp ã¸æ¸ãã¦ããã // cereal #include "cereal/cereal.hpp" #include "cereal/archives/json.hpp" // cereal-UE4 #include "cereal-UE4.hxx" // std::stringstream #include <sstream> template < typename A > void serialize( A& a, FMySomething& in ) { a ( cereal::make_nvp( "String", in.String ) , cereal::make_nvp( "DateTime", in.DateTime ) , cereal::make_nvp( "Vector", in.Vector ) , cereal::make_nvp( "ArrayI32", in.ArrayI32 ) ); }
// ããã¯ã©ããã BeginPlay 㨠EndPlay ã¨ããã好ããªã¿ã¤ãã³ã°ã§ã·ãªã¢ã©ã¤ãºãã¦æ§ããªãã // ãã¡ããã FMySomething ã¯ãã¼ã«ã«å¤æ°ã§ãªãã¦ãããããã¯ã©ã¹ã®ã¡ã³ãã¼ã§ããªãã§ãããã FMySomething MySomething; MySomething.String = "Hello, ããã«ã¡ã¯."; MySomething.DateTime = FDateTime::UtcNow(); MySomething.Vector = FVector::UpVector(); MySomething.ArrayI32 = { 123, -456, 789 }; // JSON ã¸ã·ãªã¢ã©ã¤ãº std::stringstream buffer; { cereal::JSONOutputArchive a( buffer ); a( cereal::make_nvp( "MySomething", MySomething ) ); } const auto json = FString( buffer.str().data() ); UE_LOG( LogTemp, Log, TEXT( "%s" ), *json ) // ãã¾ã: FString ããã¡ã¤ã«ã¸æ¸ãåºããããã°ãããªå ·å if ( ! FFileHelper::SaveStringToFile ( json , *( FPaths::ProjectDir / TEXT( "MySomething.json" ) ) , FFileHelper::EEncodingOptions::ForceUTF8WithoutBOM ) ) { UE_LOG( LogTemp, Fatal, TEXT( "Save Error" ) ); }
ããã§æ¬¡ã®ãã㪠MySomething.json
ãæ¸ãåºããã
{ "MySomething": { "String": "Hello, ããã«ã¡ã¯.", "DateTime": "2018-03-29T17:59:50.595Z", "Vector": { "X": 0.0, "Y": 0.0, "Z": 1.0 }, "ArrayI32": [ 123, -456, 789 ] } }
èªã¿åºãï¼ãã·ãªã¢ã©ã¤ãºï¼ãããå ´åã¯
FMySomething from_json; std::stringstream buffer; buffer << TCHAR_TO_UTF8( *json ); cereal::JSONInputArchive a( buffer ); a( from_json );
ãã㧠from_json
ã«ã¯ to_json
ã¨åãä¸èº«ã復å
ãããã
ãããã«
cereal-UE4 㯠MIT ã©ã¤ã»ã³ã¹ã® OSS ã¨ã㦠GitHub ã¸å ¬éãã¾ããã UE4/C++er ã JSON / XML / Binary ã®ã·ãªã¢ã©ã¤ã¶ã¼ã«ã¤ãã¦ãæ¨æºå®è£ ã§ã¯ä¸è¶³ããããã¯é¢åãããªç¶æ³ã«å¯¾ãã cereal-UE4 ãå©ãã¨ãªãã°å¹¸ãã§ãã対å¿ããåãããã¹ã¦ã®åã§ã®å®è£ ä¾ã¯ãªãã¸ããªã¼ã® README.md ã example/ ãåç §ãã¦ä¸ããã対å¿åã®ä¸è¶³ããã°ä¿®æ£ãããã° PR ä¸ããã