forked from danielchim/SIT.Tarkov.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatchConstants.cs
More file actions
445 lines (377 loc) · 17.9 KB
/
PatchConstants.cs
File metadata and controls
445 lines (377 loc) · 17.9 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
using BepInEx.Logging;
using Comfort.Common;
using EFT;
using FilesChecker;
using HarmonyLib;
using Newtonsoft.Json;
using SIT.Core.Misc;
using SIT.Tarkov.Core.AI;
using SIT.Tarkov.Core.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using static SIT.Core.Misc.PaulovJsonConverters;
namespace SIT.Tarkov.Core
{
public static class PatchConstants
{
public static BindingFlags PrivateFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;
private static Type[] _eftTypes;
public static Type[] EftTypes
{
get
{
if (_eftTypes == null)
{
_eftTypes = typeof(AbstractGame).Assembly.GetTypes().OrderBy(t => t.Name).ToArray();
}
return _eftTypes;
}
}
public static Type[] FilesCheckerTypes { get; private set; }
public static Type LocalGameType { get; private set; }
public static Type ExfilPointManagerType { get; private set; }
public static Type BackendInterfaceType { get; private set; }
public static Type SessionInterfaceType { get; private set; }
public static Type StartWithTokenType { get; private set; }
public static Type PoolManagerType { get; set; }
public static Type JobPriorityType { get; set; }
public static Type PlayerInfoType { get; set; }
public static Type PlayerCustomizationType { get; set; }
public static Type SpawnPointSystemInterfaceType { get; set; }
public static Type SpawnPointArrayInterfaceType { get; set; }
public static Type SpawnPointSystemClassType { get; set; }
public static Type BackendStaticConfigurationType { get; set; }
public static object BackendStaticConfigurationConfigInstance { get; set; }
public static class CharacterControllerSettings
{
public static object CharacterControllerInstance { get; set; }
public static CharacterControllerSpawner.Mode ObservedPlayerMode { get; set; }
public static CharacterControllerSpawner.Mode ClientPlayerMode { get; set; }
public static CharacterControllerSpawner.Mode BotPlayerMode { get; set; }
}
/// <summary>
/// A Key/Value dictionary of storing & obtaining an array of types by name
/// </summary>
public static readonly Dictionary<string, Type[]> TypesDictionary = new();
/// <summary>
/// A Key/Value dictionary of storing & obtaining a type by name
/// </summary>
public static Dictionary<string, Type> TypeDictionary { get; } = new();
/// <summary>
/// A Key/Value dictionary of storing & obtaining a method by type and name
/// </summary>
public static readonly Dictionary<(Type, string), MethodInfo> MethodDictionary = new();
private static string backendUrl;
/// <summary>
/// Method that returns the Backend Url (Example: https://127.0.0.1)
/// </summary>
private static string RealWSURL; //did i do this right?
//It appears to be successful :D
public static string GetBackendUrl()
{
if (string.IsNullOrEmpty(backendUrl))
{
backendUrl = BackendConnection.GetBackendConnection().BackendUrl;
}
return backendUrl;
}
public static string GetREALWSURL() //cut the server address obtained from GetBackendUrl and convert it to "ws://" w
{
if (string.IsNullOrEmpty(RealWSURL))
{
RealWSURL = BackendConnection.GetBackendConnection().BackendUrl;
int colonIndex = RealWSURL.LastIndexOf(':');
if (colonIndex != -1)
{
RealWSURL = RealWSURL.Substring(0, colonIndex);
}
RealWSURL = RealWSURL.Replace("http", "ws");
}
return RealWSURL;
}
public static string GetPHPSESSID()
{
if (BackendConnection.GetBackendConnection() == null)
Logger.LogError("Cannot get Backend Info");
return BackendConnection.GetBackendConnection().PHPSESSID;
}
public static ManualLogSource Logger { get; private set; }
public static Type GroupingType { get; }
public static Type JsonConverterType { get; }
public static JsonConverter[] JsonConverterDefault { get; }
private static ISession _backEndSession;
public static ISession BackEndSession
{
get
{
if (_backEndSession == null && Singleton<TarkovApplication>.Instantiated)
{
_backEndSession = Singleton<TarkovApplication>.Instance.GetClientBackEndSession();
}
if (_backEndSession == null && Singleton<ClientApplication<ISession>>.Instantiated)
{
_backEndSession = Singleton<ClientApplication<ISession>>.Instance.GetClientBackEndSession();
}
return _backEndSession;
}
}
public static JsonConverter[] GetJsonConvertersBSG()
{
return JsonConverterDefault;
}
public static List<JsonConverter> GetJsonConvertersPaulov()
{
var converters = new List<JsonConverter>();
converters.Add(new DateTimeOffsetJsonConverter());
converters.Add(new SimpleCharacterControllerJsonConverter());
converters.Add(new CollisionFlagsJsonConverter());
converters.Add(new PlayerJsonConverter());
return converters;
}
public static JsonSerializerSettings GetJsonSerializerSettings()
{
var converters = JsonConverterDefault;
converters.AddItem(new DateTimeOffsetJsonConverter());
converters.AddItem(new SimpleCharacterControllerJsonConverter());
converters.AddItem(new CollisionFlagsJsonConverter());
converters.AddItem(new NotesJsonConverter());
var paulovconverters = GetJsonConvertersPaulov();
converters.AddRangeToArray(paulovconverters.ToArray());
return new JsonSerializerSettings()
{
Converters = converters,
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore,
ObjectCreationHandling = ObjectCreationHandling.Replace,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
FloatFormatHandling = FloatFormatHandling.DefaultValue,
FloatParseHandling = FloatParseHandling.Double,
Error = (serializer, err) =>
{
Logger.LogError(err.ErrorContext.Error.ToString());
}
};
}
public static JsonSerializerSettings GetJsonSerializerSettingsWithoutBSG()
{
var converters = GetJsonConvertersPaulov();
return new JsonSerializerSettings()
{
Converters = converters,
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore,
ObjectCreationHandling = ObjectCreationHandling.Replace,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Error = (serializer, err) =>
{
Logger.LogError(err.ErrorContext.Error.ToString());
}
};
}
public static string SITToJson(this object o)
{
return JsonConvert.SerializeObject(o
, GetJsonSerializerSettings()
);
}
public static async Task<string> SITToJsonAsync(this object o)
{
return await Task.Run(() =>
{
return SITToJson(o);
});
}
public static T SITParseJson<T>(this string str)
{
return (T)JsonConvert.DeserializeObject<T>(str
, new JsonSerializerSettings()
{
Converters = JsonConverterDefault
,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}
);
}
public static bool TrySITParseJson<T>(this string str, out T result)
{
try
{
result = (T)JsonConvert.DeserializeObject<T>(str
, new JsonSerializerSettings()
{
Converters = JsonConverterDefault
,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}
);
return true;
}
catch
{
result = default(T);
return false;
}
}
public static object GetPlayerProfile(object __instance)
{
var instanceProfile = __instance.GetType().GetProperty("Profile"
, BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy).GetValue(__instance);
if (instanceProfile == null)
{
Logger.LogInfo("ReplaceInPlayer:PatchPostfix: Couldn't find Profile");
return null;
}
return instanceProfile;
}
public static string GetPlayerProfileAccountId(object instanceProfile)
{
var instanceAccountProp = instanceProfile.GetType().GetField("AccountId"
, BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
if (instanceAccountProp == null)
{
Logger.LogInfo($"ReplaceInPlayer:PatchPostfix: instanceAccountProp not found");
return null;
}
var instanceAccountId = instanceAccountProp.GetValue(instanceProfile).ToString();
return instanceAccountId;
}
public static IDisposable StartWithToken(string name)
{
return ReflectionHelpers.GetAllMethodsForType(StartWithTokenType).Single(x => x.Name == "StartWithToken").Invoke(null, new object[] { name }) as IDisposable;
}
public static async Task InvokeAsyncStaticByReflection(MethodInfo methodInfo, object rModel, params object[] p)
{
if (rModel == null)
{
await (Task)methodInfo
.MakeGenericMethod(new[] { rModel.GetType() })
.Invoke(null, p);
}
else
{
await (Task)methodInfo
.Invoke(null, p);
}
}
public static ClientApplication<ISession> GetClientApp()
{
return Singleton<ClientApplication<ISession>>.Instance;
}
public static TarkovApplication GetMainApp()
{
return GetClientApp() as TarkovApplication;
}
/// <summary>
/// Invoke an async Task<object> method
/// </summary>
/// <param name="type"></param>
/// <param name="outputType"></param>
/// <param name="method"></param>
/// <param name="param"></param>
/// <returns></returns>
//public static async Task<object> InvokeAsyncMethod(Type type, Type outputType, string method, object[] param)
//{
// var m = ReflectionHelpers.GetAllMethodsForType(type).First(x => x.Name == method);// foo.GetType().GetMethod(nameof(IFoo.Get));
// Logger.LogInfo("InvokeAsyncMethod." + m.Name);
// //var builder = AsyncTaskMethodBuilder.Create();
// var generic = m.MakeGenericMethod(outputType);
// var task = (Task)generic.Invoke(type, param);
// await task.ConfigureAwait(false);
// var resultProperty = task.GetType().GetProperty("Result");
// return resultProperty.GetValue(task);
//}
static PatchConstants()
{
if (Logger == null)
Logger = BepInEx.Logging.Logger.CreateLogSource("SIT.Tarkov.Core.PatchConstants");
TypesDictionary.Add("EftTypes", EftTypes);
FilesCheckerTypes = typeof(ICheckResult).Assembly.GetTypes();
LocalGameType = EftTypes.Single(x => x.Name == "LocalGame");
ExfilPointManagerType = EftTypes.Single(x => x.GetMethod("InitAllExfiltrationPoints") != null);
BackendInterfaceType = EftTypes.Single(x => x.GetMethods().Select(y => y.Name).Contains("CreateClientSession") && x.IsInterface);
SessionInterfaceType = EftTypes.Single(x => x.GetMethods().Select(y => y.Name).Contains("GetPhpSessionId") && x.IsInterface);
DisplayMessageNotifications.MessageNotificationType = EftTypes.Single(x => x.GetMethods(BindingFlags.Static | BindingFlags.Public).Select(y => y.Name).Contains("DisplayMessageNotification"));
if (DisplayMessageNotifications.MessageNotificationType == null)
{
Logger.LogInfo("SIT.Tarkov.Core:PatchConstants():MessageNotificationType:Not Found");
}
GroupingType = EftTypes.Single(x => x.GetMethods(BindingFlags.Public | BindingFlags.Static).Select(y => y.Name).Contains("CreateRaidPlayer"));
//if (GroupingType != null)
//{
// Logger.LogInfo("SIT.Tarkov.Core:PatchConstants():Found GroupingType:" + GroupingType.FullName);
//}
JsonConverterType = typeof(AbstractGame).Assembly.GetTypes()
.First(t => t.GetField("Converters", BindingFlags.Static | BindingFlags.Public) != null);
JsonConverterDefault = JsonConverterType.GetField("Converters", BindingFlags.Static | BindingFlags.Public).GetValue(null) as JsonConverter[];
//Logger.LogInfo($"PatchConstants: {JsonConverterDefault.Length} JsonConverters found");
StartWithTokenType = EftTypes.Single(x => ReflectionHelpers.GetAllMethodsForType(x).Count(y => y.Name == "StartWithToken") == 1);
BotSystemHelpers.Setup();
if (JobPriorityType == null)
{
JobPriorityType = EftTypes.Single(x =>
ReflectionHelpers.GetAllMethodsForType(x).Any(x => x.Name == "Priority")
);
//Logger.LogInfo($"Loading JobPriorityType:{JobPriorityType.FullName}");
}
if (PlayerInfoType == null)
{
PlayerInfoType = EftTypes.Single(x =>
ReflectionHelpers.GetAllMethodsForType(x).Any(x => x.Name == "AddBan")
&& ReflectionHelpers.GetAllMethodsForType(x).Any(x => x.Name == "RemoveBan")
);
//Logger.LogInfo($"Loading PlayerInfoType:{PlayerInfoType.FullName}");
}
if (PlayerCustomizationType == null)
{
PlayerCustomizationType = ReflectionHelpers.GetFieldFromType(typeof(Profile), "Customization").FieldType;
//Logger.LogInfo($"Loading PlayerCustomizationType:{PlayerCustomizationType.FullName}");
}
SpawnPointArrayInterfaceType = EftTypes.Single(x =>
ReflectionHelpers.GetAllMethodsForType(x).Any(x => x.Name == "CreateSpawnPoint")
&& ReflectionHelpers.GetAllMethodsForType(x).Any(x => x.Name == "DestroySpawnPoint")
&& x.IsInterface
);
//Logger.LogInfo($"Loading SpawnPointArrayInterfaceType:{SpawnPointArrayInterfaceType.FullName}");
BackendStaticConfigurationType = EftTypes.Single(x =>
ReflectionHelpers.GetAllMethodsForType(x).Any(x => x.Name == "LoadApplicationConfig")
//&& ReflectionHelpers.GetFieldFromType(x, "BackendUrl") != null
//&& ReflectionHelpers.GetFieldFromType(x, "Config") != null
);
//Logger.LogInfo($"Loading BackendStaticConfigurationType:{BackendStaticConfigurationType.FullName}");
//if (!TypeDictionary.ContainsKey("StatisticsSession"))
//{
// TypeDictionary.Add("StatisticsSession", EftTypes.OrderBy(x => x.Name).First(x =>
// x.IsClass
// && ReflectionHelpers.GetAllMethodsForType(x).Any(x => x.Name == "BeginStatisticsSession")
// && ReflectionHelpers.GetAllMethodsForType(x).Any(x => x.Name == "EndStatisticsSession")
// ));
// //Logger.LogInfo($"StatisticsSession:{TypeDictionary["StatisticsSession"].FullName}");
//}
//if (!TypeDictionary.ContainsKey("FilterCustomization"))
//{
// // Gather FilterCustomization
// TypeDictionary.Add("FilterCustomization", EftTypes.OrderBy(x => x.Name).Last(x =>
// x.IsClass
// && ReflectionHelpers.GetAllMethodsForType(x).Any(x => x.Name == "FilterCustomization")
// ));
// Logger.LogInfo($"FilterCustomization:{TypeDictionary["FilterCustomization"].FullName}");
//}
// TypeDictionary.Add("Profile", EftTypes.First(x =>
// x.IsClass && x.FullName == "EFT.Profile"
//));
//TypeDictionary.Add("Profile.Customization", EftTypes.First(x =>
// x.IsClass
// && x.BaseType == typeof(Dictionary<EBodyModelPart, string>)
//));
//TypeDictionary.Add("Profile.Inventory", EftTypes.First(x =>
// x.IsClass
// && ReflectionHelpers.GetAllMethodsForType(x).Any(x => x.Name == "UpdateTotalWeight")
// && ReflectionHelpers.GetAllMethodsForType(x).Any(x => x.Name == "GetAllItemByTemplate")
// && ReflectionHelpers.GetAllMethodsForType(x).Any(x => x.Name == "GetItemsInSlots")
//));
}
}
}