Skip to content

Commit

Permalink
Hook into vanilla RCS.LoadIntoOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Fayti1703 committed Apr 15, 2020
1 parent 83ad1db commit 8bd15a4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions Pathfinder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<Compile Include="Pathfinder\Event\EventManager.cs" />
<Compile Include="Pathfinder\Event\PathfinderEvent.cs" />
<Compile Include="Pathfinder\Exceptions\AttributeException.cs" />
<Compile Include="Pathfinder\Exceptions\EventException.cs" />
<Compile Include="Pathfinder\Exceptions\LoadException.cs" />
<Compile Include="Pathfinder\Internal\Replacements\ActionsLoader.cs" />
<Compile Include="Pathfinder\Pathfinder.cs" />
Expand Down
11 changes: 11 additions & 0 deletions Pathfinder/Event/BasicEvents.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
namespace Pathfinder.Event
{
public class ActionsLoadIntoOSEvent : PathfinderEvent
{
public Hacknet.OS OS { get; }
public string FilePath { get; }

public ActionsLoadIntoOSEvent(string filePath, Hacknet.OS os)
{
FilePath = filePath;
OS = os;
}
}
public class NetworkMapLoadContentEvent : PathfinderEvent
{
public Hacknet.NetworkMap NetMap { get; }
Expand Down
32 changes: 32 additions & 0 deletions Pathfinder/Exceptions/EventException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Pathfinder.Exceptions
{
public class EventException : Exception
{
public Dictionary<string, Exception> Exceptions { get; }

private static string GenMessage(string message, Dictionary<string, Exception> excepts)
{
var builder = new StringBuilder();
builder.Append(message);
if (excepts.Count > 0)
{
builder.Append(": ");
foreach (var entry in excepts)
builder.Append($"\n-----> Event Listener {entry.Key} threw: {entry.Value}");
}
else
builder.Append(".");

return builder.ToString();
}
public EventException(string message, Dictionary<string, Exception> excepts)
: base(GenMessage(message, excepts))
{
Exceptions = excepts;
}
}
}
7 changes: 7 additions & 0 deletions Pathfinder/Internal/HandlerListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public static void LoadContentComputerReplacementStart(LoadContentComputerStartE
e.IsCancelled = true;
}

public static void LoadActionsIntoOSListener(ActionsLoadIntoOSEvent e)
{
var actions = ActionsLoader.LoadConditionalActionsFromFile(e.FilePath);
e.OS.ConditionalActions.Actions.AddRange(actions.Actions);
e.IsCancelled = true;
}

public static void DaemonLoadListener(Computer c, EventExecutor exec)
{
exec.AddExecutor("Computer.ModdedDaemon", (executor, info) =>
Expand Down
2 changes: 2 additions & 0 deletions Pathfinder/Pathfinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ internal static void Initialize()

EventManager.RegisterListener<GameUnloadEvent>(Manager.UnloadMods);

EventManager.RegisterListener<ActionsLoadIntoOSEvent>(Internal.HandlerListener.LoadActionsIntoOSListener);

ActionsLoader.InitActionLoaders();
ActionsLoader.InitConditionLoaders();

Expand Down
12 changes: 12 additions & 0 deletions Pathfinder/PathfinderHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Pathfinder.Attribute;
using Pathfinder.Exceptions;
using Pathfinder.Game;
using Pathfinder.GameFilesystem;
using Pathfinder.GUI;
Expand Down Expand Up @@ -691,6 +692,17 @@ public static void onOptionsApply(OptionsMenu self)
optionsMenuApplyEvent.CallEvent();
}

[Patch("Hacknet.RunnableConditionalActions.LoadIntoOS",
flags: InjectFlags.PassParametersVal | InjectFlags.ModifyReturn)]
public static bool onLoadRunnableActionsIntoOS(string filepath, object OSobj)
{
var evt = new Event.ActionsLoadIntoOSEvent(filepath, (OS) OSobj);
var except = evt.CallEvent();
if(except.Count > 0)
throw new EventException("Failed to load conditional actions", except);
return evt.IsCancelled;
}


/* pure bug-fix patch */
[Patch("Hacknet.SCInstantly.Check", flags:
Expand Down

0 comments on commit 8bd15a4

Please sign in to comment.