diff --git a/ExplosivePresents/NetworkHandler.cs b/ExplosivePresents/NetworkHandler.cs index 43e018d..b023a24 100644 --- a/ExplosivePresents/NetworkHandler.cs +++ b/ExplosivePresents/NetworkHandler.cs @@ -16,7 +16,9 @@ public void CustomExplodeMineServerRpc(Vector3 explosionPosition) if (networkManager == null || !networkManager.IsListening) return; - if (__rpc_exec_stage != __RpcExecStage.Server && (networkManager.IsClient || networkManager.IsHost)) { } + if (__rpc_exec_stage != __RpcExecStage.Server && (networkManager.IsClient || networkManager.IsHost)) + { + } if (__rpc_exec_stage == __RpcExecStage.Server && (networkManager.IsServer || networkManager.IsHost)) { @@ -31,15 +33,19 @@ public void CustomExplodeMineClientRpc(Vector3 explosionPosition) if (networkManager == null || !networkManager.IsListening) return; - if (__rpc_exec_stage != __RpcExecStage.Client && (networkManager.IsServer || networkManager.IsHost)) { } + if (__rpc_exec_stage != __RpcExecStage.Client && (networkManager.IsServer || networkManager.IsHost)) + { + } if (__rpc_exec_stage == __RpcExecStage.Client && (networkManager.IsClient || networkManager.IsHost)) { - base.StartCoroutine(DelayedExplosion(explosionPosition, true, Plugin.Config.KillRange, Plugin.Config.DamageRange, Plugin.Config.Delay)); + base.StartCoroutine(DelayedExplosion(explosionPosition, true, Plugin.Config.KillRange, + Plugin.Config.DamageRange, Plugin.Config.Delay)); } } - static IEnumerator DelayedExplosion(Vector3 explosionPosition, bool spawnExplosionEffect = false, float killRange = 1f, float damageRange = 1f, float Delay = 1) + private static IEnumerator DelayedExplosion(Vector3 explosionPosition, bool spawnExplosionEffect = false, + float killRange = 1f, float damageRange = 1f, float Delay = 1) { Landmine landmine = Resources.FindObjectsOfTypeAll()[0]; @@ -56,7 +62,8 @@ static IEnumerator DelayedExplosion(Vector3 explosionPosition, bool spawnExplosi yield return new WaitForSeconds(Delay); mineAudio.GetComponent().PlayOneShot(landmine.mineDetonate, 1f); - Landmine.SpawnExplosion(explosionPosition + Vector3.up, spawnExplosionEffect: spawnExplosionEffect, killRange, damageRange); + Landmine.SpawnExplosion(explosionPosition + Vector3.up, spawnExplosionEffect: spawnExplosionEffect, killRange, + damageRange); // } } @@ -67,6 +74,7 @@ public override void OnNetworkSpawn() if (Instance == null) Instance = this; + base.OnNetworkSpawn(); } -} +} \ No newline at end of file diff --git a/ExplosivePresents/Patches/GiftBoxItemPatch.cs b/ExplosivePresents/Patches/GiftBoxItemPatch.cs index fd059f5..5822d0e 100644 --- a/ExplosivePresents/Patches/GiftBoxItemPatch.cs +++ b/ExplosivePresents/Patches/GiftBoxItemPatch.cs @@ -11,14 +11,13 @@ internal class GiftBoxItemPatch { [HarmonyPatch("OpenGiftBoxServerRpc")] [HarmonyPrefix] - static bool OpenGiftBox(GiftBoxItem __instance) + public static bool OpenGiftBox(GiftBoxItem __instance) { - if (Mathf.Clamp(Plugin.Config.SpawnChance, 0f, 100f) / 100f > Random.Range(0f, 0.99f)) - { - NetworkHandler.Instance.CustomExplodeMineServerRpc(__instance.transform.position); - return OpenGiftBoxCustom(__instance, true); - } - return OpenGiftBoxCustom(__instance); + if (!(Mathf.Clamp(Plugin.Config.SpawnChance, 0f, 100f) / 100f > Random.Range(0f, 0.99f))) + return OpenGiftBoxCustom(__instance); + + NetworkHandler.Instance.CustomExplodeMineServerRpc(__instance.transform.position); + return OpenGiftBoxCustom(__instance, true); } private static bool OpenGiftBoxCustom(GiftBoxItem __instance, bool explosive = false) @@ -27,35 +26,42 @@ private static bool OpenGiftBoxCustom(GiftBoxItem __instance, bool explosive = f if (networkManager == null || !networkManager.IsListening) return false; - if ((RpcExecStage)__instance.GetType().GetField("__rpc_exec_stage", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance) != RpcExecStage.Server && (networkManager.IsClient || networkManager.IsHost)) + if (GetPrivateField(__instance, "__rpc_exec_stage") != RpcExecStage.Server && + (networkManager.IsClient || networkManager.IsHost)) { ServerRpcParams serverRpcParams = new(); - FastBufferWriter bufferWriter = (FastBufferWriter)__instance.GetType().GetMethod("__beginSendServerRpc", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, [2878544999u, serverRpcParams, RpcDelivery.Reliable]); - __instance.GetType().GetMethod("__endSendServerRpc", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, [bufferWriter, 2878544999u, serverRpcParams, RpcDelivery.Reliable]); + + FastBufferWriter bufferWriter = (FastBufferWriter)GetPrivateMethod(__instance, "__beginSendServerRpc", + [2878544999u, serverRpcParams, RpcDelivery.Reliable]); + GetPrivateMethod(__instance, "__endSendServerRpc", + [bufferWriter, 2878544999u, serverRpcParams, RpcDelivery.Reliable]); } - if ((RpcExecStage)__instance.GetType().GetField("__rpc_exec_stage", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance) != RpcExecStage.Server || (!networkManager.IsClient && !networkManager.IsHost)) + + if (GetPrivateField(__instance, "__rpc_exec_stage") != RpcExecStage.Server || + (!networkManager.IsClient && !networkManager.IsHost)) return false; if (explosive) { - Plugin.Log.LogInfo("OPEN GIFTBOX WTTH BOMB AHAHAAHAAH"); + Plugin.Log.LogInfo("Opening a gift with a bomb inside :)"); __instance.OpenGiftBoxNoPresentClientRpc(); return false; } - Plugin.Log.LogInfo("OPEN GIFTBOX without bomb :("); + Plugin.Log.LogInfo("Opening a gift without a bomb :("); GameObject gameObject = null; - int presentValue = 0; + var presentValue = 0; Vector3 vector = Vector3.zero; - GameObject objectInPresent = (GameObject)__instance.GetType().GetField("objectInPresent", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance); + GameObject objectInPresent = GetPrivateField(__instance, "objectInPresent"); if (objectInPresent == null) Debug.LogError("Error: There is no object in gift box!"); else { Transform parent; - if (((__instance.playerHeldBy != null && __instance.playerHeldBy.isInElevator) || StartOfRound.Instance.inShipPhase) && RoundManager.Instance.spawnedScrapContainer != null) + if (((__instance.playerHeldBy != null && __instance.playerHeldBy.isInElevator) || + StartOfRound.Instance.inShipPhase) && RoundManager.Instance.spawnedScrapContainer != null) parent = RoundManager.Instance.spawnedScrapContainer; else parent = StartOfRound.Instance.elevatorTransform; @@ -63,7 +69,8 @@ private static bool OpenGiftBoxCustom(GiftBoxItem __instance, bool explosive = f vector = __instance.transform.position + Vector3.up * 0.25f; gameObject = Object.Instantiate(objectInPresent, vector, Quaternion.identity, parent); GrabbableObject component = gameObject.GetComponent(); - PlayerControllerB previousPlayerHeldBy = (PlayerControllerB)__instance.GetType().GetField("previousPlayerHeldBy", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance); + PlayerControllerB previousPlayerHeldBy = + GetPrivateField(__instance, "previousPlayerHeldBy"); component.startFallingPosition = vector; __instance.StartCoroutine(__instance.SetObjectToHitGroundSFX(component)); @@ -71,7 +78,7 @@ private static bool OpenGiftBoxCustom(GiftBoxItem __instance, bool explosive = f if (previousPlayerHeldBy != null && previousPlayerHeldBy.isInHangarShipRoom) previousPlayerHeldBy.SetItemInElevator(droppedInShipRoom: true, droppedInElevator: true, component); - presentValue = (int)__instance.GetType().GetField("objectInPresentValue", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance); + presentValue = GetPrivateField(__instance, "objectInPresent"); component.SetScrapValue(presentValue); component.NetworkObject.Spawn(); } @@ -85,8 +92,22 @@ private static bool OpenGiftBoxCustom(GiftBoxItem __instance, bool explosive = f private enum RpcExecStage { - None, - Server, - Client + Server + } + + private static object GetPrivateMethod(object instance, string methodName, params object[] args) + { + const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance; + MethodInfo field = instance.GetType().GetMethod(methodName, bindingFlags); + + return field?.Invoke(instance, args); + } + + private static T GetPrivateField(object instance, string fieldName) + { + const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance; + FieldInfo field = instance.GetType().GetField(fieldName, bindingFlags); + + return (T)field?.GetValue(instance); } -} +} \ No newline at end of file diff --git a/ExplosivePresents/Patches/NetworkObjectManager.cs b/ExplosivePresents/Patches/NetworkObjectManager.cs index 265a7e9..bf78dc1 100644 --- a/ExplosivePresents/Patches/NetworkObjectManager.cs +++ b/ExplosivePresents/Patches/NetworkObjectManager.cs @@ -17,14 +17,14 @@ public static void Init() // var MainAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Assets/netcodemod")); var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Assets/netcodemod"); - AssetBundle MainAssetBundle; + AssetBundle mainAssetBundle; if (File.Exists(path)) - MainAssetBundle = AssetBundle.LoadFromFile(path); + mainAssetBundle = AssetBundle.LoadFromFile(path); else - MainAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "netcodemod")); + mainAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "netcodemod")); - Plugin.Log.LogInfo($"Loading NetworkManager Prefab: {MainAssetBundle}"); - networkPrefab = (GameObject)MainAssetBundle.LoadAsset("Assets/Necode/NetwordManager.prefab"); + Plugin.Log.LogInfo($"Loading NetworkManager Prefab: {mainAssetBundle}"); + networkPrefab = (GameObject)mainAssetBundle.LoadAsset("Assets/Necode/NetwordManager.prefab"); networkPrefab.AddComponent(); NetworkManager.Singleton.AddNetworkPrefab(networkPrefab); diff --git a/ExplosivePresents/Plugin.cs b/ExplosivePresents/Plugin.cs index d25a6be..946c3a5 100644 --- a/ExplosivePresents/Plugin.cs +++ b/ExplosivePresents/Plugin.cs @@ -13,11 +13,11 @@ namespace Hypick; [BepInIncompatibility("ExplodingPresents")] public class Plugin : BaseUnityPlugin { - public static Plugin Instance { get; set; } + private static Plugin Instance { get; set; } public static ManualLogSource Log => Instance.Logger; - public static new PluginConfig Config; + public new static PluginConfig Config; private readonly Harmony _harmony = new(PluginInfo.PLUGIN_GUID); @@ -45,11 +45,9 @@ private static void NetcodePatcher() foreach (var method in methods) { var attributes = method.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), false); - if (attributes.Length > 0) - { + if (attributes.Length > 0) method.Invoke(null, null); - } } } } -} +} \ No newline at end of file diff --git a/ExplosivePresents/PluginConfig.cs b/ExplosivePresents/PluginConfig.cs index 2a6e261..4e1340b 100644 --- a/ExplosivePresents/PluginConfig.cs +++ b/ExplosivePresents/PluginConfig.cs @@ -9,18 +9,19 @@ public static class Categories public class PluginConfig { - public bool ImmediateExplosion; - public float SpawnChance; - public float KillRange; - public float DamageRange; - public float Delay; + public bool ImmediateExplosion { get; } + public float SpawnChance { get; } + public float KillRange { get; } + public float DamageRange { get; } + public float Delay { get; } public PluginConfig(ConfigFile cfg) { ImmediateExplosion = cfg.Bind(Categories.Present, nameof(ImmediateExplosion), false, "").Value; - SpawnChance = cfg.Bind(Categories.Present, nameof(SpawnChance), 10f, new ConfigDescription("Chance of the present exploding", new AcceptableValueRange(0f, 100f))).Value; + SpawnChance = cfg.Bind(Categories.Present, nameof(SpawnChance), 10f, + new ConfigDescription("Chance of the present exploding", new AcceptableValueRange(0f, 100f))).Value; KillRange = cfg.Bind(Categories.Present, nameof(KillRange), 5.7f, "Explosion kill range").Value; DamageRange = cfg.Bind(Categories.Present, nameof(DamageRange), 6.4f, "Explosion damage range").Value; Delay = cfg.Bind(Categories.Present, nameof(Delay), 0.5f, "Delay before explosion. In seconds").Value; } -} +} \ No newline at end of file diff --git a/thunderstore.toml b/thunderstore.toml index 9dc3b63..1221372 100644 --- a/thunderstore.toml +++ b/thunderstore.toml @@ -22,7 +22,7 @@ outdir = "build" [[build.copy]] source = "ExplosivePresents/bin/Hypick.ExplosivePresents.dll" -target = "ExplosivePresents.dll" +target = "BepInEx/plugins/ExplosivePresents.dll" [[build.copy]] source = "CHANGELOG.md"