Git Product home page Git Product logo

cereal-ue4's Introduction

cereal-UE4

This is the adapter library of cereal for UE4 types.

Features of this library and cereal

  • Many common UE4 types are supported! // main feature of this library!
  • Header-only library! // Easy for use your UE4 project.
  • No problems for use USTRUCT types. // UE4 built-in serializer is not supported
  • Ofcourse, common C++ types are supported. // UE4 build-in serializer is not supported
    • int8, uint8, int16, uint16, int32, uint32 int64, uint64
    • float, double, long double
    • std::string, std::vector, std::map, ... // See also cereal: Standard Library Support
  • You can use a similar methods in archiving of JSON, XML and Portable-binary.

Supported types in this library

  • TArray, TMap, TSet
  • TInterval ( with FFloatInterval, FInt32Interval )
  • TBigInt
  • FString, FName
  • FDateTime, FTimespan
  • FVector, FVector2D
  • FBox, FBox2D
  • FSphere
  • FCapsuleShape
  • FLinearColor, FColor
  • FIntVector, FIntPoint, FIntVector4, FUintVector4
  • FIntRect
  • FMatrix, FMatrix2x2
  • FScale, FScale2D, FShear2D
  • FOrientedBox
  • FPlane
  • FShpere
  • FQuat, FQuat2D
  • FTwoVectors

Motivation

  • JSON serializer of UE4 is ...
    • Not supported many common UE4 types; FVector, FTimespan, FVector2D, FBox, FBox2D, FColor, FLinearColor, FIntPoint, FMatrix, FPlane, FScale, FShpere, FBigInt, int8, uint8, int16, uint16, int64, uint64, ...
    • Implemented by inheritance model only ( using virtual functions )
      • If we use it then it will break POD ( standard layout ) of our data structure. vtable increase a data size and break packing of a data array.
      • It cannot use for USTRUCT types.
    • Very complicated and messy to use low-level APIs for a full-manually serialization. ( see FJsonSerializable, UE4/JSON/DOM )

Then, I decided to use cereal and I wrote this adapter library.

Usage

  1. Prepare a thirdparty library directory:
    1. cd <your-project>
    2. mkdir Thirdparty; or anywhere else as you like
    3. cd Thirdparty
  2. Deploy cereal library.
  3. Deploy cereal-UE4 library.
  4. Add PublicIncludePaths settings to <your-project>.Build.cs; See also below (†1)
  5. Write your serialization code in your UE4 project; See also below (†2)
    • And get your serialization results! (†3)

(†1): Step-4's <your-project>.Build.cs

// 1. Add the `using`
using System.IO;

public class YourProject
  : ModuleRules
{
  public YourProject(ReadOnlyTargetRules Target)
    : base( Target )
  {
    // 2. Add below code into your setting
    {
      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") );
    }
  }
}

(†2): Step-5's e.g.; Now, you can serialize FString, FDateTime, FVector and etc. with cereal!

your-something.h:

// cereal-UE4 serializable class(struct)
USTRUCT( BlueprintType ) struct FMySomething
{ GENERATED_BODY()
  UPROPERTY( BlueprintReadWrite ) FString String;
  UPROPERTY( BlueprintReadWrite ) FDateTime DateTime;
  UPROPERTY( BlueprintReadWrite ) FVector Vector;
  UPROPERTY( BlueprintReadWrite ) TArray< int32 > ArrayI32;
};

// cereal-UE4 external serializer definition for FMySomething type
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 )
  );
}

your-something.cpp:

/* ... ( abbreviation ) ...  */

// cereal
#include "cereal/cereal.hpp"
#include "cereal/archives/json.hpp"
// cereal-UE4
#include "cereal-UE4.hxx"
// std::stringstream
#include <sstream>

/* ... ( abbreviation ) ...  */
  
  FMySomething MySomething;
  MySomething.String = "Hello, こんにちは.";
  MySomething.DateTime = FDateTime::UtcNow();
  MySomething.Vector = FVector::UpVector();
  MySomething.ArrayI32 = { 123, -456, 789 };
  
  /* ... ( abbreviation ) ...  */
  
  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 )
  
  if
  ( !  FFileHelper::SaveStringToFile
          ( json
          , *( FPaths::ProjectDir / TEXT( "MySomething.json" ) )
          , FFileHelper::EEncodingOptions::ForceUTF8WithoutBOM )
  )
  {
    UE_LOG( LogTemp, Fatal, TEXT( "Save Error" ) );
  }

/* ... ( abbreviation ) ...  */

(†3): Step-5's expected result.

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
        ]
    }
}

Licensing

Author

cereal-ue4's People

Contributors

usagi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

cereal-ue4's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.