Skip to content

Commit b0078b0

Browse files
committed
#1: nested object support
1 parent 77a5337 commit b0078b0

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

GameData/Mutiny/test.cfg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ SCENE_PATCH
1919

2020
UnityEngine.RenderSettings
2121
{
22-
ambientLight = 0,0,1
22+
sun
23+
{
24+
color = 1,0,0
25+
}
2326
}
2427
}

Source/ModifyObject.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private static MemberInfo GetMemberInfo(Type objectType, string memberName, out
6969
return null;
7070
}
7171

72-
private static Action<object> CreateMutator(ConfigNode.Value configValue, Type objectType)
72+
private static Action<object> CreateValueMutator(ConfigNode.Value configValue, Type objectType)
7373
{
7474
var memberInfo = GetMemberInfo(objectType, configValue.name, out Type memberType);
7575

@@ -90,11 +90,31 @@ private static Action<object> CreateMutator(ConfigNode.Value configValue, Type o
9090
return null;
9191
}
9292

93+
private static Action<object> CreateObjectMutator(ConfigNode childNode, Type objectType)
94+
{
95+
var memberInfo = GetMemberInfo(objectType, childNode.name, out Type memberType);
96+
97+
if (memberInfo != null)
98+
{
99+
var memberMutators = CreateMutators(childNode, memberType);
100+
return obj =>
101+
{
102+
object member = memberInfo is FieldInfo fieldInfo ? fieldInfo.GetValue(obj) : ((PropertyInfo)memberInfo).GetValue(obj);
103+
foreach (var memberMutator in memberMutators)
104+
{
105+
memberMutator.Invoke(member);
106+
}
107+
};
108+
}
109+
110+
return null;
111+
}
112+
93113
private static void CreateObjectMutators(ConfigNode configNode, Type objectType, List<Action<object>> mutators)
94114
{
95115
foreach (ConfigNode.Value configValue in configNode.values.values)
96116
{
97-
var mutator = CreateMutator(configValue, objectType);
117+
var mutator = CreateValueMutator(configValue, objectType);
98118
if (mutator != null)
99119
{
100120
mutators.Add(mutator);
@@ -105,12 +125,18 @@ private static void CreateObjectMutators(ConfigNode configNode, Type objectType,
105125

106126
foreach (ConfigNode childNode in configNode.nodes.nodes)
107127
{
108-
// TODO: support arbitrary nested objects
109-
110128
if (customNodeHandlers != null && customNodeHandlers.TryGetValue(childNode.name, out var customNodeHandler))
111129
{
112130
customNodeHandler.Invoke(childNode, objectType, mutators);
113131
}
132+
else
133+
{
134+
var mutator = CreateObjectMutator(childNode, objectType);
135+
if (mutator != null)
136+
{
137+
mutators.Add(mutator);
138+
}
139+
}
114140
}
115141
}
116142

0 commit comments

Comments
 (0)