PMLSaveData - PULSAR-Modders/pulsar-mod-loader GitHub Wiki

The PMLSaveData class is used to add custom save data to the game's save file via a byte array.

Creating PMLSaveDatas

PMLSaveData classes inherit from PulsarModLoader.SaveData.PMLSaveData. They contain 4 overridable objects:

  • A VersionID uint which starts at 0. PML handles saving and loading this value, and passes it as an argument when loading. It should be used when the mod updates to maintain mismatched version compatability.
  • An Identifier string used to differentiate between multiple savedata classes in the same mod.
  • A method for loading data, LoadData()
  • A method for saving data, SaveData()
using PulsarModLoader.SaveData;
using System.IO;

namespace MyNamespace
{
    class MySaveData : PMLSaveData
    {
        //public override uint VersionID => 0; //This is optional. It defaults to 0, and can be ignored until you need it.

        public override string Identifier()
        {
            return "MySaveDataIdentifier";
        }

        public override void LoadData(byte[] Data, uint VersionID)
        {
            //your data should be read here
        }

        public override byte[] SaveData()
        {
            //your savedata should be written here as a byte array.
            return new byte[0];
        }
    }
}

Examples

The following are simple ways to use the LoadData and SaveData overrides. There are many ways to handle this so long as byte arrays are returned via savedata. These examples utilize BinaryWriters and BinaryReaders to handle the data.

Using SaveData()

Wrap your MemoryStream and BinaryReader in a using block to handle closing and releasing used resources. The Write() method will write most system types such as integers of various bit sizes, strings, and booleans. Data is written to the stream in byte format meaning booleans take up 8 bits rather than 1, and strings will take up the remaining byte to finish off.

public override byte[] SaveData()
{
    using (MemoryStream stream = new MemoryStream())
    {
        using (BinaryWriter binaryWriter = new BinaryWriter(stream))
        {
            //binarywriter.Write(...
            //your savedata should be written here via the binarywriter.
        }
        return stream.ToArray();
    }
}

Using LoadData()

The BinaryReader class contains many methods for reading binary data, to include integers of various bit sizes, strings, and booleans.

public override void LoadData(byte[] Data, uint VersionID)
{
    using (MemoryStream dataStream = new MemoryStream(Data))
    {
        using (BinaryReader binaryReader = new BinaryReader(dataStream))
        {
            //binaryreader.ReadInt32()...
            //your savedata should be read here via the binaryreader.
        }
    }
}