From ba15fdf3ff6ae1ca09508db2594d94af8368fb03 Mon Sep 17 00:00:00 2001 From: kkjamie Date: Mon, 21 May 2018 10:21:15 +0100 Subject: [PATCH] PR feedback --- Serialization/ArgsList.Serialization.cs | 16 +++++++-------- Serialization/ArgsList.cs | 26 ++++--------------------- Serialization/Editor/ArgsListTests.cs | 20 +++++++++++++------ 3 files changed, 26 insertions(+), 36 deletions(-) diff --git a/Serialization/ArgsList.Serialization.cs b/Serialization/ArgsList.Serialization.cs index 0aeab87..79406a6 100644 --- a/Serialization/ArgsList.Serialization.cs +++ b/Serialization/ArgsList.Serialization.cs @@ -76,28 +76,24 @@ public void OnBeforeSerialize() for (int i = 0; i < argTypes.Count; i++) { var argType = argTypes[i]; + var arg = args[i]; var list = lazyGetList(argType); + list.Add(arg); + if (argType.IsSubclassOf(typeof(Component))) { - var arg = args[i]; - list.Add(arg); typeOrderList.Add(COMPONENT_PREFIX + argType.FullName); } else if (argType.IsSubclassOf(typeof(Enum))) { - var arg = args[i]; - list.Add(arg); typeOrderList.Add(ENUM_PREFIX + argType.FullName); } else if (argType.IsSubclassOf(typeof(ScriptableObject))) { - var arg = args[i]; - list.Add(arg); typeOrderList.Add(SCRIPTABLE_OBJECT_PREFIX + argType.FullName); } else { - list.Add(args[i]); typeOrderList.Add(argType.FullName); } } @@ -225,7 +221,11 @@ public static SupportedType Create(Func getList, Action (getList(i) ?? new T[0]).Cast().ToList(), + i => + { + var list = getList(i); + return list != null ? list.Cast().ToList() : new List(); + }, (i, v) => setList(i, v.Cast().ToArray())); } } diff --git a/Serialization/ArgsList.cs b/Serialization/ArgsList.cs index fda1d06..059fa4b 100644 --- a/Serialization/ArgsList.cs +++ b/Serialization/ArgsList.cs @@ -47,40 +47,22 @@ static ArgsList() enumTypes = new Dictionary(); scriptableObjectTypes = new Dictionary(); - // Get every type that extends component - var assemblies = new [] - { - // Project assembly - Assembly.GetExecutingAssembly(), - // UnityEngine Assembly - typeof(Component).Assembly, - // UnityEngine.UI Assembly - typeof(Graphic).Assembly, - }; + var assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (var type in assemblies.SelectMany(a => a.GetTypes())) { var fullTypeName = type.FullName; if (type.IsSubclassOf(typeof(Component))) { - if (!componentTypes.ContainsKey(fullTypeName)) - { - componentTypes.Add(fullTypeName, type); - } + componentTypes[fullTypeName] = type; } else if (type.IsSubclassOf(typeof(Enum))) { - if (!enumTypes.ContainsKey(fullTypeName)) - { - enumTypes.Add(fullTypeName, type); - } + enumTypes[fullTypeName] = type; } else if (type.IsSubclassOf(typeof(ScriptableObject))) { - if (!scriptableObjectTypes.ContainsKey(fullTypeName)) - { - scriptableObjectTypes.Add(fullTypeName, type); - } + scriptableObjectTypes[fullTypeName] = type; } } } diff --git a/Serialization/Editor/ArgsListTests.cs b/Serialization/Editor/ArgsListTests.cs index b3195c8..c1d36d6 100644 --- a/Serialization/Editor/ArgsListTests.cs +++ b/Serialization/Editor/ArgsListTests.cs @@ -10,6 +10,14 @@ namespace DUCK.Serialization.Editor [TestFixture] public class ArgsListTests { + public class TestScriptableObject : ScriptableObject{} + + public enum TestEnum + { + A, + B + } + [Test] public void ExpectConstructorNotToThrow() { @@ -445,14 +453,14 @@ public void ExpectEnumSerializationToBeSupported() // test that it doesn't throw when specifying the type - Assert.DoesNotThrow(() => argsList.SetTypes(new List {typeof(GradientMode)})); + Assert.DoesNotThrow(() => argsList.SetTypes(new List {typeof(TestEnum)})); // Add some data, serialize and deserialize - var value = GradientMode.Fixed; + var value = TestEnum.A; argsList.Set(0, value); var json = JsonUtility.ToJson(argsList); var resultArgsList = JsonUtility.FromJson(json); - var result = resultArgsList.Get(0); + var result = resultArgsList.Get(0); // Now test the value is what it should be Assert.AreEqual(value, result); @@ -464,15 +472,15 @@ public void ExpectScriptableObjectSerializationToBeSupported() var argsList = new ArgsList(); // test that it doesn't throw when specifying the type - Assert.DoesNotThrow(() => argsList.SetTypes(new List {typeof(TweenConfig)})); + Assert.DoesNotThrow(() => argsList.SetTypes(new List {typeof(TestScriptableObject)})); // Add some data, serialize and deserialize - var value = ScriptableObject.CreateInstance(); + var value = ScriptableObject.CreateInstance(); argsList.Set(0, value); var json = JsonUtility.ToJson(argsList); var resultArgsList = JsonUtility.FromJson(json); - var result = resultArgsList.Get(0); + var result = resultArgsList.Get(0); // Now test the value is what it should be Assert.AreEqual(value, result);