Skip to content

Commit 85eeceb

Browse files
committed
#1: framework for modifying fields/properties on gameobjects
1 parent 3f0c2f8 commit 85eeceb

File tree

3 files changed

+108
-4
lines changed

3 files changed

+108
-4
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
SCENE_PATCH
22
{
33
name = test
4-
type = DeleteObject
4+
type = ModifyObject
55
scene = VABmodern
66

7-
ObjectNames
7+
VABmodern
88
{
9-
=VABmodern
9+
active = false
1010
}
1111
}

Source/GameObjectPatcher.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net4.8</TargetFramework>
@@ -18,6 +18,14 @@
1818
</ItemGroup>
1919

2020
<ItemGroup>
21+
<Publicize Include="Assembly-CSharp" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<PackageReference Include="Krafs.Publicizer" Version="2.3.0">
26+
<PrivateAssets>all</PrivateAssets>
27+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
28+
</PackageReference>
2129
<PackageReference Include="KSPBuildTools" Version="0.0.3-alpha.4" />
2230
</ItemGroup>
2331

Source/ModifyObject.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Security.AccessControl;
5+
using UnityEngine;
6+
using Log = KSPBuildTools.Log;
7+
8+
namespace GameObjectPatcher
9+
{
10+
internal class ModifyObject : ScenePatch
11+
{
12+
Dictionary<string, List<Action<object>>> m_gameObjectMutators = new Dictionary<string, List<Action<object>>>();
13+
14+
public override void Load(ConfigNode configNode)
15+
{
16+
base.Load(configNode);
17+
18+
foreach (var childNode in configNode.nodes.nodes)
19+
{
20+
m_gameObjectMutators.Add(childNode.name, CreateMutators(childNode, typeof(GameObject)));
21+
}
22+
}
23+
24+
private static MemberInfo GetMemberInfo(Type objectType, string memberName)
25+
{
26+
MemberInfo memberInfo = objectType.GetField(memberName);
27+
if (memberInfo == null)
28+
{
29+
memberInfo = objectType.GetProperty(memberName);
30+
}
31+
32+
if (memberInfo == null)
33+
{
34+
Log.Error($"Member {memberName} not found in type {objectType.FullName}");
35+
}
36+
37+
return memberInfo;
38+
}
39+
40+
private static Action<object> CreateMutator(ConfigNode.Value configValue, Type objectType)
41+
{
42+
var memberInfo = GetMemberInfo(objectType, configValue.name);
43+
44+
if (memberInfo != null)
45+
{
46+
if (memberInfo is FieldInfo fieldInfo)
47+
{
48+
object newValue = ConfigNode.ReadValue(fieldInfo.FieldType, configValue.value);
49+
return obj => fieldInfo.SetValue(obj, newValue);
50+
}
51+
else if (memberInfo is PropertyInfo propertyInfo)
52+
{
53+
object newValue = ConfigNode.ReadValue(propertyInfo.PropertyType, configValue.value);
54+
return obj => propertyInfo.SetValue(obj, newValue);
55+
}
56+
}
57+
58+
return null;
59+
}
60+
61+
private static List<Action<object>> CreateMutators(ConfigNode configNode, Type objectType)
62+
{
63+
List<Action<object>> mutators = new List<Action<object>>();
64+
65+
foreach (ConfigNode.Value configValue in configNode.values.values)
66+
{
67+
var mutator = CreateMutator(configValue, objectType);
68+
if (mutator != null)
69+
{
70+
mutators.Add(mutator);
71+
}
72+
}
73+
74+
return mutators;
75+
}
76+
77+
public override void Execute()
78+
{
79+
foreach (var objectMutator in m_gameObjectMutators)
80+
{
81+
GameObject gameObject = GameObject.Find(objectMutator.Key);
82+
83+
if (gameObject == null)
84+
{
85+
Log.Error("GameObject {objectName} not found");
86+
continue;
87+
}
88+
89+
foreach (var mutator in objectMutator.Value)
90+
{
91+
mutator.Invoke(gameObject);
92+
}
93+
}
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)