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; }
private static BinaryFormatter CreateBinaryFormatter() { BinaryFormatter form = new BinaryFormatter(); SurrogateSelector ss = new SurrogateSelector(); { Vector3IntSerializationSurrogate v3iss = new Vector3IntSerializationSurrogate(); ss.AddSurrogate(typeof(Vector3Int), new StreamingContext(StreamingContextStates.All), v3iss); } { Vector3SerializationSurrogate v3ss = new Vector3SerializationSurrogate(); ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); } { QuaternionSerializationSurrogate qss = new QuaternionSerializationSurrogate(); ss.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), qss); } { Vector2SerializationSurrogate v2ss = new Vector2SerializationSurrogate(); ss.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), v2ss); } form.SurrogateSelector = ss; return(form); }
// 读取一个对象 private static object loadObjData(FileStream fs, int offset, int length) { Debug.Log(string.Format("load offset {0}, len {1}", offset, length)); int len = 0; byte[] readData = new byte[length]; fs.Seek(offset, SeekOrigin.Begin); len = fs.Read(readData, 0, length); MemoryStream ms = new MemoryStream(readData); ms.Seek(0, SeekOrigin.Begin); len = (int)ms.Length; Debug.Log(string.Format("mem len {0}", len)); IFormatter formater = new BinaryFormatter(); // 处理Vector2 SurrogateSelector ss = new SurrogateSelector(); Vector2SerializationSurrogate v2ss = new Vector2SerializationSurrogate(); ss.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), v2ss); formater.SurrogateSelector = ss; object result = formater.Deserialize(ms); ms.Close(); return(result); }
/// <summary> /// Saves the Level state as completed. /// </summary> /// <param name="i_path">The World path.</param> /// <param name="i_levelPos">The Level position.</param> public void CompleteLevel(string i_path, Vector2 i_levelPos) { //Sets up the FileStream and BinaryFormatter FileStream file = File.Open(i_path + Path.DirectorySeparatorChar + "Levels" + Path.DirectorySeparatorChar + (int)i_levelPos.x + "x" + (int)i_levelPos.y + Path.DirectorySeparatorChar + (int)i_levelPos.x + "x" + (int)i_levelPos.y + LEVEL_EXT, FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); SurrogateSelector ss = new SurrogateSelector(); Vector2SerializationSurrogate v2Ss = new Vector2SerializationSurrogate(); LevelSerializationSurrogate levelSs = new LevelSerializationSurrogate(); StreamingContext sc = new StreamingContext(StreamingContextStates.All); ss.AddSurrogate(typeof(Vector2), sc, v2Ss); ss.AddSurrogate(typeof(Level), sc, levelSs); bf.SurrogateSelector = ss; //Loads the Level, updates completed value, and saves the Level. Level auxLevel; using (file) { auxLevel = ((Level)(bf.Deserialize(file))); auxLevel._completed = true; file.Position = 0; bf.Serialize(file, auxLevel); } }
public static BinaryFormatter GetBinaryFormatter() { BinaryFormatter formatter = new BinaryFormatter(); SurrogateSelector selector = new SurrogateSelector(); Vector2SerializationSurrogate vector2Surrogate = new Vector2SerializationSurrogate(); selector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), vector2Surrogate); formatter.SurrogateSelector = selector; return(formatter); }
public static T DeepClone <T>(T obj) { using (var ms = new MemoryStream()) { var formatter = new BinaryFormatter(); SurrogateSelector surrogateSelector = new SurrogateSelector(); Vector2SerializationSurrogate vector2SS = new Vector2SerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), vector2SS); formatter.SurrogateSelector = surrogateSelector; formatter.Serialize(ms, obj); ms.Position = 0; return((T)formatter.Deserialize(ms)); } }
public static T Copy <T>(T obj) { object copy = null; using (var ms = new MemoryStream()) { var formatter = new BinaryFormatter(); var ss = new SurrogateSelector(); var vec2S = new Vector2SerializationSurrogate(); ss.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), vec2S); formatter.SurrogateSelector = ss; formatter.Serialize(ms, obj); ms.Position = 0; copy = (T)formatter.Deserialize(ms); ms.Close(); } return((T)copy); }
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); }
public void Load() { // Load Player Information BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/player.dat", FileMode.Open); pData = bf.Deserialize(file) as PlayerData; file.Close(); SurrogateSelector surrogateSelector = new SurrogateSelector(); Vector2SerializationSurrogate vector2SS = new Vector2SerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), vector2SS); bf.SurrogateSelector = surrogateSelector; file = File.Open(Application.persistentDataPath + "/solar_system.dat", FileMode.Open); sData = bf.Deserialize(file) as SolarSystem; file.Close(); }
public void Save() { // Save Player information BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Create(Application.persistentDataPath + "/player.dat"); bf.Serialize(file, pData); file.Close(); SurrogateSelector surrogateSelector = new SurrogateSelector(); Vector2SerializationSurrogate vector2SS = new Vector2SerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), vector2SS); bf.SurrogateSelector = surrogateSelector; file = File.Create(Application.persistentDataPath + "/solar_system.dat"); bf.Serialize(file, sData); file.Close(); }
// 写入一个对象 private static int flushObjData(object obj, FileStream fs, int offset) { MemoryStream ms = new MemoryStream(); int len = 0; // flush data IFormatter formater = new BinaryFormatter(); // 处理Vector2 SurrogateSelector ss = new SurrogateSelector(); Vector2SerializationSurrogate v2ss = new Vector2SerializationSurrogate(); ss.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), v2ss); formater.SurrogateSelector = ss; formater.Serialize(ms, obj); len = (int)ms.Length; // 写入长度 fs.Write(BitConverter.GetBytes(len), 0, 4); fs.Write(ms.GetBuffer(), 0, (int)ms.Length); // 转入fileStream fs.Flush(); ms.Close(); return(len); }
/// <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); } } }
/// <summary> /// Loads the Levels in a previously generated World. /// </summary> /// <param name="i_path">The World path.</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 LoadLevels(string i_path, 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(); StreamingContext sc = new StreamingContext(StreamingContextStates.All); ss.AddSurrogate(typeof(World), sc, worldSs); ss.AddSurrogate(typeof(Vector2), sc, v2Ss); ss.AddSurrogate(typeof(Level), sc, levelSs); World aux; bf.SurrogateSelector = ss; //Loads the World. using (file) { aux = ((World)(bf.Deserialize(file))); } yield return(null); //Creates the Level dictionary. aux._levels = new Dictionary <Vector2, Level>(); float progress = 0; //For each column. for (int x = 0; x < aux._imageDivisionConfig[0]; x++) { //For each row. for (int y = 0; y < aux._imageDivisionConfig[1]; y++) { Level auxLevel; //Loads the Level. file = File.Open(i_path + Path.DirectorySeparatorChar + "Levels" + Path.DirectorySeparatorChar + x + "x" + y + Path.DirectorySeparatorChar + x + "x" + y + LEVEL_EXT, FileMode.Open); using (file) { auxLevel = ((Level)(bf.Deserialize(file))); } //Passes the progress value to the callback. progress += 1 / (float)(aux._imageDivisionConfig[0] * aux._imageDivisionConfig[1]); i_progressCallback(progress); yield return(null); //Adds the Level to the dictionary. aux._levels.Add(new Vector2(x, y), auxLevel); //Loads the Level image. Texture2D auxText = new Texture2D(4, 4); auxText.LoadImage(File.ReadAllBytes(i_path + Path.DirectorySeparatorChar + "Levels" + Path.DirectorySeparatorChar + x + "x" + y + Path.DirectorySeparatorChar + x + "x" + y + ".jpg")); auxLevel._img = auxText; } } //Passes the world to the callback. i_worldCallback(aux); }
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; }