-
Notifications
You must be signed in to change notification settings - Fork 38
/
CustomPiece.cs
370 lines (323 loc) · 15.6 KB
/
CustomPiece.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using System;
using System.Reflection;
using Jotunn.Configs;
using Jotunn.Managers;
using Jotunn.Utils;
using UnityEngine;
namespace Jotunn.Entities
{
/// <summary>
/// Main interface for adding custom pieces to the game.<br />
/// All custom pieces have to be wrapped inside this class to add it to Jötunns <see cref="PieceManager"/>.
/// </summary>
public class CustomPiece : CustomEntity
{
/// <summary>
/// The prefab for this custom piece.
/// </summary>
public GameObject PiecePrefab { get; }
/// <summary>
/// The <see cref="global::Piece"/> component for this custom piece as a shortcut.
/// </summary>
public Piece Piece { get; }
/// <summary>
/// Name of the <see cref="global::PieceTable"/> this custom piece belongs to.
/// </summary>
public string PieceTable { get; set; }
/// <summary>
/// Name of the category this custom piece belongs to.<br />
/// When setting this value, Piece.m_category will be updated as well.
/// </summary>
public string Category
{
get => category;
set
{
category = value;
if (Piece && !string.IsNullOrEmpty(category))
{
Piece.m_category = PieceManager.Instance.AddPieceCategory(PieceTable, category);
}
}
}
/// <summary>
/// Indicator if references from <see cref="Entities.Mock{T}"/>s will be replaced at runtime.
/// </summary>
public bool FixReference { get; set; }
/// <summary>
/// Indicator if references from configs should get replaced
/// </summary>
internal bool FixConfig { get; set; }
private string PieceName
{
get => PiecePrefab ? PiecePrefab.name : fallbackPieceName;
}
private string fallbackPieceName;
private string category;
/// <summary>
/// Custom piece from a prefab.<br />
/// Will be added to the <see cref="global::PieceTable"/> provided by name.<br />
/// Can fix references from <see cref="Mock{T}"/>s or not.
/// </summary>
/// <param name="piecePrefab">The prefab for this custom piece.</param>
/// <param name="pieceTable">
/// Name of the <see cref="global::PieceTable"/> the custom piece should be added to.
/// Can by the "internal" or the <see cref="GameObject"/>s name (e.g. "_PieceTableHammer" or "Hammer")
/// </param>
/// <param name="fixReference">If true references for <see cref="Entities.Mock{T}"/> objects get resolved at runtime by Jötunn.</param>
public CustomPiece(GameObject piecePrefab, string pieceTable, bool fixReference) : base(Assembly.GetCallingAssembly())
{
PiecePrefab = piecePrefab;
Piece = piecePrefab.GetComponent<Piece>();
PieceTable = pieceTable;
FixReference = fixReference;
}
/// <summary>
/// Custom piece from a prefab with a <see cref="PieceConfig"/> attached.<br />
/// The members and references from the <see cref="PieceConfig"/> will be referenced by Jötunn at runtime.
/// </summary>
/// <param name="piecePrefab">The prefab for this custom piece.</param>
/// <param name="pieceConfig">The <see cref="PieceConfig"/> for this custom piece.</param>
[Obsolete("Use CustomPiece(GameObject, bool, PieceConfig) instead and define if references should be fixed")]
public CustomPiece(GameObject piecePrefab, PieceConfig pieceConfig) : base(Assembly.GetCallingAssembly())
{
PiecePrefab = piecePrefab;
Piece = piecePrefab.GetComponent<Piece>();
PieceTable = pieceConfig.PieceTable;
FixReference = false;
FixConfig = true;
Category = pieceConfig.Category;
pieceConfig.Apply(piecePrefab);
}
/// <summary>
/// Custom piece from a prefab with a <see cref="PieceConfig"/> attached.<br />
/// The members and references from the <see cref="PieceConfig"/> will be referenced by Jötunn at runtime.
/// </summary>
/// <param name="piecePrefab">The prefab for this custom piece.</param>
/// <param name="fixReference">If true references for <see cref="Entities.Mock{T}"/> objects get resolved at runtime by Jötunn.</param>
/// <param name="pieceConfig">The <see cref="PieceConfig"/> for this custom piece.</param>
public CustomPiece(GameObject piecePrefab, bool fixReference, PieceConfig pieceConfig) : base(Assembly.GetCallingAssembly())
{
PiecePrefab = piecePrefab;
Piece = piecePrefab.GetComponent<Piece>();
PieceTable = pieceConfig.PieceTable;
FixReference = fixReference;
FixConfig = true;
Category = pieceConfig.Category;
pieceConfig.Apply(piecePrefab);
}
/// <summary>
/// Custom piece from a prefab loaded from an <see cref="AssetBundle"/>.<br />
/// Will be added to the <see cref="global::PieceTable"/> provided by name.<br />
/// Can fix references from <see cref="Mock{T}"/>s or not.
/// </summary>
/// <param name="assetBundle">A preloaded <see cref="AssetBundle"/></param>
/// <param name="assetName">Name of the prefab in the bundle.</param>
/// <param name = "pieceTable" >
/// Name of the <see cref="global::PieceTable"/> the custom piece should be added to.
/// Can by the "internal" or the <see cref="GameObject"/>s name (e.g. "_PieceTableHammer" or "Hammer")
/// </param>
/// <param name="fixReference">If true references for <see cref="Entities.Mock{T}"/> objects get resolved at runtime by Jötunn.</param>
public CustomPiece(AssetBundle assetBundle, string assetName, string pieceTable, bool fixReference) : base(Assembly.GetCallingAssembly())
{
fallbackPieceName = assetName;
if (!AssetUtils.TryLoadPrefab(SourceMod, assetBundle, assetName, out GameObject prefab))
{
return;
}
PiecePrefab = prefab;
Piece = PiecePrefab.GetComponent<Piece>();
PieceTable = pieceTable;
FixReference = fixReference;
}
/// <summary>
/// Custom piece from a prefab loaded from an <see cref="AssetBundle"/> with a <see cref="PieceConfig"/> attached.<br />
/// The members and references from the <see cref="PieceConfig"/> will be referenced by Jötunn at runtime.
/// </summary>
/// <param name="assetBundle">A preloaded <see cref="AssetBundle"/></param>
/// <param name="assetName">Name of the prefab in the bundle.</param>
/// <param name="pieceConfig">The <see cref="PieceConfig"/> for this custom piece.</param>
[Obsolete("Use CustomPiece(AssetBundle, string, bool, PieceConfig) instead and define if references should be fixed")]
public CustomPiece(AssetBundle assetBundle, string assetName, PieceConfig pieceConfig) : base(Assembly.GetCallingAssembly())
{
fallbackPieceName = assetName;
if (!AssetUtils.TryLoadPrefab(SourceMod, assetBundle, assetName, out GameObject prefab))
{
return;
}
PiecePrefab = prefab;
Piece = PiecePrefab.GetComponent<Piece>();
PieceTable = pieceConfig.PieceTable;
FixReference = false;
FixConfig = true;
Category = pieceConfig.Category;
pieceConfig.Apply(PiecePrefab);
}
/// <summary>
/// Custom piece from a prefab loaded from an <see cref="AssetBundle"/> with a <see cref="PieceConfig"/> attached.<br />
/// The members and references from the <see cref="PieceConfig"/> will be referenced by Jötunn at runtime.
/// </summary>
/// <param name="assetBundle">A preloaded <see cref="AssetBundle"/></param>
/// <param name="assetName">Name of the prefab in the bundle.</param>
/// <param name="fixReference">If true references for <see cref="Entities.Mock{T}"/> objects get resolved at runtime by Jötunn.</param>
/// <param name="pieceConfig">The <see cref="PieceConfig"/> for this custom piece.</param>
public CustomPiece(AssetBundle assetBundle, string assetName, bool fixReference, PieceConfig pieceConfig) : base(Assembly.GetCallingAssembly())
{
fallbackPieceName = assetName;
if (!AssetUtils.TryLoadPrefab(SourceMod, assetBundle, assetName, out GameObject prefab))
{
return;
}
PiecePrefab = prefab;
Piece = PiecePrefab.GetComponent<Piece>();
PieceTable = pieceConfig.PieceTable;
FixReference = fixReference;
FixConfig = true;
Category = pieceConfig.Category;
pieceConfig.Apply(PiecePrefab);
}
/// <summary>
/// Custom piece created as an "empty" primitive.<br />
/// Will be added to the <see cref="global::PieceTable"/> provided by name.
/// </summary>
/// <param name="name">Name of the new prefab. Must be unique.</param>
/// <param name="addZNetView">If true a ZNetView component will be added to the prefab for network sync.</param>
/// <param name = "pieceTable" >
/// Name of the <see cref="global::PieceTable"/> the custom piece should be added to.
/// Can by the "internal" or the <see cref="GameObject"/>s name (e.g. "_PieceTableHammer" or "Hammer")
/// </param>
public CustomPiece(string name, bool addZNetView, string pieceTable) : base(Assembly.GetCallingAssembly())
{
PiecePrefab = PrefabManager.Instance.CreateEmptyPrefab(name, addZNetView);
if (!PiecePrefab)
{
fallbackPieceName = name;
return;
}
Piece = PiecePrefab.AddComponent<Piece>();
Piece.m_name = name;
PieceTable = pieceTable;
}
/// <summary>
/// Custom piece created as an "empty" primitive with a <see cref="PieceConfig"/> attached.<br />
/// The members and references from the <see cref="PieceConfig"/> will be referenced by Jötunn at runtime.
/// </summary>
/// <param name="name">Name of the new prefab. Must be unique.</param>
/// <param name="addZNetView">If true a ZNetView component will be added to the prefab for network sync.</param>
/// <param name="pieceConfig">The <see cref="PieceConfig"/> for this custom piece.</param>
public CustomPiece(string name, bool addZNetView, PieceConfig pieceConfig) : base(Assembly.GetCallingAssembly())
{
PiecePrefab = PrefabManager.Instance.CreateEmptyPrefab(name, addZNetView);
if (!PiecePrefab)
{
fallbackPieceName = name;
return;
}
Piece = PiecePrefab.AddComponent<Piece>();
PieceTable = pieceConfig.PieceTable;
FixConfig = true;
Category = pieceConfig.Category;
pieceConfig.Apply(PiecePrefab);
}
/// <summary>
/// Custom piece created as a copy of a vanilla Valheim prefab.<br />
/// Will be added to the <see cref="global::PieceTable"/> provided by name.
/// </summary>
/// <param name="name">The new name of the prefab after cloning.</param>
/// <param name="baseName">The name of the base prefab the custom item is cloned from.</param>
/// <param name = "pieceTable" >
/// Name of the <see cref="global::PieceTable"/> the custom piece should be added to.
/// Can by the "internal" or the <see cref="GameObject"/>s name (e.g. "_PieceTableHammer" or "Hammer")
/// </param>
public CustomPiece(string name, string baseName, string pieceTable) : base(Assembly.GetCallingAssembly())
{
PiecePrefab = PrefabManager.Instance.CreateClonedPrefab(name, baseName);
if (!PiecePrefab)
{
fallbackPieceName = name;
return;
}
Piece = PiecePrefab.GetComponent<Piece>();
PieceTable = pieceTable;
}
/// <summary>
/// Custom piece created as a copy of a vanilla Valheim prefab with a <see cref="PieceConfig"/> attached.<br />
/// The members and references from the <see cref="PieceConfig"/> will be referenced by Jötunn at runtime.
/// </summary>
/// <param name="name">The new name of the prefab after cloning.</param>
/// <param name="baseName">The name of the base prefab the custom item is cloned from.</param>
/// <param name="pieceConfig">The <see cref="PieceConfig"/> for this custom piece.</param>
public CustomPiece(string name, string baseName, PieceConfig pieceConfig) : base(Assembly.GetCallingAssembly())
{
PiecePrefab = PrefabManager.Instance.CreateClonedPrefab(name, baseName);
if (!PiecePrefab)
{
fallbackPieceName = name;
return;
}
Piece = PiecePrefab.GetComponent<Piece>();
PieceTable = pieceConfig.PieceTable;
FixConfig = true;
Category = pieceConfig.Category;
pieceConfig.Apply(PiecePrefab);
}
/// <summary>
/// Checks if a custom piece is valid (i.e. has a prefab, a target PieceTable is set,
/// has a <see cref="global::Piece"/> component and that component has an icon).
/// </summary>
/// <returns>true if all criteria is met</returns>
public bool IsValid()
{
bool valid = true;
if (!PiecePrefab)
{
Logger.LogError(SourceMod, $"CustomPiece '{this}' has no prefab");
valid = false;
}
if (PiecePrefab && !PiecePrefab.IsValid())
{
valid = false;
}
if (!Piece)
{
Logger.LogError(SourceMod, $"CustomPiece '{this}' has no Piece component");
valid = false;
}
if (Piece && !Piece.m_icon)
{
Logger.LogError(SourceMod, $"CustomPiece '{this}' has no icon");
valid = false;
}
if (string.IsNullOrEmpty(PieceTable))
{
Logger.LogError(SourceMod, $"CustomPiece '{this}' has no PieceTable");
valid = false;
}
return valid;
}
/// <summary>
/// Helper method to determine if a prefab with a given name is a custom piece created with Jötunn.
/// </summary>
/// <param name="prefabName">Name of the prefab to test.</param>
/// <returns>true if the prefab is added as a custom piece to the <see cref="PieceManager"/>.</returns>
public static bool IsCustomPiece(string prefabName)
{
return PieceManager.Instance.Pieces.ContainsKey(prefabName);
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
return obj.GetHashCode() == GetHashCode();
}
/// <inheritdoc/>
public override int GetHashCode()
{
return PieceName.GetStableHashCode();
}
/// <inheritdoc/>
public override string ToString()
{
return PieceName;
}
}
}