public void Dictionary_With_MemoryStream() { using(var stream = new MemoryStream()) { var dictionary = new Dictionary<string, int> { {"test1", 1 }, { "test2", 2 } }; var serializer = new CrySerializer(); serializer.Serialize(stream, dictionary); serializer = new CrySerializer(); var deserializedDictionary = serializer.Deserialize(stream) as Dictionary<string, int>; Assert.NotNull(deserializedDictionary); Assert.Equal(2, deserializedDictionary.Count); Assert.Equal(2, deserializedDictionary.Count); var firstKey = deserializedDictionary.First().Key; Assert.Equal("test1", firstKey); Assert.Equal(1, deserializedDictionary[firstKey]); var secondKey = deserializedDictionary.ElementAt(1).Key; Assert.Equal("test2", secondKey); Assert.Equal(2, deserializedDictionary[secondKey]); } }
public static object DeserializeFromString(string data) { var byteArray = System.Text.Encoding.ASCII.GetBytes(data); using (var stream = new MemoryStream(byteArray)) { var serializer = new CrySerializer(); return(serializer.Deserialize(stream)); } }
public void Unused_Vec3() { using (var stream = new MemoryStream()) { var serializer = new CrySerializer(); var unused = UnusedMarker.Vec3; serializer.Serialize(stream, unused); unused = (Vec3)serializer.Deserialize(stream); Assert.True(UnusedMarker.IsUnused(unused)); } }
public void Unused_UnsignedInteger() { using (var stream = new MemoryStream()) { var serializer = new CrySerializer(); var unused = UnusedMarker.UnsignedInteger; serializer.Serialize(stream, unused); unused = (uint)serializer.Deserialize(stream); Assert.True(UnusedMarker.IsUnused(unused)); } }
public void Entity_With_DelayedFunc() { var serializer = new CrySerializer(); using (var stream = new MemoryStream()) { var scriptManager = new ScriptManager(); scriptManager.AddScriptInstance(new TestEntity(), ScriptType.Entity); serializer.Serialize(stream, scriptManager.Scripts); scriptManager.Scripts = serializer.Deserialize(stream) as List<CryScript>; } }
public void Derivation() { using(var stream = new MemoryStream()) { var serializer = new CrySerializer(); serializer.Serialize(stream, new Class_Inherit_From_Class()); serializer = new CrySerializer(); var inheritClass = serializer.Deserialize(stream) as Class_Inherit_From_Class; Assert.NotNull(inheritClass); Assert.True(inheritClass.BooleanProperty); Assert.Equal(13, inheritClass.IntegerProperty); Assert.Equal("TestString", inheritClass.StringProperty); Assert.Equal(new CryEngine.Vec3(1, 2, 3), inheritClass.Vec3Property); } }
public void Class_With_MemberInfo_Members() { using(var stream = new MemoryStream()) { var serializer = new CrySerializer(); serializer.Serialize(stream, new Class_With_MemberInfo_Member()); serializer = new CrySerializer(); var memberInfoClass = serializer.Deserialize(stream) as Class_With_MemberInfo_Member; Assert.NotNull(memberInfoClass); Assert.Same(memberInfoClass.GetType().GetMethod("Method"), memberInfoClass.MethodInfo); Assert.Same(memberInfoClass.GetType().GetField("booleanField"), memberInfoClass.FieldInfo); Assert.True(memberInfoClass.booleanField); } }
public void Serialize_Native_Scripts() { var serializer = new CrySerializer(); using(var stream = new MemoryStream()) { var scriptManager = new ScriptManager(); scriptManager.AddScriptInstance(new NativeEntity(1, IntPtr.Zero), ScriptType.Entity); scriptManager.AddScriptInstance(new NativeEntity(2, IntPtr.Zero), ScriptType.Entity); scriptManager.AddScriptInstance(new NativeActor(3), ScriptType.Actor); serializer.Serialize(stream, scriptManager.Scripts); scriptManager.Scripts = serializer.Deserialize(stream) as List<CryScript>; Assert.NotNull(scriptManager.Scripts); Assert.Equal(2, scriptManager.Scripts.Count); var entityScript = scriptManager.FindScript(ScriptType.Entity, x => x.Type == typeof(NativeEntity)); Assert.NotSame(default(CryScript), entityScript); Assert.NotNull(entityScript.ScriptInstances); Assert.Equal<int>(1, (entityScript.ScriptInstances[0] as NativeEntity).Id); Assert.Equal<int>(2, (entityScript.ScriptInstances[1] as NativeEntity).Id); var actorScript = scriptManager.FindScript(ScriptType.Entity, x => x.Type == typeof(NativeActor)); Assert.NotSame(default(CryScript), actorScript); Assert.NotNull(actorScript.ScriptInstances); Assert.Equal<int>(3, (actorScript.ScriptInstances[0] as NativeActor).Id); } }
public void GenericEnumerableception() { var dictionary = new Dictionary<int, List<TestClass>>(); for(int i = 0; i < 10; i++) { var list = new List<TestClass> { SetupTestClass(), null }; dictionary.Add(i, list); } using(var stream = new MemoryStream()) { var serializer = new CrySerializer(); serializer.Serialize(stream, dictionary); serializer = new CrySerializer(); var deserializedDictionary = serializer.Deserialize(stream) as Dictionary<int, List<TestClass>>; Assert.NotNull(deserializedDictionary); } }
public void TestClass_With_MemoryStream() { using(var stream = new MemoryStream()) { var serializer = new CrySerializer(); serializer.Serialize(stream, SetupTestClass()); serializer = new CrySerializer(); var testClass = serializer.Deserialize(stream) as TestClass; Assert.NotNull(testClass); Assert.True(testClass.Boolean); Assert.Equal(3, testClass.Integer); Assert.Equal("testString", testClass.String); Assert.NotNull(testClass.nestedClass); Assert.Equal(testClass.nestedClass.NestedEnum, TestClass.NestedEnum.Nested_NotQuite); } }
public void String_With_MemoryStream() { using(var stream = new MemoryStream()) { var serializer = new CrySerializer(); serializer.Serialize(stream, "Test str1ng_I5 V37y tEsTy%‹Œm´ð!"); serializer = new CrySerializer(); var testString = serializer.Deserialize(stream) as string; Assert.Equal("Test str1ng_I5 V37y tEsTy%‹Œm´ð!", testString); } }
public void String_Array_With_MemoryStream() { using(var stream = new MemoryStream()) { var list = new List<object> {"first_string", "second_string", "third_string"}; var serializer = new CrySerializer(); serializer.Serialize(stream, list.ToArray()); serializer = new CrySerializer(); var array = serializer.Deserialize(stream) as object[]; Assert.NotNull(array); Assert.NotEmpty(array); Assert.Equal("first_string", array.ElementAt(0)); Assert.Equal("second_string", array.ElementAt(1)); Assert.Equal("third_string", array.ElementAt(2)); } }
public void Reference_Object_Serialization() { using(var stream = new MemoryStream()) { var referenceTestClass = new Multiple_Reference_Test_Class(); var serializer = new CrySerializer(); serializer.Serialize(stream, referenceTestClass); serializer = new CrySerializer(); referenceTestClass = serializer.Deserialize(stream) as Multiple_Reference_Test_Class; Assert.NotSame(referenceTestClass.ClassWithTestClassReference, referenceTestClass.TestClassSeperate); Assert.Equal(referenceTestClass.ClassWithTestClassReference.TestClass, referenceTestClass.TestClassReference); /*Assert.Equal(referenceTestClass.ClassWithTestClassReference.TestClass, referenceTestClass.TestClassReference, "Objects were not the same; expected hash code: {0} but was: {1}", referenceTestClass.ClassWithTestClassReference.GetHashCode(), referenceTestClass.TestClassReference.GetHashCode());*/ } }
public void Object_Array_With_MemoryStream() { using(var stream = new MemoryStream()) { var list = new List<object> { "testString", 1337, true }; var serializer = new CrySerializer(); serializer.Serialize(stream, list.ToArray()); serializer = new CrySerializer(); var array = serializer.Deserialize(stream) as object[]; Assert.NotNull(array); Assert.NotEmpty(array); Assert.Equal("testString", array.ElementAt(0)); Assert.Equal(1337, array.ElementAt(1)); Assert.Equal(true, array.ElementAt(2)); } }
public void List_With_MemoryStream() { using(var stream = new MemoryStream()) { var list = new List<string> { "test1", "test2" }; var serializer = new CrySerializer(); serializer.Serialize(stream, list); serializer = new CrySerializer(); var deserialized = serializer.Deserialize(stream) as List<string>; Assert.NotNull(deserialized); Assert.Equal(2, deserialized.Count()); Assert.Equal("test1", deserialized.ElementAt(0)); Assert.Equal("test2", deserialized.ElementAt(1)); } }
public void Serialize_CryScript_List() { var serializer = new CrySerializer(); using (var stream = new MemoryStream()) { var list = new List<CryScript>(); CryScript script; if (CryScript.TryCreate(typeof(NativeActor), out script)) { script.ScriptInstances = new List<CryScriptInstance>(); script.ScriptInstances.Add(new NativeActor(759)); script.ScriptInstances.Add(new NativeActor(5)); list.Add(script); } if (CryScript.TryCreate(typeof(NativeEntity), out script)) { script.ScriptInstances = new List<CryScriptInstance>(); script.ScriptInstances.Add(new NativeEntity(987, IntPtr.Zero)); script.ScriptInstances.Add(new NativeEntity(8, IntPtr.Zero)); script.ScriptInstances.Add(null); list.Add(script); } serializer.Serialize(stream, list); serializer = new CrySerializer(); list = serializer.Deserialize(stream) as List<CryScript>; Assert.NotNull(list); Assert.NotEmpty(list); Assert.Equal(2, list.Count); var nativeActorScript = list.ElementAt(0); Assert.NotNull(nativeActorScript.ScriptInstances); Assert.Equal(2, nativeActorScript.ScriptInstances.Count); Assert.NotNull(nativeActorScript.ScriptInstances.ElementAt(0)); Assert.Equal<int>(759, (nativeActorScript.ScriptInstances.ElementAt(0) as EntityBase).Id); Assert.NotNull(nativeActorScript.ScriptInstances.ElementAt(1)); Assert.Equal<int>(5, (nativeActorScript.ScriptInstances.ElementAt(1) as EntityBase).Id); var nativeEntityScript = list.ElementAt(1); Assert.NotNull(nativeEntityScript.ScriptInstances); Assert.Equal(3, nativeEntityScript.ScriptInstances.Count); Assert.NotNull(nativeEntityScript.ScriptInstances.ElementAt(0)); Assert.Equal<int>(987, (nativeEntityScript.ScriptInstances.ElementAt(0) as EntityBase).Id); Assert.NotNull(nativeEntityScript.ScriptInstances.ElementAt(1)); Assert.Equal<int>(8, (nativeEntityScript.ScriptInstances.ElementAt(1) as EntityBase).Id); Assert.Null(nativeEntityScript.ScriptInstances.ElementAt(2)); } }
public static object DeserializeFromString(string data) { var byteArray = System.Text.Encoding.ASCII.GetBytes(data); using (var stream = new MemoryStream(byteArray)) { var serializer = new CrySerializer(); return serializer.Deserialize(stream); } }