0
0
mirror of https://github.com/sp-tarkov/modules.git synced 2025-02-13 09:50:43 -05:00
modules/project/Aki.Core/Patches/TransportPrefixPatch.cs
Terkoiz 337a0733ae Publicized assembly refactor (!58)
Depends on SPT-AKI/SPT-AssemblyTool#3

* Refactored Modules for better consistency and general readability, along with preparing the code for a publicized assembly
* Added `PublicDeclaredFlags` to `PatchConstants` to cover a set of commonly used flags to get methods post-publicizing
* Added a replacement to LINQ's `.Single()` - `.SingleCustom()` which has improved logging to help with debugging Module code. Replaced all `.Single()` usages where applicable
* Replaced most method info fetching with `AccessTools` for consistency and better readability, especially in places where methods were being retrieved by their name anyways

**NOTE:**
As a side effect of publicizing all properties, some property access code such as `Player.Position` will now show "ambiguous reference" errors during compile, due to there being multiple interfaces with the Property name being defined on the class. The way to get around this is to use a cast to an explicit interface
Example:
```cs
Singleton<GameWorld>.Instance.MainPlayer.Position
```
will now need to be
```cs
((IPlayer)Singleton<GameWorld>.Instance.MainPlayer).Position
```

Co-authored-by: Terkoiz <terkoiz@spt.dev>
Reviewed-on: SPT-AKI/Modules#58
Co-authored-by: Terkoiz <terkoiz@noreply.dev.sp-tarkov.com>
Co-committed-by: Terkoiz <terkoiz@noreply.dev.sp-tarkov.com>
2024-01-13 22:08:29 +00:00

80 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using Aki.Reflection.Patching;
using Aki.Reflection.Utils;
using HarmonyLib;
namespace Aki.Core.Patches
{
public class TransportPrefixPatch : ModulePatch
{
public TransportPrefixPatch()
{
try
{
var type = PatchConstants.EftTypes.SingleOrDefault(t => t.GetField("TransportPrefixes") != null);
if (type == null)
{
throw new Exception($"{nameof(TransportPrefixPatch)} failed: Could not find type to patch.");
}
var value = Traverse.Create(type).Field("TransportPrefixes").GetValue<Dictionary<ETransportProtocolType, string>>();
value[ETransportProtocolType.HTTPS] = "http://";
value[ETransportProtocolType.WSS] = "ws://";
}
catch (Exception ex)
{
Logger.LogError($"{nameof(TransportPrefixPatch)}: {ex}");
throw;
}
}
protected override MethodBase GetTargetMethod()
{
return PatchConstants.EftTypes.SingleCustom(t => t.GetMethods().Any(m => m.Name == "CreateFromLegacyParams"))
.GetMethod("CreateFromLegacyParams", BindingFlags.Static | BindingFlags.Public);
}
[PatchPrefix]
private static bool PatchPrefix(ref LegacyParamsStruct legacyParams)
{
legacyParams.Url = legacyParams.Url
.Replace("https://", "")
.Replace("http://", "");
return true; // do original method after
}
[PatchTranspiler]
private static IEnumerable<CodeInstruction> PatchTranspile(ILGenerator generator, IEnumerable<CodeInstruction> instructions)
{
var codes = new List<CodeInstruction>(instructions);
var searchCode = new CodeInstruction(OpCodes.Ldstr, "https://");
var searchIndex = -1;
for (var i = 0; i < codes.Count; i++)
{
if (codes[i].opcode == searchCode.opcode && codes[i].operand == searchCode.operand)
{
searchIndex = i;
break;
}
}
if (searchIndex == -1)
{
Logger.LogError($"{nameof(TransportPrefixPatch)} failed: Could not find reference code.");
return instructions;
}
codes[searchIndex] = new CodeInstruction(OpCodes.Ldstr, "http://");
return codes.AsEnumerable();
}
}
}