Skip to content

Commit

Permalink
Merge pull request #40 from Fayti1703/patch-hook-actions
Browse files Browse the repository at this point in the history
Hook into RunnableConditionalActions:LoadIntoOS
  • Loading branch information
Spartan322 authored Apr 15, 2020
2 parents abcd261 + e90477c commit e4b45f0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 38 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
50 changes: 12 additions & 38 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 Expand Up @@ -748,44 +760,6 @@ public static bool onSCOnConnectCheck(SCOnConnect self, out bool retVal, ref Com

return true;
}
/* TODO : Fix this to use the new XML system
[Patch("Hacknet.RunnableConditionalActions.Deserialize", flags: InjectFlags.PassParametersRef | InjectFlags.ModifyReturn)]
public static bool onDeserializeRunnableConditionalActions(out RunnableConditionalActions result, ref XmlReader reader)
{
var runnable = new RunnableConditionalActions();
var processor = new SaxProcessor();
processor.AddActionForTag("ConditionalActions", info =>
{
foreach (var serialInfo in info.Elements)
{
var actionSet = new SerializableConditionalActionSet
{
Condition = Serializable.Handler.LoadCondition(serialInfo)
};
foreach (var actionInfo in serialInfo.Elements)
actionSet.Actions.Add(Serializable.Handler.LoadAction(actionInfo));
runnable.Actions.Add(actionSet);
}
});
processor.Process(reader.ToStream(reader.BaseURI));
result = runnable;
return true;
}*/

/*public static void onAddSerializableConditions(ref Dictionary<string, Func<XmlReader, SerializableCondition>> dict)
{
// HACKNET BUG FIX : DoesNotHaveFlags not in dictionary
dict.Add("DoesNotHaveFlags", info => new SCDoesNotHaveFlags { Flags = info.Attributes.GetValue("Flags") });
foreach (var pair in SC.Handler.Deserializers)
dict.Add(pair.Key, pair.Value);
}
public static void onAddSerializableActions(ref Dictionary<string, Func<XmlReader, SerializableAction>> dict)
{
foreach (var pair in SA.Handler.Deserializers)
dict.Add(pair.Key, pair.Value);
}*/

[Patch("Hacknet.ComputerLoader.filter", flags: InjectFlags.PassParametersRef | InjectFlags.ModifyReturn)]
public static bool onFilterString(out string result, ref string input)
Expand Down

0 comments on commit e4b45f0

Please sign in to comment.