Created
December 22, 2019 19:51
-
-
Save tudddorrr/60df1e93d814966b7170ad88475115a4 to your computer and use it in GitHub Desktop.
Old way of doing serialisable world objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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