Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        /*
         * 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);
        }
Ejemplo n.º 3
0
        // @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);
        }
Ejemplo n.º 4
0
 static AcosLut()
 {
     DebugUtils.Assert(table.Length == (COUNT + 1));
 }