Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of SkilledCarryWeight v1.6.1
SkilledCarryWeight.dll
Decompiled 3 weeks agousing System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using BepInEx; using BepInEx.Configuration; using BepInEx.Logging; using HarmonyLib; using Jotunn.Extensions; using Jotunn.Managers; using Jotunn.Utils; using Logging; using SkilledCarryWeight.Patches; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: AssemblyTitle("SkilledCarryWeight")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("SkilledCarryWeight")] [assembly: AssemblyCopyright("Copyright © 2023")] [assembly: AssemblyTrademark("")] [assembly: ComVisible(false)] [assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")] [assembly: AssemblyFileVersion("1.6.1")] [assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("1.6.1.0")] [module: UnverifiableCode] namespace Logging { internal static class Log { internal enum InfoLevel { Low, Medium, High } private static ManualLogSource logSource; internal static ConfigEntry<InfoLevel> Verbosity { get; set; } internal static InfoLevel VerbosityLevel => Verbosity.Value; internal static bool IsVerbosityLow => Verbosity.Value >= InfoLevel.Low; internal static bool IsVerbosityMedium => Verbosity.Value >= InfoLevel.Medium; internal static bool IsVerbosityHigh => Verbosity.Value >= InfoLevel.High; internal static void Init(ManualLogSource logSource) { Log.logSource = logSource; } internal static void LogDebug(object data) { logSource.LogDebug(data); } internal static void LogError(object data) { logSource.LogError(data); } internal static void LogFatal(object data) { logSource.LogFatal(data); } internal static void LogMessage(object data) { logSource.LogMessage(data); } internal static void LogWarning(object data) { logSource.LogWarning(data); } internal static void LogInfo(object data, InfoLevel level = InfoLevel.Low) { if (Verbosity == null || VerbosityLevel >= level) { logSource.LogInfo(data); } } internal static void LogGameObject(GameObject prefab, bool includeChildren = false) { //IL_006b: Unknown result type (might be due to invalid IL or missing references) //IL_0071: Expected O, but got Unknown LogInfo("***** " + ((Object)prefab).name + " *****"); Component[] components = prefab.GetComponents<Component>(); for (int i = 0; i < components.Length; i++) { LogComponent(components[i]); } if (!includeChildren) { return; } LogInfo("***** " + ((Object)prefab).name + " (children) *****"); foreach (Transform item in prefab.transform) { Transform val = item; if (Object.op_Implicit((Object)(object)val)) { LogInfo(" - " + ((Object)val).name); components = ((Component)val).GetComponents<Component>(); for (int i = 0; i < components.Length; i++) { LogComponent(components[i]); } } } } internal static void LogComponent(Component compo) { if (!Object.op_Implicit((Object)(object)compo)) { return; } try { LogInfo("--- " + ((object)compo).GetType().Name + ": " + ((Object)compo).name + " ---"); } catch (Exception ex) { LogError(ex.ToString()); LogWarning("Could not get type name for component!"); return; } try { foreach (PropertyInfo declaredProperty in AccessTools.GetDeclaredProperties(((object)compo).GetType())) { try { LogInfo($" - {declaredProperty.Name} = {declaredProperty.GetValue(compo)}"); } catch (Exception ex2) { LogError(ex2.ToString()); LogWarning("Could not get property: " + declaredProperty.Name + " for component!"); } } } catch (Exception ex3) { LogError(ex3.ToString()); LogWarning("Could not get properties for component!"); } try { foreach (FieldInfo declaredField in AccessTools.GetDeclaredFields(((object)compo).GetType())) { try { LogInfo($" - {declaredField.Name} = {declaredField.GetValue(compo)}"); } catch (Exception ex4) { LogError(ex4.ToString()); LogWarning("Could not get field: " + declaredField.Name + " for component!"); } } } catch (Exception ex5) { LogError(ex5.ToString()); LogWarning("Could not get fields for component!"); } } } } namespace SkilledCarryWeight { [BepInPlugin("Searica.Valheim.SkilledCarryWeight", "SkilledCarryWeight", "1.6.1")] [BepInDependency("com.jotunn.jotunn", "2.30.2")] [NetworkCompatibility(/*Could not decode attribute arguments.*/)] [SynchronizationMode(/*Could not decode attribute arguments.*/)] internal sealed class SkilledCarryWeight : BaseUnityPlugin { internal class SkillConfig { public ConfigEntry<bool> enabledConfig; public ConfigEntry<float> coeffConfig; public ConfigEntry<float> powConfig; public bool IsEnabled => enabledConfig.Value; public float Coeff => coeffConfig.Value; public float Pow => powConfig.Value; } internal const string Author = "Searica"; public const string PluginName = "SkilledCarryWeight"; public const string PluginGUID = "Searica.Valheim.SkilledCarryWeight"; public const string PluginVersion = "1.6.1"; internal static SkilledCarryWeight Instance; internal static ConfigFileWatcher ConfigWatcher; internal static readonly Dictionary<SkillType, SkillConfig> SkillConfigsMap = new Dictionary<SkillType, SkillConfig>(); internal static ConfigEntry<bool> EnableCartPatch; internal static ConfigEntry<float> CartPower; internal static ConfigEntry<float> MaxMassReduction; internal static ConfigEntry<float> MinCarryWeight; internal static ConfigEntry<float> BaseCarryWeight; private const string MainSection = "Global"; private const string CartSection = "Cart Mass"; private static bool SettingsUpdated = false; public void Awake() { //IL_004f: Unknown result type (might be due to invalid IL or missing references) //IL_0059: Expected O, but got Unknown Instance = this; Log.Init(((BaseUnityPlugin)this).Logger); ((BaseUnityPlugin)this).Config.SaveOnConfigSet = false; Initialize(); ((BaseUnityPlugin)this).Config.Save(); Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "Searica.Valheim.SkilledCarryWeight"); Game.isModded = true; ConfigWatcher = new ConfigFileWatcher(((BaseUnityPlugin)this).Config, 1000L); ConfigWatcher.OnConfigFileReloaded += UpdatePlugin; SynchronizationManager.OnConfigurationWindowClosed += UpdatePlugin; SynchronizationManager.OnConfigurationSynchronized += delegate { UpdatePlugin(); }; } public void OnDestroy() { ((BaseUnityPlugin)this).Config.Save(); } private void UpdatePlugin() { if (SettingsUpdated) { ((BaseUnityPlugin)this).Config.Save(); SettingsUpdated = false; } } private static void OnSettingChanged(object sender, EventArgs e) { if (!SettingsUpdated) { SettingsUpdated = true; } } internal unsafe static void Initialize() { //IL_01ee: Unknown result type (might be due to invalid IL or missing references) //IL_01ef: Unknown result type (might be due to invalid IL or missing references) //IL_01f5: Invalid comparison between Unknown and I4 //IL_0221: Unknown result type (might be due to invalid IL or missing references) //IL_02fd: Unknown result type (might be due to invalid IL or missing references) Log.Verbosity = ConfigFileExtensions.BindConfigInOrder<Log.InfoLevel>(((BaseUnityPlugin)Instance).Config, "Global", "Verbosity", Log.InfoLevel.Low, "Low will log basic information about the mod. Medium will log information that is useful for troubleshooting. High will log a lot of information, do not set it to this without good reason as it will slow down your game.", false, true, true, (AcceptableValueBase)null, (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null); Log.Verbosity.SettingChanged += OnSettingChanged; BaseCarryWeight = ConfigFileExtensions.BindConfigInOrder<float>(((BaseUnityPlugin)Instance).Config, "Global", "BaseCarryWeight", 300f, "Base maximum carry weight for the player. Vanilla value is 300.", true, true, true, (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 1000f), (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null); BaseCarryWeight.SettingChanged += PlayerPatch.UpdateBaseCarryWeight; BaseCarryWeight.SettingChanged += OnSettingChanged; EnableCartPatch = ConfigFileExtensions.BindConfigInOrder<bool>(((BaseUnityPlugin)Instance).Config, "Cart Mass", "CarryWeightAffectsCart", true, "Set to true/enabled to allow your max carry weight affect how easy carts are to pull by reducing the mass of carts you pull.", true, true, true, (AcceptableValueBase)null, (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null); EnableCartPatch.SettingChanged += OnSettingChanged; CartPower = ConfigFileExtensions.BindConfigInOrder<float>(((BaseUnityPlugin)Instance).Config, "Cart Mass", "Power", 1f, "Affects how much your maximum carry weight making pulling carts easier. Higher powers make your maximum carry weight reduce the mass of carts more.", true, true, true, (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 3f), (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null); CartPower.SettingChanged += OnSettingChanged; MaxMassReduction = ConfigFileExtensions.BindConfigInOrder<float>(((BaseUnityPlugin)Instance).Config, "Cart Mass", "MaxMassReduction", 0.7f, "Maximum reduction in cart mass due to increased max carry weight. Limits effective cart mass to always be equal to or greater than Mass * (1 - MaxMassReduction)", true, true, true, (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null); MaxMassReduction.SettingChanged += OnSettingChanged; MinCarryWeight = ConfigFileExtensions.BindConfigInOrder<float>(((BaseUnityPlugin)Instance).Config, "Cart Mass", "MinCarryWeightForCartReduction", 300f, "Minimum value your maximum carry weight must be before it starts making carts easier to pull.", true, true, true, (AcceptableValueBase)(object)new AcceptableValueRange<float>(300f, 1000f), (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null); MinCarryWeight.SettingChanged += OnSettingChanged; SkillType[] s_allSkills = Skills.s_allSkills; for (int i = 0; i < s_allSkills.Length; i++) { SkillType val = s_allSkills[i]; if ((int)val != 999) { string text = ((object)(*(SkillType*)(&val))/*cast due to .constrained prefix*/).ToString(); SkillConfig skillConfig = new SkillConfig(); skillConfig.enabledConfig = ConfigFileExtensions.BindConfigInOrder<bool>(((BaseUnityPlugin)Instance).Config, text, "Enabled", GetDefaultEnabledValue(val), "Set to true/enabled to allow this skill to increase your max carry weight.", true, true, true, (AcceptableValueBase)null, (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null); skillConfig.enabledConfig.SettingChanged += OnSettingChanged; skillConfig.coeffConfig = ConfigFileExtensions.BindConfigInOrder<float>(((BaseUnityPlugin)Instance).Config, text, "Coefficient", 0.25f, "Value to multiply the skill level by to determine how much extra carry weight it grants.", true, true, true, (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 10f), (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null); skillConfig.coeffConfig.SettingChanged += OnSettingChanged; skillConfig.powConfig = ConfigFileExtensions.BindConfigInOrder<float>(((BaseUnityPlugin)Instance).Config, text, "Power", 1f, "Power the skill level is raised to before multiplying by Coefficient to determine extra carry weight.", true, true, true, (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 10f), (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null); skillConfig.powConfig.SettingChanged += OnSettingChanged; SkillConfigsMap[val] = skillConfig; } } } private static bool GetDefaultEnabledValue(SkillType skillType) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0003: Invalid comparison between Unknown and I4 //IL_0005: Unknown result type (might be due to invalid IL or missing references) //IL_0008: Invalid comparison between Unknown and I4 //IL_000a: Unknown result type (might be due to invalid IL or missing references) //IL_000d: Unknown result type (might be due to invalid IL or missing references) //IL_003f: Expected I4, but got Unknown if ((int)skillType != 12) { if ((int)skillType != 13) { return (skillType - 100) switch { 2 => true, 0 => true, 3 => true, 10 => true, 1 => true, 8 => true, 6 => true, _ => false, }; } return true; } return true; } } } namespace SkilledCarryWeight.Patches { [HarmonyPatch(typeof(Player))] internal static class PlayerPatch { private static float VanillaBaseCarryWeight = float.NaN; private static Skills skills; private static Dictionary<SkillType, Skill> skillData; internal static void UpdateBaseCarryWeight(object sender, EventArgs e) { if (Player.m_localPlayerExists) { if (float.IsNaN(VanillaBaseCarryWeight)) { VanillaBaseCarryWeight = Player.m_localPlayer.m_maxCarryWeight; } Player.m_localPlayer.m_maxCarryWeight = SkilledCarryWeight.BaseCarryWeight.Value; } } [HarmonyPostfix] [HarmonyPatch("Start")] private static void AdjustBaseCarryWeight(Player __instance) { if (Object.op_Implicit((Object)(object)__instance) && !((Object)(object)__instance != (Object)(object)Player.m_localPlayer)) { if (float.IsNaN(VanillaBaseCarryWeight)) { VanillaBaseCarryWeight = __instance.m_maxCarryWeight; } __instance.m_maxCarryWeight = SkilledCarryWeight.BaseCarryWeight.Value; } } [HarmonyPostfix] [HarmonyPatch("GetMaxCarryWeight")] private static void GetMaxCarryWeight(Player __instance, ref float __result) { //IL_0042: Unknown result type (might be due to invalid IL or missing references) //IL_005e: Unknown result type (might be due to invalid IL or missing references) skills = ((Character)__instance).GetSkills(); skillData = skills.m_skillData; if (skillData == null) { return; } foreach (SkillDef skill in skills.m_skills) { if (SkilledCarryWeight.SkillConfigsMap.TryGetValue(skill.m_skill, out var value) && value.IsEnabled && skillData.TryGetValue(skill.m_skill, out var value2)) { __result += value.Coeff * Mathf.Pow(value2.m_level, value.Pow); if (float.IsInfinity(__result)) { __result = float.MaxValue; break; } } } } } [HarmonyPatch(typeof(Vagon))] internal static class VagonPatch { [HarmonyPrefix] [HarmonyPatch("SetMass")] private static void SetMassPrefix(Vagon __instance, ref float mass) { if (SkilledCarryWeight.EnableCartPatch.Value && Object.op_Implicit((Object)(object)__instance) && !((Object)(object)__instance.m_nview == (Object)null) && __instance.m_nview.IsOwner() && __instance.IsAttached((Character)(object)Player.m_localPlayer)) { Player localPlayer = Player.m_localPlayer; float num = mass * Mathf.Pow(SkilledCarryWeight.MinCarryWeight.Value / localPlayer.GetMaxCarryWeight(), SkilledCarryWeight.CartPower.Value); mass = Mathf.Max(num, mass * (1f - SkilledCarryWeight.MaxMassReduction.Value)); } } } } namespace SkilledCarryWeight.Extensions { internal static class EventExtensions { public static void SafeInvoke(this Action events) { if (events == null) { return; } Delegate[] invocationList = events.GetInvocationList(); for (int i = 0; i < invocationList.Length; i++) { Action action = (Action)invocationList[i]; try { action(); } catch (Exception ex) { Log.LogWarning($"Exception thrown at event {new StackFrame(1).GetMethod().Name} in {action.Method.DeclaringType.Name}.{action.Method.Name}:\n{ex}"); } } } public static void SafeInvoke<TArg1>(this Action<TArg1> events, TArg1 arg1) { if (events == null) { return; } Delegate[] invocationList = events.GetInvocationList(); for (int i = 0; i < invocationList.Length; i++) { Action<TArg1> action = (Action<TArg1>)invocationList[i]; try { action(arg1); } catch (Exception ex) { Log.LogWarning($"Exception thrown at event {new StackFrame(1).GetMethod().Name} in {action.Method.DeclaringType.Name}.{action.Method.Name}:\n{ex}"); } } } public static void SafeInvoke<TArg1, TArg2>(this Action<TArg1, TArg2> events, TArg1 arg1, TArg2 arg2) { if (events == null) { return; } Delegate[] invocationList = events.GetInvocationList(); for (int i = 0; i < invocationList.Length; i++) { Action<TArg1, TArg2> action = (Action<TArg1, TArg2>)invocationList[i]; try { action(arg1, arg2); } catch (Exception ex) { Log.LogWarning($"Exception thrown at event {new StackFrame(1).GetMethod().Name} in {action.Method.DeclaringType.Name}.{action.Method.Name}:\n{ex}"); } } } public static void SafeInvoke<TEventArg>(this EventHandler<TEventArg> events, object sender, TEventArg arg1) { if (events == null) { return; } Delegate[] invocationList = events.GetInvocationList(); for (int i = 0; i < invocationList.Length; i++) { EventHandler<TEventArg> eventHandler = (EventHandler<TEventArg>)invocationList[i]; try { eventHandler(sender, arg1); } catch (Exception ex) { Log.LogWarning($"Exception thrown at event {new StackFrame(1).GetMethod().Name} in {eventHandler.Method.DeclaringType.Name}.{eventHandler.Method.Name}:\n{ex}"); } } } } internal static class ReflectionUtils { public const BindingFlags AllBindings = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.SetField | BindingFlags.GetProperty | BindingFlags.SetProperty; internal static MethodInfo GetMethod(Type type, string name, Type[] types) { MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.SetField | BindingFlags.GetProperty | BindingFlags.SetProperty); foreach (MethodInfo methodInfo in methods) { if (methodInfo.Name == name && HasMatchingParameterTypes(0, types, methodInfo.GetParameters())) { return methodInfo; } } return null; } internal static MethodInfo GetGenericMethod(Type type, string name, int genericParameterCount, Type[] types) { MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.SetField | BindingFlags.GetProperty | BindingFlags.SetProperty); foreach (MethodInfo methodInfo in methods) { if (methodInfo.IsGenericMethod && methodInfo.ContainsGenericParameters && methodInfo.Name == name && HasMatchingParameterTypes(genericParameterCount, types, methodInfo.GetParameters())) { return methodInfo; } } return null; } private static bool HasMatchingParameterTypes(int genericParameterCount, Type[] types, ParameterInfo[] parameters) { if (parameters.Length < genericParameterCount || parameters.Length != types.Length) { return false; } int num = 0; for (int i = 0; i < parameters.Length; i++) { if (parameters[i].ParameterType.IsGenericParameter) { num++; } else if (types[i] != parameters[i].ParameterType) { return false; } } if (num != genericParameterCount) { return false; } return true; } } }