public void SaveToFileAndReadBack() { byte[] bytes = RandomData.Build(1024, 512); // todo:应该使用反射,取得枚举的最大可能值。 Schema.Context context = (Schema.Context)RandomData.Random.Next((int)Schema.Context.Skeletons, (int)Schema.Context.AnimationClip); const string filePath = TestData.testData_path + "TestFile.doub"; if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } ByteBuffer bb = new ByteBuffer(bytes); FileSaver.Save(bb, context, filePath); Schema.Context out_context = Context.Unknown; ByteBuffer bbOut = FileUnserializer.LoadFromFile(filePath, out out_context); Assert.AreEqual(context, out_context); Assert.AreEqual(bytes.Length, bbOut.Length - bbOut.Position); for (int i = 0; i < bytes.Length; i++) { Assert.AreEqual(bytes [i], bbOut.Data [bbOut.Position + i]); } if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } }
static public byte[] GetBytes(Schema.Context context) { byte[] bytes = new byte[numberOfBytes]; ByteBuffer bf = new ByteBuffer(bytes); bf.PutInt(0, magic); bf.PutInt(4, version); bf.PutInt(8, (int)context); return(bytes); }
static public Schema.Context FromBytes(byte[] bytes) { if (bytes.Length >= numberOfBytes) { ByteBuffer bf = new ByteBuffer(bytes); int _magic = bf.GetInt(0); int _version = bf.GetInt(4); Schema.Context _context = (Schema.Context)bf.GetInt(8); if (_magic == magic && _version == version) { return(_context); } } return(Schema.Context.Unknown); }