-
Notifications
You must be signed in to change notification settings - Fork 38
/
PieceConfig.cs
202 lines (176 loc) · 7.01 KB
/
PieceConfig.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
using System;
using System.Collections.Generic;
using HarmonyLib;
using Jotunn.Entities;
using Jotunn.Managers;
using UnityEngine;
namespace Jotunn.Configs
{
/// <summary>
/// Configuration class for adding custom pieces.<br />
/// Use this in a constructor of <see cref="CustomPiece"/> and
/// Jötunn resolves the references to the game objects at runtime.
/// </summary>
public class PieceConfig
{
/// <summary>
/// The name for your piece. May be tokenized.
/// </summary>
public string Name { get; set; } = string.Empty;
/// <summary>
/// The description of your piece. May be tokenized.
/// </summary>
public string Description { get; set; } = string.Empty;
/// <summary>
/// Whether this piece is buildable or not. Defaults to <c>true</c>.
/// </summary>
public bool Enabled { get; set; } = true;
/// <summary>
/// Can this piece be built in dungeons? Defaults to <c>false</c>.
/// </summary>
public bool AllowedInDungeons { get; set; } = false;
/// <summary>
/// The name of the piece table where this piece will be added.
/// </summary>
public string PieceTable
{
get => pieceTable;
set => pieceTable = PieceTables.GetInternalName(value);
}
/// <summary>
/// The name of the category this piece will appear on. If categories are disabled on the
/// target <see cref="global::PieceTable"/>, this setting will be ignored.<br />
/// If categories are enabled but the given category can't be found, a new
/// <see cref="Piece.PieceCategory"/> will be added to the table.
/// </summary>
public string Category
{
get => category;
set => category = PieceCategories.GetInternalName(value);
}
/// <summary>
/// The name of the crafting station prefab which needs to be in close proximity to build this piece.
/// </summary>
public string CraftingStation
{
get => craftingStation;
set => craftingStation = CraftingStations.GetInternalName(value);
}
/// <summary>
/// The name of the crafting station prefab to which this piece will be an upgrade to.
/// </summary>
public string ExtendStation
{
get => extendStation;
set => extendStation = CraftingStations.GetInternalName(value);
}
/// <summary>
/// Icon which is displayed in the crafting GUI.
/// </summary>
public Sprite Icon { get; set; } = null;
/// <summary>
/// Array of <see cref="RequirementConfig"/>s for all crafting materials it takes to craft the recipe.
/// </summary>
public RequirementConfig[] Requirements { get; set; } = Array.Empty<RequirementConfig>();
private string pieceTable = string.Empty;
private string category = string.Empty;
private string craftingStation = string.Empty;
private string extendStation = string.Empty;
/// <summary>
/// Apply this configs values to a piece GameObject.
/// </summary>
/// <param name="prefab"></param>
public void Apply(GameObject prefab)
{
var piece = prefab.GetComponent<Piece>();
if (piece == null)
{
Logger.LogWarning($"GameObject has no Piece attached");
return;
}
piece.m_enabled = Enabled;
piece.m_allowedInDungeons = AllowedInDungeons;
// Set name if given
if (!string.IsNullOrEmpty(Name))
{
piece.m_name = Name;
}
// Set description if given
if (!string.IsNullOrEmpty(Description))
{
piece.m_description = Description;
}
// Set icon if overriden
if (Icon != null)
{
piece.m_icon = Icon;
}
// Assign all needed resources for this piece if provided
if (Requirements.Length > 0)
{
piece.m_resources = GetRequirements();
}
// Assign the CraftingStation for this piece
if (!string.IsNullOrEmpty(CraftingStation))
{
piece.m_craftingStation = Mock<CraftingStation>.Create(CraftingStation);
}
// Assign an extension station for this piece
if (!string.IsNullOrEmpty(ExtendStation))
{
var stationExt = prefab.GetOrAddComponent<StationExtension>();
stationExt.m_craftingStation = Mock<CraftingStation>.Create(ExtendStation);
}
if (!string.IsNullOrEmpty(Category))
{
piece.m_category = PieceManager.Instance.AddPieceCategory(PieceTable, Category);
}
}
/// <summary>
/// Converts the <see cref="RequirementConfig">RequirementConfigs</see> to Valheim style <see cref="Piece.Requirement"/> array.
/// </summary>
/// <returns>The Valheim <see cref="Piece.Requirement"/> array</returns>
public Piece.Requirement[] GetRequirements()
{
List<Piece.Requirement> reqs = new List<Piece.Requirement>();
foreach (RequirementConfig requirement in Requirements)
{
if (requirement != null && requirement.IsValid())
{
reqs.Add(requirement.GetRequirement());
}
}
return reqs.ToArray();
}
/// <summary>
/// Loads a single PieceConfig from a JSON string
/// </summary>
/// <param name="json">JSON text</param>
/// <returns>Loaded PieceConfig</returns>
public static PieceConfig FromJson(string json)
{
return SimpleJson.SimpleJson.DeserializeObject<PieceConfig>(json);
}
/// <summary>
/// Loads a list of PieceConfigs from a JSON string
/// </summary>
/// <param name="json">JSON text</param>
/// <returns>Loaded list of PieceConfigs</returns>
public static List<PieceConfig> ListFromJson(string json)
{
return SimpleJson.SimpleJson.DeserializeObject<List<PieceConfig>>(json);
}
/// <summary>
/// Appends a new <see cref="RequirementConfig"/> to the array of existing ones.<br />
/// If the requirement is null or is not valid (has not item name or amount set) nothing will be added.
/// </summary>
/// <param name="requirementConfig"></param>
public void AddRequirement(RequirementConfig requirementConfig)
{
if (requirementConfig != null && requirementConfig.IsValid())
{
Requirements = Requirements.AddToArray(requirementConfig);
}
}
}
}