static void CreateSurrogates() { ss = new SurrogateSelector(); //VECTOR2 Vector2SerializationSurrogate v2_ss = new Vector2SerializationSurrogate(); ss.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), v2_ss); //VECTOR3 Vector3SerializationSurrogate v3_ss = new Vector3SerializationSurrogate(); ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3_ss); //VECTOR4 Vector4SerializationSurrogate v4_ss = new Vector4SerializationSurrogate(); ss.AddSurrogate(typeof(Vector4), new StreamingContext(StreamingContextStates.All), v4_ss); //QUATERNION QuaternionSerializationSurrogate q_ss = new QuaternionSerializationSurrogate(); ss.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), q_ss); //COLOR AND COLOR32 ColorSerializationSurrogate color_ss = new ColorSerializationSurrogate(); ss.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), color_ss); ss.AddSurrogate(typeof(Color32), new StreamingContext(StreamingContextStates.All), color_ss); //TEXTURE2D Texture2DSerializationSurrogate texture_ss = new Texture2DSerializationSurrogate(); ss.AddSurrogate(typeof(Texture2D), new StreamingContext(StreamingContextStates.All), texture_ss); Texture2DCompressionType_old = Texture2DCompressionType; }
public static BinaryFormatter GetBinaryFormatter() { BinaryFormatter bf = new BinaryFormatter(); SurrogateSelector surrogateSelector = new SurrogateSelector(); Vector3IntSerializationSurrogate vector3SS = new Vector3IntSerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Vector3Int), new StreamingContext(StreamingContextStates.All), vector3SS); ColorSerializationSurrogate colour3SS = new ColorSerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), colour3SS); BuffSerializationSurrogate buffSerializationSurrogate = new BuffSerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Buff), new StreamingContext(StreamingContextStates.All), buffSerializationSurrogate); CardSerializationSurrogate cardSerializationSurrogate = new CardSerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Card), new StreamingContext(StreamingContextStates.All), cardSerializationSurrogate); EquipmentSerializationSurrogate equipmentSerializationSurrogate = new EquipmentSerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Equipment), new StreamingContext(StreamingContextStates.All), equipmentSerializationSurrogate); bf.SurrogateSelector = surrogateSelector; return(bf); }
public static BinaryFormatter GetBinaryFormatter() { BinaryFormatter formatter = new BinaryFormatter(); SurrogateSelector selector = new SurrogateSelector(); Vector3SerializationSurrogate vector3Surrogate = new Vector3SerializationSurrogate(); ColorSerializationSurrogate colorSurrogate = new ColorSerializationSurrogate(); selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3Surrogate); selector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), colorSurrogate); formatter.SurrogateSelector = selector; return(formatter); }
public static SaveGame Load() { BinaryFormatter bf = new BinaryFormatter(); //surrogate serializer setup. SurrogateSelector ss = new SurrogateSelector(); ColorSerializationSurrogate css = new ColorSerializationSurrogate(); ss.AddSurrogate(typeof(Color),new StreamingContext(StreamingContextStates.All),css); //use surrogate selector. bf.SurrogateSelector = ss; FileStream file = File.Open(Application.persistentDataPath+"/saveGame.serious",FileMode.Open); SaveGame loadedGame = (SaveGame)bf.Deserialize(file); file.Close(); return loadedGame; }
private static BinaryFormatter CreatePaintingBinaryFormatter() { BinaryFormatter bf = new BinaryFormatter(); SurrogateSelector ss = new SurrogateSelector(); StreamingContext sc = new StreamingContext(StreamingContextStates.All); ColorSerializationSurrogate css = new ColorSerializationSurrogate(); Vector3SerializationSurrogate vss = new Vector3SerializationSurrogate(); // any non-serializable class needs to have a serialization surrogate ss.AddSurrogate(typeof(Color), sc, css); ss.AddSurrogate(typeof(Vector3), sc, vss); bf.SurrogateSelector = ss; return(bf); }
public static void Save(SaveGame save) { BinaryFormatter bf = new BinaryFormatter(); //surrogate serializer setup. SurrogateSelector ss = new SurrogateSelector(); ColorSerializationSurrogate css = new ColorSerializationSurrogate(); ss.AddSurrogate(typeof(Color),new StreamingContext(StreamingContextStates.All),css); //use surrogate selector. bf.SurrogateSelector = ss; FileStream file = File.Create(Application.persistentDataPath+"/saveGame.serious"); bf.Serialize(file, save); file.Close (); Debug.Log(Application.persistentDataPath+"/saveGame.serious"); }
public static BinaryFormatter GetBinaryFormatter() { BinaryFormatter bf = new BinaryFormatter(); SurrogateSelector surrogateSelector = new SurrogateSelector(); Vector3IntSerializationSurrogate vector3SS = new Vector3IntSerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Vector3Int), new StreamingContext(StreamingContextStates.All), vector3SS); ColorSerializationSurrogate colour3SS = new ColorSerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), colour3SS); bf.SurrogateSelector = surrogateSelector; return(bf); }
static BinaryFormatter GetBinaryFormatter() { var formatter = new BinaryFormatter(); var selector = new SurrogateSelector(); var v3Surrogate = new Vector3SerializationSurrogate(); var qSurrogate = new QuaternionSerializationSurrogate(); var cSurrogate = new ColorSerializationSurrogate(); selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3Surrogate); selector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), qSurrogate); selector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), cSurrogate); formatter.SurrogateSelector = selector; return(formatter); }
public static void AddAllUnitySurrogate(this SurrogateSelector surrogateSelector) { var colorSS = new ColorSerializationSurrogate(); var quaternionSS = new QuaternionSerializationSurrogate(); var vector2IntSS = new Vector2IntSerializationSurrogate(); var vector2SS = new Vector2SerializationSurrogate(); var vector3IntSS = new Vector3IntSerializationSurrogate(); var vector3SS = new Vector3SerializationSurrogate(); var vector4SS = new Vector4SerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), colorSS); surrogateSelector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), quaternionSS); surrogateSelector.AddSurrogate(typeof(Vector2Int), new StreamingContext(StreamingContextStates.All), vector2IntSS); surrogateSelector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), vector2SS); surrogateSelector.AddSurrogate(typeof(Vector3Int), new StreamingContext(StreamingContextStates.All), vector3IntSS); surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SS); surrogateSelector.AddSurrogate(typeof(Vector4), new StreamingContext(StreamingContextStates.All), vector4SS); }
/// <summary> /// Loads a Level in a previously generated World. /// </summary> /// <param name="i_path">The World path.</param> /// <param name="i_pos">>The Level position.</param> /// <param name="i_worldCallback">The callback used to catch the loaded world.</param> /// <param name="i_progressCallback">The callback used to catch the progress value.</param> public IEnumerator LoadLevel(string i_path, Vector2 i_pos, Action <World> i_worldCallback, Action <float> i_progressCallback) { if (!Directory.Exists(i_path)) { yield break; } string[] files = Directory.GetFiles(i_path); if (files.Length != 2 || !(Path.GetExtension(files[0]).ToLower().Equals(".jpg") || Path.GetExtension(files[0]).ToLower().Equals(WORLD_EXT)) || !(Path.GetExtension(files[1]).ToLower().Equals(".jpg") || Path.GetExtension(files[1]).ToLower().Equals(WORLD_EXT)) || Path.GetExtension(files[0]).ToLower().Equals(Path.GetExtension(files[1]).ToLower())) { yield break; } string path1 = files[0].Remove(files[0].Length - Path.GetExtension(files[0]).Length, Path.GetExtension(files[0]).Length); string path2 = files[1].Remove(files[1].Length - Path.GetExtension(files[1]).Length, Path.GetExtension(files[1]).Length); if (!path1.Equals(path2)) { yield break; } //Sets up the FileStream and BinaryFormatter. FileStream file = File.Open(path1 + WORLD_EXT, FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); SurrogateSelector ss = new SurrogateSelector(); //Used for World serialization. WorldSerializationSurrogate worldSs = new WorldSerializationSurrogate(); //Used for Level serialization. Vector2SerializationSurrogate v2Ss = new Vector2SerializationSurrogate(); LevelSerializationSurrogate levelSs = new LevelSerializationSurrogate(); //Used for LevelCell serialization. ColorSerializationSurrogate colorSs = new ColorSerializationSurrogate(); LevelCellSerializationSurrogate levelCellSs = new LevelCellSerializationSurrogate(); StreamingContext sc = new StreamingContext(StreamingContextStates.All); ss.AddSurrogate(typeof(World), sc, worldSs); ss.AddSurrogate(typeof(Vector2), sc, v2Ss); ss.AddSurrogate(typeof(Level), sc, levelSs); ss.AddSurrogate(typeof(Color), sc, colorSs); ss.AddSurrogate(typeof(LevelCell), sc, levelCellSs); bf.SurrogateSelector = ss; World aux; //Loads the World. using (file) { aux = ((World)(bf.Deserialize(file))); } yield return(null); //Creates the Level dictionary. aux._levels = new Dictionary <Vector2, Level>(); Level auxLevel; //Loads the Level. file = File.Open(i_path + Path.DirectorySeparatorChar + "Levels" + Path.DirectorySeparatorChar + (int)i_pos.x + "x" + (int)i_pos.y + Path.DirectorySeparatorChar + (int)i_pos.x + "x" + (int)i_pos.y + LEVEL_EXT, FileMode.Open); using (file) { auxLevel = ((Level)(bf.Deserialize(file))); } aux._levels.Add(i_pos, auxLevel); yield return(null); //Creates the LevelCell dictionary. auxLevel._cells = new Dictionary <Vector2, LevelCell>(); float progress = 0; //For each column for (int i = 0; i < ImgProcessManager.Instance._mapSize; i++) { //For each row. for (int j = 0; j < ImgProcessManager.Instance._mapSize; j++) { LevelCell auxCell; Texture2D auxText; //Loads the LevelCell. file = File.Open(i_path + Path.DirectorySeparatorChar + "Levels" + Path.DirectorySeparatorChar + (int)i_pos.x + "x" + (int)i_pos.y + Path.DirectorySeparatorChar + "Cells" + Path.DirectorySeparatorChar + i + "x" + j + Path.DirectorySeparatorChar + i + "x" + j + LEVEL_CELL_EXT, FileMode.Open); using (file) { auxCell = ((LevelCell)(bf.Deserialize(file))); } auxLevel._cells.Add(new Vector2(i, j), auxCell); //Passes the progress value to the callback. progress += 1 / (float)(ImgProcessManager.Instance._mapSize * ImgProcessManager.Instance._mapSize); i_progressCallback(progress); yield return(null); //Loads the LevelCell image. auxText = new Texture2D(4, 4); auxText.LoadImage(File.ReadAllBytes(i_path + Path.DirectorySeparatorChar + "Levels" + Path.DirectorySeparatorChar + (int)i_pos.x + "x" + (int)i_pos.y + Path.DirectorySeparatorChar + "Cells" + Path.DirectorySeparatorChar + i + "x" + j + Path.DirectorySeparatorChar + i + "x" + j + ".jpg")); auxCell._img = auxText; yield return(null); } } //Passes the World to the callback. i_worldCallback(aux); }
/// <summary> /// Saves the a World that has been previously processed. /// </summary> /// <param name="i_world">The World to save.</param> public IEnumerator SaveWorld(World i_world) { //Creates the folder name. string dirName; do { dirName = Path.GetRandomFileName().Replace(".", string.Empty); }while (Directory.Exists(Application.persistentDataPath + Path.DirectorySeparatorChar + dirName)); dirName = Application.persistentDataPath + Path.DirectorySeparatorChar + dirName; Directory.CreateDirectory(dirName); //Sets up the FileStream and BinaryFormatter. FileStream file = null; BinaryFormatter bf = new BinaryFormatter(); SurrogateSelector ss = new SurrogateSelector(); //Used for World serialization. WorldSerializationSurrogate worldSs = new WorldSerializationSurrogate(); //Used for Level serialization. Vector2SerializationSurrogate v2Ss = new Vector2SerializationSurrogate(); LevelSerializationSurrogate levelSs = new LevelSerializationSurrogate(); //Used for LevelCell serialization. ColorSerializationSurrogate colorSs = new ColorSerializationSurrogate(); LevelCellSerializationSurrogate levelCellSs = new LevelCellSerializationSurrogate(); StreamingContext sc = new StreamingContext(StreamingContextStates.All); ss.AddSurrogate(typeof(World), sc, worldSs); ss.AddSurrogate(typeof(Vector2), sc, v2Ss); ss.AddSurrogate(typeof(Level), sc, levelSs); ss.AddSurrogate(typeof(Color), sc, colorSs); ss.AddSurrogate(typeof(LevelCell), sc, levelCellSs); bf.SurrogateSelector = ss; file = File.Create(dirName + Path.DirectorySeparatorChar + i_world._name + WORLD_EXT); //Saves the World object. using (file) { bf.Serialize(file, i_world); } yield return(null); //Saves the World image. File.WriteAllBytes(dirName + Path.DirectorySeparatorChar + i_world._name + ".jpg", i_world._img.EncodeToJPG()); yield return(null); //Creates the "Levels" directory. dirName = dirName + Path.DirectorySeparatorChar + "Levels"; Directory.CreateDirectory(dirName); //For each Level in the World. foreach (KeyValuePair <Vector2, Level> levelDictEntry in i_world._levels) { //Creates the "columnxrow" directory for the Level. Directory.CreateDirectory(dirName + Path.DirectorySeparatorChar + (int)levelDictEntry.Key.x + "x" + (int)levelDictEntry.Key.y); //Saves the Level object. file = File.Create(dirName + Path.DirectorySeparatorChar + (int)levelDictEntry.Key.x + "x" + (int)levelDictEntry.Key.y + Path.DirectorySeparatorChar + (int)levelDictEntry.Key.x + "x" + (int)levelDictEntry.Key.y + LEVEL_EXT); using (file) { bf.Serialize(file, levelDictEntry.Value); } yield return(null); //Saves the Level image. File.WriteAllBytes(dirName + Path.DirectorySeparatorChar + (int)levelDictEntry.Key.x + "x" + (int)levelDictEntry.Key.y + Path.DirectorySeparatorChar + (int)levelDictEntry.Key.x + "x" + (int)levelDictEntry.Key.y + ".jpg", levelDictEntry.Value._img.EncodeToJPG()); yield return(null); //Creates the "Cells" directory. Directory.CreateDirectory(dirName + Path.DirectorySeparatorChar + (int)levelDictEntry.Key.x + "x" + (int)levelDictEntry.Key.y + Path.DirectorySeparatorChar + "Cells"); //For each LevelCell. foreach (KeyValuePair <Vector2, LevelCell> levelCellDictEntry in levelDictEntry.Value._cells) { //Creates the "columnxrow" directory for the LevelCell. Directory.CreateDirectory(dirName + Path.DirectorySeparatorChar + (int)levelDictEntry.Key.x + "x" + (int)levelDictEntry.Key.y + Path.DirectorySeparatorChar + "Cells" + Path.DirectorySeparatorChar + (int)levelCellDictEntry.Key.x + "x" + (int)levelCellDictEntry.Key.y); //Saves the LevelCell object. file = File.Create(dirName + Path.DirectorySeparatorChar + (int)levelDictEntry.Key.x + "x" + (int)levelDictEntry.Key.y + Path.DirectorySeparatorChar + "Cells" + Path.DirectorySeparatorChar + (int)levelCellDictEntry.Key.x + "x" + (int)levelCellDictEntry.Key.y + Path.DirectorySeparatorChar + (int)levelCellDictEntry.Key.x + "x" + (int)levelCellDictEntry.Key.y + LEVEL_CELL_EXT); using (file) { bf.Serialize(file, levelCellDictEntry.Value); } yield return(null); //Saves the LevelCell image. File.WriteAllBytes(dirName + Path.DirectorySeparatorChar + (int)levelDictEntry.Key.x + "x" + (int)levelDictEntry.Key.y + Path.DirectorySeparatorChar + "Cells" + Path.DirectorySeparatorChar + (int)levelCellDictEntry.Key.x + "x" + (int)levelCellDictEntry.Key.y + Path.DirectorySeparatorChar + (int)levelCellDictEntry.Key.x + "x" + (int)levelCellDictEntry.Key.y + ".jpg", levelCellDictEntry.Value._img.EncodeToJPG()); yield return(null); } } }
static void CreateSurrogates() { ss = new SurrogateSelector(); //VECTOR2 Vector2SerializationSurrogate v2_ss = new Vector2SerializationSurrogate(); ss.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), v2_ss); //VECTOR3 Vector3SerializationSurrogate v3_ss = new Vector3SerializationSurrogate(); ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3_ss); //VECTOR4 Vector4SerializationSurrogate v4_ss = new Vector4SerializationSurrogate(); ss.AddSurrogate(typeof(Vector4), new StreamingContext(StreamingContextStates.All), v4_ss); //QUATERNION QuaternionSerializationSurrogate q_ss = new QuaternionSerializationSurrogate(); ss.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), q_ss); //COLOR AND COLOR32 ColorSerializationSurrogate color_ss = new ColorSerializationSurrogate(); ss.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), color_ss); ss.AddSurrogate(typeof(Color32), new StreamingContext(StreamingContextStates.All), color_ss); //TEXTURE2D Texture2DSerializationSurrogate texture_ss = new Texture2DSerializationSurrogate(); ss.AddSurrogate(typeof(Texture2D), new StreamingContext(StreamingContextStates.All), texture_ss); Texture2DCompressionType_old = Texture2DCompressionType; }