Skip to content

Commit

Permalink
add integration for automatics trader ships mod
Browse files Browse the repository at this point in the history
  • Loading branch information
Arakos committed Apr 30, 2022
1 parent 243b6b0 commit 4725aea
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
8 changes: 5 additions & 3 deletions Source/CallATrader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RimWorld;
using HarmonyLib;
using RimWorld;
using System.Reflection;
using UnityEngine;
using Verse;
Expand All @@ -25,8 +26,9 @@ private set { }
public CallATrader(ModContentPack pack) : base(pack)
{
instance = this;
new HarmonyLib.Harmony("arakos.callatrader.acat.callatrader")
.PatchAll(Assembly.GetExecutingAssembly());
Harmony harmony = new HarmonyLib.Harmony("arakos.callatrader.acat.callatrader");
harmony.PatchAll(Assembly.GetExecutingAssembly());
TraderShips_Patch.TryPatch(harmony);
}

public override void DoSettingsWindowContents(Rect inRect)
Expand Down
3 changes: 2 additions & 1 deletion Source/CalllATrader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CallATrader.cs" />
<Compile Include="integrations\TraderShips_Patch.cs" />
<Compile Include="rimworld\ChoiceLetter_SelectTrader.cs" />
<Compile Include="Constants.cs" />
<Compile Include="rimworld\ICommunicable_OrbitalTradersHub.cs" />
<Compile Include="rimworld\IncidentWorker_OrbitalTraderVisitingOffer.cs" />
<Compile Include="Settings.cs" />
<Compile Include="integrations\HarmonyPatch.cs" />
<Compile Include="integrations\Rimworld_Patch.cs" />
<Compile Include="rimworld\JobDriver_CallATrader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="State.cs" />
Expand Down
4 changes: 2 additions & 2 deletions Source/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
File renamed without changes.
80 changes: 80 additions & 0 deletions Source/integrations/TraderShips_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using HarmonyLib;
using RimWorld;
using Verse;
using System;
using System.Linq;
using System.Reflection;

namespace Arakos.CallATrader
{
public static class TraderShips_Patch
{
private static Assembly assembly_TraderShips = null;
private static Type type_CompShip = null;
private static MethodInfo method_CompShip_GenerateInternalTradeShip = null;
private static MethodInfo method_IncidentWorkerTraderShip_LandShip = null;

public static void TryPatch(Harmony harmony)
{
ModContentPack mod_TraderShips = null;
foreach (ModContentPack mod in LoadedModManager.RunningMods)
{
foreach (Assembly assembly in mod.assemblies.loadedAssemblies)
{
if (assembly.GetName().Name.Equals("TraderShips"))
{
assembly_TraderShips = assembly;
mod_TraderShips = mod;
break;
}
}
}

if (assembly_TraderShips != null)
{
try
{
type_CompShip = assembly_TraderShips.GetType("TraderShips.CompShip", true);
method_CompShip_GenerateInternalTradeShip = type_CompShip.GetMethod("GenerateInternalTradeShip") ?? throw new Exception("Method TraderShips.CompShip.GenerateInternalTradeShip not found");
method_IncidentWorkerTraderShip_LandShip = assembly_TraderShips.GetType("TraderShips.IncidentWorkerTraderShip").GetMethod("LandShip") ?? throw new Exception("Method TraderShips.IncidentWorkerTraderShip.LandShip not found");

MethodInfo method_TryExecuteWorker = assembly_TraderShips.GetType("TraderShips.IncidentWorkerTraderShip", true)
.GetMethod("TryExecuteWorker", BindingFlags.Instance | BindingFlags.NonPublic, Type.DefaultBinder, new[] { typeof(IncidentParms) }, null) ?? throw new Exception("Method TraderShips.IncidentWorkerTraderShip.TryExecuteWorker not found");

harmony.Patch(method_TryExecuteWorker, prefix: new HarmonyMethod(typeof(TraderShips_Patch).GetMethod(nameof(TraderShips_Patch.TryExecuteWorker_Patch))));

Log.Message($"Mod Call A Trader integrated with Mod { mod_TraderShips.Name }.");
}
catch (Exception ex)
{
Log.Warning($"Mod Call A Trader detected but failed to integrate with with Mod { mod_TraderShips.Name }. " +
$"Method patch was not applied, hence TraderShips will not necessarily fit to requested trader type. Reason: { ex }");
}
}
}

public static bool TryExecuteWorker_Patch(ref bool __result, IncidentParms parms)
{
try
{
Map map = (Map) parms.target;
TraderKindDef traderKindDef = parms.traderKind;

ThingWithComps ship = (ThingWithComps) ThingMaker.MakeThing(DefDatabase<ThingDef>.GetNamed("TraderShipsShip"));
Object comp = ship.GetComps<ThingComp>().Where(c => Object.Equals(c.GetType(), type_CompShip)).First();
method_CompShip_GenerateInternalTradeShip.Invoke(comp, new Object[] { map, traderKindDef });
method_IncidentWorkerTraderShip_LandShip.Invoke(null, new Object[] { map, ship });

Log.Message("Successfully executed patched 'TraderShips.IncidentWorkerTraderShip.TryExecuteWorker(..)' method");
__result = true;
return false;
}
catch (Exception ex)
{
Log.Error("Failed to invoke patched 'TraderShips.IncidentWorkerTraderShip.TryExecuteWorker(..)' method! Invoking original method instead. " + ex);
return true;
}
}

}
}

0 comments on commit 4725aea

Please sign in to comment.