-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.cs
124 lines (102 loc) · 3.95 KB
/
Plugin.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
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ATS_API;
using BepInEx;
using BepInEx.Logging;
using Eremite;
using Eremite.Controller;
using HarmonyLib;
using JLPlugin;
using UnityEngine;
namespace ATS_JSONLoader;
[HarmonyPatch]
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
[BepInDependency("API", BepInDependency.DependencyFlags.HardDependency)]
public class Plugin : BaseUnityPlugin
{
public static string PluginDirectory;
public static Plugin Instance;
public static ManualLogSource Log;
public static string JSONLoaderDirectory = "";
public static string BepInExDirectory = "";
public static string ExportDirectory => Path.Combine(Application.persistentDataPath, "JSONLoader", "Exported");
private Harmony harmony;
private static List<string> GetAllJLDRFiles()
{
string exportedFolder = Path.Combine(JSONLoaderDirectory, "Exported");
string examplesFolder = Path.Combine(JSONLoaderDirectory, "Examples");
return Directory.GetFiles(Paths.PluginPath, "*.json", SearchOption.AllDirectories)
.Where(a=> !a.Contains(exportedFolder) && !a.Contains(examplesFolder))
.ToList();
}
private void Awake()
{
Logger.LogInfo($"Loading JSONLoader!");
Instance = this;
Log = Logger;
JSONLoaderDirectory = Path.GetDirectoryName(Info.Location);
// Stops Unity from destroying it for some reason. Same as Setting the BepInEx config HideManagerGameObject to true.
gameObject.hideFlags = HideFlags.HideAndDontSave;
harmony = Harmony.CreateAndPatchAll(typeof(Plugin).Assembly, PluginInfo.PLUGIN_GUID);
int bepInExIndex = Info.Location.LastIndexOf("BepInEx");
if (bepInExIndex > 0)
{
BepInExDirectory = Info.Location.Substring(0, bepInExIndex);
}
else
{
BepInExDirectory = Directory.GetParent(JSONLoaderDirectory)?.FullName ?? "";
}
Configs.InitializeConfigs(Config);
Hotkeys.RegisterKey(PluginInfo.PLUGIN_NAME, "reload", "Reload all JSON Files", [KeyCode.F5], () =>
{
Logger.LogInfo($"Reloading JSONLoader!");
LoadAllFiles();
});
Logger.LogInfo($"Loaded JSONLoader!");
}
[HarmonyPatch(typeof(MainController), nameof(MainController.OnServicesReady))]
[HarmonyPostfix]
private static void HookMainControllerSetup()
{
// Too difficult to predict when GameController will exist and I can hook observers to it
// So just use Harmony and save us all some time. This method will run after every game start
var isNewGame = MB.GameSaveService.IsNewGame();
Instance.Logger.LogInfo($"Entered a game. Is this a new game: {isNewGame}.");
// Wait 1 frame because most mods add content in this method
// And some JSON files will modify these
Instance.StartCoroutine(GameLoaded());
}
private static IEnumerator GameLoaded()
{
yield return new WaitForEndOfFrame();
LoadAllFiles();
if (Configs.Export)
{
ExportAllFiles();
}
}
private static void LoadAllFiles()
{
List<string> files = GetAllJLDRFiles();
GoodsLoader.LoadAll(files);
RaceLoader.LoadAll(files);
DifficultyLoader.LoadAll(files);
MetaRewardLoader.LoadAll(files);
}
private static void ExportAllFiles()
{
if (!Directory.Exists(ExportDirectory))
{
Directory.CreateDirectory(ExportDirectory);
}
Log.LogInfo($"Exporting all files to {ExportDirectory}... Grab a coffee... this will take a long time.");
JSONSchemaGenerator.GenerateAndExport();
GoodsLoader.ExportAll();
RaceLoader.ExportAll();
DifficultyLoader.ExportAll();
MetaRewardLoader.ExportAll();
}
}