public static void LoadState() { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/recentState.gd", FileMode.Open); SaveLoad.recentState = (GameOp)bf.Deserialize(file); file.Close(); }
/// <summary> /// Deserializes a byte array into a list of Commands. /// </summary> /// <param name="Source">Source.</param> public static List <Command> Deserialize(byte[] Source) { List <Command> DeserializedCommands = new List <Command>(); //The deserialization process using (MemoryStream m = new MemoryStream(Source)) { using (BinaryReader reader = new BinaryReader(m)) { //As long as there are still bytes to read, there are still Commands to deserialize while (reader.BaseStream.Position < reader.BaseStream.Length) { //First, let's get the amount of bytes in this Command's package, a byte that we serializd. byte DataLength = reader.ReadByte(); //Then create an int that represents how many bytes of the Command's package we've read. int curRead = 1; //Create the Command from the deserialized operation GameOp operation = (GameOp)reader.ReadByte(); Command com = new Command(operation); //Don't forget to increase curRead curRead++; //As long as we haven't read more than the amount in the Command's package, there are more variables. while (curRead < DataLength) { //Get the type of data to deserialize DataType type = (DataType)reader.ReadByte(); curRead++; //Plug it into the magical switch of deserialization; deserialization is hard-coded here. switch (type) { case DataType.Position: com.SerialPosition = new Vector2d( FInt.Create((long)reader.ReadUInt64()), FInt.Create((long)reader.ReadUInt64()) ); //Make sure to increase curRead by the amount of bytes read curRead += 16; break; } } //Now that we've deserialized the Command, let's add it into the list DeserializedCommands.Add(com); } } } //Now let's return the list of commands and be on our way with the simulation! return(DeserializedCommands); }
public Command(GameOp operation) { Operation = operation; }