Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Create ExecutableListEvent #209

Merged
merged 2 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions PathfinderAPI/Event/Gameplay/ExecutableListEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Hacknet;
using HarmonyLib;

namespace Pathfinder.Event.Gameplay;

[HarmonyPatch]
public class ExecutableListEvent : PathfinderEvent
{
public OS OS { get; }

public List<string> EmbeddedExes { get; } = new List<string>
{
"PortHack", "ForkBomb", "Shell", "Tutorial"
};
public Dictionary<FileEntry, bool> BinExes { get; }

public ExecutableListEvent(OS os, Dictionary<FileEntry, bool> binExes)
{
OS = os;
BinExes = binExes;
}

[HarmonyPrefix]
[HarmonyPatch(typeof(Programs), nameof(Programs.execute))]
private static bool ProgramsExecuteReplacement(string[] args, OS os){
var binFiles = os.thisComputer.files.root.searchForFolder("bin").files; // folders[2].files;

var binExes = new Dictionary<FileEntry, bool>();
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<ExecutableListEvent>.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;
}
}
47 changes: 8 additions & 39 deletions PathfinderAPI/Executable/ExecutableManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static ExecutableManager()
{
EventManager<TextReplaceEvent>.AddHandler(GetTextReplacementExe);
EventManager<ExecutableExecuteEvent>.AddHandler(OnExeExecute);
EventManager<ExecutableListEvent>.AddHandler(OnExecutableList);
EventManager.onPluginUnload += OnPluginUnload;
}

Expand Down Expand Up @@ -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<Func<Folder, int, OS, bool>>((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]
Expand Down