Decompiled source of ShockingCompany v0.2.0

ShockingCompany.dll

Decompiled a year ago
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("LethalShock")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LethalShock")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("cd28d4f9-fcca-4592-bc84-5c978885d93c")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LethalShock
{
	[BepInPlugin("MrdTika.LethalShock", "Lethal Shock", "0.1.4")]
	public class LethalShockBase : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(PlayerControllerB))]
		internal class CheckPlayer
		{
			private static int storedDamageNumber;

			[HarmonyPostfix]
			[HarmonyPatch("DamagePlayer")]
			private static void DamagePlayerPostfix(PlayerControllerB __instance, int damageNumber)
			{
				int health = __instance.health;
				storedDamageNumber = damageNumber;
				Logger.LogInfo((object)$"Lol is this our script? {damageNumber} damage: {health}");
				Logger.LogInfo((object)$"Health Difference: {damageNumber}");
				if (damageNumber != 0 || Instance.previousHealth == -1)
				{
					Instance.Intensity = damageNumber;
					try
					{
						Instance.CallApiAsync(damageNumber, 1, 0);
						Logger.LogInfo((object)$"Player Zapped with value {damageNumber}");
					}
					catch (Exception ex)
					{
						Logger.LogInfo((object)("Error in CallApiAsync: " + ex.Message));
					}
				}
				Instance.previousHealth = health;
			}

			[HarmonyPostfix]
			[HarmonyPatch("KillPlayer")]
			private static void KillPlayerPostFix(PlayerControllerB __instance)
			{
				if (storedDamageNumber != 0)
				{
					Logger.LogError((object)"Cannot double shock for user safety!");
				}
				else
				{
					Logger.LogInfo((object)"Player Killed by Unkown Cause!");
					Instance.CallApiAsync(100, 1, 0);
					Logger.LogInfo((object)"Player Zapped with no mercy, player was killed");
				}
				if (storedDamageNumber != 0 || Instance.previousHealth == -1)
				{
					Logger.LogInfo((object)"Player Killed by Unkown Cause!");
					Instance.CallApiAsync(100, 1, 0);
					Logger.LogInfo((object)"Player Zapped with no mercy, player was killed");
				}
			}

			private static bool IsPlayerValid(PlayerControllerB player)
			{
				if (IsChildOfPlayersContainer(((Component)player).gameObject))
				{
					return false;
				}
				NetworkObject component = ((Component)player).gameObject.GetComponent<NetworkObject>();
				if ((Object)(object)component != (Object)null)
				{
					return component.IsOwner;
				}
				return false;
			}

			private static bool IsChildOfPlayersContainer(GameObject gameObject)
			{
				Transform parent = gameObject.transform.parent;
				while ((Object)(object)parent != (Object)null)
				{
					if (((Object)parent).name == "PlayersContainer")
					{
						return true;
					}
					parent = parent.parent;
				}
				return false;
			}
		}

		private const string modGUID = "MrdTika.LethalShock";

		private const string modName = "Lethal Shock";

		private const string modVersion = "0.1.4";

		private readonly Harmony harmony = new Harmony("MrdTika.LethalShock");

		private static LethalShockBase Instance;

		private const string Name = "Lethal_Shock_CMD";

		public int ShockerMode;

		public int Intensity;

		public int Duration;

		private static ConfigEntry<string> Username;

		private static ConfigEntry<string> ApiKey;

		private static ConfigEntry<string> Code;

		private int previousHealth = -1;

		internal static ConfigFile Config { get; set; }

		public static ManualLogSource Logger { get; private set; }

		private async void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
				Logger = ((BaseUnityPlugin)this).Logger;
			}
			Config = new ConfigFile(Paths.ConfigPath + "\\MrdTika.LethalShock.cfg", true);
			Username = Config.Bind<string>("Settings", "Username", "JohnDoe", "Your username");
			ApiKey = Config.Bind<string>("Settings", "ApiKey", "5c678926-d19e-4f86-42ad-21f5a76126db", "Your API key");
			Code = Config.Bind<string>("Settings", "Code", "17519CD8GAP", "Your share code");
			ShockerMode = 0;
			Intensity = 100;
			Duration = 5;
			harmony.PatchAll(typeof(LethalShockBase));
			harmony.PatchAll(typeof(CheckPlayer));
			Logger.LogInfo((object)"Mod attached to a player object. Hierarchy:");
			LogHierarchy(((Component)this).gameObject);
			Logger.LogInfo((object)"Hello there:");
			Logger.LogInfo((object)Username.Value);
			await CallApiAsync(100, 3, 2);
			Logger.LogInfo((object)"Please check if it beeped, if not and there is an error check config files.");
		}

		private void LogHierarchy(GameObject obj, int depth = 0)
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			if ((Object)(object)obj == (Object)null)
			{
				return;
			}
			Logger.LogInfo((object)(new string('-', depth) + " " + ((Object)obj).name));
			foreach (Transform item in obj.transform)
			{
				Transform val = item;
				LogHierarchy(((Component)val).gameObject, depth + 1);
			}
		}

		private async Task CallApiAsync(int intensity, int duration, int mode)
		{
			HttpClient client = new HttpClient();
			try
			{
				StringContent val = new StringContent(string.Format("{{\"Username\":\"{0}\",\"Name\":\"{1}\",\"Code\":\"{2}\",\"Intensity\":\"{3}\",\"Duration\":\"{4}\",\"Apikey\":\"{5}\",\"Op\":\"{6}\"}}", Username.Value, "Lethal_Shock_CMD", Code.Value, intensity, duration, ApiKey.Value, mode), Encoding.UTF8, "application/json");
				try
				{
					HttpResponseMessage val2 = await client.PostAsync("https://do.pishock.com/api/apioperate", (HttpContent)(object)val);
					if (val2.IsSuccessStatusCode)
					{
						string text = await val2.Content.ReadAsStringAsync();
						Logger.LogInfo((object)("API call successful. Response: " + text));
					}
					else
					{
						Logger.LogError((object)$"API call failed with status code: {val2.StatusCode}");
					}
				}
				catch (Exception ex)
				{
					Logger.LogError((object)("Error: " + ex.Message));
				}
			}
			finally
			{
				((IDisposable)client)?.Dispose();
			}
		}
	}
}
namespace LethalShock.Patches
{
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class CheckPlayer
	{
		private static int previousHealth;

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void healthCheck(ref int ___health)
		{
			if (___health < previousHealth)
			{
				_ = previousHealth;
			}
			previousHealth = ___health;
		}
	}
}