Initial upload

This commit is contained in:
Matthew 2022-11-01 12:15:06 -04:00
parent feb28b10a4
commit 7585da099c
106 changed files with 4860 additions and 0 deletions

View file

@ -0,0 +1,21 @@
using RimWorld;
using Verse;
namespace rjwquirks.Modules.Shared.Events
{
/// <summary>
/// Copy of the HistoryEvent.
/// Made it it's own thing because it use cases are different
/// </summary>
public struct RjwEvent
{
public RjwEventDef def;
public SignalArgs args;
public RjwEvent(RjwEventDef def, params NamedArgument[] args)
{
this.def = def;
this.args = new SignalArgs(args);
}
}
}

View file

@ -0,0 +1,11 @@
namespace rjwquirks.Modules.Shared.Events
{
public static class RjwEventArgNames
{
public static readonly string Pawn = "Pawn";
public static readonly string SexProps = "SexProps";
public static readonly string Quirk = "Quirk";
public static readonly string Record = "Record";
public static readonly string Satisfaction = "Satisfaction";
}
}

View file

@ -0,0 +1,10 @@
using System.Collections.Generic;
using Verse;
namespace rjwquirks.Modules.Shared.Events
{
public class RjwEventDef : Def
{
public List<string> obligatoryArgs;
}
}

View file

@ -0,0 +1,14 @@
using RimWorld;
namespace rjwquirks.Modules.Shared.Events
{
[DefOf]
public static class RjwEventDefOf
{
public static readonly RjwEventDef QuirkAddedTo;
public static readonly RjwEventDef QuirkRemovedFrom;
public static readonly RjwEventDef Orgasm;
public static readonly RjwEventDef RecordChanged;
public static readonly RjwEventDef PawnSexualized;
}
}

View file

@ -0,0 +1,8 @@
namespace rjwquirks.Modules.Shared.Events
{
public abstract class RjwEventHandler
{
public RjwEventHandlerDef def;
public abstract void HandleEvent(RjwEvent ev);
}
}

View file

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using Verse;
namespace rjwquirks.Modules.Shared.Events
{
public class RjwEventHandlerDef : Def
{
public Type workerClass;
public List<RjwEventDef> handlesEvents;
private RjwEventHandler _worker;
public RjwEventHandler Worker
{
get
{
if (_worker == null)
{
_worker = (RjwEventHandler)Activator.CreateInstance(workerClass);
_worker.def = this;
}
return _worker;
}
}
public override IEnumerable<string> ConfigErrors()
{
foreach (string error in base.ConfigErrors())
{
yield return error;
}
if (workerClass == null)
{
yield return "<workerClass> is empty";
}
if (handlesEvents == null || handlesEvents.Count == 0)
{
yield return "<handlesEvents> is empty";
}
}
}
}

View file

@ -0,0 +1,97 @@
using RimWorld;
using rjw.Modules.Shared.Logs;
using System;
using System.Collections.Generic;
using System.Text;
using Verse;
namespace rjwquirks.Modules.Shared.Events
{
public static class RjwEventManager
{
private static readonly Dictionary<RjwEventDef, List<RjwEventHandler>> _handlers = BuildHandlerDictionary();
private static readonly ILog _logger = LogManager.GetLogger("RjwEventManager");
/// <summary>
/// Routes RJW events to the relevant event handlers based on the def subscriptions
/// </summary>
public static void NotifyEvent(RjwEvent ev)
{
//implement own settings later
if (Prefs.DevMode)
{
_logger.Message($"RJW Event {ev.def}{SignalArgsToString(ev.args)}");
}
if (!_handlers.TryGetValue(ev.def, out List<RjwEventHandler> eventHandlers))
{
return;
}
if (Prefs.DevMode)
{
// Since event args are filled in C#, no reason to waste time checking them outside of the actual mod developement
CheckObligatoryArgs(ev);
}
foreach (RjwEventHandler handler in eventHandlers)
{
try
{
handler.HandleEvent(ev);
}
catch (Exception e)
{
// suppress exceptions so one bad mod wouldn't break everything
_logger.Error($"Handler exception when handling {ev.def}", e);
}
}
}
private static Dictionary<RjwEventDef, List<RjwEventHandler>> BuildHandlerDictionary()
{
Dictionary<RjwEventDef, List<RjwEventHandler>> handlers = new Dictionary<RjwEventDef, List<RjwEventHandler>>();
foreach (RjwEventHandlerDef handlerDef in DefDatabase<RjwEventHandlerDef>.AllDefsListForReading)
{
foreach (RjwEventDef eventDef in handlerDef.handlesEvents)
{
if (handlers.ContainsKey(eventDef))
{
handlers[eventDef].Add(handlerDef.Worker);
}
else
{
handlers[eventDef] = new List<RjwEventHandler> { handlerDef.Worker };
}
}
}
return handlers;
}
private static void CheckObligatoryArgs(RjwEvent ev)
{
foreach (string argName in ev.def.obligatoryArgs)
{
if (!ev.args.TryGetArg(argName, out _))
{
_logger.Error($"Got a {ev.def} event without the obligatory argument '{argName}'");
}
}
}
private static string SignalArgsToString(SignalArgs args)
{
StringBuilder message = new StringBuilder();
foreach (var arg in args.Args)
{
message.Append(", ");
message.Append(arg);
}
return message.ToString();
}
}
}