generated from LethalCompanyCommunity/LethalCompanyTemplate
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMoreSuits.cs
427 lines (368 loc) · 23.4 KB
/
MoreSuits.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace MoreSuits
{
[BepInPlugin(modGUID, modName, modVersion)]
public class MoreSuitsMod : BaseUnityPlugin
{
private const string modGUID = "x753.More_Suits";
private const string modName = "More Suits";
private const string modVersion = "1.4.3";
private readonly Harmony harmony = new Harmony(modGUID);
private static MoreSuitsMod Instance;
public static bool SuitsAdded = false;
public static string DisabledSuits;
public static bool LoadAllSuits;
public static bool MakeSuitsFitOnRack;
public static bool UnlockAll;
public static int MaxSuits;
public static List<Material> customMaterials = new List<Material>();
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
DisabledSuits = Config.Bind("General", "Disabled Suit List", "UglySuit751.png,UglySuit752.png,UglySuit753.png", "Comma-separated list of suits that shouldn't be loaded").Value;
LoadAllSuits = Config.Bind("General", "Ignore !less-suits.txt", false, "If true, ignores the !less-suits.txt file and will attempt to load every suit, except those in the disabled list. This should be true if you're not worried about having too many suits.").Value;
MakeSuitsFitOnRack = Config.Bind("General", "Make Suits Fit on Rack", true, "If true, squishes the suits together so more can fit on the rack.").Value;
UnlockAll = Config.Bind("General", "Unlock All Suits", false, "If true, unlocks all custom suits that would normally be sold in the shop.").Value;
MaxSuits = Config.Bind("General", "Max Suits", 100, "The maximum number of suits to load. If you have more, some will be ignored.").Value;
harmony.PatchAll();
Logger.LogInfo($"Plugin {modName} is loaded!");
}
[HarmonyPatch(typeof(StartOfRound))]
internal class StartOfRoundPatch
{
[HarmonyPatch("Start")]
[HarmonyPrefix]
static void StartPatch(ref StartOfRound __instance)
{
try
{
if (!SuitsAdded) // we only need to add the new suits to the unlockables list once per game launch
{
int originalUnlockablesCount = __instance.unlockablesList.unlockables.Count;
UnlockableItem originalSuit = new UnlockableItem();
int addedSuitCount = 0;
for (int i = 0; i < __instance.unlockablesList.unlockables.Count; i++)
{
UnlockableItem unlockableItem = __instance.unlockablesList.unlockables[i];
if (unlockableItem.suitMaterial != null && unlockableItem.alreadyUnlocked) // find the default suit to use as a base
{
originalSuit = unlockableItem;
// Get all .png files from all folders named moresuits in the BepInEx/plugins folder
List<string> suitsFolderPaths = Directory.GetDirectories(Paths.PluginPath, "moresuits", SearchOption.AllDirectories).ToList<string>();
List<string> texturePaths = new List<string>();
List<string> assetPaths = new List<string>();
List<string> disabledSuits = DisabledSuits.ToLower().Replace(".png", "").Split(',').ToList();
List<string> disabledDefaultSuits = new List<string>();
// Check through each moresuits folder for a text file called !less-suits.txt, which signals not to load any of the original suits that come with this mod
if (!LoadAllSuits)
{
foreach (string suitsFolderPath in suitsFolderPaths)
{
if (File.Exists(Path.Combine(suitsFolderPath, "!less-suits.txt")))
{
string[] defaultSuits = { "glow", "kirby", "knuckles", "luigi", "mario", "minion", "skeleton", "slayer", "smile" };
disabledDefaultSuits.AddRange(defaultSuits); // add every default suit in the mod to the disabled suits list
break;
}
}
}
foreach (string suitsFolderPath in suitsFolderPaths)
{
if (suitsFolderPath != "")
{
string[] pngFiles = Directory.GetFiles(suitsFolderPath, "*.png");
string[] bundleFiles = Directory.GetFiles(suitsFolderPath, "*.matbundle");
texturePaths.AddRange(pngFiles);
assetPaths.AddRange(bundleFiles);
}
}
assetPaths.Sort();
texturePaths.Sort();
//assetPaths = assetPaths.OrderBy(Path.GetFileNameWithoutExtension).ThenBy(p => p).ToList();
//texturePaths = texturePaths.OrderBy(Path.GetFileNameWithoutExtension).ThenBy(p => p).ToList();
try
{
foreach (string assetPath in assetPaths)
{
AssetBundle assetBundle = AssetBundle.LoadFromFile(assetPath);
UnityEngine.Object[] assets = assetBundle.LoadAllAssets();
foreach (UnityEngine.Object asset in assets)
{
if (asset is Material)
{
Material material = (Material)asset;
customMaterials.Add(material);
}
}
}
}
catch (Exception ex)
{
Debug.Log("Something went wrong with More Suits! Could not load materials from asset bundle(s). Error: " + ex);
}
// Create new suits for each .png
foreach (string texturePath in texturePaths)
{
// skip each suit that is in the disabled suits list
if (disabledSuits.Contains(Path.GetFileNameWithoutExtension(texturePath).ToLower())) { continue; }
string originalMoreSuitsPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (disabledDefaultSuits.Contains(Path.GetFileNameWithoutExtension(texturePath).ToLower()) && texturePath.Contains(originalMoreSuitsPath)) { continue; }
UnlockableItem newSuit;
Material newMaterial;
if (Path.GetFileNameWithoutExtension(texturePath).ToLower() == "default")
{
newSuit = originalSuit;
newMaterial = newSuit.suitMaterial;
}
else
{
// Serialize and deserialize to create a deep copy of the original suit item
newSuit = JsonUtility.FromJson<UnlockableItem>(JsonUtility.ToJson(originalSuit));
newMaterial = Instantiate(newSuit.suitMaterial);
}
byte[] fileData = File.ReadAllBytes(texturePath);
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(fileData);
texture.Apply(true, true);
newMaterial.mainTexture = texture;
newSuit.unlockableName = Path.GetFileNameWithoutExtension(texturePath);
// Optional modification of other properties like normal maps, emission, etc
// https://docs.unity3d.com/Packages/[email protected]/manual/Lit-Shader.html
try
{
string advancedJsonPath = Path.Combine(Path.GetDirectoryName(texturePath), "advanced", newSuit.unlockableName + ".json");
if (File.Exists(advancedJsonPath))
{
string[] lines = File.ReadAllLines(advancedJsonPath);
foreach (string line in lines)
{
string[] keyValue = line.Trim().Split(':');
if (keyValue.Length == 2)
{
string keyData = keyValue[0].Trim('"', ' ', ',');
string valueData = keyValue[1].Trim('"', ' ', ',');
if (valueData.Contains(".png"))
{
string advancedTexturePath = Path.Combine(Path.GetDirectoryName(texturePath), "advanced", valueData);
byte[] advancedTextureData = File.ReadAllBytes(advancedTexturePath);
Texture2D advancedTexture = new Texture2D(2, 2);
advancedTexture.LoadImage(advancedTextureData);
advancedTexture.Apply(true, true);
newMaterial.SetTexture(keyData, advancedTexture);
}
else if (keyData == "PRICE" && int.TryParse(valueData, out int intValue)) // If the advanced json has a price, set it up so it rotates into the shop
{
try
{
if(!UnlockAll)
newSuit = AddToRotatingShop(newSuit, intValue, __instance.unlockablesList.unlockables.Count);
}
catch (Exception ex)
{
Debug.Log("Something went wrong with More Suits! Could not add a suit to the rotating shop. Error: " + ex);
}
}
else if (valueData == "KEYWORD")
{
newMaterial.EnableKeyword(keyData);
}
else if (valueData == "DISABLEKEYWORD")
{
newMaterial.DisableKeyword(keyData);
}
else if (valueData == "SHADERPASS")
{
newMaterial.SetShaderPassEnabled(keyData, true);
}
else if (valueData == "DISABLESHADERPASS")
{
newMaterial.SetShaderPassEnabled(keyData, false);
}
else if (keyData == "SHADER")
{
Shader newShader = Shader.Find(valueData);
newMaterial.shader = newShader;
}
else if (keyData == "MATERIAL")
{
foreach (Material material in customMaterials)
{
if (material.name == valueData)
{
newMaterial = Instantiate(material);
newMaterial.mainTexture = texture;
break;
}
}
}
else if (float.TryParse(valueData, out float floatValue))
{
newMaterial.SetFloat(keyData, floatValue);
}
else if (TryParseVector4(valueData, out Vector4 vectorValue))
{
newMaterial.SetVector(keyData, vectorValue);
}
}
}
}
}
catch (Exception ex)
{
Debug.Log("Something went wrong with More Suits! Error: " + ex);
}
newSuit.suitMaterial = newMaterial;
if (newSuit.unlockableName.ToLower() != "default")
{
if (addedSuitCount == MaxSuits)
{
Debug.Log("Attempted to add a suit, but you've already reached the max number of suits! Modify the config if you want more.");
}
else
{
__instance.unlockablesList.unlockables.Add(newSuit);
addedSuitCount++;
}
}
}
SuitsAdded = true;
break;
}
}
UnlockableItem dummySuit = JsonUtility.FromJson<UnlockableItem>(JsonUtility.ToJson(originalSuit));
dummySuit.alreadyUnlocked = false;
dummySuit.hasBeenMoved = false;
dummySuit.placedPosition = Vector3.zero;
dummySuit.placedRotation = Vector3.zero;
dummySuit.unlockableType = 753; // this unlockable type is not used
while (__instance.unlockablesList.unlockables.Count < originalUnlockablesCount + MaxSuits)
{
__instance.unlockablesList.unlockables.Add(dummySuit);
}
}
}
catch (Exception ex)
{
Debug.Log("Something went wrong with More Suits! Error: " + ex);
}
}
[HarmonyPatch("PositionSuitsOnRack")]
[HarmonyPrefix]
static bool PositionSuitsOnRackPatch(ref StartOfRound __instance)
{
List<UnlockableSuit> suits = UnityEngine.Object.FindObjectsOfType<UnlockableSuit>().ToList<UnlockableSuit>();
suits = suits.OrderBy(suit => suit.syncedSuitID.Value).ToList();
int index = 0;
foreach (UnlockableSuit suit in suits)
{
AutoParentToShip component = suit.gameObject.GetComponent<AutoParentToShip>();
component.overrideOffset = true;
float offsetModifier = 0.18f;
if (MakeSuitsFitOnRack && suits.Count > 13)
{
offsetModifier = offsetModifier / (Math.Min(suits.Count, 20) / 12f); // squish the suits together to make them all fit
}
component.positionOffset = new Vector3(-2.45f, 2.75f, -8.41f) + __instance.rightmostSuitPosition.forward * offsetModifier * (float)index;
component.rotationOffset = new Vector3(0f, 90f, 0f);
index++;
}
return false; // don't run the original
}
}
private static TerminalNode cancelPurchase;
private static TerminalKeyword buyKeyword;
private static UnlockableItem AddToRotatingShop(UnlockableItem newSuit, int price, int unlockableID)
{
Terminal terminal = UnityEngine.Object.FindObjectOfType<Terminal>();
for (int i = 0; i < terminal.terminalNodes.allKeywords.Length; i++)
{
if (terminal.terminalNodes.allKeywords[i].name == "Buy")
{
buyKeyword = terminal.terminalNodes.allKeywords[i];
break;
}
}
newSuit.alreadyUnlocked = false;
newSuit.hasBeenMoved = false;
newSuit.placedPosition = Vector3.zero;
newSuit.placedRotation = Vector3.zero;
newSuit.shopSelectionNode = ScriptableObject.CreateInstance<TerminalNode>();
newSuit.shopSelectionNode.name = newSuit.unlockableName + "SuitBuy1";
newSuit.shopSelectionNode.creatureName = newSuit.unlockableName + " suit";
newSuit.shopSelectionNode.displayText = "You have requested to order " + newSuit.unlockableName + " suits.\nTotal cost of item: [totalCost].\n\nPlease CONFIRM or DENY.\n\n";
newSuit.shopSelectionNode.clearPreviousText = true;
newSuit.shopSelectionNode.shipUnlockableID = unlockableID;
newSuit.shopSelectionNode.itemCost = price;
newSuit.shopSelectionNode.overrideOptions = true;
CompatibleNoun confirm = new CompatibleNoun();
confirm.noun = ScriptableObject.CreateInstance<TerminalKeyword>();
confirm.noun.word = "confirm";
confirm.noun.isVerb = true;
confirm.result = ScriptableObject.CreateInstance<TerminalNode>();
confirm.result.name = newSuit.unlockableName + "SuitBuyConfirm";
confirm.result.creatureName = "";
confirm.result.displayText = "Ordered " + newSuit.unlockableName + " suits! Your new balance is [playerCredits].\n\n";
confirm.result.clearPreviousText = true;
confirm.result.shipUnlockableID = unlockableID;
confirm.result.buyUnlockable = true;
confirm.result.itemCost = price;
confirm.result.terminalEvent = "";
CompatibleNoun deny = new CompatibleNoun();
deny.noun = ScriptableObject.CreateInstance<TerminalKeyword>();
deny.noun.word = "deny";
deny.noun.isVerb = true;
if (cancelPurchase == null)
{
cancelPurchase = ScriptableObject.CreateInstance<TerminalNode>(); // we can use the same Cancel Purchase node
}
deny.result = cancelPurchase;
deny.result.name = "MoreSuitsCancelPurchase";
deny.result.displayText = "Cancelled order.\n";
newSuit.shopSelectionNode.terminalOptions = new CompatibleNoun[] { confirm, deny };
TerminalKeyword suitKeyword = ScriptableObject.CreateInstance<TerminalKeyword>();
suitKeyword.name = newSuit.unlockableName + "Suit";
suitKeyword.word = newSuit.unlockableName.ToLower() + " suit";
suitKeyword.defaultVerb = buyKeyword;
CompatibleNoun suitCompatibleNoun = new CompatibleNoun();
suitCompatibleNoun.noun = suitKeyword;
suitCompatibleNoun.result = newSuit.shopSelectionNode;
List<CompatibleNoun> buyKeywordList = buyKeyword.compatibleNouns.ToList<CompatibleNoun>();
buyKeywordList.Add(suitCompatibleNoun);
buyKeyword.compatibleNouns = buyKeywordList.ToArray();
List<TerminalKeyword> allKeywordsList = terminal.terminalNodes.allKeywords.ToList();
allKeywordsList.Add(suitKeyword);
allKeywordsList.Add(confirm.noun);
allKeywordsList.Add(deny.noun);
terminal.terminalNodes.allKeywords = allKeywordsList.ToArray();
return newSuit;
}
public static bool TryParseVector4(string input, out Vector4 vector)
{
vector = Vector4.zero;
string[] components = input.Split(',');
if (components.Length == 4)
{
if (float.TryParse(components[0], out float x) &&
float.TryParse(components[1], out float y) &&
float.TryParse(components[2], out float z) &&
float.TryParse(components[3], out float w))
{
vector = new Vector4(x, y, z, w);
return true;
}
}
return false;
}
}
}