[Serializable()] public class NodesV1 { public Dictionary<short, string> Name;// public Dictionary<short, string> Text;// }
short CalcNewItemIndex() { short Index = -1; // while (Nodes.Name.ContainsKey(++Index)); // return Index; // }
public class SaveLoad_Data { BinaryFormatter bformatter = new BinaryFormatter(); public void Save(object data, string filepath)// { Stream stream = File.Open(filepath + ".txt", FileMode.Create);// BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Binder = new ClassBinder();// bformatter.Serialize(stream, data);//C stream.Close();// } public object Load(string filepath)// { byte[] data = File.ReadAllBytes(filepath + ".txt");// MemoryStream stream = new MemoryStream(data);// bformatter.Binder = new ClassBinder();// NodesV1 _NodesV1 = (NodesV1)bformatter.Deserialize(stream);// stream.Close();// return _NodesV1;// } } public sealed class ClassBinder : SerializationBinder // { public override Type BindToType(string assemblyName, string typeName) { if (!string.IsNullOrEmpty(assemblyName) && !string.IsNullOrEmpty(typeName)) { Type typeToDeserialize = null; assemblyName = Assembly.GetExecutingAssembly().FullName; typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName)); return typeToDeserialize; } return null; } }
NodesV1 Nodes;// , private void Form1_Load(object sender, EventArgs e) // { Nodes = new NodesV1();// , // Nodes.Name = new Dictionary<short, string>(); Nodes.Text = new Dictionary<short, string>(); } private void button1_Click(object sender, EventArgs e)// { SaveLoad_Data _SaveNodes = new SaveLoad_Data();// _SaveNodes.Save(Nodes, @"C:\UnityProjects\Blueprint_Editor_Plugin\Assets\Resources\HabrSerialisText");// } private void button2_Click(object sender, EventArgs e)// { SaveLoad_Data _LoadNodes = new SaveLoad_Data();// Nodes = (NodesV1)_LoadNodes.Load(@"C:\UnityProjects\Blueprint_Editor_Plugin\Assets\Resources\HabrSerialisText"); // }
public class SaveLoad_Data { BinaryFormatter bformatter = new BinaryFormatter(); public void Save(object data, string filepath)// { int Version;// BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Binder = new ClassBinder();// MemoryStream streamReader = new MemoryStream(); bformatter.Serialize(streamReader, data);//C Version = Convert.ToInt32(data.GetType().ToString().Replace("HabrSerialis.NodesV", ""));// byte[] arr = streamReader.ToArray();// byte[] versionBytes = BitConverter.GetBytes(Version);// byte[] result = new byte[arr.Length + 4]; // // , . int - 4 Array.Copy(arr, 0, result, 4, arr.Length);// Array.Copy(versionBytes, 0, result, 0, versionBytes.Length);// File.WriteAllBytes(filepath + ".txt", result);// streamReader.Close();// } public object Load(string filepath)// { byte[] back = File.ReadAllBytes(filepath + ".txt");// int versionBack = BitConverter.ToInt32(back, 0);// byte[] data = new byte[back.Length - 4]; // Array.Copy(back, 4, data, 0, back.Length - 4);// MemoryStream stream = new MemoryStream(data);// bformatter.Binder = new ClassBinder();// if (versionBack == 1)// 1 { NodesV1 _NodesV1 = (NodesV1)bformatter.Deserialize(stream);// 1 stream.Close();// return _NodesV1; } return null; } } public sealed class ClassBinder : SerializationBinder { public override Type BindToType(string assemblyName, string typeName) { if (!string.IsNullOrEmpty(assemblyName) && !string.IsNullOrEmpty(typeName)) { Type typeToDeserialize = null; assemblyName = Assembly.GetExecutingAssembly().FullName; typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName)); return typeToDeserialize; } return null; } }
[Serializable()] public class NodesV2 { public Dictionary<short, string> Name; public Dictionary<short, string> Text; public Dictionary<short, bool> Perm; }
Nodes.Perm = new Dictionary<short, bool>();
if (versionBack == 1)// 1 { NodesV1 _NodesV1 = (NodesV1)bformatter.Deserialize(stream);// 1 stream.Close();// NodesV2 NodeV2ret = new NodesV2();// NodeV2ret.Name = _NodesV1.Name; // NodeV2ret.Text = _NodesV1.Text; // NodeV2ret.Perm = new Dictionary<short, bool>(); // 1 Perm foreach (KeyValuePair<short, string> name in NodeV2ret.Name) { NodeV2ret.Perm.Add(name.Key, false);// false } return NodeV2ret; // } else if (versionBack == 2)// 2 - ( ) { NodesV2 _NodesV2 = (NodesV2)bformatter.Deserialize(stream);// stream.Close();// return _NodesV2; }
using UnityEngine; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; using System.Reflection; public class HabrSerialis : MonoBehaviour { NodesV2 Nodes; SaveLoad_Data _LoadNodes; void Start() { Nodes = new NodesV2(); _LoadNodes = new SaveLoad_Data(); Nodes = (NodesV2)_LoadNodes.Load("HabrSerialisText"); } float Offset; void OnGUI() { Offset = 100; for (short i = 0; i < Nodes.Name.Count; i++) { Nodes.Name[i] = GUI.TextField(new Rect(Offset, 100, 100, 30), Nodes.Name[i]); Nodes.Text[i] = GUI.TextArea(new Rect(Offset, 130, 100, 200), Nodes.Text[i]); Offset += 120; } if (GUI.Button(new Rect(10, 10, 70, 30), "Save")) { _LoadNodes.Save(Nodes, "HabrSerialisText"); } } } [Serializable()] public class NodesV1 { public Dictionary<short, string> Name; public Dictionary<short, string> Text; } [Serializable()] public class NodesV2 { public Dictionary<short, string> Name; public Dictionary<short, string> Text; public Dictionary<short, bool> Perm; } public class SaveLoad_Data { private int Version; BinaryFormatter bformatter = new BinaryFormatter(); public void Save(object data, string filepath)// { BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Binder = new ClassBinder();// MemoryStream streamReader = new MemoryStream(); bformatter.Serialize(streamReader, data);//C Version = Convert.ToInt32(data.GetType().ToString().Replace("NodesV", ""));// byte[] arr = streamReader.ToArray(); byte[] versionBytes = BitConverter.GetBytes(Version);// byte[] result = new byte[arr.Length + 4]; // // , . int - 4 Array.Copy(arr, 0, result, 4, arr.Length);// Array.Copy(versionBytes, 0, result, 0, versionBytes.Length);// File.WriteAllBytes("Assets/Resources/" + filepath + ".txt", result);// streamReader.Close();// } public object Load(string filepath)// { TextAsset asset = Resources.Load(filepath) as TextAsset;// byte[] back = asset.bytes; int versionBack = BitConverter.ToInt32(back, 0);// byte[] data = new byte[back.Length - 4]; // Array.Copy(back, 4, data, 0, back.Length - 4);// Stream stream = new MemoryStream(data);// bformatter.Binder = new ClassBinder();// //////////////////////////////////////////////////////// if (versionBack == 1)// 1 { NodesV1 _NodesV1 = (NodesV1)bformatter.Deserialize(stream);// 1 stream.Close();// NodesV2 NodeV2ret = new NodesV2();// NodeV2ret.Name = _NodesV1.Name; // NodeV2ret.Text = _NodesV1.Text; // NodeV2ret.Perm = new Dictionary<short, bool>(); // 1 Perm foreach (KeyValuePair<short, string> name in NodeV2ret.Name) { NodeV2ret.Perm.Add(name.Key, false);// } return NodeV2ret;// } else if (versionBack == 2)// 2 - ( ) { NodesV2 _NodesV2 = (NodesV2)bformatter.Deserialize(stream);// stream.Close();// return _NodesV2; } ////////////////////////////////////////////////////////////// return null; } } public sealed class ClassBinder : SerializationBinder { public override Type BindToType(string assemblyName, string typeName) { if (!string.IsNullOrEmpty(assemblyName) && !string.IsNullOrEmpty(typeName)) { Type typeToDeserialize = null; assemblyName = Assembly.GetExecutingAssembly().FullName; typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName)); return typeToDeserialize; } return null; } }
byte[] back = File.ReadAllBytes(filepath + ".txt");
TextAsset asset = Resources.Load(filepath) as TextAsset; byte[] back = asset.bytes;
byte[] back = File.ReadAllBytes("Assets/Resources/" + filepath + ".txt");
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; using System.Reflection; namespace HabrSerialis { public partial class Form1 : Form { public Form1() { InitializeComponent(); } ///////////////////////////////////////////////////////////////////////////////////////////////// short _SelectedNodeID = 0; // NodesV2 Nodes;// , private void Form1_Load(object sender, EventArgs e) // { Nodes = new NodesV2();// , // Nodes.Name = new Dictionary<short, string>(); Nodes.Text = new Dictionary<short, string>(); Nodes.Perm = new Dictionary<short, bool>(); } private void button1_Click(object sender, EventArgs e)// { SaveLoad_Data _SaveNodes = new SaveLoad_Data();// _SaveNodes.Save(Nodes, @"C:\UnityProjects\Blueprint_Editor_Plugin\Assets\Resources\HabrSerialisText"); } private void button2_Click(object sender, EventArgs e)// { SaveLoad_Data _LoadNodes = new SaveLoad_Data(); Nodes = (NodesV2)_LoadNodes.Load(@"C:\UnityProjects\Blueprint_Editor_Plugin\Assets\Resources\HabrSerialisText"); UpdateList();// } /////////////////////////////////////////////////////////////////////////////////// private void listBox1_SelectedIndexChanged(object sender, EventArgs e) // { _SelectedNodeID = (short)listBox1.SelectedIndex; if (Nodes.Name.ContainsKey(_SelectedNodeID))// { textBox1.Text = Nodes.Name[_SelectedNodeID];// 1 textBox2.Text = Nodes.Text[_SelectedNodeID];// 2 } } /////////////////////////////////////////////////// private void button3_Click(object sender, EventArgs e)// () { short _NewNodeID = CalcNewItemIndex(); Nodes.Name.Add(_NewNodeID, "New Node name");// Nodes.Text.Add(_NewNodeID, "New Node Text");// Nodes.Perm.Add(_NewNodeID, false);// UpdateList();// listBox1.SelectedIndex = _NewNodeID; } /////////////////////////////////////////////////// private void textBox2_TextChanged(object sender, EventArgs e) { Nodes.Text[_SelectedNodeID] = textBox2.Text;// } /////////////////////////////////////////////////// private void textBox1_TextChanged(object sender, EventArgs e)// { Nodes.Name[_SelectedNodeID] = textBox1.Text;// listBox1.Items[_SelectedNodeID] = "ID: " + _SelectedNodeID + " " + textBox1.Text;// } /////////////////////////////////////////////////// short CalcNewItemIndex()// { short Index = -1; while (Nodes.Name.ContainsKey(++Index)); return Index; } /////////////////////////////////////////////////// void UpdateList()// { listBox1.Items.Clear(); foreach (KeyValuePair<short, string> node in Nodes.Name) { listBox1.Items.Add("ID: " + node.Key + " " + node.Value); } } } } ////////////////////////////////////////////////// [Serializable()] public class NodesV1 { public Dictionary<short, string> Name; public Dictionary<short, string> Text; } [Serializable()] public class NodesV2 { public Dictionary<short, string> Name; public Dictionary<short, string> Text; public Dictionary<short, bool> Perm; } public class SaveLoad_Data { private int Version; BinaryFormatter bformatter = new BinaryFormatter(); public void Save(object data, string filepath) { BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Binder = new ClassBinder();// MemoryStream streamReader = new MemoryStream(); bformatter.Serialize(streamReader, data);//C Version = Convert.ToInt32(data.GetType().ToString().Replace("NodesV", ""));// byte[] arr = streamReader.ToArray(); byte[] versionBytes = BitConverter.GetBytes(Version);// byte[] result = new byte[arr.Length + 4]; // // , . int - 4 Array.Copy(arr, 0, result, 4, arr.Length);// Array.Copy(versionBytes, 0, result, 0, versionBytes.Length);// File.WriteAllBytes(filepath + ".txt", result);// streamReader.Close();// } public object Load(string filepath) { byte[] back = File.ReadAllBytes(filepath + ".txt");// int versionBack = BitConverter.ToInt32(back, 0);// byte[] data = new byte[back.Length - 4]; // Array.Copy(back, 4, data, 0, back.Length - 4);// MemoryStream stream = new MemoryStream(data);// bformatter.Binder = new ClassBinder();// //////////////////// //////////////////////////////////// if (versionBack == 1)// 1 { NodesV1 _NodesV1 = (NodesV1)bformatter.Deserialize(stream);// 1 stream.Close();// NodesV2 NodeV2ret = new NodesV2();// NodeV2ret.Name = _NodesV1.Name; // NodeV2ret.Text = _NodesV1.Text; // NodeV2ret.Perm = new Dictionary<short, bool>(); // 1 Perm foreach (KeyValuePair<short, string> name in NodeV2ret.Name) { NodeV2ret.Perm.Add(name.Key, false);// } return NodeV2ret; } else if (versionBack == 2)// 2 - ( ) { NodesV2 _NodesV2 = (NodesV2)bformatter.Deserialize(stream);// stream.Close();// return _NodesV2; } ////////////////////////////////////////////////////////////// return null; } } public sealed class ClassBinder : SerializationBinder { public override Type BindToType(string assemblyName, string typeName) { if (!string.IsNullOrEmpty(assemblyName) && !string.IsNullOrEmpty(typeName)) { Type typeToDeserialize = null; assemblyName = Assembly.GetExecutingAssembly().FullName; typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName)); return typeToDeserialize; } return null; } }
Source: https://habr.com/ru/post/242069/
All Articles