-
Notifications
You must be signed in to change notification settings - Fork 38
/
CustomPrefab.cs
107 lines (93 loc) · 3.78 KB
/
CustomPrefab.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
using System.Reflection;
using BepInEx;
using Jotunn.Managers;
using Jotunn.Utils;
using UnityEngine;
namespace Jotunn.Entities
{
/// <summary>
/// Wrapper for custom added GameObjects holding the mod reference.
/// </summary>
public class CustomPrefab : CustomEntity, IModPrefab
{
/// <summary>
/// Original prefab
/// </summary>
public GameObject Prefab { get; }
/// <summary>
/// Indicator if references from <see cref="Entities.Mock{T}"/>s will be replaced at runtime.
/// </summary>
public bool FixReference { get; set; }
private string PrefabName
{
get => Prefab ? Prefab.name : fallbackPrefabName;
}
private string fallbackPrefabName;
/// <summary>
/// Internal ctor with provided <see cref="BepInPlugin"/> metadata.<br />
/// Does not fix references.
/// </summary>
/// <param name="prefab">Prefab added</param>
/// <param name="sourceMod">Metadata of the mod adding this prefab</param>
internal CustomPrefab(GameObject prefab, BepInPlugin sourceMod) : base(sourceMod)
{
Prefab = prefab;
}
/// <summary>
/// Custom prefab.<br />
/// Can fix references for <see cref="Entities.Mock{T}"/>s.
/// </summary>
/// <param name="prefab">The prefab for this custom item.</param>
/// <param name="fixReference">If true references for <see cref="Entities.Mock{T}"/> objects get resolved at runtime by Jötunn.</param>
public CustomPrefab(GameObject prefab, bool fixReference) : base(Assembly.GetCallingAssembly())
{
Prefab = prefab;
FixReference = fixReference;
}
/// <summary>
/// Custom prefab loaded from an <see cref="AssetBundle"/>.<br />
/// Can fix references for <see cref="Entities.Mock{T}"/>s.
/// </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>
public CustomPrefab(AssetBundle assetBundle, string assetName, bool fixReference) : base(Assembly.GetCallingAssembly())
{
fallbackPrefabName = assetName;
if (!AssetUtils.TryLoadPrefab(SourceMod, assetBundle, assetName, out GameObject prefab))
{
return;
}
Prefab = prefab;
FixReference = fixReference;
}
/// <summary>
/// Checks if a custom item is valid (i.e. has a prefab, an <see cref="ItemDrop"/> and an icon, if it should be craftable).
/// </summary>
/// <returns>true if all criteria is met</returns>
public bool IsValid()
{
bool valid = true;
if (!Prefab)
{
Logger.LogError(SourceMod, $"CustomPrefab '{this}' has no prefab");
valid = false;
}
return valid;
}
/// <summary>
/// Helper method to determine if a prefab with a given name is a custom prefab 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 prefab to the <see cref="PrefabManager"/>.</returns>
public static bool IsCustomPrefab(string prefabName)
{
return PrefabManager.Instance.Prefabs.ContainsKey(prefabName);
}
/// <inheritdoc/>
public override string ToString()
{
return PrefabName;
}
}
}