Skip to content

Instantly share code, notes, and snippets.

@tudddorrr
Created December 22, 2019 19:51
Show Gist options
  • Select an option

  • Save tudddorrr/60df1e93d814966b7170ad88475115a4 to your computer and use it in GitHub Desktop.

Select an option

Save tudddorrr/60df1e93d814966b7170ad88475115a4 to your computer and use it in GitHub Desktop.
Old way of doing serialisable world objects
public class DirtFragment {
public string Id { get; set; }
public float FertilisationFactor { get; set; }
public int ChanceToFind { get; set; }
public float X { get; set; }
public float Y { get; set; }
public static object Deserialize(byte[] data) {
DirtFragment result = new DirtFragment();
using (MemoryStream m = new MemoryStream(data)) {
using (BinaryReader reader = new BinaryReader(m)) {
result.Id = reader.ReadString();
result.FertilisationFactor = reader.ReadSingle();
result.ChanceToFind = reader.ReadInt32();
result.X = reader.ReadSingle();
result.Y = reader.ReadSingle();
}
}
return result;
}
public static byte[] Serialize(object customType) {
using (MemoryStream m = new MemoryStream()) {
using (BinaryWriter writer = new BinaryWriter(m)) {
DirtFragment t = (DirtFragment)customType;
writer.Write(t.Id);
writer.Write(t.FertilisationFactor);
writer.Write(t.ChanceToFind);
writer.Write(t.X);
writer.Write(t.Y);
}
return m.ToArray();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment