Skip to content
This repository has been archived by the owner on Nov 25, 2018. It is now read-only.

Commit

Permalink
Switch detouring method to Harmony
Browse files Browse the repository at this point in the history
  • Loading branch information
notfood committed Jun 3, 2017
1 parent 92702be commit 6dabfba
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 278 deletions.
Binary file added Assemblies/0Harmony.dll
Binary file not shown.
Binary file modified Assemblies/Infused.dll
Binary file not shown.
2 changes: 0 additions & 2 deletions Source/Defs/Def.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ public override void ResolveReferences ()

statDef.parts.Add (new StatPart_InfusionModifier(statDef));
}

ModInjector.Inject ();
}

public static Def Named( string defName )
Expand Down
17 changes: 6 additions & 11 deletions Source/Infused.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<HintPath>..\..\..\..\..\Steam\steamapps\common\RimWorld\RimWorldLinux_Data\Managed\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="0Harmony">
<HintPath>..\Assemblies\0Harmony.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
Expand All @@ -47,9 +51,6 @@
<Compile Include="Gens\GenInfusionColor.cs" />
<Compile Include="Gens\MathUtility.cs" />
<Compile Include="MapComponent\MapComponent_InfusionManager.cs" />
<Compile Include="ModInitializers\Detours.cs" />
<Compile Include="ModInitializers\ModDetour.cs" />
<Compile Include="ModInitializers\ModInjector.cs" />
<Compile Include="StatParts\StatPart_InfusionModifier.cs" />
<Compile Include="UI\ITab_Infusion.cs" />
<Compile Include="InfusionSet.cs" />
Expand All @@ -58,16 +59,10 @@
<Compile Include="Defs\Def.cs" />
<Compile Include="Gens\GenInfusionText.cs" />
<Compile Include="MapComponent\InfusionLabelManager.cs" />
<Compile Include="ModInitializers\MapComponentInjector.cs" />
<Compile Include="ModInitializers\InfusedMod.cs" />
<Compile Include="ModInitializers\CompQualityPatch.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<MonoDevelop>
<Properties>
<Deployment.LinuxDeployData generatePcFile="False" />
</Properties>
</MonoDevelop>
</ProjectExtensions>
<ItemGroup>
<Folder Include="Comp\" />
<Folder Include="Gens\" />
Expand Down
47 changes: 47 additions & 0 deletions Source/ModInitializers/CompQualityPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;

using Harmony;

using Verse;
using RimWorld;

namespace Infused
{
[HarmonyPatch (typeof (CompQuality))]
[HarmonyPatch ("SetQuality")]
[HarmonyPatch (new Type [] { typeof (QualityCategory), typeof (ArtGenerationContext) })]
class CompQualityPatch
{
static void Postfix (CompQuality __instance, QualityCategory q, ArtGenerationContext source)
{
// Can we be infused?
CompInfusion compInfusion = __instance.parent.TryGetComp<CompInfusion> ();
if (compInfusion != null) {
var thing = __instance.parent;
var def = __instance.parent.def;
// Get those Infusions rolling
var prefix = roll (thing, q);
var suffix = roll (thing, q);

var tierMult = def.techLevel < TechLevel.Industrial ? 3 : 1;

if (prefix)
compInfusion.InitializeInfusionPrefix (GenInfusion.GetTier (q, tierMult));
if (suffix)
compInfusion.InitializeInfusionSuffix (GenInfusion.GetTier (q, tierMult));
if (prefix || suffix)
__instance.parent.HitPoints = __instance.parent.MaxHitPoints;
}
}

private static bool roll (Thing thing, QualityCategory qc)
{
var chance = GenInfusion.GetInfusionChance (thing, qc);
var rand = Rand.Value;
#if DEBUG
Log.Message ("Infused :: Rolled " + ((rand < chance) ? "success" : "failure") + " " + rand + " < " + chance + " for " + thing + " and " + qc);
#endif
return rand < chance;
}
}
}
87 changes: 0 additions & 87 deletions Source/ModInitializers/Detours.cs

This file was deleted.

54 changes: 54 additions & 0 deletions Source/ModInitializers/InfusedMod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using Verse;
using RimWorld;

namespace Infused
{
class InfusedMod : Mod
{
public InfusedMod(ModContentPack mcp) : base (mcp)
{
Harmony.HarmonyInstance.Create ("rimworld.infused").PatchAll (Assembly.GetExecutingAssembly ());

LongEventHandler.ExecuteWhenFinished (Inject);
}

private static void Inject ()
{
var defs = (
from def in DefDatabase<ThingDef>.AllDefs
where (def.IsMeleeWeapon || def.IsRangedWeapon || def.IsApparel)
&& def.HasComp (typeof (CompQuality)) && !def.HasComp (typeof (CompInfusion))
select def
);

var tabType = typeof (ITab_Infusion);
var tab = InspectTabManager.GetSharedInstance (tabType);
var compProperties = new CompProperties { compClass = typeof (CompInfusion) };

foreach (var def in defs) {
def.comps.Add (compProperties);
#if DEBUG
Log.Message ("Infused :: Component added to " + def.label);
#endif

if (def.inspectorTabs == null || def.inspectorTabs.Count == 0) {
def.inspectorTabs = new List<Type> ();
def.inspectorTabsResolved = new List<InspectTabBase> ();
}

def.inspectorTabs.Add (tabType);
def.inspectorTabsResolved.Add (tab);
}
#if DEBUG
Log.Message ("Infused :: Injected " + defs.Count() + " " + DefDatabase<ThingDef>.AllDefs.Count());
#endif
}

}

}
30 changes: 0 additions & 30 deletions Source/ModInitializers/MapComponentInjector.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Source/ModInitializers/MapComponentInjectorBehavior.cs

This file was deleted.

65 changes: 0 additions & 65 deletions Source/ModInitializers/ModDetour.cs

This file was deleted.

Loading

0 comments on commit 6dabfba

Please sign in to comment.