From 0729f11226d77b88765f2778a2ec69e474d4b04f Mon Sep 17 00:00:00 2001 From: Sound Date: Thu, 19 Jan 2023 11:14:22 -0600 Subject: [PATCH 1/2] Create ExecutableListEvent --- .../Event/Gameplay/ExecutableListEvent.cs | 47 +++++++++++++++++++ PathfinderAPI/Executable/ExecutableManager.cs | 47 ++++--------------- 2 files changed, 55 insertions(+), 39 deletions(-) create mode 100644 PathfinderAPI/Event/Gameplay/ExecutableListEvent.cs diff --git a/PathfinderAPI/Event/Gameplay/ExecutableListEvent.cs b/PathfinderAPI/Event/Gameplay/ExecutableListEvent.cs new file mode 100644 index 00000000..8cf869f5 --- /dev/null +++ b/PathfinderAPI/Event/Gameplay/ExecutableListEvent.cs @@ -0,0 +1,47 @@ +using Hacknet; +using HarmonyLib; + +namespace Pathfinder.Event.Gameplay; + +[HarmonyPatch] +public class ExecutableListEvent : PathfinderEvent +{ + public OS OS { get; } + + public List EmbeddedExes { get; } = new List + { + "PortHack", "ForkBomb", "Shell", "Tutorial" + }; + public Dictionary BinExes { get; } + + public ExecutableListEvent(OS os, Dictionary binExes) + { + OS = os; + BinExes = binExes; + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(Programs), nameof(Programs.execute))] + private static bool ProgramsExecuteReplacement(string[] args, OS os){ + var binFiles = (os.connectedComp ?? os.thisComputer).files.root.searchForFolder("bin").files; // folders[2].files; + + var binExes = new Dictionary(); + foreach (FileEntry exeFile in binFiles) + binExes[exeFile] = + PortExploits.crackExeData .Any(x => x.Value == exeFile.data) || + PortExploits.crackExeDataLocalRNG.Any(x => x.Value == exeFile.data); + + var programsExecute = new ExecutableListEvent(os, binExes); + EventManager.InvokeAll(programsExecute); + + os.write("Available Executables:\n"); + + foreach (string embedded in programsExecute.EmbeddedExes) + os.write(embedded); + foreach (FileEntry file in binFiles.Where(x => binExes[x])) + os.write(file.name.Replace(".exe", "")); + + os.write(" "); + return false; + } +} \ No newline at end of file diff --git a/PathfinderAPI/Executable/ExecutableManager.cs b/PathfinderAPI/Executable/ExecutableManager.cs index a280061a..c030e769 100644 --- a/PathfinderAPI/Executable/ExecutableManager.cs +++ b/PathfinderAPI/Executable/ExecutableManager.cs @@ -28,6 +28,7 @@ static ExecutableManager() { EventManager.AddHandler(GetTextReplacementExe); EventManager.AddHandler(OnExeExecute); + EventManager.AddHandler(OnExecutableList); EventManager.onPluginUnload += OnPluginUnload; } @@ -139,46 +140,14 @@ private static bool AddExePrefix(OS __instance, ExeModule exe) } return true; } - - [HarmonyILManipulator] - [HarmonyPatch(typeof(Programs), nameof(Programs.execute))] - private static void programsExecuteFix(ILContext il){ - var c = new ILCursor(il); - - ILLabel branchCondition = null; - - c.GotoNext(MoveType.After, - // for (int i = 0; i < folder.files.Count; i++) - // int i = 0; - x => x.MatchNop(), - x => x.MatchLdcI4(0), - x => x.MatchStloc(2), - x => x.MatchBr(out branchCondition), - // for (int j = 0; j < PortExploits.exeNums.Count; j++) - // int j = 0; - x => x.MatchNop() - ); - - c.Emit(OpCodes.Ldloc_1); - c.Emit(OpCodes.Ldloc_2); - c.Emit(OpCodes.Ldarg_1); - c.EmitDelegate>((folder, i, os) => { - if(CustomExes.Any(x => x.ExeData == folder.files[i].data)){ - os.write(folder.files[i].name.Replace(".exe", "")); - return true; - } - return false; - }); - - int i = c.Index; - - c.GotoLabel(branchCondition, MoveType.Before); - c.Index -= 5; - ILLabel branchIncrement = c.MarkLabel(); - - c.Index = i; - c.Emit(OpCodes.Brtrue, branchIncrement); + private static void OnExecutableList(ExecutableListEvent e) + { + foreach (FileEntry exeFile in e.BinExes.Keys.ToList()) + { + if(CustomExes.Any(x => x.ExeData == exeFile.data)) + e.BinExes[exeFile] = true; + } } [HarmonyILManipulator] From 9c4ead8501729109688a5b8a0c1cc6fc5df0a7dc Mon Sep 17 00:00:00 2001 From: Sound Date: Thu, 19 Jan 2023 11:19:31 -0600 Subject: [PATCH 2/2] Correct behavior to only use `OS.thisComputer`. --- PathfinderAPI/Event/Gameplay/ExecutableListEvent.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PathfinderAPI/Event/Gameplay/ExecutableListEvent.cs b/PathfinderAPI/Event/Gameplay/ExecutableListEvent.cs index 8cf869f5..21f7ff60 100644 --- a/PathfinderAPI/Event/Gameplay/ExecutableListEvent.cs +++ b/PathfinderAPI/Event/Gameplay/ExecutableListEvent.cs @@ -23,7 +23,7 @@ public ExecutableListEvent(OS os, Dictionary binExes) [HarmonyPrefix] [HarmonyPatch(typeof(Programs), nameof(Programs.execute))] private static bool ProgramsExecuteReplacement(string[] args, OS os){ - var binFiles = (os.connectedComp ?? os.thisComputer).files.root.searchForFolder("bin").files; // folders[2].files; + var binFiles = os.thisComputer.files.root.searchForFolder("bin").files; // folders[2].files; var binExes = new Dictionary(); foreach (FileEntry exeFile in binFiles)