-
Notifications
You must be signed in to change notification settings - Fork 40
/
KitbashManager.cs
200 lines (174 loc) · 7.32 KB
/
KitbashManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using Jotunn.Configs;
using Jotunn.Entities;
using System;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Jotunn.Managers
{
/// <summary>
/// Manager for handling Kitbashed objects
/// </summary>
public class KitbashManager : IManager
{
private static KitbashManager _instance;
/// <summary>
/// The singleton instance of this manager.
/// </summary>
public static KitbashManager Instance => _instance ??= new KitbashManager();
/// <summary>
/// Hide .ctor
/// </summary>
private KitbashManager() { }
static KitbashManager()
{
((IManager)Instance).Init();
}
/// <summary>
/// Internal list of objects to which Kitbashing should be applied.
/// </summary>
private readonly List<KitbashObject> KitbashObjects = new List<KitbashObject>();
/// <summary>
/// Registers all hooks.
/// </summary>
void IManager.Init()
{
Main.LogInit("KitbashManager");
ItemManager.OnKitbashItemsAvailable += ApplyKitbashes;
}
/// <summary>
/// Register a prefab with a KitbashConfig to be applied when the vanilla prefabs are available
/// </summary>
/// <param name="prefab">Prefab to add kitbashed parts to</param>
/// <param name="kitbashConfig">KitbashConfig to apply to the prefab</param>
/// <returns>The KitbashObject container for this prefab</returns>
public KitbashObject AddKitbash(GameObject prefab, KitbashConfig kitbashConfig)
{
if(prefab.transform.parent == null)
{
string name = prefab.name;
prefab = Object.Instantiate(prefab, PrefabManager.Instance.PrefabContainer.transform);
prefab.name = name;
}
KitbashObject kitbashObject = new KitbashObject
{
Config = kitbashConfig,
Prefab = prefab
};
KitbashObjects.Add(kitbashObject);
return kitbashObject;
}
/// <summary>
/// Apply all Kitbashs to the objects registered in the manager.
/// </summary>
private void ApplyKitbashes()
{
if (KitbashObjects.Count > 0)
{
Logger.LogInfo($"Applying Kitbash in {KitbashObjects.Count} objects");
foreach (KitbashObject kitbashObject in KitbashObjects)
{
try
{
if (kitbashObject.Config.FixReferences)
{
kitbashObject.Prefab.FixReferences();
}
ApplyKitbash(kitbashObject);
}
catch (Exception e)
{
Logger.LogError(kitbashObject.SourceMod, e);
}
}
ItemManager.OnKitbashItemsAvailable -= ApplyKitbashes;
}
}
/// <summary>
/// Apply kitbash to a single object.
/// </summary>
/// <param name="kitbashObject"></param>
/// <returns></returns>
private bool ApplyKitbash(KitbashObject kitbashObject)
{
foreach (KitbashSourceConfig config in kitbashObject.Config.KitbashSources)
{
if (!Instance.Kitbash(kitbashObject, config))
{
Logger.LogWarning(kitbashObject.SourceMod, $"Kitbash failed for {kitbashObject}");
return false;
}
}
if (kitbashObject.Config.Layer != null)
{
int layer = LayerMask.NameToLayer(kitbashObject.Config.Layer);
foreach (Transform transform in kitbashObject.Prefab.GetComponentsInChildren<Transform>())
{
transform.gameObject.layer = layer;
}
}
kitbashObject.OnKitbashApplied?.SafeInvoke();
return true;
}
private bool Kitbash(KitbashObject kitbashObject, KitbashSourceConfig config)
{
// Try to load a custom prefab
GameObject sourcePrefab = PrefabManager.Instance.GetPrefab(config.SourcePrefab);
if (!sourcePrefab)
{
Logger.LogWarning(kitbashObject.SourceMod, $"Prefab '{config.SourcePrefab}' not found for {config.ToString()}");
return false;
}
Transform sourceTransform = sourcePrefab.transform.Find(config.SourcePath);
if (!sourceTransform)
{
Logger.LogWarning(kitbashObject.SourceMod, $"Source path '{config.SourcePath}' not found for {config.ToString()}");
return false;
}
GameObject kitbashPrefab = kitbashObject.Prefab;
Transform parentTransform = config.TargetParentPath != null ? kitbashPrefab.transform.Find(config.TargetParentPath) : kitbashPrefab.transform;
if (!parentTransform)
{
Logger.LogWarning(kitbashObject.SourceMod, $"Target parent not found for {config.ToString()}");
return false;
}
GameObject sourceGameObject = sourceTransform.gameObject;
GameObject kitbashedObject = Object.Instantiate(sourceGameObject, parentTransform);
kitbashedObject.name = config.Name ?? sourceGameObject.name;
kitbashedObject.transform.localPosition = config.Position;
kitbashedObject.transform.localRotation = config.Rotation;
kitbashedObject.transform.localScale = config.Scale;
if (config.Materials != null)
{
Material[] sourceMaterials = GetSourceMaterials(config);
if (sourceMaterials == null)
{
Logger.LogWarning(kitbashObject.SourceMod, $"No materials found for {config.ToString()}");
return false;
}
SkinnedMeshRenderer[] targetSkinnedMeshRenderers = kitbashedObject.GetComponentsInChildren<SkinnedMeshRenderer>();
foreach (SkinnedMeshRenderer targetSkinnedMeshRenderer in targetSkinnedMeshRenderers)
{
targetSkinnedMeshRenderer.sharedMaterials = sourceMaterials;
targetSkinnedMeshRenderer.materials = sourceMaterials;
}
MeshRenderer[] targetMeshRenderers = kitbashedObject.GetComponentsInChildren<MeshRenderer>();
foreach (MeshRenderer targetMeshRenderer in targetMeshRenderers)
{
targetMeshRenderer.sharedMaterials = sourceMaterials;
targetMeshRenderer.materials = sourceMaterials;
}
}
return true;
}
private Material[] GetSourceMaterials(KitbashSourceConfig config)
{
Material[] result = new Material[config.Materials.Length];
for (int i = 0; i < config.Materials.Length; i++)
{
result[i] = (Material) PrefabManager.Cache.GetPrefab(typeof(Material), config.Materials[i]);
}
return result;
}
}
}