-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.cs
More file actions
279 lines (249 loc) · 11.7 KB
/
Plugin.cs
File metadata and controls
279 lines (249 loc) · 11.7 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
/*
Copyright NeonFlames 2024
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*/
using System.Collections;
using System.Linq;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using BepInEx.Unity.IL2CPP.Utils.Collections;
using GPUInstancer;
using HarmonyLib;
using UnityEngine;
using UnityStandardAssets.ImageEffects;
namespace MIDNIGHT_RUNNER;
public class OptionsTemplate {
public ConfigEntry<bool> enable;
public void Configure(ConfigFile c, string s) {
enable = c.Bind(s, "Enable", false, $"Enables {s} modifications");
}
}
public class VHSOptions : OptionsTemplate {
public ConfigEntry<bool> showNoise, showJitter, bleed, fisheye, filmgrain,
feedback, vignette;
public new void Configure(ConfigFile c, string s) {
base.Configure(c, s);
showNoise = c.Bind(s, "ShowNoise", true);
showJitter = c.Bind(s, "ShowJitter", true);
bleed = c.Bind(s, "Bleed", true);
feedback = c.Bind(s, "Feedback", true);
fisheye = c.Bind(s, "Fisheye", true);
filmgrain = c.Bind(s, "Filmgrain", true);
vignette = c.Bind(s, "Vignette", true);
}
}
public class DOFOptions : OptionsTemplate {
public ConfigEntry<DepthOfField.BlurSampleCount> blurSampleCount;
public ConfigEntry<DepthOfField.BlurType> blurType;
public ConfigEntry<bool> visualizeFocus, highResolution;
public new void Configure(ConfigFile c, string s) {
base.Configure(c, s);
highResolution = c.Bind(s, "HighResolution", true);
blurSampleCount = c.Bind(s, "BlurSampleCount", DepthOfField.BlurSampleCount.Medium);
blurType = c.Bind(s, "BlurType", DepthOfField.BlurType.DiscBlur);
visualizeFocus = c.Bind(s, "VisualizeFocus", true);
}
}
public class GIMMIOptions : OptionsTemplate {
public ConfigEntry<bool> importDetails, importObjects, importTrees, prefabInThreads, detailInThreads;
public new void Configure(ConfigFile c, string s) {
base.Configure(c, s);
importDetails = c.Bind(s, "ImportDetails", true);
importObjects = c.Bind(s, "ImportObjects", true);
importTrees = c.Bind(s, "ImportTrees", true);
detailInThreads = c.Bind(s, "DetailRunInThreads", true);
prefabInThreads = c.Bind(s, "PrefabRunInThreads", true);
}
}
public class GITSOptions : OptionsTemplate {
public ConfigEntry<float> maxDetailDistance, maxTreeDistance;
public new void Configure(ConfigFile c, string s) {
base.Configure(c, s);
maxDetailDistance = c.Bind(s, "MaxDetailDistance", 1.0f);
maxTreeDistance = c.Bind(s, "MaxTreeDistance", 1.0f);
}
}
public class EngineOptions : OptionsTemplate {
public ConfigEntry<float> lodBias;
public ConfigEntry<int> mipmapMaxReduction;
public ConfigEntry<QualityLevel> qualityLevel;
public ConfigEntry<ShadowResolution> shadowRes;
public int _qlevels;
public new void Configure(ConfigFile c, string s) {
base.Configure(c, s);
lodBias = c.Bind(s, "LodBias", 1.0f, "Value must be greater than 0");
mipmapMaxReduction = c.Bind(s, "MipmapMaxReduction", 2, "Maximum level of mipmap reduction");
if (lodBias.Value < 0.0f) lodBias.Value = 0.0f;
string[] _qnames = QualitySettings.names;
string _qname = _qnames.First();
if (_qnames.Length > 1) foreach (string str in _qnames[1..]) {
_qname += str;
}
qualityLevel = c.Bind(s, "QualityLevel", QualitySettings.currentLevel, $"Available levels: {_qname}");
shadowRes = c.Bind(s, "ShadowResolution", ShadowResolution.High);
}
}
public class MRCFG {
public bool dofEnable, vhsEnable, gimmiEnable, gitsEnable, engineEnable;
public delegate void dofD(DepthOfField inst);
public dofD dofd;
public delegate void vhsD(postVHSPro inst);
public vhsD vhsd;
public delegate void gimmiD(GPUInstancerMapMagicIntegration inst);
public gimmiD gimmid;
public delegate void gitsD(GPUInstancerTerrainSettings inst);
public gitsD gitsd;
public delegate void engineD();
public engineD engined;
public void Consume(DOFOptions dof, VHSOptions vhs, GIMMIOptions gimmi, GITSOptions gits, EngineOptions engine) {
if (engine.enable.Value) {
engineEnable = true;
engined = () => {
QualitySettings.lodBias = engine.lodBias.Value;
QualitySettings.SetQualityLevel((int)engine.qualityLevel.Value, true);
QualitySettings.shadowResolution = engine.shadowRes.Value;
QualitySettings.streamingMipmapsMaxLevelReduction = engine.mipmapMaxReduction.Value;
};
} else engineEnable = false;
if (dof.enable.Value) {
dofEnable = true;
dofd = (DepthOfField inst) => {
inst.blurSampleCount = dof.blurSampleCount.Value;
inst.blurType = dof.blurType.Value;
};
if (!dof.highResolution.Value) dofd += (DepthOfField inst) => {inst.highResolution = false;};
if (!dof.visualizeFocus.Value) dofd += (DepthOfField inst) => {inst.visualizeFocus = false;};
} else dofEnable = false;
if (vhs.enable.Value) {
vhsEnable = true;
vhsd = (_) => {};
if (!vhs.showNoise.Value) vhsd += (postVHSPro inst) => {inst.g_showNoise = false;};
if (!vhs.showJitter.Value) vhsd += (postVHSPro inst) => {inst.g_showJitter = false;};
if (!vhs.bleed.Value) vhsd += (postVHSPro inst) => {inst.bleedOn = false;};
if (!vhs.filmgrain.Value) vhsd += (postVHSPro inst) => {inst.filmgrainOn = false;};
if (!vhs.fisheye.Value) vhsd += (postVHSPro inst) => {inst.fisheyeOn = false;};
if (!vhs.feedback.Value) vhsd += (postVHSPro inst) => {inst.feedbackOn = false;};
if (!vhs.vignette.Value) vhsd += (postVHSPro inst) => {inst.vignetteOn = false;};
} else vhsEnable = false;
if (gimmi.enable.Value) {
gimmiEnable = true;
gimmid = (GPUInstancerMapMagicIntegration inst) => {
inst.importObjects = gimmi.importObjects.Value;
inst.importTrees = gimmi.importTrees.Value;
inst.importDetails = gimmi.importDetails.Value;
inst.prefabRunInThreads = gimmi.prefabInThreads.Value;
inst.detailRunInThreads = gimmi.detailInThreads.Value;
};
if (gits.enable.Value) {
gitsEnable = true;
gitsd += (GPUInstancerTerrainSettings inst) => {
inst.maxDetailDistance = gits.maxDetailDistance.Value;
inst.maxTreeDistance = gits.maxTreeDistance.Value;
};
} else gitsEnable = false;
} else gimmiEnable = false;
}
}
public class VHS_Patch {
static IEnumerator IE(postVHSPro inst) {
while (true) {
Plugin.CFG.vhsd(inst);
yield return new WaitForSeconds(2.5f);
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(postVHSPro), "Awake")]
public static void AwakePost(ref postVHSPro __instance) {
__instance.StartCoroutine(IE(__instance).WrapToIl2Cpp());
}
}
public class DOF_Patch {
static IEnumerator IE(DepthOfField inst) {
while (true) {
Plugin.CFG.dofd(inst);
yield return new WaitForSeconds(2.5f);
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(DepthOfField), "OnEnable")]
public static void OnEnablePost(ref DepthOfField __instance) {
__instance.StartCoroutine(IE(__instance).WrapToIl2Cpp());
}
[HarmonyPostfix]
[HarmonyPatch(typeof(DepthOfField), "OnDisable")]
public static void OnDisablePost(ref DepthOfField __instance) {
__instance.StopCoroutine(IE(__instance).WrapToIl2Cpp());
}
}
public partial class MRLive : MonoBehaviour {
MRCFG.gimmiD gimmi;
MRCFG.gitsD gits;
delegate void gameD();
gameD gamed;
public void Awake() {
gits = (_) => {};
gamed = () => {};
if (Plugin.CFG.gimmiEnable) {
gimmi = Plugin.CFG.gimmid;
if (Plugin.CFG.gitsEnable) gits = Plugin.CFG.gitsd;
gamed += () => {
foreach (GPUInstancerMapMagicIntegration inst in FindObjectsOfType<GPUInstancerMapMagicIntegration>()) {
gimmi(inst);
gits(inst.terrainSettings);
}
};
}
if (Plugin.CFG.engineEnable) {
Plugin.CFG.engined();
}
}
public void Start() {
StartCoroutine(Game().WrapToIl2Cpp());
}
public IEnumerator Game() {
while (true) {
gamed();
yield return new WaitForSeconds(5f);
}
}
}
[BepInPlugin("neonflames.midnight_runner", MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
public class Plugin : BasePlugin {
internal static new ManualLogSource Log;
static ConfigEntry<bool> enable;
public DOFOptions dofOpts = new();
public VHSOptions vhsOpts = new();
public GIMMIOptions gimmiOpts = new();
public GITSOptions gitsOpts = new();
public EngineOptions engineOpts = new();
static MRCFG cfg = new();
Harmony harmony;
public static MRCFG CFG { get => cfg; set => cfg = value; }
public override void Load() {
Log = base.Log;
Log.LogInfo("Loading");
enable = Config.Bind("", "Enable", true, "Enable MIDNIGHT_RUNNER");
dofOpts.Configure(Config, "A.DepthOfField");
vhsOpts.Configure(Config, "A.VHS");
engineOpts.Configure(Config, "B.Engine");
gimmiOpts.Configure(Config, "Z.GPUInstancerMapMagicIntegration");
gitsOpts.Configure(Config, "Z.GPUInstancerMapMagicIntegration.TerrainSettings");
if (!enable.Value) {
Log.LogInfo("Disabling");
return;
}
cfg.Consume(dofOpts, vhsOpts, gimmiOpts, gitsOpts, engineOpts);
Log.LogInfo("Configured");
harmony = new("neonflames.midnight_runner");
if (cfg.vhsEnable) harmony.PatchAll(typeof(VHS_Patch));
if (cfg.dofEnable) harmony.PatchAll(typeof(DOF_Patch));
AddComponent<MRLive>();
Log.LogInfo("Loaded");
}
}