-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.cs
159 lines (140 loc) · 5.39 KB
/
Main.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
using System;
using System.Diagnostics;
using System.IO;
using MelonLoader;
namespace ML_OpenVR_FSR
{
public static class BuildInfo
{
public const string Name = "ML_OpenVR_FSR";
public const string Description = "Loads the OpenVR FSR Mod at Runtime.";
public const string Author = "Herp Derpinstine";
public const string Company = "Lava Gang";
public const string Version = "1.0.4";
public const string DownloadLink = "https://github.com/HerpDerpinstine/ML_OpenVR_FSR";
}
public class ML_OpenVR_FSR : MelonPlugin
{
private static string fsrFilePath = null;
private static NativeLibrary openVRLib = null;
private static NativeLibrary fsrLib = null;
private static string[] ExportTbl =
{
"VRHeadsetView",
"VR_GetGenericInterface",
"VR_GetInitToken",
"VR_GetRuntimePath",
"VR_GetStringForHmdError",
"VR_GetVRInitErrorAsEnglishDescription",
"VR_GetVRInitErrorAsSymbol",
"VR_InitInternal",
"VR_InitInternal2",
"VR_IsHmdPresent",
"VR_IsInterfaceVersionValid",
"VR_IsRuntimeInstalled",
"VR_RuntimePath",
"VR_ShutdownInternal"
};
unsafe public override void OnApplicationEarlyStart()
{
ExtractResources();
MelonLogger.Msg("Searching for OpenVR API...");
string plugins_path = Path.Combine(Path.Combine(MelonUtils.GameDirectory,
$"{Process.GetCurrentProcess().ProcessName}_Data"),
"Plugins");
string orig_openvr = null;
foreach (string file_path in Directory.GetFiles(plugins_path, "*.*", SearchOption.AllDirectories))
{
if (string.IsNullOrEmpty(file_path))
continue;
string file_name = Path.GetFileNameWithoutExtension(file_path);
if (file_name.Equals("openvr_api")
|| file_name.Equals("openvr_api64"))
{
orig_openvr = file_path;
break;
}
}
if (orig_openvr == null)
{
MelonLogger.Error("Unable to Find OpenVR API!");
return;
}
MelonLogger.Msg("Loading OpenVR API...");
openVRLib = NativeLibrary.Load(orig_openvr);
if (openVRLib == null)
return;
MelonLogger.Msg("Loading FSR Mod...");
fsrLib = NativeLibrary.Load(fsrFilePath);
if (fsrLib == null)
return;
MelonLogger.Msg("Attaching Exports...");
foreach (string export_name in ExportTbl)
{
IntPtr export_ptr_orig;
try { export_ptr_orig = openVRLib.GetExport(export_name); }
catch { continue; }
if (export_ptr_orig == IntPtr.Zero)
continue;
IntPtr export_ptr_new;
try { export_ptr_new = fsrLib.GetExport(export_name); }
catch { continue; }
if (export_ptr_new == IntPtr.Zero)
continue;
IntPtr* export_ptr_orig_ptr = &export_ptr_orig;
MelonUtils.NativeHookAttach((IntPtr)export_ptr_orig_ptr, export_ptr_new);
}
MelonLogger.Msg("Initialized!");
}
private static void ExtractResources()
{
string fsr_folder = Path.Combine(MelonUtils.UserDataDirectory, BuildInfo.Name);
if (!Directory.Exists(fsr_folder))
{
Directory.CreateDirectory(fsr_folder);
MelonLogger.Msg("UserData Folder Created!");
}
string fsr_name = "openvr_mod";
try
{
string fsr_log = Path.Combine(fsr_folder, $"{fsr_name}.log");
if (File.Exists(fsr_log))
{
MelonLogger.Msg("Removing Existing Log File...");
File.Delete(fsr_log);
}
}
catch (Exception ex)
{
MelonDebug.Error(ex.ToString());
MelonDebug.Error($"Failed to Remove Existing Log File! Ignoring...");
}
string fsr_cfg = Path.Combine(fsr_folder, $"{fsr_name}.cfg");
if (!File.Exists(fsr_cfg))
{
MelonLogger.Msg("Extracting Default Config File...");
File.WriteAllBytes(fsr_cfg, Properties.Resources.openvr_mod_cfg);
}
fsrFilePath = Path.Combine(fsr_folder, $"{fsr_name}.dll");
try
{
if (File.Exists(fsrFilePath))
{
MelonLogger.Msg("Removing Existing FSR Mod DLL...");
File.Delete(fsrFilePath);
}
}
catch (Exception ex)
{
MelonDebug.Error(ex.ToString());
MelonDebug.Error($"Failed to Extract FSR Mod DLL! Ignoring...");
return;
}
MelonLogger.Msg("Extracting FSR Mod DLL...");
File.WriteAllBytes(fsrFilePath,
//MelonUtils.IsGame32Bit()
//? Properties.Resources.openvr_mod_x86_dll :
Properties.Resources.openvr_mod_dll);
}
}
}