-
Notifications
You must be signed in to change notification settings - Fork 0
/
GHPluginCore.cs
320 lines (273 loc) · 8.85 KB
/
GHPluginCore.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.SceneManagement;
using Component = UnityEngine.Component;
using System.Reflection;
using System.Collections.ObjectModel;
using System.CodeDom;
using static Miniscript.Intrinsic;
namespace GHPluginCoreLib
{
/// <summary>
/// The GHPluginCore class can be inherited from by your BepInEx plugin, however it is
/// not mandatory for your BepInEx plugin to inherit from this class.
/// The GHPluginCore class is responsible for implementing and exposing the functionality
/// that this library offers (e.g. the ability to create and implement custom
/// programs that can be used in GreyOS). It can be accessed through its "instance" field.
/// </summary>
public class GHPluginCore
{
private const string harmonyID = "gh.plugin.core";
private Harmony harmony = new Harmony(harmonyID);
internal GHPluginLogger logger = new GHPluginLogger("GH Plugin Core");
/// <summary>
/// An instance of the GHPluginCore class which can be used to access instances of
/// other classes like the GreyOSProgramManager.
/// </summary>
public static readonly GHPluginCore instance = new GHPluginCore();
/// <summary>
/// An instance of the GreyOSProgramManager, which allows you to register custom
/// programs that can be executed and used in GreyOS, the operating system which
/// a player's in-game computer runs on.
/// </summary>
public readonly GreyOSProgramManager greyOSProgramManager = new GreyOSProgramManager();
private GHPluginCore()
{
}
private void ApplyPatchWithPatchType(MethodInfo targetMethodInfo, MethodInfo patchMethodInfo, PatchTypes patchType)
{
this.logger.LogInfo("Patching " + targetMethodInfo.Name);
switch (patchType)
{
case PatchTypes.Prefix:
harmony.Patch(
original: targetMethodInfo,
prefix: new HarmonyMethod(patchMethodInfo)
);
break;
case PatchTypes.Postfix:
harmony.Patch(
original: targetMethodInfo,
postfix: new HarmonyMethod(patchMethodInfo)
);
break;
case PatchTypes.Transpiler:
harmony.Patch(
original: targetMethodInfo,
transpiler: new HarmonyMethod(patchMethodInfo)
);
break;
case PatchTypes.Finalizer:
harmony.Patch(
original: targetMethodInfo,
finalizer: new HarmonyMethod(patchMethodInfo)
);
break;
default:
break;
}
this.logger.LogInfo("Patched " + targetMethodInfo.Name);
}
private void ApplyPatch(MethodInfo targetMethodInfo, MethodInfo patchMethodInfo, PatchTypes patchType)
{
ReadOnlyCollection<Patch> targetMethodPatches = null;
if (Harmony.GetPatchInfo(targetMethodInfo) == null)
{
this.ApplyPatchWithPatchType(
targetMethodInfo,
patchMethodInfo,
patchType
);
return;
}
switch (patchType)
{
case PatchTypes.Prefix:
targetMethodPatches = Harmony.GetPatchInfo(
targetMethodInfo
).Prefixes;
break;
case PatchTypes.Postfix:
targetMethodPatches = Harmony.GetPatchInfo(
targetMethodInfo
).Postfixes;
break;
case PatchTypes.Transpiler:
targetMethodPatches = Harmony.GetPatchInfo(
targetMethodInfo
).Transpilers;
break;
case PatchTypes.Finalizer:
targetMethodPatches = Harmony.GetPatchInfo(
targetMethodInfo
).Finalizers;
break;
default:
break;
}
foreach (Patch targetMethodPatch in targetMethodPatches)
{
if (targetMethodPatch.owner == harmonyID)
{
return;
}
}
this.ApplyPatchWithPatchType(
targetMethodInfo,
patchMethodInfo,
patchType
);
}
private void ApplyCorePatches()
{
this.ApplyPatch(
AccessTools.Method(
typeof(Ventana),
nameof(Ventana.Awake)
),
AccessTools.Method(
typeof(VentanaPatcher),
nameof(VentanaPatcher.Awake_PrefixPatch),
new Type[]
{
typeof(Ventana)
}
),
PatchTypes.Prefix
);
this.ApplyPatch(
AccessTools.Method(
typeof(Terminal),
nameof(Terminal.InicializarComandos)
),
AccessTools.Method(
typeof(TerminalPatcher),
nameof(TerminalPatcher.InicializarComandos_PostfixPatch),
new Type[]
{
typeof(Terminal)
}
),
PatchTypes.Postfix
);
this.ApplyPatch(
AccessTools.Method(
typeof(PlayerServerMethods),
nameof(PlayerServerMethods.PrepareCommandServerRpc),
new Type[]
{
typeof(string),
typeof(byte[]),
typeof(string),
typeof(string),
typeof(int),
typeof(int),
typeof(int),
typeof(bool)
}
),
AccessTools.Method(
typeof(PlayerServerMethodsPatcher),
nameof(PlayerServerMethodsPatcher.PrepareCommandServerRpc_PrefixPatch),
new Type[]
{
typeof(string),
typeof(byte[]),
typeof(string),
typeof(string),
typeof(int),
typeof(int),
typeof(int),
typeof(bool),
typeof(PlayerServerMethods)
}
),
PatchTypes.Prefix
);
this.ApplyPatch(
AccessTools.Method(
typeof(PlayerServerMethods),
nameof(PlayerServerMethods.UserLogin),
new Type[]
{
typeof(ulong),
typeof(bool)
}
),
AccessTools.Method(
typeof(PlayerServerMethodsPatcher),
nameof(PlayerServerMethodsPatcher.UserLogin_PostfixPatch),
new Type[]
{
typeof(ulong),
typeof(bool),
typeof(PlayerServerMethods)
}
),
PatchTypes.Postfix
);
}
/// <summary>
/// This method must be called in the Awake method of your BepInEx plugin,
/// preferably at the start of it if possible. It is responsible for executing crucial
/// initialization procedures to ensure that the library works correctly.
/// </summary>
public void Init()
{
this.ApplyCorePatches();
this.logger.LogInfo("All methods patched successfully.");
}
/*
For future reference (Do not remove):
Programs are stored as files on the player's computer's FileSystem, all files
(including these program files) are represented as instances of the
FileSystem.Archivo class, which inherits from the FileSystem.Fichero class.
Note: PlayerClient.Singleton.pc can be used to retrieve the player's computer
(represented as an instance of the PlayerComputer class).
Key classes/fields/methods of interest found during previous analysis:
-> PlayerServerMethods.PrepareCommandServerRpc
-> Computer.AddProcess
-> FileSystem.AddFileRef (Used for adding files to a Computer's FileSystem)
-> PlayerClientMethods.SendCommandClientRpc
-> PlayerServerMethods.StartProgram
-> Database.RestorePlayerComputer
-> PlayerServerMethods.UserLogin
-> PlayerComputer.ConfigureStartupProcess
-> PlayerUtils.CrearArchivo
-> Database.RestoreComputer
-> Database.RestorePlayerComputer
-> FileSystem.Deserializar
-> Terminal.InicializarComandos
Note: In singleplayer mode, the actual filesystem of the player's computer is stored
in the "fileSystem" field of a PlayerComputer instance stored in a PlayerServerMethods
instance (which can be accessed through a PlayerServer instance).
Note: The PlayerServerMethods.UserLogin method is called once during the start of the
boot sequence of the player's computer.
Note: The Terminal.InicializarComandos method is called once during the boot sequence
of the player's computer as well as once every time the player opens a new Terminal.
The steps to adding custom programs to the GreyOS which can be run from the Terminal
in the player's computer are as follows:
-> Add the program file associated with the custom program to the player's computer's
filesystem (represented as instance of the FileSystem class) (this should be done
just after the "UserLogin" method of the PlayerServerMethods class is called, this
can be done using a postfix patch or through other means if there are any).
-> Add an instance of the custom program (for the regular programs that are already
a part of GreyOS, such as the File Explorer and Browser programs, they are represented
as instances of the ProgramWindow class, for custom programs, they are represented
as an instance of the GreyOSProgram class which inherits from the ProgramWindow class)
to the "programs" field of the Terminal instance (this should be done after the
"InicializarComandos" method of the Terminal class is called, this method adds
the programs that come with GreyOS by default to the "programs" field of the Terminal
instance).
*/
}
}