forked from Wacky-Mole/WackysDatabase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWackysDatabase.cs
532 lines (471 loc) · 24.7 KB
/
WackysDatabase.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Thank you AzumattDev for the template. It is very good https://github.com/AzumattDev/ItemManagerModTemplate
// Thanks to the Odin Discord server, for being active and good for the valheim community.
// Taking from Azu OpenDatabase code and the orginal now. https://www.nexusmods.com/valheim/mods/319?tab=description
// CustomArmor code from https://github.com/aedenthorn/ValheimMods/blob/master/CustomArmorStats/BepInExPlugin.cs
// Thank you to Rexabit for Visual Modifier - It really is an amazing mod.
using System.IO;
using System.Reflection;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using ServerSync;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using BepInEx.Bootstrap;
using System.Linq;
using PieceManager;
using System.Text;
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Reflection.Emit;
using YamlDotNet;
using wackydatabase.Datas;
using Object = UnityEngine.Object;
using wackydatabase.Startup;
using wackydatabase.Util;
using wackydatabase.SetData;
using wackydatabase.Read;
using UnityEngine.Rendering;
using API;
using wackydatabase.OBJimporter;
using RainbowTrollArmor;
namespace wackydatabase
{
[BepInPlugin(ModGUID, ModName, ModVersion)]
public class WMRecipeCust : BaseUnityPlugin
{
internal const string ModName = "WackysDatabase";
internal const string ModVersion = "2.2.5";
internal const string Author = "WackyMole";
internal const string ModGUID = Author + "." + ModName;
internal static string ConfigFileName = ModGUID + ".cfg";
internal static string ConfigFileFullPath = Paths.ConfigPath + Path.DirectorySeparatorChar + ConfigFileName;
internal static WMRecipeCust context;
internal readonly Harmony _harmony = new(ModGUID);
public static readonly ManualLogSource WLog =
BepInEx.Logging.Logger.CreateLogSource(ModName);
internal static readonly ConfigSync ConfigSync = new(ModGUID)
{ DisplayName = ModName, MinimumRequiredVersion = "2.2.5" }; // it is very picky on version number
public static ConfigEntry<string> NexusModID;
public static ConfigEntry<bool> modEnabled;
public static ConfigEntry<bool> isDebug;
public static ConfigEntry<bool> isautoreload;
public static ConfigEntry<bool> isDebugString;
public static ConfigEntry<string> WaterName;
public static ConfigEntry<float> globalArmorDurabilityLossMult;
public static ConfigEntry<float> globalArmorMovementModMult;
public static ConfigEntry<string> waterModifierName;
public static ConfigEntry<bool> ServerDedLoad;
public static ConfigEntry<bool> extraSecurity;
public static ConfigEntry<bool> enableYMLWatcher;
public static ConfigEntry<bool> clonedcache;
public static ConfigEntry<string> extraEffectList;
internal static ConfigEntry<bool>? _serverConfigLocked;
internal static readonly CustomSyncedValue<string> skillConfigData = new(ConfigSync, "skillConfig", ""); // doesn't show up in config
internal static readonly CustomSyncedValue<string> largeTransfer = new(ConfigSync, "largeTransfer", ""); // Experimental
internal static bool issettoSinglePlayer = false;
internal static bool isSettoAutoReload = false;
internal static bool recieveServerInfo = false;
internal static bool NoMoreLoading = false; // for shutdown from Server
internal static bool LoadinMultiplayerFirst = false; // forces multiplayer sync to wait for first time
internal static string ConnectionError = "";
internal static bool Firstrun = true;
internal static bool AwakeHasRun = false;
internal static bool FirstSessionRun = false;
internal static bool FirstSS = true;
internal static int spawnedinWorld = 0;
internal static bool dedLoad = false;
internal static bool ssLock = false;
internal static bool waitingforFirstLoad = false;
public static List<RecipeData_json> recipeDatas = new List<RecipeData_json>();
public static List<WItemData_json> ItemDatas = new List<WItemData_json>();
public static List<PieceData_json> PieceDatas = new List<PieceData_json>();
public static List<ArmorData_json> armorDatas = new List<ArmorData_json>();
public static List<RecipeData> recipeDatasYml = new List<RecipeData>();
public static List<WItemData> itemDatasYml = new List<WItemData>();
public static List<PieceData> pieceDatasYml = new List<PieceData>();
public static List<ArmorData> armorDatasYml = new List<ArmorData>();
public static List<StatusData> effectDataYml = new List<StatusData>();
public static List<CreatureData> creatureDatasYml = new List<CreatureData>();
public static List<WItemData> cacheItemsYML = new List<WItemData>();// cacheonly
public static List<MaterialInstance> cacheMaterials = new List<MaterialInstance>();// cacheonly
public static List<string> ClonedI = new List<string>(); // items
public static List<string> ClonedINoZ = new List<string>(); // items No Znet // not used
public static Dictionary<string, GameObject> MasterCloneList = new Dictionary<string, GameObject>();
public static readonly Dictionary<string, string> ClonedPrefabsMap = new();
public static List<string> ClonedP = new List<string>(); // pieces
public static List<string> ClonedR = new List<string>(); // recipes
public static List<string> ClonedE = new List<string>(); // effects
public static List<string> ClonedC = new List<string>(); // creatures
public static Dictionary<string, GameObject> ClonedCC = new Dictionary<string, GameObject>(); //Cloned creatureDick
public static List<string> ClonedCR = new List<string>(); // creaturesReplacer
public static List<string> MockI = new List<string>(); // MockItems
public static List<string> BlacklistClone = new List<string>();
public static List<string> MultiplayerApproved = new List<string>();
public static List<GameObject> SnapshotPiecestoDo = new List<GameObject>();
// internal static Dictionary<string, Texture2D> mainTextures = new Dictionary<string, Texture2D>(); // cached Textures for fun
internal static string assetPath;
internal static string assetPathconfig;
internal static string assetPathItems;
internal static string assetPathRecipes;
internal static string assetPathPieces;
internal static string assetPathTextures;
internal static string assetPathMaterials;
internal static string assetPathObjects;
internal static string assetPathCreatures;
internal static string assetPathOldJsons;
internal static string assetPathBulkYML;
internal static string assetPathBulkYMLItems;
internal static string assetPathBulkYMLPieces;
internal static string assetPathBulkYMLEffects;
internal static string assetPathBulkYMLRecipes;
internal static string assetPathIcons;
internal static string assetPathEffects;
internal static string assetPathCache;
internal static string jsonstring;
internal static string ymlstring;
internal static char StringSeparator = 'Ⰴ'; // handcuffs The fifth letter of the Glagolitic alphabet.
internal static bool Admin = true; // for single player, sets to false for multiplayer on connect
public static List<string> pieceWithLvl = new List<string>();
internal static GameObject Root;
internal static GameObject MockItemBase;
public static PieceTable selectedPiecehammer;
//private static List<string> piecemods = new List<string>();
public static PieceTable[] MaybePieceStations;
public static Dictionary<GameObject, GameObject> AdminPiecesOnly;
public static List<string> RealPieceStations = new List<string>();
public static List<CraftingStation> NewCraftingStations = new List<CraftingStation>();
public static Dictionary<string, Material> originalMaterials;
public static Dictionary<string, GameObject> originalVFX;
public static Dictionary<string, GameObject> originalSFX;
public static Dictionary<string, GameObject> originalFX;
public static Dictionary<string, GameObject> extraEffects;
public static Dictionary<string, int> RecipeMaxStationLvl = new Dictionary<string, int>();
public static Dictionary<string, Dictionary<ItemDrop, int>> QualityRecipeReq = new Dictionary<string, Dictionary<ItemDrop, int>>();
public static Dictionary<string, Dictionary<bool, float>> AttackSpeed = new Dictionary<string, Dictionary<bool, float>>();
internal static Startupserver startupserver = new Startupserver();
public static ReadFiles readFiles = new ReadFiles();
public static Reload CurrentReload = new Reload();
public static List<string> NoNotTheseSEs = new List<string>() { "GoblinShaman_shield", "SE_Dvergr_heal", "SE_Greydwarf_shaman_heal" }; // problematic
// public static List<string> extraEffectList = new List<string>() { "lightningAOE" };
internal static int kickcount = 0;
internal static bool jsonsFound = false;
public static bool ForceLogout = false;
internal static bool LobbyRegistered = false;
internal static bool HasLobbied = false;
internal static IEnumerable<string> jsonfiles;
internal static bool ReloadingOkay = false;
internal static int ProcessWait = 10;
internal static int ProcessWaitforRead = 10;
internal static float WaitTime = .3f;
internal static bool LockReload = false;
internal static bool Reloading = false;
internal static bool IsDedServer => SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null;
public void Awake() // start
{
StartupConfig(); // startup varables
assetPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "wackysDatabase");
assetPathconfig = Path.Combine(Path.GetDirectoryName(Paths.ConfigPath + Path.DirectorySeparatorChar), "wackysDatabase");
assetPathItems = Path.Combine(assetPathconfig, "Items");
assetPathRecipes = Path.Combine(assetPathconfig, "Recipes");
assetPathPieces = Path.Combine(assetPathconfig, "Pieces");
assetPathMaterials = Path.Combine(assetPathconfig, "Materials");
assetPathTextures = Path.Combine(assetPathconfig, "Textures");
assetPathEffects = Path.Combine(assetPathconfig, "Effects");
assetPathObjects = Path.Combine(assetPathconfig, "Objects");
assetPathCreatures = Path.Combine(assetPathconfig, "Creatures");
assetPathOldJsons = Path.Combine(Path.GetDirectoryName(Paths.ConfigPath + Path.DirectorySeparatorChar), "wackysDatabase-OldJsons");
assetPathBulkYML = Path.Combine(Path.GetDirectoryName(Paths.ConfigPath + Path.DirectorySeparatorChar), "wackyDatabase-BulkYML");
assetPathBulkYMLItems = Path.Combine(assetPathBulkYML, "Items");
assetPathBulkYMLPieces = Path.Combine(assetPathBulkYML, "Pieces");
assetPathBulkYMLRecipes = Path.Combine(assetPathBulkYML, "Recipes");
assetPathBulkYMLEffects = Path.Combine(assetPathBulkYML, "Effects");
assetPathIcons = Path.Combine(assetPathconfig, "Icons");
assetPathCache = Path.Combine(assetPathconfig, "Cache");
// testme(); // function for testing things
// ending files
AnimationSpeedManager.Add((character, speed) =>
{
if (character is not Player player || !player.InAttack() || player.m_currentAttack is null)
{
return speed;
}
var skilltype = player.GetCurrentWeapon().m_shared.m_skillType;
if (skilltype == Skills.SkillType.Unarmed) return speed; // no unarmed
GameObject val = player.GetCurrentWeapon()?.m_dropPrefab;
if (WMRecipeCust.AttackSpeed.TryGetValue(val.name, out Dictionary<bool, float> Paul))
{
if (player.m_currentAttackIsSecondary)
{
return speed * Paul[true];
}
else
{
return speed * Paul[false];
}
}
return speed;
});
Assembly assembly = Assembly.GetExecutingAssembly();
_harmony.PatchAll(assembly);
jsonfiles = startupserver.CheckForJsons(); // read jsons for server
if (jsonsFound)
{
WMRecipeCust.WLog.LogWarning("Jsons Found");
//startupserver.BeginConvertingJsons(jsoncount);
}
WMRecipeCust.context.StartCoroutine(readFiles.GetDataFromFiles()); // YML get
AwakeHasRun = true;
readFiles.SetupWatcher();
skillConfigData.ValueChanged += CustomSyncEventDetected; // custom sync watcher for yml file synced from server
largeTransfer.ValueChanged += LargeTransferDetected;
}
public static void Dbgl(string str = "", bool pref = true)
{
if (isDebug.Value)
Debug.Log((pref ? ModName + " " : "") + str);
}
private void StartupConfig()
{
_serverConfigLocked = config("General", "Force Server Config", true, "Force Server Config");
_ = ConfigSync.AddLockingConfigEntry(_serverConfigLocked);
//PrefabContainer.transform.parent = Main.RootObject.transform; ? JVL onto something
WMRecipeCust.Root = new GameObject("myroot");
WMRecipeCust.Root.SetActive(false);
WMRecipeCust.DontDestroyOnLoad(WMRecipeCust.Root); // clone magic
// ^^ // starting files
context = this;
modEnabled = config<bool>("General", "Enabled", true, "Enable this mod");
NexusModID = config<string>("General", "NexusModID", "1825", "NexusModID Number", false);
isDebug = config<bool>("General", "IsDebug", true, "Enable debug logs", false);
isDebugString = config<bool>("General", "StringisDebug", false, "Do You want to see the String Debug Log - extra logs");
isautoreload = config<bool>("General", "IsAutoReload", false, new ConfigDescription("Enable auto reload after wackydb_save or wackydb_clone for singleplayer", null, new ConfigurationManagerAttributes { Browsable = false })); // not browseable and can only be set before launch
WaterName = config<string>("Armor", "WaterName", "Water", "Water name for Armor Resistance", false);
ServerDedLoad = config<bool>("General", "DedServer load Memory", true, "Dedicated Servers will load wackydb files as a client would");
extraSecurity = config<bool>("General", "ExtraSecurity on Servers", true, "Makes sure a player can't load into a server after going into Singleplayer -resulting in Game Ver .0.0.1, - Recommended to keep this enabled");
enableYMLWatcher = config<bool>("General", "FileWatcher for YMLs", true, "EnableYMLWatcher Servers/Singleplayer, YMLs will autoreload if Wackydatabase folder changes(created,renamed,edited) - disable for some servers that auto reload too much");
// clonedcache = config<bool>("General", "Enabled Cloned Cache", true, "Turn on CloneCache so that Character items appear in the Start Menu");
extraEffectList = config<string>("Effects", "List of Extra Effects", "lightningAOE", "Extra Effects to look for from base game or Mods - (Use_a_comma,No_spaces)");
ConfigSync.CurrentVersion = ModVersion;
WLog.LogDebug("Mod Version " + ConfigSync.CurrentVersion);
if (isautoreload.Value) // only sets at start
isSettoAutoReload = true;
else isSettoAutoReload = false;
}
private void OnDestroy()
{
Config.Save();
//WLog.LogInfo("Calling the Destroyer of Worlds -End Game"); past its time :(
}
private void ReadConfigValues(object sender, FileSystemEventArgs e)
{
if (!File.Exists(ConfigFileFullPath)) return;
try
{
Config.Reload();
}
catch { WLog.LogError($"There was an issue loading Config File "); }
}
private ConfigEntry<T> config<T>(string group, string name, T value, ConfigDescription description,
bool synchronizedSetting = true)
{
ConfigDescription extendedDescription =
new(
description.Description +
(synchronizedSetting ? " [Synced with Server]" : " [Not Synced with Server]"),
description.AcceptableValues, description.Tags);
ConfigEntry<T> configEntry = Config.Bind(group, name, value, extendedDescription);
//var configEntry = Config.Bind(group, name, value, description);
SyncedConfigEntry<T> syncedConfigEntry = ConfigSync.AddConfigEntry(configEntry);
syncedConfigEntry.SynchronizedConfig = synchronizedSetting;
return configEntry;
}
private ConfigEntry<T> config<T>(string group, string name, T value, string description,
bool synchronizedSetting = true)
{
return config(group, name, value, new ConfigDescription(description), synchronizedSetting);
}
private class ConfigurationManagerAttributes
{
public bool? Browsable = false;
}
internal static void AddBlacklistClone(string prefab)
{
if (prefab == null) return;
if (BlacklistClone.Contains(prefab)) return;
BlacklistClone.Add(prefab);
}
private void CustomSyncEventDetected()
{
if (WMRecipeCust.NoMoreLoading)
return;
CurrentReload.SyncEventDetected();
}
private void LargeTransferDetected()
{
HandleData.RecievedData();
}
internal static void CheckModFolder()
{
/*
if (Directory.Exists(assetPath) && !Directory.Exists(assetPathconfig)) Too OLD NOW, you should have upgraded
{
WLog.LogWarning("Creating Config Mod folder and Moving Old WackysDatafolder to configs");
try { Directory.Move(assetPath, assetPathconfig); } catch { WLog.LogWarning("Error caught,but should have moved wackyDatabase folder correctly though"); }
} */
if (!Directory.Exists(assetPathconfig))
{
Dbgl("Creating Config Mod folder");
Directory.CreateDirectory(assetPathconfig);
Directory.CreateDirectory(assetPathItems);
Directory.CreateDirectory(assetPathPieces);
Directory.CreateDirectory(assetPathRecipes);
Directory.CreateDirectory(assetPathTextures);
Directory.CreateDirectory(assetPathMaterials);
}
if (!Directory.Exists(assetPathItems))
{
Dbgl("Creating Items Folder");
Directory.CreateDirectory(assetPathItems);
}
if (!Directory.Exists(assetPathPieces))
{
Dbgl("Creating Pieces Folder");
Directory.CreateDirectory(assetPathPieces);
}
if (!Directory.Exists(assetPathRecipes))
{
Dbgl("Creating Recipes Folder");
Directory.CreateDirectory(assetPathRecipes);
}
if (!Directory.Exists(assetPathIcons))
{
Dbgl("Creating Icons Folder");
Directory.CreateDirectory(assetPathIcons);
}
if (!Directory.Exists(assetPathEffects))
{
Dbgl("Creating Effects folder");
Directory.CreateDirectory(assetPathEffects);
}
if (!Directory.Exists(assetPathCache))
{
Dbgl("Creating Cache folder");
Directory.CreateDirectory(assetPathCache);
}
if (!Directory.Exists(assetPathObjects))
{
Dbgl("Creating Objects folder");
Directory.CreateDirectory(assetPathObjects);
}
if (!Directory.Exists(assetPathTextures))
{
Dbgl("Creating Texture folder");
Directory.CreateDirectory(assetPathTextures);
}
if (!Directory.Exists(assetPathMaterials))
{
Dbgl("Creating Materials folder");
Directory.CreateDirectory(assetPathMaterials);
}
if (!Directory.Exists(assetPathCreatures))
{
Dbgl("Creating Creature folder");
Directory.CreateDirectory(assetPathCreatures);
}
/*
var versionpath = Path.Combine(assetPathCache, $"Last_Cleared.txt");
if (File.Exists(versionpath))
{
var filev = File.ReadAllText(versionpath);
if (filev != ModVersion)
{
WMRecipeCust.WLog.LogWarning("New Wackydb Version, deleteing Cache");
DeleteCache();
}
}
else
{
File.WriteAllText(versionpath, ModVersion);
}
*/
}
public static void DeleteCache()
{
var versionpath = Path.Combine(assetPathCache, $"Last_Cleared.txt");
Directory.Delete(assetPathCache, true);
Directory.CreateDirectory(assetPathCache);
File.WriteAllText(versionpath, ModVersion);
}
public static void AdminReload(long peer, ZPackage go)
{
WMRecipeCust.WLog.LogInfo("Recieved Admin Request to Reload");
ReadFiles readnow = new ReadFiles();
WMRecipeCust.context.StartCoroutine(readnow.GetDataFromFiles());
WMRecipeCust.readFiles = readnow;
WMRecipeCust.skillConfigData.Value = ymlstring;// push to clients
SetData.Reload josh = new SetData.Reload();
WMRecipeCust.CurrentReload = josh;
WMRecipeCust.WLog.LogInfo("Sent YML to clients");
if (WMRecipeCust.ServerDedLoad.Value)
WMRecipeCust.context.StartCoroutine(josh.LoadAllRecipeData(true, true)); // Admin Reload
}
public static void GetAllMaterials(bool skipMD = false) // Get all Materials, SFX, VFX, FX
{
Material[] array = Resources.FindObjectsOfTypeAll<Material>();
GameObject[] array3 = Resources.FindObjectsOfTypeAll<GameObject>();
originalMaterials = new Dictionary<string, Material>();
originalVFX = new Dictionary<string, GameObject>();
originalSFX = new Dictionary<string, GameObject>();
originalFX = new Dictionary<string, GameObject>();
extraEffects = new Dictionary<string, GameObject>();
foreach (Material val in array)
{
originalMaterials[val.name] = val;
}
foreach (GameObject val1 in array3)
{
if (val1.name.ToLower().StartsWith("vfx"))
{
originalVFX[val1.name] = val1;
}
else if (val1.name.ToLower().StartsWith("sfx"))
{
originalSFX[val1.name] = val1;
}
else if (val1.name.ToLower().StartsWith("fx_"))
{
originalFX[val1.name] = val1;
}
else if (extraEffectList.Value.Split(',').ToList().Contains(val1.name))
{
extraEffects[val1.name] = val1;
}
}
if (!skipMD)
{
MaterialDataManager.Instance.LoadFiles();
}
}
/*
[HarmonyPatch(typeof(FejdStartup), "Start")]
static class FejdstartupWackyBlackPatch // Add your name/modname to this class method - should be unique
{
private static void Prefix()
{
if (FejdStartup.m_firstStartup)
{
if (WackyDatabase_API.IsInstalled())
{
// Add blacklist here
// WackyDatabase_API.AddBlacklistClone("Wood");
}
}
}
} example code for modders
*/
}
}