-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
integrated CE compatibility + downing chance settings + downed on dea…
…th threshold chance
- Loading branch information
Showing
10 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
Binary file not shown.
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
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
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
122 changes: 122 additions & 0 deletions
122
Source/WhatTheHack/Harmony/CombatExtended/CE_JobGiver_TakeAndEquip.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,122 @@ | ||
using Harmony; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using System.Text; | ||
using Verse; | ||
|
||
namespace WhatTheHack.Harmony | ||
{ | ||
[HarmonyPatch] | ||
class CE_JobGiver_TakeAndEquip_TryGiveJob | ||
{ | ||
static MethodBase TargetMethod() | ||
{ | ||
Assembly assemblyCE = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault((Assembly assembly) => assembly.FullName.StartsWith("CombatExtended")); | ||
MethodInfo stub = typeof(CE_JobGiver_TakeAndEquip_TryGiveJob).GetMethod("Stub"); | ||
if (assemblyCE == null) | ||
{ | ||
return stub; | ||
} | ||
Type type = assemblyCE.GetTypes().FirstOrDefault((Type t) => t.Name == "JobGiver_TakeAndEquip"); | ||
//Type type = assemblyCE.GetType("JobGiver_TakeAndEquip"); | ||
if(type == null) | ||
{ | ||
return stub; | ||
} | ||
MethodInfo minfo = AccessTools.Method(type, "TryGiveJob"); | ||
if(minfo == null) | ||
{ | ||
return stub; | ||
} | ||
return minfo; | ||
} | ||
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
var instructionsList = new List<CodeInstruction>(instructions); | ||
for (var i = 0; i < instructionsList.Count; i++) | ||
{ | ||
CodeInstruction instruction = instructionsList[i]; | ||
if (instruction.operand == typeof(Pawn).GetMethod("get_RaceProps")) | ||
{ | ||
yield return new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(CE_JobGiver_TakeAndEquip_TryGiveJob), "ShouldReload", new Type[] { typeof(Pawn) }));//Injected code | ||
} | ||
else if (instruction.operand == AccessTools.Method(typeof(RaceProperties), "get_Humanlike")) | ||
{ | ||
//Ommit this instruction | ||
} | ||
else | ||
{ | ||
yield return instruction; | ||
} | ||
|
||
} | ||
} | ||
public static void Stub() | ||
{ | ||
//This is patched when harmony can't find the CE method ShouldReload. | ||
} | ||
public static bool ShouldReload(Pawn p) | ||
{ | ||
//For mechanoids replace the check of is p.RaceProps.HumanLike by custom logic | ||
Log.Message("calling ShouldReload for pawn: " + p.Name); | ||
if (p.RaceProps.IsMechanoid && p.IsHacked()) | ||
{ | ||
Log.Message("calling ShouldReload for hacked mechanoid: " + p.Name); | ||
|
||
//return true when a mechanoid is hacked and does not have much ammo. | ||
ThingComp inventory = TryGetCompByTypeName(p, "CompInventory", "CombatExtended"); | ||
ThingWithComps eq = p.equipment.Primary; | ||
bool shouldTransfer = false; | ||
if(eq == null) | ||
{ | ||
eq = p.inventory.GetDirectlyHeldThings().FirstOrDefault() as ThingWithComps; | ||
shouldTransfer = eq == null ? false : true; | ||
} | ||
if(eq == null) | ||
{ | ||
Log.Message("eq is null"); | ||
} | ||
if(inventory == null) | ||
{ | ||
Log.Message("inventory comp is null"); | ||
} | ||
if (inventory != null && eq != null) | ||
{ | ||
//Everything is done using reflection, so we don't need to include a dependency | ||
ThingComp ammoUser = TryGetCompByTypeName(eq, "CompAmmoUser", "CombatExtended"); | ||
if(ammoUser == null) | ||
{ | ||
Log.Message("ammoUser is null"); | ||
} | ||
if (ammoUser != null) | ||
{ | ||
var currentAmmo = Traverse.Create(ammoUser).Property("CurrentAmmo").GetValue(); | ||
int ammoCount = Traverse.Create(inventory).Method("AmmoCountOfDef", new object[] { currentAmmo }).GetValue<int>(); | ||
var props = Traverse.Create(ammoUser).Property("Props").GetValue(); | ||
int magazineSize = Traverse.Create(props).Field("magazineSize").GetValue<int>(); | ||
int minAmmo = magazineSize == 0 ? 10 : magazineSize; //No magic numbers? | ||
if (ammoCount < minAmmo) | ||
{ | ||
Log.Message("reload needed, returning true"); | ||
if (shouldTransfer) | ||
{ | ||
Log.Message("transferring eq from inventory"); | ||
p.equipment.AddEquipment(eq.SplitOff(1) as ThingWithComps); | ||
} | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return p.RaceProps.Humanlike; | ||
} | ||
private static ThingComp TryGetCompByTypeName(ThingWithComps thing, string typeName, string assemblyName = "") | ||
{ | ||
return thing.AllComps.FirstOrDefault((ThingComp comp) => comp.GetType().Name == typeName); | ||
} | ||
} | ||
|
||
} |
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
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