using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace KnifeMod;
[BepInPlugin("com.trae.m9knife", "M9 Draw", "4.0.0")]
public class M9Plugin : BaseUnityPlugin
{
internal static ManualLogSource Log;
private static KnifeModRunner _runner;
private void Awake()
{
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
//IL_002c: Unknown result type (might be due to invalid IL or missing references)
//IL_0031: Unknown result type (might be due to invalid IL or missing references)
//IL_0037: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
new Harmony("com.trae.m9knife").PatchAll();
if ((Object)(object)_runner == (Object)null)
{
GameObject val = new GameObject("KnifeModRunner");
Object.DontDestroyOnLoad((Object)val);
_runner = val.AddComponent<KnifeModRunner>();
}
Log.LogInfo((object)"[KnifeMod] loaded: v4.0 (播放模型自带拔刀动画)");
}
public static void PlayDrawThenIdle(Animation anim)
{
if ((Object)(object)_runner != (Object)null)
{
((MonoBehaviour)_runner).StartCoroutine(_runner.PlayDrawThenIdle(anim));
}
}
}
internal class KnifeModRunner : MonoBehaviour
{
private const bool Reverse = true;
private const float Speed = 1.5f;
private const float StopAt = 0.42f;
public IEnumerator PlayDrawThenIdle(Animation anim)
{
AnimationState st = anim["Inspect"];
if (!((TrackedReference)(object)st == (TrackedReference)null))
{
anim.Stop();
st.speed = -1.5f;
st.time = st.clip.length;
st.wrapMode = (WrapMode)8;
anim.Play("Inspect");
M9Plugin.Log.LogInfo((object)("[KnifeMod] 拔刀: 检视动画 倒放 " + 1.5f.ToString("F1") + "x"));
float timeout = Time.time + 2.5f;
while (anim.IsPlaying("Inspect") && Time.time < timeout && !(st.normalizedTime <= 0.42f))
{
yield return null;
}
anim.Stop();
anim.Play("Idle");
st.speed = 1f;
st.wrapMode = (WrapMode)1;
M9Plugin.Log.LogInfo((object)"[KnifeMod] 拔刀动画结束 → Idle");
}
}
}
internal static class KnifeHooks
{
[HarmonyPatch(typeof(PlayerToolMovement), "SpawnFromInventory")]
internal static class SpawnFromInventoryPatch
{
private static void Postfix(PlayerToolMovement __instance)
{
//IL_0093: Unknown result type (might be due to invalid IL or missing references)
//IL_009a: Expected O, but got Unknown
try
{
object? value = AccessTools.Field(typeof(PlayerToolMovement), "_tool").GetValue(__instance);
Tool val = (Tool)((value is Tool) ? value : null);
if ((Object)(object)val == (Object)null || (Object)(object)((Component)val).GetComponent<Melee>() == (Object)null)
{
return;
}
object? value2 = AccessTools.Field(typeof(Tool), "_anim").GetValue(val);
Animation val2 = (Animation)((value2 is Animation) ? value2 : null);
if ((Object)(object)val2 == (Object)null)
{
M9Plugin.Log.LogWarning((object)"[KnifeMod] _anim 为 null");
return;
}
List<string> list = new List<string>();
foreach (AnimationState item in val2)
{
AnimationState val3 = item;
string text = (((Object)(object)val3.clip != (Object)null) ? ((Object)val3.clip).name : "?");
list.Add(text + "(" + val3.length.ToString("F2") + "s)");
}
M9Plugin.Log.LogInfo((object)("[KnifeMod] clips: " + string.Join(", ", list.ToArray())));
if ((TrackedReference)(object)val2["Inspect"] == (TrackedReference)null)
{
M9Plugin.Log.LogInfo((object)"[KnifeMod] 无 Inspect clip, 保持原版");
}
else
{
M9Plugin.PlayDrawThenIdle(val2);
}
}
catch (Exception ex)
{
M9Plugin.Log.LogWarning((object)("[KnifeMod] Spawn: " + ex.Message));
}
}
}
}