Skip to content

Commit

Permalink
chore: Update GiftPatch, minor changes in code
Browse files Browse the repository at this point in the history
Release-As: 1.0.1
  • Loading branch information
Hypick122 committed Jan 25, 2024
1 parent 2c22972 commit 6ed26df
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 105 deletions.
2 changes: 0 additions & 2 deletions ExplosivePresents.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExplosivePresents", "ExplosivePresents\ExplosivePresents.csproj", "{2DC60A9D-F44B-4B06-8A37-9B05D6D2CAC8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{CF5E8FBA-B7B1-4EBA-A3D9-F3AFBE869CB1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
4 changes: 4 additions & 0 deletions ExplosivePresents/ExplosivePresents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>

<AssemblyName>Hypick.ExplosivePresents</AssemblyName>
<Product>ExplosivePresents</Product>
<Version>1.0.1</Version>

<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>

Expand Down
76 changes: 69 additions & 7 deletions ExplosivePresents/Patches/GiftBoxItemPatch.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections;
using System.Reflection;
using GameNetcodeStuff;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;
using Random = System.Random;

namespace Hypick.Patches;

Expand All @@ -21,15 +23,75 @@ static IEnumerator DelayedExplosion(GiftBoxItem __instance, bool effect, float k
}

[HarmonyPatch("OpenGiftBoxServerRpc")]
[HarmonyPostfix]
static void OpenGiftBox(GiftBoxItem __instance)
[HarmonyPrefix]
public static bool OpenGiftBox(GiftBoxItem __instance)
{
Random random = new Random();

if (random.Next(0, 100) <= Plugin.Config.SpawnChance)
if (Mathf.Clamp(Plugin.Config.SpawnChance, 0f, 100f) / 100f > Random.Range(0f, 0.99f))
{
__instance.StartCoroutine(DelayedExplosion(__instance, true, Plugin.Config.KillRange, Plugin.Config.DamageRange, Plugin.Config.Delay));
return OpenGiftBoxCustom(__instance, true);
}
return OpenGiftBoxCustom(__instance);
}

private static bool OpenGiftBoxCustom(GiftBoxItem __instance, bool explosive = false)
{
NetworkManager networkManager = __instance.NetworkManager;
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))
{
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]);
}
if ((RpcExecStage)__instance.GetType().GetField("__rpc_exec_stage", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance) != RpcExecStage.Server || (!networkManager.IsClient && !networkManager.IsHost))
{
return false;
}

if (!explosive) {
GameObject gameObject = null;
int presentValue = 0;
Vector3 vector = Vector3.zero;
GameObject objectInPresent = (GameObject)__instance.GetType().GetField("objectInPresent", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance);

if (objectInPresent == null)
{
Debug.LogError("Error: There is no object in gift box!");
}
else
{
Transform parent = ((((!(__instance.playerHeldBy != null) || !__instance.playerHeldBy.isInElevator) && !StartOfRound.Instance.inShipPhase) || !(RoundManager.Instance.spawnedScrapContainer != null)) ? StartOfRound.Instance.elevatorTransform : RoundManager.Instance.spawnedScrapContainer);
vector = __instance.transform.position + Vector3.up * 0.25f;
gameObject = Object.Instantiate(objectInPresent, vector, Quaternion.identity, parent);
GrabbableObject component = gameObject.GetComponent<GrabbableObject>();
PlayerControllerB previousPlayerHeldBy = (PlayerControllerB)__instance.GetType().GetField("previousPlayerHeldBy", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance);
component.startFallingPosition = vector;
__instance.StartCoroutine(__instance.SetObjectToHitGroundSFX(component));
component.targetFloorPosition = component.GetItemFloorPosition(__instance.transform.position);
if (previousPlayerHeldBy != null && previousPlayerHeldBy.isInHangarShipRoom)
previousPlayerHeldBy.SetItemInElevator(droppedInShipRoom: true, droppedInElevator: true, component);
presentValue = (int)__instance.GetType().GetField("objectInPresentValue", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance);
component.SetScrapValue(presentValue);
component.NetworkObject.Spawn();
}
if (gameObject != null)
{
__instance.OpenGiftBoxClientRpc(gameObject.GetComponent<NetworkObject>(), presentValue, vector);
}
}
__instance.OpenGiftBoxNoPresentClientRpc();
return false;
}

private enum RpcExecStage
{
None,
Server,
Client
}
}

Expand All @@ -49,4 +111,4 @@ static void OpenGiftBox(GiftBoxItem __instance)
// gameObject.GetComponent<NetworkObject>().Spawn(false);
// }
// }
// }
// }
5 changes: 0 additions & 5 deletions ExplosivePresents/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Hypick.Services;

namespace Hypick;

Expand All @@ -16,8 +15,6 @@ public class Plugin : BaseUnityPlugin

private readonly Harmony _harmony = new(PluginInfo.PLUGIN_GUID);

public TemplateService Service;

public Plugin()
{
Instance = this;
Expand All @@ -26,8 +23,6 @@ public Plugin()
private void Awake()
{
Config = new PluginConfig(base.Config);

Service = new TemplateService();

Log.LogInfo($"Applying patches...");
_harmony.PatchAll();
Expand Down
14 changes: 7 additions & 7 deletions ExplosivePresents/PluginConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ namespace Hypick;

public static class Categories
{
public const string Present = "Present";
public const string Present = nameof(Present);
}

public class PluginConfig
{
public int SpawnChance;
public float SpawnChance;
public float KillRange;
public float DamageRange;
public float Delay;

public PluginConfig(ConfigFile cfg)
{
SpawnChance = cfg.Bind<int>(Categories.Present, "spawnChance", 10, "Chance of the present exploding").Value;
KillRange = cfg.Bind<float>(Categories.Present, "killRange", 10f, "Explosion kill range").Value;
DamageRange = cfg.Bind<float>(Categories.Present, "damageRange", 10f, "Explosion damage range").Value;
Delay = cfg.Bind<float>(Categories.Present, "delay", 0.5f, "Delay before explosion. In seconds").Value;
SpawnChance = cfg.Bind<float>(Categories.Present, nameof(SpawnChance), 10f, new ConfigDescription("Chance of the present exploding", new AcceptableValueRange<float>(0f, 100f))).Value;
KillRange = cfg.Bind<float>(Categories.Present, nameof(KillRange), 5.7f, "Explosion kill range").Value;
DamageRange = cfg.Bind<float>(Categories.Present, nameof(DamageRange), 6.4f, "Explosion damage range").Value;
Delay = cfg.Bind<float>(Categories.Present, nameof(Delay), 0.5f, "Delay before explosion. In seconds").Value;
}
}
}
14 changes: 0 additions & 14 deletions ExplosivePresents/Services/TemplateService.cs

This file was deleted.

20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,36 @@
<details>
<summary><strong>English</strong></summary>

This mod copies and improves some aspects of the [ExplosiveUnboxing](https://thunderstore.io/c/lethal-company/p/Nebulaetrix/ExplosiveUnboxing/) mod by Nebulaetrix, which was marked as outdated by it.

Chance of explosion, range, etc. can be configured in the config ```Hypick.ExplosivePresents.cfg```
ExplosivePresents

This mod copies and improves some aspects of the [ExplosiveUnboxing](https://thunderstore.io/c/lethal-company/p/Nebulaetrix/ExplosiveUnboxing/) mod by Nebulaetrix, which was marked as outdated by it.
## Known issues

- Due to changes in the gift code, this mod is not compatible with other mods that change the gift code
- Gift explosion cannot explode all players

</details>

<details>
<summary><strong>Русский</strong></summary>

Этот мод копирует и улучшает некоторые аспекты мода [ExplosiveUnboxing](https://thunderstore.io/c/lethal-company/p/Nebulaetrix/ExplosiveUnboxing/) от Nebulaetrix, который был помечен им как устаревшив.

Шанс взрыв, дальность поражения и т.д. можно настроить в конфиге ```Hypick.ExplosivePresents.cfg```

Этот мод копирует и улучшает некоторые аспекты мода [ExplosiveUnboxing](https://thunderstore.io/c/lethal-company/p/Nebulaetrix/ExplosiveUnboxing/) от Nebulaetrix, который был помечен им как устаревшив.
## Известные проблемы

- Из-за измнений в коде подарка, этот мод не совместим с другими модами, которые меняют код подарка
- Взрыв подарка не может взорвать всех игроков

</details>

## Changelog

The list of changes can be found on the [Github releases page](https://github.com/Hypick122/ExplosivePresents)
## [1.0.1] - 25.01.2024 | Current version

- The config has been slightly updated, I advise you to rewrite it completely
- Fixed an issue where an item would drop out along with a mine
- The code has been rewritten, so there may be problems with other mods for the gift
40 changes: 0 additions & 40 deletions Test/Services/TemplateServiceTest.cs

This file was deleted.

26 changes: 0 additions & 26 deletions Test/Test.csproj

This file was deleted.

2 changes: 1 addition & 1 deletion thunderstore.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ readme = "README.md"
outdir = "build"

[[build.copy]]
source = "ExplosivePresents/bin/ExplosivePresents/ExplosivePresents.dll"
source = "ExplosivePresents/bin/ExplosivePresents/Hypick.ExplosivePresents.dll"
target = "ExplosivePresents.dll"

[[build.copy]]
Expand Down

0 comments on commit 6ed26df

Please sign in to comment.