|
| 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