kfischer-okarin / hoard

Serialize/Deserialize arbitrarily complex values in your DragonRuby games.
MIT License
2 stars 0 forks source link
dragonruby serialization

Hoard

MIT License Tests Status

About

The built-in serialization/deserialization functions of DragonRuby fail when used with moderately big data.

With Hoard you can serialize and deserialize arbitrarily big data without having to think about splitting them into manageable small pieces.

Installation

Via Smaug

Add following line to your Smaug.toml:

hoard =  "https://github.com/kfischer-okarin/hoard/releases/download/v1.0.0/hoard.zip"

Alternatively you can also just run smaug add hoard though that will add the whole repository to your project.

Manually

Download the current release, extract it, and put the contained lib/hoard.rb anywhere in your game folder and require it.

Usage

To serialize data:

serialized_data = Hoard.serialize save_game_data

# Save the data
$gtk.write_file 'saves/001.sav', serialized_data

To deserialize data:

serialized_data = $gtk.read_file 'saves/001.sav'

save_game_data = Hoard.deserialize serialized_data

Supported Data Types

Caution

Entities created by args.state.new_entity or args.state.new_entity_strict are serialized/deserialized as a whole. So if a single entity is very big it could still mean that your data cannot be properly deserialized (DragonRuby will print out a warning in that case).

Therefore if you need to serialize huge units of data it might be better to use Hashes.

Define your own Serializer

A serializer is a class derived from Hoard::Serializer::BaseSerializer that defines several class methods as below.

class MySerializer < Hoard::Serializer::BaseSerializer
  class << self
    def type
      # Return a unique symbol identifier for your serializer
    end

    def can_serialize?(value)
      # Return true if the value can be serialized by your serializer
    end

    def serialize(value)
      # Return a serialized string representation of your data
    end

    def deserialize(serialized_value)
      # Return the deserialized data
    end
  end
end

You can check the implementations of the default serializers if you need more details.

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Thank your for your willingness to help out with the development!

If you have any contributions, please fork the repository, add your changes to a feature branch and open a Pull Request.

Contact

You can find me usually on the DragonRuby Discord under the username kfischer_okarin.