Decompiled source of WC3Reskin v1.1.0

WC3Reskin.dll

Decompiled 2 months 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 System.Security;
using System.Security.Permissions;
using ATS_API.Helpers;
using ATS_JSONLoader.Sounds;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Eremite;
using Eremite.Buildings;
using Eremite.Characters;
using Eremite.Characters.Behaviours;
using Eremite.Characters.Villagers;
using Eremite.Controller;
using Eremite.Model;
using Eremite.Model.Sound;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TinyJson;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: AssemblyCompany("WC3Reskin")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Reskins against the storm to look like warcraft 3")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("WC3Reskin")]
[assembly: AssemblyTitle("WC3Reskin")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 ATS_WC3Reskin
{
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "WC3Reskin";

		public const string PLUGIN_NAME = "WC3Reskin";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}
namespace JamesGames
{
	internal static class Configs
	{
		private static ConfigEntry<bool> buildingCompleteSound;

		private static ConfigEntry<bool> seasonChangeSounds;

		private static ConfigEntry<bool> treeFallSounds;

		private static ConfigEntry<bool> woodChopSounds;

		private static ConfigFile configFile;

		internal static bool PlayBuildingCompleteSound => buildingCompleteSound.Value;

		internal static bool PlaySeasonChangeSounds => seasonChangeSounds.Value;

		public static bool PlayTreeFallSounds => treeFallSounds.Value;

		public static bool PlayWoodChopSounds => woodChopSounds.Value;

		public static void InitializeConfigs(ConfigFile config)
		{
			configFile = config;
			buildingCompleteSound = config.Bind<bool>("Sounds", "Building Complete", true, "When set to true villagers will say when they completed a building.");
			seasonChangeSounds = config.Bind<bool>("Sounds", "Season Change", true, "When set to true sounds when season change will use wc3 sounds.");
			treeFallSounds = config.Bind<bool>("Sounds", "Tree Fall", true, "When set to true trees will sue wc3 sounds when they are depleted.");
			woodChopSounds = config.Bind<bool>("Sounds", "Wood Chop", true, "When set to true villagers will use wc3 sounds when chopping trees.");
		}
	}
	[HarmonyPatch]
	[BepInPlugin("WC3Reskin", "ATS_WC3Reskin", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		private const string GUID = "WC3Reskin";

		private const string NAME = "ATS_WC3Reskin";

		private const string VERSION = "1.1.0";

		public static ManualLogSource Log;

		private static Dictionary<string, SoundRef> soundCollections = new Dictionary<string, SoundRef>();

		private static HashSet<Building> completedBuildings = new HashSet<Building>();

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony.CreateAndPatchAll(typeof(Plugin).Assembly, "WC3Reskin");
			Configs.InitializeConfigs(((BaseUnityPlugin)this).Config);
			TryLoadSounds("harpyFinishBuildingSounds.json");
			TryLoadSounds("beaverFinishBuildingSounds.json");
			TryLoadSounds("foxesFinishBuildingSounds.json");
			TryLoadSounds("humanFinishBuildingSounds.json");
			TryLoadSounds("lizardFinishBuildingSounds.json");
			Log.LogInfo((object)"ATS_WC3Reskin v1.1.0 Plugin loaded");
		}

		[HarmonyPatch(typeof(MainController), "OnServicesReady")]
		[HarmonyPostfix]
		private static void HookMainControllerSetup()
		{
			ChangeSeasonSounds();
			ChangeWoodChopSounds();
			ChangeTreeFallSounds();
		}

		private static void ChangeTreeFallSounds()
		{
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Expected O, but got Unknown
			if (!Configs.PlayTreeFallSounds)
			{
				Log.LogInfo((object)"Tree fall sounds disabled");
				return;
			}
			foreach (NaturalResourceModel item in MB.Settings.NaturalResources.Where((NaturalResourceModel a) => (int)GoodsTypesExtensions.ToGoodsTypes(((Object)a.refGood).name) == 42))
			{
				if ((Object)(object)item == (Object)null)
				{
					Log.LogError((object)"Failed to find NaturalResourceModel for Mat_Raw_Wood");
					break;
				}
				List<SoundModel> list = new List<SoundModel>();
				for (int i = 1; i <= 3; i++)
				{
					AudioClip val = AudioHelpers.LoadAudioClip($"TreeFall{i}.wav");
					if ((Object)(object)val != (Object)null)
					{
						SoundModel val2 = new SoundModel();
						val2.audioClip = val;
						val2.volume = 0.5f;
						list.Add(val2);
					}
				}
				if (list.Count == 0)
				{
					Log.LogError((object)"Failed to load any tree fall sounds");
					break;
				}
				item.finalSound.sounds = list.ToArray();
			}
		}

		private static void ChangeWoodChopSounds()
		{
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Expected O, but got Unknown
			if (!Configs.PlayWoodChopSounds)
			{
				Log.LogInfo((object)"PlayWoodChopSounds is disabled");
				return;
			}
			foreach (NaturalResourceModel item in MB.Settings.NaturalResources.Where((NaturalResourceModel a) => (int)GoodsTypesExtensions.ToGoodsTypes(((Object)a.refGood).name) == 42))
			{
				if ((Object)(object)item == (Object)null)
				{
					Log.LogError((object)"Failed to find NaturalResourceModel for Mat_Raw_Wood");
					break;
				}
				List<SoundModel> list = new List<SoundModel>();
				for (int i = 1; i <= 4; i++)
				{
					AudioClip val = AudioHelpers.LoadAudioClip($"AxeMediumChopWood{i}.wav");
					if ((Object)(object)val != (Object)null)
					{
						SoundModel val2 = new SoundModel();
						val2.audioClip = val;
						list.Add(val2);
					}
				}
				if (list.Count == 0)
				{
					Log.LogError((object)"Failed to load any wood chop sounds");
					break;
				}
				item.gatheringSound.sounds = list.ToArray();
			}
		}

		private static void ChangeSeasonSounds()
		{
			if (Configs.PlaySeasonChangeSounds)
			{
				AudioClip val = AudioHelpers.LoadAudioClip("DuskWolf.wav");
				if ((Object)(object)val != (Object)null)
				{
					SoundModel soundForSeasonStart = MB.Sounds.GetSoundForSeasonStart((Season)2);
					soundForSeasonStart.audioClip = val;
				}
				else
				{
					Log.LogError((object)"Failed to load DuskWolf.wav");
				}
				AudioClip val2 = AudioHelpers.LoadAudioClip("DaybreakRooster.wav");
				if ((Object)(object)val2 != (Object)null)
				{
					SoundModel soundForSeasonStart2 = MB.Sounds.GetSoundForSeasonStart((Season)0);
					soundForSeasonStart2.audioClip = val2;
				}
				else
				{
					Log.LogError((object)"Failed to load DaybreakRooster.wav");
				}
			}
		}

		private void TryLoadSounds(string fileName)
		{
			string text = Directory.GetFiles(Paths.PluginPath, fileName, SearchOption.AllDirectories).FirstOrDefault();
			if (File.Exists(text))
			{
				ImportExportUtils.SetID(fileName);
				ImportExportUtils.SetDebugPath(text);
				string text2 = File.ReadAllText(text);
				SoundCollection val = JSONParser.FromJson<SoundCollection>(text2);
				if (soundCollections == null)
				{
					Log.LogError((object)("Failed to load sound collection: " + fileName));
					return;
				}
				if (val.sounds == null)
				{
					Log.LogError((object)("Failed to load sound collection: " + fileName));
					return;
				}
				Log.LogInfo((object)("Loading sound collection: " + fileName + " " + val.sounds.Count));
				SoundRef value = ScriptableObject.CreateInstance<SoundRef>();
				ImportExportUtils.ApplyValue<SoundRef, SoundCollection>(ref value, ref val, true, "TryLoadSounds", fileName);
				soundCollections.Add(fileName, value);
			}
			else
			{
				Log.LogError((object)("File not found: " + fileName));
			}
		}

		private static void TryPlaySounds(string fileName)
		{
			if (soundCollections.TryGetValue(fileName, out var value))
			{
				SoundModel next = value.GetNext();
				if (next != null)
				{
					MB.SoundsManager.PlaySoundEffect(next);
				}
			}
		}

		[HarmonyPatch(typeof(Construct), "OnEnd")]
		[HarmonyPrefix]
		private static void FinishConstruction(Construct __instance)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Invalid comparison between Unknown and I4
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Expected I4, but got Unknown
			if (!Configs.PlayBuildingCompleteSound)
			{
				return;
			}
			Building targetBuilding = __instance.GetTargetBuilding();
			BuildingTypes val = BuildingTypesExtensions.ToBuildingTypes(targetBuilding.ModelName);
			if ((int)val == 263 || (int)val == 146 || !completedBuildings.Add(targetBuilding))
			{
				return;
			}
			Actor actor = ((ActorTask)__instance).actor;
			VillagerView val2 = ((Villager)(((actor is Villager) ? actor : null)?)).view;
			if (!((Object)(object)val2 == (Object)null))
			{
				string name = ((Object)val2.pickSound.race).name;
				RaceTypes val3 = RaceTypesExtensions.ToRaceTypes(name);
				RaceTypes val4 = val3;
				RaceTypes val5 = val4;
				switch (val5 - 1)
				{
				case 0:
					TryPlaySounds("beaverFinishBuildingSounds.json");
					break;
				case 1:
					TryPlaySounds("foxesFinishBuildingSounds.json");
					break;
				case 3:
					TryPlaySounds("harpyFinishBuildingSounds.json");
					break;
				case 4:
					TryPlaySounds("humanFinishBuildingSounds.json");
					break;
				case 5:
					TryPlaySounds("lizardFinishBuildingSounds.json");
					break;
				case 2:
					break;
				}
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}