|
|
|
@ -6,48 +6,158 @@ using System.Reflection;
|
|
|
|
|
using System.Reflection.Emit;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Verse;
|
|
|
|
|
using AlienRace;
|
|
|
|
|
|
|
|
|
|
namespace Rimworld_Animations {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Rimworld_Animations
|
|
|
|
|
{
|
|
|
|
|
[StaticConstructorOnStartup]
|
|
|
|
|
public static class HarmonyPatch_AlienRace
|
|
|
|
|
{
|
|
|
|
|
static readonly Type AlienRace_HarmonyPatches = AccessTools.TypeByName("AlienRace.HarmonyPatches");
|
|
|
|
|
|
|
|
|
|
static readonly MethodInfo InnerDrawAddon;
|
|
|
|
|
static readonly FieldInfo AccessForPawn;
|
|
|
|
|
static readonly FieldInfo AccessForRotation;
|
|
|
|
|
|
|
|
|
|
static HarmonyPatch_AlienRace()
|
|
|
|
|
{
|
|
|
|
|
(new Harmony("rjwanim")).Patch(AccessTools.Method(AccessTools.TypeByName("AlienRace.HarmonyPatches"), "DrawAddons"),
|
|
|
|
|
prefix: new HarmonyMethod(AccessTools.Method(typeof(HarmonyPatch_AlienRace), "Prefix_AnimateHeadAddons")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* todo: replace jank prefix with this
|
|
|
|
|
public static void Prefix_DrawAddonsFinalHook(ref Pawn pawn, ref AlienPartGenerator.BodyAddon addon, ref Rot4 rot, ref Graphic graphic, ref Vector3 offsetVector, ref float angle, ref Material mat)
|
|
|
|
|
AccessForPawn = null;
|
|
|
|
|
AccessForRotation = null;
|
|
|
|
|
InnerDrawAddon = AccessTools.FirstMethod(AlienRace_HarmonyPatches, (mi) =>
|
|
|
|
|
{
|
|
|
|
|
CompBodyAnimator animator = pawn.TryGetComp<CompBodyAnimator>();
|
|
|
|
|
if (mi.GetCustomAttribute<CompilerGeneratedAttribute>() is null) return false;
|
|
|
|
|
if (mi.ReturnType != typeof(void)) return false;
|
|
|
|
|
if (!mi.Name.Contains("DrawAddon")) return false;
|
|
|
|
|
|
|
|
|
|
if (animator == null || !animator.isAnimating)
|
|
|
|
|
var parameters = mi.GetParameters();
|
|
|
|
|
if (parameters.Length != 4) return false;
|
|
|
|
|
if (parameters[0].ParameterType != typeof(AlienPartGenerator.BodyAddon)) return false;
|
|
|
|
|
if (parameters[1].ParameterType != typeof(Graphic)) return false;
|
|
|
|
|
if (parameters[2].ParameterType != typeof(Vector2)) return false;
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (InnerDrawAddon is null)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("[rjwanim] Failed to apply Alien Race patches: could not find local `DrawAddon` method.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(addon.alignWithHead || addon.drawnInBed)
|
|
|
|
|
{
|
|
|
|
|
rot = animator.headFacing;
|
|
|
|
|
angle = animator.headAngle;
|
|
|
|
|
offsetVector += animator.deltaPos + animator.bodyAngle * animator.headBob;
|
|
|
|
|
// Extract the closure struct. This is passed with `ref`, so we have
|
|
|
|
|
// to also pull the element type to get rid of that.
|
|
|
|
|
var displayClassType = InnerDrawAddon.GetParameters()[3].ParameterType.GetElementType();
|
|
|
|
|
|
|
|
|
|
if (displayClassType is null)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("[rjwanim] Failed to apply Alien Race patches: could not get type of `DrawAddon` closure.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AccessForPawn = AccessTools.Field(displayClassType, "pawn");
|
|
|
|
|
|
|
|
|
|
if (AccessForPawn is null)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("[rjwanim] Failed to apply Alien Race patches: could not find field `pawn` of closure.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AccessForRotation = AccessTools.Field(displayClassType, "rotation");
|
|
|
|
|
|
|
|
|
|
if (AccessForRotation is null)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("[rjwanim] Failed to apply Alien Race patches: could not find field `rotation` of closure.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Got access to everything. It should be safe to setup the patches.
|
|
|
|
|
var harmonyInstance = new Harmony("rjwanim");
|
|
|
|
|
harmonyInstance.Patch(
|
|
|
|
|
InnerDrawAddon,
|
|
|
|
|
prefix: new HarmonyMethod(AccessTools.Method(typeof(HarmonyPatch_AlienRace), nameof(Prefix_FixDrawAddonRotation))),
|
|
|
|
|
postfix: new HarmonyMethod(AccessTools.Method(typeof(HarmonyPatch_AlienRace), nameof(Postfix_FixDrawAddonRotation)))
|
|
|
|
|
);
|
|
|
|
|
harmonyInstance.Patch(
|
|
|
|
|
AccessTools.Method(AlienRace_HarmonyPatches, "DrawAddonsFinalHook"),
|
|
|
|
|
postfix: new HarmonyMethod(AccessTools.Method(typeof(HarmonyPatch_AlienRace), nameof(Postfix_DrawAddonsFinalHook)))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool ShouldForceDrawForBody(Pawn pawn, AlienPartGenerator.BodyAddon addon)
|
|
|
|
|
{
|
|
|
|
|
if (pawn.def is not ThingDef_AlienRace alienProps) return false;
|
|
|
|
|
|
|
|
|
|
if (alienProps.defName.Contains("Orassan") && addon.path.ToLower().Contains("tail"))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The parameter `__3` is the compiler generated closure struct.
|
|
|
|
|
// Even though `__3` is a value type passed by reference, receiving it as
|
|
|
|
|
// an object boxes the underlying reference, so we don't need `ref` here.
|
|
|
|
|
// In fact, if you try to add it, RimWorld will crash outright!
|
|
|
|
|
public static void Prefix_FixDrawAddonRotation(AlienPartGenerator.BodyAddon ba, object __3, out Rot4 __state)
|
|
|
|
|
{
|
|
|
|
|
// Store the original rotation so we can restore it later.
|
|
|
|
|
__state = (Rot4)AccessForRotation.GetValue(__3);
|
|
|
|
|
if (ba is null) return;
|
|
|
|
|
|
|
|
|
|
var pawn = (Pawn)AccessForPawn.GetValue(__3);
|
|
|
|
|
if (pawn.TryGetComp<CompBodyAnimator>() is not { } animator) return;
|
|
|
|
|
if (!animator.isAnimating) return;
|
|
|
|
|
|
|
|
|
|
var forceDrawForBody = ShouldForceDrawForBody(pawn, ba);
|
|
|
|
|
|
|
|
|
|
// Set the rotation according to the animation.
|
|
|
|
|
if ((ba.drawnInBed && !forceDrawForBody) || ba.alignWithHead)
|
|
|
|
|
AccessForRotation.SetValue(__3, animator.headFacing);
|
|
|
|
|
else
|
|
|
|
|
AccessForRotation.SetValue(__3, animator.bodyFacing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Postfix_FixDrawAddonRotation(object __3, Rot4 __state)
|
|
|
|
|
{
|
|
|
|
|
// Restore the original value, since we're in a loop and the next
|
|
|
|
|
// part may need the previous rotation. Just being safe.
|
|
|
|
|
AccessForRotation.SetValue(__3, __state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Postfix_DrawAddonsFinalHook(Pawn pawn, AlienPartGenerator.BodyAddon addon, Rot4 rot, ref Vector3 offsetVector, ref float angle)
|
|
|
|
|
{
|
|
|
|
|
if (pawn.TryGetComp<CompBodyAnimator>() is not { } animator) return;
|
|
|
|
|
if (!animator.isAnimating) return;
|
|
|
|
|
|
|
|
|
|
var forceDrawForBody = ShouldForceDrawForBody(pawn, addon);
|
|
|
|
|
|
|
|
|
|
if ((addon.drawnInBed && !forceDrawForBody) || addon.alignWithHead)
|
|
|
|
|
{
|
|
|
|
|
angle = animator.headAngle;
|
|
|
|
|
offsetVector += animator.deltaPos + animator.headBob;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rot = animator.bodyFacing;
|
|
|
|
|
if (AnimationSettings.controlGenitalRotation && addon.path.ToLower().Contains("penis"))
|
|
|
|
|
angle = animator.genitalAngle;
|
|
|
|
|
else
|
|
|
|
|
angle = animator.bodyAngle;
|
|
|
|
|
offsetVector += animator.deltaPos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rot == Rot4.North && addon.layerInvert)
|
|
|
|
|
{
|
|
|
|
|
offsetVector.y = -offsetVector.y;
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (rot == Rot4.East)
|
|
|
|
|
{
|
|
|
|
|
angle *= -1f;
|
|
|
|
|
offsetVector.x = -offsetVector.x;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool Prefix_AnimateHeadAddons(PawnRenderFlags renderFlags, Vector3 vector, Vector3 headOffset, Pawn pawn, Quaternion quat, Rot4 rotation)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
@ -120,14 +230,23 @@ namespace Rimworld_Animations {
|
|
|
|
|
|
|
|
|
|
if (AnimationSettings.controlGenitalRotation && pawnAnimator.controlGenitalAngle && ba?.hediffGraphics != null && ba.hediffGraphics.Count != 0 && ba.hediffGraphics[0]?.path != null && (ba.hediffGraphics[0].path.Contains("Penis") || ba.hediffGraphics[0].path.Contains("penis")))
|
|
|
|
|
{
|
|
|
|
|
GenDraw.DrawMeshNowOrLater(mesh: addonGraphic.MeshAt(rot: rotation), loc: vector + (ba.alignWithHead ? headOffset : Vector3.zero) + vector2.RotatedBy(angle: Mathf.Acos(f: Quaternion.Dot(a: Quaternion.identity, b: addonRotation)) * 2f * 57.29578f),
|
|
|
|
|
quat: Quaternion.AngleAxis(angle: pawnAnimator.genitalAngle, axis: Vector3.up), mat: addonGraphic.MatAt(rot: rotation), renderFlags.FlagSet(PawnRenderFlags.DrawNow));
|
|
|
|
|
GenDraw.DrawMeshNowOrLater(
|
|
|
|
|
mesh: addonGraphic.MeshAt(rot: rotation),
|
|
|
|
|
loc: vector + (ba.alignWithHead ? headOffset : Vector3.zero) + vector2.RotatedBy(angle: Mathf.Acos(f: Quaternion.Dot(a: Quaternion.identity, b: addonRotation)) * 2f * 57.29578f),
|
|
|
|
|
quat: Quaternion.AngleAxis(angle: pawnAnimator.genitalAngle, axis: Vector3.up),
|
|
|
|
|
mat: addonGraphic.MatAt(rot: rotation),
|
|
|
|
|
drawNow: renderFlags.FlagSet(PawnRenderFlags.DrawNow)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GenDraw.DrawMeshNowOrLater(mesh: addonGraphic.MeshAt(rot: rotation), loc: vector + (ba.alignWithHead ? headOffset : Vector3.zero) + vector2.RotatedBy(angle: Mathf.Acos(f: Quaternion.Dot(a: Quaternion.identity, b: addonRotation)) * 2f * 57.29578f),
|
|
|
|
|
quat: Quaternion.AngleAxis(angle: num, axis: Vector3.up) * addonRotation, mat: addonGraphic.MatAt(rot: rotation), renderFlags.FlagSet(PawnRenderFlags.DrawNow));
|
|
|
|
|
GenDraw.DrawMeshNowOrLater(
|
|
|
|
|
mesh: addonGraphic.MeshAt(rot: rotation),
|
|
|
|
|
loc: vector + (ba.alignWithHead ? headOffset : Vector3.zero) + vector2.RotatedBy(angle: Mathf.Acos(f: Quaternion.Dot(a: Quaternion.identity, b: addonRotation)) * 2f * 57.29578f),
|
|
|
|
|
quat: Quaternion.AngleAxis(angle: num, axis: Vector3.up) * addonRotation,
|
|
|
|
|
mat: addonGraphic.MatAt(rot: rotation),
|
|
|
|
|
drawNow: renderFlags.FlagSet(PawnRenderFlags.DrawNow)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|