-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #802 from zymex22/issue788
Fixes Issue where Smart Recycler Won't produce anything for non Smeltable Stuff
- Loading branch information
Showing
5 changed files
with
177 additions
and
80 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
...e/ProjectRimFactory/Common/HarmonyPatches/Patch_GenRecipe_GenerateQualityCreatedByPawn.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using ProjectRimFactory.Drones; | ||
using RimWorld; | ||
using Verse; | ||
|
||
namespace ProjectRimFactory.Common.HarmonyPatches; | ||
|
||
[HarmonyPatch(typeof(QualityUtility), "GenerateQualityCreatedByPawn", new Type[] { typeof(Pawn), typeof(SkillDef) })] | ||
class Patch_GenRecipe_GenerateQualityCreatedByPawn | ||
{ | ||
static bool Prefix(ref QualityCategory __result, Pawn pawn, SkillDef relevantSkill) | ||
{ | ||
ISetQualityDirectly isqd = PatchStorageUtil.Get<ISetQualityDirectly>(pawn.Map, pawn.Position); | ||
if (isqd != null) | ||
{ | ||
__result = isqd.GetQuality(relevantSkill); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
//Prevent Biotech(Mech Changes) from interfering with Drone Skills | ||
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
|
||
bool foundReplaceMarker = false; | ||
bool repacedCheck = false; | ||
int cnt = 0; | ||
foreach (var instruction in instructions) | ||
{ | ||
cnt++; | ||
//Search for IL_0000: ldarg.0 -> Loading the Pawn | ||
if (instruction.opcode == OpCodes.Ldarg_0 && !repacedCheck) | ||
{ | ||
repacedCheck = true; | ||
foundReplaceMarker = true; | ||
cnt = 0; | ||
} | ||
//remove IL_0001: callvirt instance class Verse.RaceProperties Verse.Pawn::get_RaceProps() | ||
if (instruction.opcode == OpCodes.Callvirt && foundReplaceMarker && cnt == 1) | ||
{ | ||
continue; | ||
} | ||
if (instruction.opcode == OpCodes.Callvirt && foundReplaceMarker && cnt == 2) | ||
{ | ||
//Replace IL_0006: callvirt instance bool Verse.RaceProperties::get_IsMechanoid() | ||
//with a call to Patch_GenRecipe_GenerateQualityCreatedByPawn:IsMechanoid(Pawn pawn) | ||
instruction.operand = AccessTools.Method( | ||
typeof(Patch_GenRecipe_GenerateQualityCreatedByPawn), | ||
nameof(Patch_GenRecipe_GenerateQualityCreatedByPawn.IsMechanoid), new[] { typeof(Pawn)}); | ||
foundReplaceMarker = false; | ||
} | ||
|
||
|
||
yield return instruction; | ||
} | ||
|
||
} | ||
|
||
/// <summary> | ||
/// if a Pawn is a Mechanoid then it's skills will be ignored by QualityUtility:GenerateQualityCreatedByPawn | ||
/// This prevents Drones from being detected as Mechanoid | ||
/// </summary> | ||
/// <param name="pawn"></param> | ||
/// <returns></returns> | ||
public static bool IsMechanoid(Pawn pawn) | ||
{ | ||
if (pawn is Pawn_Drone) | ||
{ | ||
return false; | ||
} | ||
return pawn.RaceProps.IsMechanoid; | ||
} | ||
} | ||
|
||
interface ISetQualityDirectly | ||
{ | ||
QualityCategory GetQuality(SkillDef relevantSkill); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
Source/ProjectRimFactory/Common/HarmonyPatches/Patch_Thing_SmeltProducts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using System.Runtime.CompilerServices; | ||
using HarmonyLib; | ||
using Verse; | ||
|
||
namespace ProjectRimFactory.Common.HarmonyPatches; | ||
|
||
/// <summary> | ||
/// Change Thing.SmeltProducts so that if | ||
/// ProjectRimFactory.Common.HarmonyPatches.Patch_Thing_SmeltProducts.RecyclerProducingItems | ||
/// is Set, the Check "costListAdj[j].thingDef.smeltable" will be Ignored | ||
/// | ||
/// Since Vanilla uses the actual Value instead of the Getter we Can't simply patch the Getter, | ||
/// Instead we need to Transpile Thing.SmeltProducts | ||
/// </summary> | ||
[HarmonyPatch] | ||
public class Patch_Thing_SmeltProducts | ||
{ | ||
private static readonly Type hiddenClass = AccessTools.FirstInner( | ||
typeof(Thing), | ||
type => type.HasAttribute<CompilerGeneratedAttribute>() && type.Name.Contains(nameof(Thing.SmeltProducts))); | ||
|
||
public static MethodBase TargetMethod() | ||
{ | ||
// Decompiler showed the hidden inner class is "<SmeltProducts>d__223" | ||
if (hiddenClass == null) | ||
{ | ||
Log.Error("Couldn't find iterator class -- This should never be reached."); | ||
return null; | ||
} | ||
// and we want the iterator's MoveNext: | ||
MethodBase iteratorMethod = HarmonyLib.AccessTools.Method(hiddenClass, "MoveNext"); | ||
if (iteratorMethod == null) Log.Error("Couldn't find MoveNext"); | ||
return iteratorMethod; | ||
} | ||
|
||
private static bool smeltableFound = false; | ||
|
||
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
foreach (var instruction in instructions) | ||
{ | ||
if (!smeltableFound) | ||
{ | ||
if (instruction.opcode == OpCodes.Ldfld && instruction.operand.ToString().Contains("smeltable")) | ||
{ | ||
smeltableFound = true; | ||
|
||
// now replace it with a call to Patch_Thing_SmeltProducts.Smeltable | ||
yield return new CodeInstruction(OpCodes.Call, AccessTools.Method( | ||
typeof(Patch_Thing_SmeltProducts), | ||
nameof(Patch_Thing_SmeltProducts.Smeltable))); | ||
continue; | ||
|
||
} | ||
else | ||
{ | ||
yield return instruction; | ||
continue; | ||
} | ||
} | ||
|
||
// Don't touch the rest | ||
yield return instruction; | ||
} | ||
|
||
|
||
} | ||
|
||
public static bool RecyclerProducingItems = false; | ||
|
||
/// <summary> | ||
/// Skips the def.smeltable Check if RecyclerProducingItems | ||
/// </summary> | ||
/// <param name="def"></param> | ||
/// <returns></returns> | ||
public static bool Smeltable(ThingDef def) | ||
{ | ||
return RecyclerProducingItems || def.smeltable; | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters