Decompiled source of CruiserSkinRevived v1.0.3

CruiserSkinRevived.dll

Decompiled 6 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using LethalNetworkAPI;
using LethalNetworkAPI.Utils;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("CruiserSkinRevived")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CruiserSkinRevived")]
[assembly: AssemblyTitle("CruiserSkinRevived")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CruiserSkinRevived;

[BepInPlugin("Fitrusielle.CruiserSkinRevived", "CruiserSkinRevived", "1.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class CSR : BaseUnityPlugin
{
	public static Dictionary<Texture2D, Texture2D> skins = new Dictionary<Texture2D, Texture2D>();

	public static Material cruiserMaterial = null;

	public static Material cruiserDestroyedMaterial = null;

	public static ManualLogSource Log;

	public static ConfigEntry<bool> randomWithoutRepeat;

	private void Awake()
	{
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		cruiserMaterial = null;
		cruiserDestroyedMaterial = null;
		randomWithoutRepeat = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "NoRepeats", true, "If true, skins are chosen randomly without repeating twice.");
		LoadSkins(Paths.PluginPath);
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Skins count: " + skins.Count));
		if (skins.Count == 0)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Not applying patch because skins count is 0.");
		}
		else
		{
			new Harmony("Fitrusielle.CruiserSkinRevived").PatchAll(typeof(Patch));
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)"CSR successfully Loaded.");
	}

	private void LoadSkins(string path)
	{
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Expected O, but got Unknown
		//IL_005a: Expected O, but got Unknown
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Expected O, but got Unknown
		//IL_0080: Expected O, but got Unknown
		string[] directories = Directory.GetDirectories(path);
		foreach (string path2 in directories)
		{
			LoadSkins(path2);
		}
		string[] files = Directory.GetFiles(path);
		Texture2D val = null;
		Texture2D val2 = null;
		directories = files;
		foreach (string text in directories)
		{
			if (text.EndsWith("cruiser.png"))
			{
				Texture2D val3 = new Texture2D(2, 2);
				ImageConversion.LoadImage(val3, File.ReadAllBytes(text));
				val = val3;
			}
			else if (text.EndsWith("cruiser_destroyed.png"))
			{
				Texture2D val4 = new Texture2D(2, 2);
				ImageConversion.LoadImage(val4, File.ReadAllBytes(text));
				val2 = val4;
			}
			if ((Object)(object)val != (Object)null && (Object)(object)val2 != (Object)null)
			{
				skins.Add(val, val2);
				val = null;
				val2 = null;
			}
		}
	}
}
internal class Patch
{
	public static int skinLocal = 0;

	public static LNetworkMessage<int> skinMessage;

	private static Random random;

	[HarmonyPatch(typeof(StartOfRound), "StartGame")]
	[HarmonyPrefix]
	private static void StartGame(StartOfRound __instance)
	{
		if ((Object)(object)CSR.cruiserMaterial == (Object)null || (Object)(object)CSR.cruiserDestroyedMaterial == (Object)null)
		{
			VehicleController[] array = Object.FindObjectsByType<VehicleController>((FindObjectsSortMode)0);
			foreach (VehicleController val in array)
			{
				if (val.vehicleID == 0)
				{
					LoadCruiserMaterials(val);
				}
			}
			if ((Object)(object)CSR.cruiserMaterial == (Object)null || (Object)(object)CSR.cruiserDestroyedMaterial == (Object)null)
			{
				return;
			}
		}
		if (LNetworkUtils.IsHostOrServer)
		{
			skinLocal = getRandomSkin();
			CSR.Log.LogInfo((object)("(Server) skinLocal is " + skinLocal + ". Sending it to clients."));
			skinMessage.SendClients(skinLocal);
		}
	}

	[HarmonyPatch(typeof(VehicleController), "Start")]
	[HarmonyPostfix]
	private static void Start(VehicleController __instance)
	{
		if (__instance.vehicleID == 0)
		{
			LoadCruiserMaterials(__instance);
			if (LNetworkUtils.IsHostOrServer)
			{
				CSR.Log.LogInfo((object)"(Server) VehicleController started. Sending skinLocal to clients.");
				skinMessage.SendClients(skinLocal);
			}
			else
			{
				CSR.Log.LogInfo((object)"(Client) VehicleController started. Sending ping to server.");
				skinMessage.SendServer(0);
			}
		}
	}

	private static void LoadCruiserMaterials(VehicleController vc)
	{
		if ((Object)(object)CSR.cruiserMaterial != (Object)null && (Object)(object)CSR.cruiserMaterial != (Object)null)
		{
			return;
		}
		MeshRenderer componentInChildren = ((Component)vc).GetComponentInChildren<MeshRenderer>();
		if ((Object)(object)componentInChildren != (Object)null && (Object)(object)((Renderer)componentInChildren).sharedMaterial != (Object)null && ((Object)((Renderer)componentInChildren).sharedMaterial).name.StartsWith("TruckTexture"))
		{
			CSR.cruiserMaterial = ((Renderer)componentInChildren).sharedMaterial;
			CSR.Log.LogInfo((object)"(Client) Default cruiser material is loaded.");
		}
		GameObject[] array = Object.FindObjectsByType<GameObject>((FindObjectsInactive)1, (FindObjectsSortMode)0);
		foreach (GameObject val in array)
		{
			if (((Object)val).name.Contains("MainBodyDestroyed") && ((Object)((Renderer)val.GetComponent<MeshRenderer>()).sharedMaterial).name.StartsWith("TruckTextureDestroyed"))
			{
				CSR.cruiserDestroyedMaterial = ((Renderer)val.GetComponent<MeshRenderer>()).sharedMaterial;
				CSR.Log.LogInfo((object)"(Client) Destroyed cruiser material is loaded.");
			}
		}
		if ((Object)(object)CSR.cruiserMaterial == (Object)null || (Object)(object)CSR.cruiserDestroyedMaterial == (Object)null)
		{
			CSR.Log.LogWarning((object)"(Client) For some reason one of cruiser materials failed to load. Skins will not work.");
		}
	}

	private static void OnSkinRecievedServer(int skin, ulong player)
	{
		CSR.Log.LogInfo((object)("(Server) Recieved ping from client. Sending host skin back: " + skinLocal));
		skinMessage.SendClient(skinLocal, player);
	}

	private static void OnSkinRecievedClient(int skin)
	{
		CSR.Log.LogInfo((object)("(Client) Recieved skin index from server: " + skin));
		skinLocal = skin;
		SetSkin(skinLocal);
	}

	private static void SetSkin(int index)
	{
		if ((Object)(object)CSR.cruiserMaterial == (Object)null || (Object)(object)CSR.cruiserDestroyedMaterial == (Object)null)
		{
			CSR.Log.LogError((object)"(Client) Cannot set cruiser skin. Cruiser material is not loaded.");
			return;
		}
		CSR.cruiserMaterial.mainTexture = (Texture)(object)CSR.skins.ElementAt(index).Key;
		CSR.cruiserDestroyedMaterial.mainTexture = (Texture)(object)CSR.skins.ElementAt(index).Value;
		CSR.Log.LogInfo((object)("(Client) Cruiser skin set to " + index));
	}

	private static int getRandomSkin()
	{
		if (CSR.skins.Count != 1)
		{
			if (!CSR.randomWithoutRepeat.Value)
			{
				return random.Next(0, CSR.skins.Count);
			}
			return getRandomIntNoRepeatRecursive();
		}
		return 0;
	}

	private static int getRandomIntNoRepeatRecursive()
	{
		int num = random.Next(0, CSR.skins.Count);
		if (num == skinLocal)
		{
			return getRandomIntNoRepeatRecursive();
		}
		return num;
	}

	static Patch()
	{
		Action<int> action = OnSkinRecievedClient;
		skinMessage = LNetworkMessage<int>.Connect("CSR.skinMessage", (Action<int, ulong>)OnSkinRecievedServer, action, (Action<int, ulong>)null);
		random = new Random();
	}
}