-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlugin.cs
191 lines (163 loc) · 7.15 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
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
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using System;
using System.IO;
using System.Reflection;
using TarkovVR.ModSupport;
using Unity.XR.OpenVR;
using UnityEngine;
using UnityEngine.XR.Management;
using Valve.VR;
namespace TarkovVR
{
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
public class Plugin : BaseUnityPlugin
{
public static ManualLogSource MyLog;
private bool vrInitializedSuccessfully = false;
private void Awake()
{
Logger.LogInfo($"Plugin {MyPluginInfo.PLUGIN_GUID} is loading!");
MyLog = Logger;
if (!InitializeVR())
{
Logger.LogError("VR initialization failed. Skipping the rest of the plugin setup.");
return;
}
Logger.LogInfo("VR initialized successfully.");
vrInitializedSuccessfully = true;
ApplyPatches("TarkovVR.Patches");
InitializeConditionalPatches();
}
private bool InitializeVR()
{
try
{
SteamVR_Actions.PreInitialize();
SteamVR_Settings.instance.pauseGameWhenDashboardVisible = true;
var generalSettings = ScriptableObject.CreateInstance<XRGeneralSettings>();
var managerSettings = ScriptableObject.CreateInstance<XRManagerSettings>();
var xrLoader = ScriptableObject.CreateInstance<OpenVRLoader>();
var settings = OpenVRSettings.GetSettings();
settings.StereoRenderingMode = OpenVRSettings.StereoRenderingModes.MultiPass;
generalSettings.Manager = managerSettings;
managerSettings.loaders.Clear();
managerSettings.loaders.Add(xrLoader);
managerSettings.InitializeLoaderSync();
XRGeneralSettings.AttemptInitializeXRSDKOnLoad();
XRGeneralSettings.AttemptStartXRSDKOnBeforeSplashScreen();
// Initialize SteamVR
SteamVR.Initialize();
// Verify SteamVR is running
if (!SteamVR.active)
{
Logger.LogError("[SteamVR] Initialization failed. SteamVR is not active.");
return false;
}
// Verify OpenVR initialization
if (SteamVR.instance == null || SteamVR.instance.hmd == null)
{
Logger.LogError("[OpenVR] HMD not found or OpenVR initialization failed.");
return false;
}
Logger.LogInfo("[VR] Initialization completed successfully.");
return true;
}
catch (Exception ex)
{
Logger.LogError($"[VR Initialization Error] {ex.Message}");
return false;
}
}
private void InitializeConditionalPatches()
{
if (!vrInitializedSuccessfully)
return; // Skip patching if VR failed to initialize
string modDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BepInEx\\plugins\\kmyuhkyuk-EFTApi\\EFTConfiguration.dll");
if (File.Exists(modDllPath))
{
// Load the assembly
Assembly modAssembly = Assembly.LoadFrom(modDllPath);
// Check for the required types and methods in the loaded assembly
Type configViewType = modAssembly.GetType("EFTConfiguration.Views.EFTConfigurationView");
if (configViewType != null)
{
// Apply conditional patches
InstalledMods.EFTApiInstalled = true;
ApplyPatches("TarkovVR.ModSupport.EFTApi");
MyLog.LogInfo("Dependent mod found and patches applied.");
}
else
{
MyLog.LogWarning("Required types/methods not found in the dependent mod.");
}
}
else
{
MyLog.LogWarning("EFT API dll not found, support patches will not be applied.");
}
modDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BepInEx\\plugins\\AmandsGraphics.dll");
if (File.Exists(modDllPath))
{
// Load the assembly
Assembly modAssembly = Assembly.LoadFrom(modDllPath);
// Check for the required types and methods in the loaded assembly
Type configViewType = modAssembly.GetType("AmandsGraphics.AmandsGraphicsClass");
if (configViewType != null)
{
// Apply conditional patches
InstalledMods.AmandsGraphicsInstalled = true;
ApplyPatches("TarkovVR.ModSupport.AmandsGraphics");
MyLog.LogInfo("AmandsGraphics found and patches applied.");
}
else
{
MyLog.LogWarning("Required types/methods not found in the dependent mod.");
}
}
else
{
MyLog.LogWarning("AmandsGraphics dll not found, support patches will not be applied.");
}
modDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BepInEx\\plugins\\Fika.Core.dll");
if (File.Exists(modDllPath))
{
// Load the assembly
Assembly modAssembly = Assembly.LoadFrom(modDllPath);
// Check for the required types and methods in the loaded assembly
Type configViewType = modAssembly.GetType("MatchMakerUI");
if (configViewType != null)
{
// Apply conditional patches
InstalledMods.FIKAInstalled = true;
ApplyPatches("TarkovVR.ModSupport.FIKA");
MyLog.LogInfo("Dependent mod found and patches applied.");
}
else
{
MyLog.LogWarning("Required types/methods not found in the dependent mod.");
}
}
else
{
MyLog.LogWarning("FIKA Core dll not found, support patches will not be applied.");
}
// Repeat for other mods (AmandsGraphics, FIKA) as needed
}
private void ApplyPatches(string @namespace)
{
if (!vrInitializedSuccessfully)
return; // Skip patching if VR failed to initialize
var harmony = new Harmony(MyPluginInfo.PLUGIN_GUID);
var assembly = Assembly.GetExecutingAssembly();
foreach (var type in assembly.GetTypes())
{
if (type.Namespace != null && type.Namespace.StartsWith(@namespace))
{
harmony.CreateClassProcessor(type).Patch();
}
}
}
}
}