Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kkjamie committed May 21, 2018
1 parent 965d7aa commit ba15fdf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
16 changes: 8 additions & 8 deletions Serialization/ArgsList.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -225,7 +221,11 @@ public static SupportedType Create<T>(Func<ArgsList, T[]> getList, Action<ArgsLi
{
return new SupportedType(
typeof(T),
i => (getList(i) ?? new T[0]).Cast<object>().ToList(),
i =>
{
var list = getList(i);
return list != null ? list.Cast<object>().ToList() : new List<object>();
},
(i, v) => setList(i, v.Cast<T>().ToArray()));
}
}
Expand Down
26 changes: 4 additions & 22 deletions Serialization/ArgsList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,40 +47,22 @@ static ArgsList()
enumTypes = new Dictionary<string, Type>();
scriptableObjectTypes = new Dictionary<string, Type>();

// 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;
}
}
}
Expand Down
20 changes: 14 additions & 6 deletions Serialization/Editor/ArgsListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -445,14 +453,14 @@ public void ExpectEnumSerializationToBeSupported()

// test that it doesn't throw when specifying the type

Assert.DoesNotThrow(() => argsList.SetTypes(new List<Type> {typeof(GradientMode)}));
Assert.DoesNotThrow(() => argsList.SetTypes(new List<Type> {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<ArgsList>(json);
var result = resultArgsList.Get<GradientMode>(0);
var result = resultArgsList.Get<TestEnum>(0);

// Now test the value is what it should be
Assert.AreEqual(value, result);
Expand All @@ -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<Type> {typeof(TweenConfig)}));
Assert.DoesNotThrow(() => argsList.SetTypes(new List<Type> {typeof(TestScriptableObject)}));

// Add some data, serialize and deserialize
var value = ScriptableObject.CreateInstance<TweenConfig>();
var value = ScriptableObject.CreateInstance<TestScriptableObject>();
argsList.Set(0, value);

var json = JsonUtility.ToJson(argsList);
var resultArgsList = JsonUtility.FromJson<ArgsList>(json);
var result = resultArgsList.Get<TweenConfig>(0);
var result = resultArgsList.Get<TestScriptableObject>(0);

// Now test the value is what it should be
Assert.AreEqual(value, result);
Expand Down

0 comments on commit ba15fdf

Please sign in to comment.