Decompiled source of WC3Reskin v1.0.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.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.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WC3Reskin")]
[assembly: AssemblyTitle("WC3Reskin")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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.0.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.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private const string GUID = "WC3Reskin";

		private const string NAME = "ATS_WC3Reskin";

		private const string VERSION = "1.0.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.0.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) == 38))
			{
				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) == 38))
			{
				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(Builder), "FinishBuilding")]
		[HarmonyPostfix]
		private static void FinishBuilding(Builder __instance, Building building)
		{
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Invalid comparison between Unknown and I4
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Invalid comparison between Unknown and I4
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Expected I4, but got Unknown
			Log.LogInfo((object)("FinishBuilding " + ((Object)((Role)__instance).villager).name + " " + building.ModelName));
			if (!Configs.PlayBuildingCompleteSound)
			{
				return;
			}
			if (!building.BuildingState.finished)
			{
				Log.LogInfo((object)"Building not finished???");
				return;
			}
			BuildingTypes val = BuildingTypesExtensions.ToBuildingTypes(building.ModelName);
			if ((int)val != 247 && (int)val != 141 && completedBuildings.Add(building))
			{
				VillagerView view = ((Role)__instance).villager.view;
				string name = ((Object)view.pickSound.race).name;
				RaceTypes val2 = RaceTypesExtensions.ToRaceTypes(name);
				RaceTypes val3 = val2;
				RaceTypes val4 = val3;
				switch (val4 - 1)
				{
				case 0:
					TryPlaySounds("beaverFinishBuildingSounds.json");
					break;
				case 1:
					TryPlaySounds("foxesFinishBuildingSounds.json");
					break;
				case 2:
					TryPlaySounds("harpyFinishBuildingSounds.json");
					break;
				case 3:
					TryPlaySounds("humanFinishBuildingSounds.json");
					break;
				case 4:
					TryPlaySounds("lizardFinishBuildingSounds.json");
					break;
				}
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}