forked from Flashkirby/WeaponOut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModConf.cs
More file actions
292 lines (256 loc) · 12.3 KB
/
ModConf.cs
File metadata and controls
292 lines (256 loc) · 12.3 KB
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
using System.Collections.Generic;
using System.IO;
using Terraria;
using Terraria.IO;
using Terraria.ModLoader;
namespace WeaponOut
{
/// <summary>
/// Tutorial by goldenapple: https://forums.terraria.org/index.php?threads/modders-guide-to-config-files-and-optional-features.48581/
/// </summary>
public static class ModConf
{
public const int configVersion = 3;
internal static bool showWeaponOut = true;
public static bool ShowWeaponOut { get { return showWeaponOut; } }
private const string showWeaponOutField = "show_weaponOut_visuals";
internal static bool forceShowWeaponOut = false;
public static bool ForceShowWeaponOut { get { return forceShowWeaponOut; } }
private const string forceShowWeaponOutField = "forceshow_weaponOut_visuals";
internal static bool toggleWaistRotation = false;
public static bool ToggleWaistRotation { get { return toggleWaistRotation; } }
private const string toggleWaistRotationField = "toggle_weaponOut_waist_rotation";
internal static bool enableBasicContent = true;
public static bool EnableBasicContent { get { return enableBasicContent; } }
private const string enableBasicContentField = "enable_base_weapons_and_tiles";
internal static bool enableWhips = true;
public static bool EnableWhips { get { return enableWhips; } }
private const string enableWhipsField = "enable_whips";
internal static bool enableFists = true;
public static bool EnableFists { get { return enableFists; } }
private const string enableFistsField = "enable_fists";
internal static bool enableDualWeapons = true;
public static bool EnableDualWeapons { get { return enableDualWeapons; } }
private const string enableDualWeaponsField = "enable_dual_weapons";
internal static bool enableAccessories = true;
public static bool EnableAccessories { get { return enableAccessories; } }
private const string enableAccessoriesField = "enable_accessories";
internal static bool enableEmblems = true;
public static bool EnableEmblems { get { return enableEmblems; } }
private const string enableEmblemsField = "enable_emblems";
internal static bool enableSabres = true;
public static bool EnableSabres { get { return enableSabres; } }
private const string enableSabresField = "enable_sabres";
static string ConfigPath = Path.Combine(Main.SavePath, "Mod Configs/WeaponOut.json");
static Preferences ModConfig = new Preferences(ConfigPath);
internal static void Load()
{
bool success = ReadConfig();
if (!success)
{
ErrorLogger.Log("WeaponOut: Couldn't load config file, creating new file. ");
CreateConfig();
}
}
/// <returns> true is loaded successfully </returns>
internal static bool ReadConfig()
{
if (ModConfig.Load())
{
int readVersion = 0;
ModConfig.Get("version", ref readVersion);
if (readVersion != configVersion)
{
bool canUpdate = false;
if (readVersion == 0)
{
canUpdate = true;
readVersion = 1;
ModConfig.Put("version", readVersion);
ModConfig.Put(enableEmblemsField, enableEmblems);
ModConfig.Save();
}
if (readVersion == 1)
{
canUpdate = true;
readVersion = 2;
ModConfig.Put("version", readVersion);
ModConfig.Put(toggleWaistRotationField, toggleWaistRotation);
ModConfig.Save();
}
if (readVersion == 2)
{
canUpdate = true;
readVersion = 3;
ModConfig.Put("version", readVersion);
ModConfig.Put(enableSabresField, enableSabres);
ModConfig.Save();
}
if (!canUpdate) return false;
}
ModConfig.Get(showWeaponOutField, ref showWeaponOut);
ModConfig.Get(forceShowWeaponOutField, ref forceShowWeaponOut);
ModConfig.Get(toggleWaistRotationField, ref toggleWaistRotation);
ModConfig.Get(enableBasicContentField, ref enableBasicContent);
ModConfig.Get(enableWhipsField, ref enableWhips);
ModConfig.Get(enableFistsField, ref enableFists);
ModConfig.Get(enableSabresField, ref enableSabres);
ModConfig.Get(enableDualWeaponsField, ref enableDualWeapons);
ModConfig.Get(enableAccessoriesField, ref enableAccessories);
ModConfig.Get(enableEmblemsField, ref enableEmblems);
return true;
}
return false;
}
/// <summary>
/// Create a new config file for the player to edit.
/// </summary>
internal static void CreateConfig()
{
ModConfig.Clear();
ModConfig.Put("version", configVersion);
ModConfig.Put(showWeaponOutField, showWeaponOut);
ModConfig.Put(forceShowWeaponOutField, forceShowWeaponOut);
ModConfig.Put(toggleWaistRotationField, toggleWaistRotation);
ModConfig.Put(enableBasicContentField, enableBasicContent);
ModConfig.Put(enableWhipsField, enableWhips);
ModConfig.Put(enableFistsField, enableFists);
ModConfig.Put(enableSabresField, enableSabres);
ModConfig.Put(enableDualWeaponsField, enableDualWeapons);
ModConfig.Put(enableAccessoriesField, enableAccessories);
ModConfig.Put(enableEmblemsField, enableEmblems);
ModConfig.Put("readme", "First off, make sure to reload before the configs will take any effect. Most of the fields do exactly as they say, they will allow the mod to load, or choose not to, sets of content from the mod. The only field that does not do this is forceshow_weaponOut_visuals, which simply forces the weapon to always show regardless of the visibility of the first accessory slot as this is an oft requested feature. WARNING: Clients will desync if their local config is different to the server - this cannot be fixed without forcing the clients to download the server's mods and forcing the mods to reload. So don't mess with this too much outside of singleplayer unless you know what you're doing. And no I'm too lazy to find out how to even fix this behaviour, though a simple server mismatch warning might be a good idea. Feel free to delete this.");
ModConfig.Save();
}
}
public static class ModConfWeaponOutCustom
{
public const int configVersion = 0;
/// <summary>
/// Hold positions range from 1 and upwards. 0 represents default, aka nothing.
/// </summary>
static Dictionary<string, int> customHoldPositions = new Dictionary<string, int>();
/// <summary>
/// Pre-calculated array mapping itemIDs and their holdstyles.
/// ItemIDs of vanilla items are type numbers.
/// ItemIDs of modded items are their Namespace.Type
/// </summary>
static int[] customHoldStyleArray;
static string ConfigPath = Path.Combine(Main.SavePath, "Mod Configs/WeaponOut_Custom.json");
static Preferences ModConfig = new Preferences(ConfigPath);
internal static bool ModConfigLoaded = false;
internal static bool ModConfigUpdated = false;
internal static void Load()
{
customHoldStyleArray = new int[ItemLoader.ItemCount];
customHoldPositions = new Dictionary<string, int>();
ModConfigLoaded = ReadConfig();
}
public static bool ItemTypeHasCustomHoldStyle(int type)
{
return customHoldStyleArray[type] > 0;
}
public static bool TryGetCustomHoldStyle(int type, out int style)
{
style = customHoldStyleArray[type];
return customHoldStyleArray[type] > 0;
}
/// <summary>
/// Add a custom hold for the item
/// </summary>
/// <param name="item"></param>
/// <param name="style">Set to -1 to remove the custom item</param>
public static void UpdateCustomHold(Item item, int style)
{
customHoldStyleArray[item.type] = style;
if (item.modItem != null)
{ UpdateCustomHold(item.modItem.GetType().ToString(), style); }
else
{ UpdateCustomHold(item.type.ToString(), style); }
}
public static void UpdateCustomHoldIncrement(Item item, int amount)
{
int currentStyle;
if (item.modItem != null)
{ customHoldPositions.TryGetValue(item.modItem.GetType().ToString(), out currentStyle); }
else
{ customHoldPositions.TryGetValue(item.type.ToString(), out currentStyle); }
currentStyle += amount;
// this is tightly coupled, but can easily be set in a constructor if necessary
if (currentStyle > PlayerWOFX.HoldTypeCount) currentStyle = 0;
if (currentStyle < 0) currentStyle = PlayerWOFX.HoldTypeCount;
UpdateCustomHold(item, currentStyle);
}
private static void UpdateCustomHold(string itemID, int style)
{
int currentStyle;
customHoldPositions.TryGetValue(itemID, out currentStyle);
if (style > 0)
{
if (customHoldPositions.ContainsKey(itemID))
{ customHoldPositions[itemID] = style; }
else
{ customHoldPositions.Add(itemID, style); }
}
else
{
customHoldPositions.Remove(itemID);
}
if (style != currentStyle) ModConfigUpdated = true;
}
/// <returns> true is loaded successfully </returns>
internal static bool ReadConfig()
{
if (ModConfig.Load())
{
int readVersion = 0;
ModConfig.Get("version", ref readVersion);
if (readVersion != configVersion)
{
bool canUpdate = false;
if (readVersion == 0)
{
canUpdate = true;
}
if (!canUpdate) return false;
}
List<string> allKeys = ModConfig.GetAllKeys();
foreach (string key in allKeys)
{
customHoldPositions.Add(key, ModConfig.Get(key, -1));
}
for(int i = 0; i < ItemLoader.ItemCount; i++)
{
ModItem item = ItemLoader.GetItem(i);
if (item != null)
{
string typeString = item.GetType().ToString();
if (customHoldPositions.ContainsKey(typeString))
{ customHoldStyleArray[i] = customHoldPositions[typeString]; }
}
else
{
if (customHoldPositions.ContainsKey(i.ToString()))
{ customHoldStyleArray[i] = customHoldPositions[i.ToString()]; }
}
}
return true;
}
return false;
}
internal static void SaveConfig()
{
if (ModConfigUpdated)
{
ModConfig.Clear();
ModConfig.Put("version", configVersion);
foreach (KeyValuePair<string, int> pair in customHoldPositions)
{
ModConfig.Put(pair.Key, pair.Value);
}
ModConfig.Save();
ModConfigUpdated = false;
}
}
}
}