Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of EnchantedGoldenApple v0.1.0
EnchantedGoldenApple.dll
Decompiled 4 hours agousing System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using BepInEx; using BepInEx.Configuration; using BepInEx.Logging; using EnchantedGoldenApple.Apple; using EnchantedGoldenApple.Shield; using HarmonyLib; using Microsoft.CodeAnalysis; using UnityEngine; using UnityEngine.SceneManagement; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")] [assembly: AssemblyCompany("EnchantedGoldenApple")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyInformationalVersion("1.0.0")] [assembly: AssemblyProduct("EnchantedGoldenApple")] [assembly: AssemblyTitle("EnchantedGoldenApple")] [assembly: AssemblyVersion("1.0.0.0")] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } namespace EnchantedGoldenApple { [HarmonyPatch(typeof(GameDirector), "Update")] public static class FrameDriver { private static bool _created; private static void Postfix() { //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Expected O, but got Unknown if (!_created) { _created = true; GameObject val = new GameObject("[EGA_Driver]"); Object.DontDestroyOnLoad((Object)val); val.AddComponent<EgaDriver>(); } Plugin.TickFrame(); } } public class EgaDriver : MonoBehaviour { private float _diagTimer; private void Update() { Plugin.TickFrame(); } private void OnGUI() { //IL_002d: Unknown result type (might be due to invalid IL or missing references) //IL_0032: Unknown result type (might be due to invalid IL or missing references) _diagTimer -= Time.unscaledDeltaTime; if (_diagTimer <= 0f) { _diagTimer = 2f; try { Scene activeScene = SceneManager.GetActiveScene(); string name = ((Scene)(ref activeScene)).name; string arg = "n/a"; try { arg = (((Object)(object)GameDirector.instance != (Object)null) ? ((object)Unsafe.As<gameState, gameState>(ref GameDirector.instance.currentState)/*cast due to .constrained prefix*/).ToString() : "gd=null"); } catch { } ManualLogSource log = Plugin.Log; if (log != null) { log.LogInfo((object)($"[DIAG-GUI] inLevel={ShieldManager.InLevel} scene='{name}' state={arg} " + $"local={(Object)(object)Plugin.LocalHealth != (Object)null} menuAvatar={(Object)(object)Plugin.LocalHealth != (Object)null && Plugin.LocalHealth.isMenuAvatar}")); } } catch (Exception ex) { ManualLogSource log2 = Plugin.Log; if (log2 != null) { log2.LogInfo((object)("[DIAG-GUI] " + ex.Message)); } } } ShieldManager.DrawHud(Plugin.LocalHealth); } } [BepInPlugin("lch.enchantedgoldenapple", "Enchanted Golden Apple", "0.1.0")] public class Plugin : BaseUnityPlugin { public const string GUID = "lch.enchantedgoldenapple"; public const string NAME = "Enchanted Golden Apple"; public const string VERSION = "0.1.0"; internal static ManualLogSource Log; internal static Harmony Harmony; internal static ConfigEntry<bool> CfgShowHud; internal static ConfigEntry<bool> CfgHRefill; internal static PlayerHealth LocalHealth; private void Awake() { //IL_005f: Unknown result type (might be due to invalid IL or missing references) //IL_0069: Expected O, but got Unknown Log = ((BaseUnityPlugin)this).Logger; Log.LogInfo((object)"[Enchanted Golden Apple] v0.1.0 loaded."); CfgShowHud = ((BaseUnityPlugin)this).Config.Bind<bool>("HUD", "ShowShieldHearts", true, "是否在左上角显示护盾爱心(开关护盾HUD)"); CfgHRefill = ((BaseUnityPlugin)this).Config.Bind<bool>("Keys", "HKeyRefill", false, "按H是否自动回满护盾;默认关闭,按H无效果,只能靠拾取金苹果回盾"); Harmony = new Harmony("lch.enchantedgoldenapple"); Harmony.PatchAll(); Log.LogInfo((object)"[Enchanted Golden Apple] Harmony patched."); } private void Update() { TickFrame(); } internal static void TickFrame() { PlayerAvatar val = SemiFunc.PlayerAvatarLocal(); LocalHealth = (((Object)(object)val != (Object)null) ? val.playerHealth : null); AppleSpawner.Tick(); SpeedBoost.Tick(); if (Input.GetKeyDown((KeyCode)104) && (Object)(object)val != (Object)null) { ShieldManager.AddShield(LocalHealth, 50); SpeedBoost.Activate(val); } } private void OnGUI() { ShieldManager.DrawHud(LocalHealth); } } } namespace EnchantedGoldenApple.Shield { public static class ShieldManager { [HarmonyPatch(typeof(PlayerHealth), "Hurt")] public static class HurtPatch { private static bool Prefix(PlayerHealth __instance, ref int damage, bool hurtByHeal) { if (damage <= 0 || hurtByHeal) { return true; } int num = Get(__instance); if (num <= 0) { return true; } int num2 = Mathf.Min(num, damage); num -= num2; damage -= num2; Shield[__instance] = num; Plugin.Log.LogInfo((object)$"[Shield] absorbed {num2}, shield left {num}, remaining dmg {damage}"); if (damage <= 0) { return false; } return true; } } public const int MaxShield = 50; internal static bool InLevel; private static readonly Dictionary<PlayerHealth, int> Shield = new Dictionary<PlayerHealth, int>(); private static FieldInfo _paField; private static FieldInfo _spawnedField; private static readonly Dictionary<int, Texture2D> HeartCache = new Dictionary<int, Texture2D>(); public static int Get(PlayerHealth hp) { if (!((Object)(object)hp != (Object)null) || !Shield.TryGetValue(hp, out var value)) { return 0; } return value; } public static void AddShield(PlayerHealth hp, int amount) { if (!((Object)(object)hp == (Object)null)) { int num = Get(hp); int num2 = Mathf.Min(50, num + amount); Shield[hp] = num2; Plugin.Log.LogInfo((object)$"[Shield] +{amount} -> {num2}/{50}"); } } public static void Clear(PlayerHealth hp) { if ((Object)(object)hp != (Object)null) { Shield[hp] = 0; } } public static void ResetAll() { Shield.Clear(); } public static void DrawHud(PlayerHealth local) { //IL_0094: Unknown result type (might be due to invalid IL or missing references) if ((Plugin.CfgShowHud != null && !Plugin.CfgShowHud.Value) || !InLevel || !SemiFunc.IsMasterClientOrSingleplayer() || (Object)(object)local == (Object)null || !RealPlayerSpawned(local)) { return; } try { int num = Get(local); if (num > 0) { float num2 = 174f; float num3 = ((float)Screen.width - num2) / 2f; float num4 = (float)Screen.height - 175f; for (int i = 0; i < 5; i++) { Texture2D heartTex = GetHeartTex(HeartState(num, i)); GUI.DrawTexture(new Rect(num3 + (float)i * 36f, num4, 30f, 30f), (Texture)(object)heartTex); } } } catch (Exception ex) { if (Plugin.Log != null) { Plugin.Log.LogError((object)("[Hud] draw failed: " + ex.Message)); } } } private static bool RealPlayerSpawned(PlayerHealth hp) { try { if (_paField == null) { _paField = typeof(PlayerHealth).GetField("playerAvatar", BindingFlags.Instance | BindingFlags.NonPublic); } object? value = _paField.GetValue(hp); PlayerAvatar val = (PlayerAvatar)((value is PlayerAvatar) ? value : null); if ((Object)(object)val == (Object)null) { return false; } if (_spawnedField == null) { _spawnedField = typeof(PlayerAvatar).GetField("spawned", BindingFlags.Instance | BindingFlags.NonPublic); } return _spawnedField != null && (bool)_spawnedField.GetValue(val); } catch { return false; } } private static int HeartState(int shield, int i) { int num = i * 10; int num2 = (i + 1) * 10; if (shield >= num2) { return 2; } if (shield > num) { return 1; } return 0; } private static Texture2D GetHeartTex(int state) { if (HeartCache.TryGetValue(state, out var value)) { return value; } Texture2D val = BuildHeart(state); HeartCache[state] = val; return val; } private static Texture2D BuildHeart(int state) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Expected O, but got Unknown //IL_01e2: Unknown result type (might be due to invalid IL or missing references) //IL_01fb: Unknown result type (might be due to invalid IL or missing references) //IL_0278: Unknown result type (might be due to invalid IL or missing references) //IL_0275: Unknown result type (might be due to invalid IL or missing references) //IL_026c: Unknown result type (might be due to invalid IL or missing references) //IL_0269: Unknown result type (might be due to invalid IL or missing references) //IL_0279: Unknown result type (might be due to invalid IL or missing references) //IL_026d: Unknown result type (might be due to invalid IL or missing references) //IL_0280: Unknown result type (might be due to invalid IL or missing references) Texture2D val = new Texture2D(16, 15, (TextureFormat)4, false); Color val2 = default(Color); ((Color)(ref val2))..ctor(0f, 0f, 0f, 0f); Color val3 = default(Color); ((Color)(ref val3))..ctor(1f, 0.78f, 0.22f); Color val4 = default(Color); ((Color)(ref val4))..ctor(0.62f, 0.42f, 0.92f); int[][] array = new int[15][] { new int[4] { 2, 3, 12, 13 }, new int[4] { 1, 4, 11, 14 }, new int[4] { 0, 5, 10, 15 }, new int[2] { 0, 15 }, new int[2] { 0, 15 }, new int[2] { 0, 15 }, new int[2] { 0, 15 }, new int[2] { 1, 14 }, new int[2] { 2, 13 }, new int[2] { 3, 12 }, new int[2] { 4, 11 }, new int[2] { 5, 10 }, new int[2] { 6, 9 }, new int[2] { 7, 8 }, new int[2] { 7, 8 } }; bool[,] array2 = new bool[16, 15]; for (int i = 0; i < 15; i++) { int[] array3 = array[i]; for (int j = 0; j < array3.Length; j += 2) { for (int k = array3[j]; k <= array3[j + 1]; k++) { array2[k, i] = true; } } } for (int l = 0; l < 15; l++) { for (int m = 0; m < 16; m++) { if (!array2[m, l]) { val.SetPixel(m, l, val2); continue; } if (state == 1 && m >= 8) { val.SetPixel(m, l, val2); continue; } bool flag = !array2[Mathf.Max(0, m - 1), l] || !array2[Mathf.Min(15, m + 1), l] || !array2[m, Mathf.Max(0, l - 1)] || !array2[m, Mathf.Min(14, l + 1)]; Color val5 = ((state != 0) ? (flag ? val4 : val3) : (flag ? val4 : val2)); val.SetPixel(m, l, val5); } } val.Apply(); return val; } } } namespace EnchantedGoldenApple.Apple { public static class AppleSpawner { [HarmonyPatch(typeof(GameDirector), "gameStateStart")] public static class StartPatch { private static void Postfix() { ShieldManager.InLevel = true; ShieldManager.ResetAll(); if (SemiFunc.IsMasterClientOrSingleplayer()) { ClearAll(); spawnTimer = 1.5f; } } } [HarmonyPatch(typeof(GameDirector), "gameStateEnd")] public static class EndPatch { private static void Postfix() { LeaveLevel(); } } [HarmonyPatch(typeof(GameDirector), "gameStateOutro")] public static class OutroPatch { private static void Postfix() { LeaveLevel(); } } [HarmonyPatch(typeof(GameDirector), "gameStateDeath")] public static class DeathPatch { private static void Postfix() { LeaveLevel(); } } public const int MaxPerLevel = 8; private static readonly List<GameObject> Alive = new List<GameObject>(); private static float spawnTimer = -1f; private static void LeaveLevel() { ShieldManager.InLevel = false; ShieldManager.ResetAll(); ClearAll(); } public static void Tick() { if (!(spawnTimer < 0f)) { spawnTimer -= Time.deltaTime; if (!(spawnTimer > 0f)) { spawnTimer = -1f; SpawnAroundLocalPlayer(8); } } } private static void SpawnAroundLocalPlayer(int total) { //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_001e: Unknown result type (might be due to invalid IL or missing references) //IL_0030: Unknown result type (might be due to invalid IL or missing references) //IL_0065: Unknown result type (might be due to invalid IL or missing references) //IL_0089: Unknown result type (might be due to invalid IL or missing references) //IL_008e: Unknown result type (might be due to invalid IL or missing references) //IL_0093: Unknown result type (might be due to invalid IL or missing references) //IL_00aa: Unknown result type (might be due to invalid IL or missing references) //IL_00af: Unknown result type (might be due to invalid IL or missing references) //IL_00cc: Unknown result type (might be due to invalid IL or missing references) //IL_00d1: Unknown result type (might be due to invalid IL or missing references) //IL_00db: Unknown result type (might be due to invalid IL or missing references) //IL_00e0: Unknown result type (might be due to invalid IL or missing references) PlayerAvatar val = SemiFunc.PlayerAvatarLocal(); if ((Object)(object)val == (Object)null) { return; } Vector3 val2 = (((Object)(object)val.playerTransform != (Object)null) ? val.playerTransform.position : Vector3.zero); int num = 0; int num2 = 0; RaycastHit val3 = default(RaycastHit); while (num < total && num2 < 600) { num2++; float num3 = Random.Range(0f, (float)Math.PI * 2f); float num4 = Random.Range(4f, 25f); if (Physics.Raycast(val2 + new Vector3(Mathf.Cos(num3) * num4, Random.Range(2f, 9f), Mathf.Sin(num3) * num4), Vector3.down, ref val3, 22f, -1, (QueryTriggerInteraction)1) && !(Vector3.Distance(((RaycastHit)(ref val3)).point, val2) < 3.5f)) { GameObject val4 = GoldenApple.Build(); val4.transform.position = ((RaycastHit)(ref val3)).point + Vector3.up * 0.35f; Alive.Add(val4); num++; } } } private static bool TryPlace(Vector3 origin) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0024: Unknown result type (might be due to invalid IL or missing references) //IL_0029: Unknown result type (might be due to invalid IL or missing references) //IL_0033: Unknown result type (might be due to invalid IL or missing references) //IL_0038: Unknown result type (might be due to invalid IL or missing references) RaycastHit val = default(RaycastHit); if (Physics.Raycast(origin, Vector3.down, ref val, 22f, -1, (QueryTriggerInteraction)1)) { GameObject val2 = GoldenApple.Build(); val2.transform.position = ((RaycastHit)(ref val)).point + Vector3.up * 0.35f; Alive.Add(val2); return true; } return false; } public static void NotifyEaten() { } private static void ClearAll() { foreach (GameObject item in Alive) { if ((Object)(object)item != (Object)null) { Object.Destroy((Object)(object)item); } } Alive.Clear(); } } public class GoldenApple : MonoBehaviour { public const float EatRadius = 0.8f; private float spinTimer; private Vector3 basePos; private bool eaten; private static Material MakeMat(Color color, Color emission, float emissionStrength) { //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_001e: Expected O, but got Unknown //IL_001f: Unknown result type (might be due to invalid IL or missing references) //IL_0043: Unknown result type (might be due to invalid IL or missing references) //IL_0045: Unknown result type (might be due to invalid IL or missing references) Material val = new Material(Shader.Find("Standard") ?? Shader.Find("Diffuse")); val.color = color; if (val.HasProperty("_EmissionColor")) { val.EnableKeyword("_EMISSION"); val.SetColor("_EmissionColor", emission * emissionStrength); } return val; } public static GameObject Build() { //IL_0005: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Expected O, but got Unknown //IL_0043: Unknown result type (might be due to invalid IL or missing references) //IL_0062: Unknown result type (might be due to invalid IL or missing references) //IL_0076: Unknown result type (might be due to invalid IL or missing references) //IL_00cc: Unknown result type (might be due to invalid IL or missing references) //IL_00eb: Unknown result type (might be due to invalid IL or missing references) //IL_010a: Unknown result type (might be due to invalid IL or missing references) //IL_0129: Unknown result type (might be due to invalid IL or missing references) //IL_012e: Unknown result type (might be due to invalid IL or missing references) //IL_0184: Unknown result type (might be due to invalid IL or missing references) //IL_01a3: Unknown result type (might be due to invalid IL or missing references) //IL_01c2: Unknown result type (might be due to invalid IL or missing references) //IL_01e1: Unknown result type (might be due to invalid IL or missing references) //IL_01f5: Unknown result type (might be due to invalid IL or missing references) GameObject val = new GameObject("EnchantedGoldenApple"); GameObject obj = GameObject.CreatePrimitive((PrimitiveType)0); ((Object)obj).name = "Body"; obj.transform.SetParent(val.transform, false); obj.transform.localScale = new Vector3(0.3f, 0.26f, 0.3f); ((Renderer)obj.GetComponent<MeshRenderer>()).material = MakeMat(new Color(1f, 0.78f, 0.18f), new Color(1f, 0.6f, 0.05f), 0.45f); Object.Destroy((Object)(object)obj.GetComponent<Collider>()); GameObject obj2 = GameObject.CreatePrimitive((PrimitiveType)2); ((Object)obj2).name = "Stem"; obj2.transform.SetParent(val.transform, false); obj2.transform.localScale = new Vector3(0.035f, 0.13f, 0.035f); obj2.transform.localPosition = new Vector3(0.03f, 0.28f, 0f); obj2.transform.localRotation = Quaternion.Euler(-12f, 0f, 6f); ((Renderer)obj2.GetComponent<MeshRenderer>()).material = MakeMat(new Color(0.45f, 0.28f, 0.12f), Color.black, 0f); Object.Destroy((Object)(object)obj2.GetComponent<Collider>()); GameObject obj3 = GameObject.CreatePrimitive((PrimitiveType)3); ((Object)obj3).name = "Leaf"; obj3.transform.SetParent(val.transform, false); obj3.transform.localScale = new Vector3(0.16f, 0.02f, 0.09f); obj3.transform.localPosition = new Vector3(0.16f, 0.3f, 0f); obj3.transform.localRotation = Quaternion.Euler(0f, 35f, 18f); ((Renderer)obj3.GetComponent<MeshRenderer>()).material = MakeMat(new Color(0.25f, 0.62f, 0.28f), new Color(0.1f, 0.3f, 0.1f), 0.15f); Object.Destroy((Object)(object)obj3.GetComponent<Collider>()); SphereCollider obj4 = val.AddComponent<SphereCollider>(); ((Collider)obj4).isTrigger = true; obj4.radius = 0.8f; val.AddComponent<GoldenApple>(); return val; } private void Start() { //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_000c: 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) basePos = ((Component)this).transform.position; Light obj = ((Component)this).gameObject.AddComponent<Light>(); obj.color = new Color(1f, 0.75f, 0.25f); obj.intensity = 0.6f; obj.range = 3f; obj.shadows = (LightShadows)0; } private void Update() { //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001e: Unknown result type (might be due to invalid IL or missing references) //IL_0040: Unknown result type (might be due to invalid IL or missing references) //IL_0045: Unknown result type (might be due to invalid IL or missing references) //IL_007c: Unknown result type (might be due to invalid IL or missing references) //IL_0087: Unknown result type (might be due to invalid IL or missing references) spinTimer += Time.deltaTime; ((Component)this).transform.position = basePos + Vector3.up * (0.06f + Mathf.Sin(spinTimer * 1.6f) * 0.04f); if (!eaten) { PlayerAvatar val = SemiFunc.PlayerAvatarLocal(); if (!((Object)(object)val == (Object)null) && !((Object)(object)val.playerTransform == (Object)null) && Vector3.Distance(val.playerTransform.position, ((Component)this).transform.position) <= 0.8f) { eaten = true; Eat(val); } } } private void Eat(PlayerAvatar local) { ShieldManager.AddShield(local.playerHealth, 50); SpeedBoost.Activate(local); Object.Destroy((Object)(object)((Component)this).gameObject); AppleSpawner.NotifyEaten(); } } public static class SpeedBoost { public const float Duration = 5f; public const float Multiplier = 2f; private static float _until; private static PlayerController _pc; private static float _origDrain; private static bool _saved; public static void Activate(PlayerAvatar avatar) { _pc = PlayerController.instance; if (!((Object)(object)_pc == (Object)null)) { if (!_saved) { _origDrain = _pc.EnergySprintDrain; _saved = true; } _pc.OverrideSpeed(2f, 5f); _until = Time.time + 5f; } } public static void Tick() { if ((Object)(object)_pc == (Object)null) { return; } if (Time.time >= _until) { if (_saved) { _pc.EnergySprintDrain = _origDrain; _saved = false; } } else { _pc.EnergySprintDrain = 0f; _pc.EnergyCurrent = _pc.EnergyStart; } } } }