public static T Deserialize <T> (byte[] data) { DebugUtils.Assert(data != null && data.Length > 0); T dataObject; using (MemoryStream m = new MemoryStream(data)) { dataObject = (T)serializer.Deserialize(m, null, typeof(T)); } return(dataObject); }
/* * public string RootPath * { * get{ #if UNITY_EDITOR * string rootPath = Application.dataPath ; #else * string rootPath = Application.persistentDataPath; #endif * string folder = rootPath + protoPath; * return folder; * } * } * * public T LoadProtoData<T> (string name) * { * //string md5Name = CommonUtils.GenerateMD5 (Encoding.UTF8.GetBytes (name)); * * string path = RootPath + name + ".bytes"; * * Debug.Assert(File.Exists(path),"No file at path :" + path); * * DebugUtils.Log (DebugUtils.Type.Data, "load proto file " + name + ", path = " + path); * byte[] data = File.ReadAllBytes (path); * return Deserialize<T> (data); * } * * public void SavaProtoData (byte[] data, string fileName) * { * Stream sw; * * //string md5Name = CommonUtils.GenerateMD5 (Encoding.UTF8.GetBytes (fileName)); * * string folder = RootPath ; * if(!Directory.Exists(folder)){ * Directory.CreateDirectory(folder); * } * * string path = folder + fileName + ".bytes"; * DebugUtils.Log (DebugUtils.Type.Data, "path: " + path + " originName : " + fileName); * FileInfo f = new FileInfo (path); * if (f.Exists) { * f.Delete (); * f = new FileInfo (path); * sw = f.Create (); * } else { * sw = f.Create (); * } * sw.Write (data, 0, data.Length); * sw.Close (); * sw.Dispose (); * } */ /* * public static byte[] Serialize<T>( T dataObject ) * { * DebugUtils.Assert( dataObject != null ); * byte[] buffer = null; * using( MemoryStream m = new MemoryStream() ) // use a cached memory stream instead? * { * Serializer.Serialize<T>( m, dataObject ); * m.Position = 0; * int length = (int)m.Length; * buffer = new byte[length]; * m.Read( buffer, 0, length ); * } * return buffer; * } * * public static T Deserialize<T>( byte[] data ) * { * DebugUtils.Assert( data != null && data.Length > 0 ); * T dataObject; * using( MemoryStream m = new MemoryStream( data ) ) * { * dataObject = Serializer.Deserialize<T>( m ); * } * return dataObject; * } */ public static byte[] Serialize(object dataObject) { DebugUtils.Assert(dataObject != null); byte[] buffer = null; using (MemoryStream m = new MemoryStream()) { // use a cached memory stream instead? serializer.Serialize(m, dataObject); m.Position = 0; int length = (int)m.Length; buffer = new byte[length]; m.Read(buffer, 0, length); } return(buffer); }
// @Test this needs to be tested, I doubt it works. public void RemoveListener <T>(Action <T> listener) where T : TBase { Type t = typeof(T); DebugUtils.Assert(eventTable.ContainsKey(t)); Action <TBase> action = (obj) => { var cast = (T)Convert.ChangeType(obj, t); listener(cast); }; eventTable[t].Remove(action); }
static AcosLut() { DebugUtils.Assert(table.Length == (COUNT + 1)); }