Skip to content

Commit

Permalink
Merge pull request #240 from Arkhist/develop
Browse files Browse the repository at this point in the history
Prepare release 5.3.3
  • Loading branch information
Arkhist authored Jun 3, 2024
2 parents 30cc95e + b98062d commit a776916
Show file tree
Hide file tree
Showing 14 changed files with 407 additions and 41 deletions.
39 changes: 31 additions & 8 deletions BepInEx.Hacknet/HacknetChainloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,8 @@ internal static class ExtensionPluginPatches

private static bool FirstExtensionLoaded = false;

[HarmonyPrefix]
[HarmonyPatch(typeof(ExtensionsMenuScreen), nameof(ExtensionsMenuScreen.ActivateExtensionPage))]
internal static void LoadTempPluginsPrefix(ExtensionInfo info, ref bool __runOriginal)
internal static bool LoadTempPlugins(ExtensionInfo info)
{
if (!__runOriginal)
return;

if (!FirstExtensionLoaded)
{
HacknetChainloader.Instance.HarmonyInstance.PatchAll(typeof(ChainloaderFix));
Expand All @@ -136,17 +131,45 @@ internal static void LoadTempPluginsPrefix(ExtensionInfo info, ref bool __runOri
HacknetChainloader.Instance.Execute();
}

__runOriginal = true;
return true;
}
catch (Exception ex)
{
HN.Settings.IsInExtensionMode = false;

HacknetChainloader.Instance.Log.LogError($"A fatal exception occured while loading extension plugins, aborting:\n{ex}");

__runOriginal = false;
return false;
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ExtensionsMenuScreen), nameof(ExtensionsMenuScreen.ActivateExtensionPage))]
internal static void LoadTempPluginsOnExtensionPage(ExtensionInfo info, ref bool __runOriginal)
{
if (!__runOriginal)
return;

__runOriginal = LoadTempPlugins(info);
}
[HarmonyILManipulator]
[HarmonyPatch(typeof(HN.Game1), nameof(HN.Game1.LoadInitialScreens))]
internal static void LoadTempPluginsOnExtstart(ILContext il)
{
ILCursor c = new ILCursor(il);

c.GotoNext(MoveType.Before,
x => x.MatchLdsfld(AccessTools.Field(typeof(HN.Settings), nameof(HN.Settings.MenuStartup)))
);
ILLabel skipLabel = c.MarkLabel();

c.GotoPrev(MoveType.After,
x => x.MatchStsfld(AccessTools.Field(typeof(ExtensionLoader), nameof(ExtensionLoader.ActiveExtensionInfo)))
);
c.EmitDelegate<Func<bool>>(() =>
LoadTempPlugins(ExtensionLoader.ActiveExtensionInfo)
);
c.Emit(OpCodes.Brfalse, skipLabel);
}

[HarmonyPostfix]
[HarmonyPatch(typeof(HN.OS), nameof(HN.OS.quitGame))]
Expand Down
36 changes: 32 additions & 4 deletions BepInEx.Hacknet/HacknetPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using BepInEx.Logging;
using BepInEx.Logging;
using BepInEx.Configuration;
using HarmonyLib;
using Hacknet.Extensions;
using HN = global::Hacknet;

namespace BepInEx.Hacknet;

Expand All @@ -14,19 +16,45 @@ protected HacknetPlugin()

Log = Logger.CreateLogSource(metadata.Name);

Config = new ConfigFile(System.IO.Path.Combine(Paths.ConfigPath, metadata.GUID + ".cfg"), false, metadata);
InstalledGlobally = !HN.Settings.IsInExtensionMode;

Config = new ConfigFile(Path.Combine(Paths.ConfigPath, metadata.GUID + ".cfg"), false, metadata);

if (!InstalledGlobally)
UserConfig = new ConfigFile(
Path.Combine("BepInEx/config/", ExtensionLoader.ActiveExtensionInfo.GetFoldersafeName(), metadata.GUID + ".cfg"),
false,
metadata
);
}

public ManualLogSource Log { get; }

public bool InstalledGlobally { get; }

/// <summary>
/// If this plugin is installed in an extension, holds a <see cref="ConfigFile"/> to be edited by the extension developer.
/// <br/>
/// Otherwise, if its installed globally, holds a <see cref="ConfigFile"/> to be edited by the user.
/// <br/><br/>
/// For the extension player's <see cref="ConfigFile"/>, see <see cref="UserConfig"/>.
/// </summary>
public ConfigFile Config { get; }
/// <summary>
/// If this plugin is installed in an extension, holds a <see cref="ConfigFile"/> to be edited by the extension player.
/// <br/>
/// Otherwise, if its installed globally, holds the value <c>null</c>.
/// <br/><br/>
/// For the extension developer's <see cref="ConfigFile"/>, see <see cref="Config"/>.
/// </summary>
public ConfigFile UserConfig { get; }

public Harmony HarmonyInstance { get; set; }

public abstract bool Load();

/// <summary>
/// Runs after all plugins have executed thier Load method
/// Runs after all plugins have executed their Load method
/// </summary>
public virtual void PostLoad() {}

Expand All @@ -35,4 +63,4 @@ public virtual bool Unload()
HarmonyInstance.UnpatchSelf();
return true;
}
}
}
2 changes: 1 addition & 1 deletion Linux/StartPathfinder.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cd "$(dirname "$0")"

if [ "$(uname -m)" == "x86_64" ]; then
if [ "$(uname -m)" = "x86_64" ]; then
LD_PRELOAD="$(pwd)/intercept.so /usr/lib/libmono-2.0.so" MONO_DEBUG=explicit-null-checks ./HacknetPathfinder.bin.x86_64 "$@"
else
LD_PRELOAD="$(pwd)/intercept.so /usr/lib/libmono-2.0.so" MONO_DEBUG=explicit-null-checks ./HacknetPathfinder.bin.x86 "$@"
Expand Down
23 changes: 23 additions & 0 deletions PathfinderAPI/BaseGameFixes/FlickeringTextReportNull.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Reflection;
using Hacknet.Effects;
using HarmonyLib;
using Mono.Cecil.Cil;
using MonoMod.Cil;

namespace Pathfinder.BaseGameFixes;

[HarmonyPatch]
public static class FlickeringTextReportNull {
[HarmonyPatch(typeof(FlickeringTextEffect), nameof(FlickeringTextEffect.GetReportString))]
[HarmonyILManipulator]
private static void GetReportStringManipulator(ILContext context) {
ILCursor cursor = new(context);

ILLabel unskipLabel = context.DefineLabel();
cursor.Emit(OpCodes.Ldsfld, typeof(FlickeringTextEffect).GetField(nameof(FlickeringTextEffect.LinedItemTarget), BindingFlags.Static | BindingFlags.Public));
cursor.Emit(OpCodes.Brtrue, unskipLabel);
cursor.Emit(OpCodes.Ldstr, "FlickeringTextEffect was not used in this execution.");
cursor.Emit(OpCodes.Ret);
unskipLabel.Target = cursor.Next;
}
}
46 changes: 46 additions & 0 deletions PathfinderAPI/BaseGameFixes/KeepBranchesOnMailMissionCompletion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Hacknet;
using HarmonyLib;
using Mono.Cecil;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using MonoMod.Utils;

namespace Pathfinder.BaseGameFixes;

[HarmonyPatch]
public static class KeepBranchesOnMailMissionCompletion {
[HarmonyPatch(typeof(MailServer), nameof(MailServer.doRespondDisplay))]
[HarmonyILManipulator]
public static void DoRespondDisplayManipulator(ILContext context) {
ILCursor cursor = new(context);

cursor.GotoNext(MoveType.After,
x => x.MatchLdarg(0),
x => x.MatchLdarg(0),
x => x.MatchLdfld<Hacknet.Daemon>("os"),
x => x.MatchLdfld<OS>("branchMissions"),
x => x.MatchLdloc(4),
x => x.MatchCallvirt(out MethodReference method) &&
method.DeclaringType.Is(typeof(List<ActiveMission>)) &&
method.Name == "get_Item"
,
x => x.MatchCall<MailServer>("attemptCompleteMission"),
x => x.MatchStloc(11)
);
Instruction startOfRange = cursor.Next;

cursor.GotoNext(MoveType.After,
x => x.MatchLdarg(0),
x => x.MatchLdfld<Hacknet.Daemon>("os"),
x => x.MatchLdfld<OS>("branchMissions"),
x => x.MatchCallvirt(out MethodReference method) &&
method.DeclaringType.Is(typeof(List<ActiveMission>)) &&
method.Name == "Clear"
);
Instruction endOfRange = cursor.Prev;

cursor.Goto(startOfRange);
int count = cursor.Instrs.IndexOf(endOfRange) - cursor.Instrs.IndexOf(startOfRange) + 1;
cursor.RemoveRange(count);
}
}
36 changes: 36 additions & 0 deletions PathfinderAPI/BaseGameFixes/KillExeCheckIdentifierName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Hacknet;
using HarmonyLib;
using Mono.Cecil.Cil;
using MonoMod.Cil;

namespace Pathfinder.BaseGameFixes;

[HarmonyPatch]
public static class KillExeCheckIdentifierName
{
[HarmonyILManipulator]
[HarmonyPatch(typeof(SAKillExe), nameof(SAKillExe.Trigger))]
internal static void SAKillExeTriggerIL(ILContext il)
{
ILCursor c = new ILCursor(il);

// if (oS.exes[i].name.ToLower().Contains(ExeName.ToLower()))
c.GotoNext(MoveType.After,
// (...)
x => x.MatchLdfld(AccessTools.Field(typeof(SAKillExe), nameof(SAKillExe.ExeName))),
x => x.MatchCallvirt(AccessTools.Method(typeof(string), nameof(string.ToLower))),
x => x.MatchCallvirt(AccessTools.Method(typeof(string), nameof(string.Contains), new Type[]{ typeof(string) })),
x => x.MatchLdcI4(0),
x => x.MatchCeq()
);
c.Emit(OpCodes.Ldarg_0);
c.Emit(OpCodes.Ldloc_0);
c.Emit(OpCodes.Ldloc_1);
c.EmitDelegate<Func<SAKillExe, OS, int, bool>>((__instance, oS, i) =>
oS.exes[i].IdentifierName.ToLower().Contains(__instance.ExeName.ToLower())
);
c.Emit(OpCodes.Ldc_I4_0);
c.Emit(OpCodes.Ceq);
c.Emit(OpCodes.And);
}
}
129 changes: 117 additions & 12 deletions PathfinderAPI/BaseGameFixes/Performance/NodeLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,6 @@ internal static void OnChangeIPTrigger2(SAChangeIP __instance, string __state)
}
}

[HarmonyILManipulator]
[HarmonyPatch(typeof(ISPDaemon), nameof(ISPDaemon.DrawIPEntryScreen))]
internal static void OnISPChangeIP(ILContext il)
{
ILCursor c = new ILCursor(il);

c.GotoNext(MoveType.After, x => x.MatchStfld(AccessTools.Field(typeof(Computer), nameof(Computer.ip))));

c.Emit(OpCodes.Ldnull);
c.Emit(OpCodes.Call, AccessTools.Method(typeof(ComputerLookup), nameof(ComputerLookup.RebuildLookups)));
}

[HarmonyPostfix]
[HarmonyPatch(typeof(AircraftDaemon), nameof(AircraftDaemon.CrashAircraft))]
internal static void OnAircraftDaemonChangeIP()
Expand Down Expand Up @@ -137,6 +125,123 @@ private static void AddMissionGenComputer(object __result)
ComputerLookup.Add(comp);
}

[HarmonyILManipulator]
[HarmonyPatch(typeof(ISPDaemon), nameof(ISPDaemon.DrawIPEntryScreen))]
internal static void ModifyISPDaemonDrawIPEntryScreen(ILContext il)
{
ILCursor c = new(il);

ILLabel loopHead = null;
c.GotoNext(MoveType.Before,
// flag = false;
x => x.MatchLdcI4(0),
x => x.MatchStloc(2),
// for (int i = 0; i < os.netMap.nodes.Count; i++)
x => x.MatchLdcI4(0),
x => x.MatchStloc(3),
// (no C# code)
x => x.MatchBr(out loopHead)
);
int i = c.Index;

c.GotoLabel(loopHead);
c.GotoNext(MoveType.After,
// for (int i = 0; i < os.netMap.nodes.Count; i++)
// ...
// for (int i = 0; i < os.netMap.nodes.Count; i++)
// ...
x => x.MatchBrtrue(out _)
);
int j = c.Index;

c.Index = i;
c.RemoveRange(j-i);

c.Emit(OpCodes.Ldarg_0);
c.EmitDelegate<Func<ISPDaemon, bool>>(__instance => {
Computer c = ComputerLookup.FindByIp(__instance.scannedComputer.ip, false);

return (c != null) && (c.idName != __instance.scannedComputer.idName);
});
c.Emit(OpCodes.Stloc_2);

c.Emit(OpCodes.Ldnull);
c.Emit(OpCodes.Call, AccessTools.Method(typeof(ComputerLookup), nameof(ComputerLookup.RebuildLookups)));
}
[HarmonyILManipulator]
[HarmonyPatch(typeof(ISPDaemon), nameof(ISPDaemon.DrawLoadingScreen))]
internal static void ModifyISPDaemonDrawLoadingScreen(ILContext il)
{
ILCursor c = new(il);

ILLabel loopHead = null;
c.GotoNext(MoveType.Before,
// scannedComputer = null;
x => x.MatchNop(),
x => x.MatchLdarg(0),
x => x.MatchLdnull(),
x => x.MatchStfld(AccessTools.Field(typeof(ISPDaemon), nameof(ISPDaemon.scannedComputer))),
// for (int i = 0; i < os.netMap.nodes.Count; i++)
x => x.MatchLdcI4(0),
x => x.MatchStloc(1),
// (no C# code)
x => x.MatchBr(out loopHead)
);
int i = ++c.Index;

c.GotoLabel(loopHead);
c.GotoNext(MoveType.After,
// for (int i = 0; i < os.netMap.nodes.Count; i++)
// ...
// for (int i = 0; i < os.netMap.nodes.Count; i++)
// ...
x => x.MatchBrtrue(out ILLabel _)
);
int j = c.Index;

c.Index = i;
c.RemoveRange(j-i);

c.Emit(OpCodes.Ldarg_0);
c.EmitDelegate<Action<ISPDaemon>>(__instance =>
__instance.scannedComputer = ComputerLookup.FindByIp(__instance.ipSearch, false));
}

[HarmonyPrefix]
[HarmonyPatch(typeof(MissionFunctions), nameof(MissionFunctions.findComp))]
internal static bool ModifyMissionFunctionsFindComp(string target, ref Computer __result)
{
__result = ComputerLookup.FindById(target);
return false;
}

[HarmonyPrefix]
[HarmonyPatch(typeof(Multiplayer), nameof(Multiplayer.getComp))]
internal static bool ModifyMultiplayerGetComp(string ip, OS os, ref Computer __result)
{
__result = ComputerLookup.FindByIp(ip, false);
return false;
}

[HarmonyPrefix]
[HarmonyPatch(typeof(NetworkMap), nameof(NetworkMap.discoverNode), new Type[]{ typeof(string) })]
internal static bool ModifyNetworkMapDiscoverNodeString(NetworkMap __instance, string cName)
{
Computer c = ComputerLookup.FindById(cName);
if (c != null)
__instance.discoverNode(c);

return false;
}

[HarmonyPrefix]
[HarmonyPatch(typeof(Programs), nameof(Programs.computerExists))]
internal static bool ModifyProgramsComputerExists(OS os, string ip, ref bool __result)
{
__result = ComputerLookup.Find(ip, SearchType.Ip | SearchType.Name) != null;
return false;
}

[HarmonyPrefix]
[HarmonyPatch(typeof(Programs), nameof(Programs.scan))]
internal static bool ScanReplacement(string[] args, OS os)
Expand Down
Loading

0 comments on commit a776916

Please sign in to comment.