using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Sparroh")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]
[assembly: AssemblyProduct("MoreStackables")]
[assembly: AssemblyTitle("MoreStackables")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[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;
}
}
}
public static class ConfigManager
{
private const float DebounceSeconds = 0.25f;
private static ConfigFile config;
private static ManualLogSource logger;
private static FileSystemWatcher configWatcher;
private static volatile bool reloadPending;
private static float lastReloadTime;
public static ConfigEntry<bool> EnableStackables { get; private set; }
public static ConfigEntry<bool> LogPromotedUpgrades { get; private set; }
public static void Initialize(ConfigFile configFile, ManualLogSource log)
{
config = configFile;
logger = log;
EnableStackables = config.Bind<bool>("General", "Enable Stackables", true, "When enabled, upgrades that are stackable in Ouroboros (CanStackInMission) become stackable in the base game as well (CanStack).");
LogPromotedUpgrades = config.Bind<bool>("Debug", "Log Promoted Upgrades", false, "Log each upgrade that receives the CanStack flag.");
try
{
SetupFileWatcher();
}
catch (Exception ex)
{
logger.LogError((object)("Error setting up config file watcher: " + ex.Message));
}
}
public static void Tick()
{
if (!reloadPending || Time.unscaledTime - lastReloadTime < 0.25f)
{
return;
}
reloadPending = false;
lastReloadTime = Time.unscaledTime;
try
{
config.Reload();
logger.LogInfo((object)"Config reloaded from disk.");
}
catch (Exception ex)
{
logger.LogError((object)("Error reloading config: " + ex.Message));
}
}
public static void Dispose()
{
if (configWatcher != null)
{
configWatcher.EnableRaisingEvents = false;
configWatcher.Changed -= OnConfigFileChanged;
configWatcher.Created -= OnConfigFileChanged;
configWatcher.Renamed -= OnConfigFileChanged;
configWatcher.Dispose();
configWatcher = null;
}
}
private static void SetupFileWatcher()
{
configWatcher = new FileSystemWatcher(Paths.ConfigPath, "sparroh.morestackables.cfg");
configWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite;
configWatcher.Changed += OnConfigFileChanged;
configWatcher.Created += OnConfigFileChanged;
configWatcher.Renamed += OnConfigFileChanged;
configWatcher.EnableRaisingEvents = true;
}
private static void OnConfigFileChanged(object sender, FileSystemEventArgs e)
{
reloadPending = true;
}
}
[BepInPlugin("sparroh.morestackables", "MoreStackables", "1.0.2")]
[MycoMod(/*Could not decode attribute arguments.*/)]
public class MoreStackablesPlugin : BaseUnityPlugin
{
public const string PluginGUID = "sparroh.morestackables";
public const string PluginName = "MoreStackables";
public const string PluginVersion = "1.0.2";
internal static ManualLogSource Logger;
internal static MoreStackablesPlugin Instance;
private static bool applied;
private void Awake()
{
Instance = this;
Logger = ((BaseUnityPlugin)this).Logger;
ConfigManager.Initialize(((BaseUnityPlugin)this).Config, Logger);
PlayerData.AddRegisterUpgradesCallback((Action)OnUpgradesRegistered);
try
{
if (!applied && (Object)(object)Global.Instance != (Object)null)
{
OnUpgradesRegistered();
}
}
catch (Exception ex)
{
Logger.LogDebug((object)("[MoreStackables] Early apply skipped: " + ex.Message));
}
Logger.LogInfo((object)"MoreStackables v1.0.2 loaded.");
}
private void Update()
{
ConfigManager.Tick();
}
private void OnDestroy()
{
ConfigManager.Dispose();
Instance = null;
}
private static void OnUpgradesRegistered()
{
if (applied)
{
return;
}
if (ConfigManager.EnableStackables == null || !ConfigManager.EnableStackables.Value)
{
Logger.LogInfo((object)"[MoreStackables] Disabled in config; no flags changed.");
applied = true;
return;
}
try
{
if ((Object)(object)Global.Instance == (Object)null)
{
Logger.LogWarning((object)"[MoreStackables] Global.Instance is null; will retry on next registration callback.");
return;
}
int num = StackFlagPromoter.Apply(ConfigManager.LogPromotedUpgrades != null && ConfigManager.LogPromotedUpgrades.Value);
applied = true;
Logger.LogInfo((object)$"[MoreStackables] Promoted {num} upgrade(s) from CanStackInMission to CanStack.");
}
catch (Exception arg)
{
Logger.LogError((object)$"[MoreStackables] Failed to apply stack flags: {arg}");
}
}
}
internal static class StackFlagPromoter
{
private static PropertyInfo flagsProperty;
private static FieldInfo flagsBackingField;
private static bool writePathResolved;
public static int Apply(bool logPromoted)
{
ResolveWritePath();
HashSet<Upgrade> seen = new HashSet<Upgrade>();
int num = 0;
if (Global.Instance.AllGear != null)
{
IUpgradable[] allGear = Global.Instance.AllGear;
foreach (IUpgradable gear in allGear)
{
num += PromoteGearUpgrades(gear, seen, logPromoted);
}
}
if (Global.Instance.Characters != null)
{
Character[] characters = Global.Instance.Characters;
foreach (Character gear2 in characters)
{
num += PromoteGearUpgrades((IUpgradable)(object)gear2, seen, logPromoted);
}
}
return num + PromoteGearUpgrades((IUpgradable)(object)Global.Instance, seen, logPromoted);
}
private static int PromoteGearUpgrades(IUpgradable gear, HashSet<Upgrade> seen, bool logPromoted)
{
object obj;
if (gear == null)
{
obj = null;
}
else
{
GearInfo info = gear.Info;
obj = ((info != null) ? info.Upgrades : null);
}
if (obj == null)
{
return 0;
}
int num = 0;
List<Upgrade> upgrades = gear.Info.Upgrades;
for (int i = 0; i < upgrades.Count; i++)
{
Upgrade val = upgrades[i];
if (!((Object)(object)val == (Object)null) && seen.Add(val) && TryPromote(val, logPromoted))
{
num++;
}
}
return num;
}
private static bool TryPromote(Upgrade upgrade, bool logPromoted)
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_0007: 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_0012: Unknown result type (might be due to invalid IL or missing references)
//IL_0014: Unknown result type (might be due to invalid IL or missing references)
//IL_0019: Unknown result type (might be due to invalid IL or missing references)
//IL_001b: Unknown result type (might be due to invalid IL or missing references)
//IL_001c: Unknown result type (might be due to invalid IL or missing references)
//IL_001e: Unknown result type (might be due to invalid IL or missing references)
UpgradeFlags flags = upgrade.Flags;
if ((flags & 0x4000) == 0)
{
return false;
}
if ((flags & 8) != 0)
{
return false;
}
UpgradeFlags value = (UpgradeFlags)(flags | 8);
if (!TrySetFlags(upgrade, value))
{
MoreStackablesPlugin.Logger.LogWarning((object)("[MoreStackables] Could not write Flags on '" + SafeName(upgrade) + "'."));
return false;
}
if (logPromoted)
{
string arg = "unknown";
try
{
GearInfo val = PlayerData.FindGearInfo(upgrade);
if ((Object)(object)val != (Object)null)
{
arg = val.Name ?? val.APIName ?? "unknown";
}
}
catch
{
}
MoreStackablesPlugin.Logger.LogInfo((object)$"[MoreStackables] +CanStack: {SafeName(upgrade)} (gear={arg}, id={upgrade.NumberID})");
}
return true;
}
private static void ResolveWritePath()
{
if (!writePathResolved)
{
writePathResolved = true;
Type typeFromHandle = typeof(Upgrade);
flagsBackingField = typeFromHandle.GetField("<Flags>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic) ?? typeFromHandle.GetField("Flags", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
flagsProperty = typeFromHandle.GetProperty("Flags", BindingFlags.Instance | BindingFlags.Public);
}
}
private static bool TrySetFlags(Upgrade upgrade, UpgradeFlags value)
{
//IL_004c: Unknown result type (might be due to invalid IL or missing references)
//IL_0058: Unknown result type (might be due to invalid IL or missing references)
//IL_005d: Unknown result type (might be due to invalid IL or missing references)
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
//IL_002b: Unknown result type (might be due to invalid IL or missing references)
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
if (flagsProperty != null && flagsProperty.CanWrite)
{
try
{
flagsProperty.SetValue(upgrade, value);
return upgrade.Flags == value;
}
catch
{
}
}
if (flagsBackingField != null)
{
try
{
flagsBackingField.SetValue(upgrade, value);
return upgrade.Flags == value;
}
catch (Exception ex)
{
MoreStackablesPlugin.Logger.LogError((object)("[MoreStackables] Backing-field write failed: " + ex.Message));
}
}
return false;
}
private static string SafeName(Upgrade upgrade)
{
try
{
return upgrade.Name ?? upgrade.APIName ?? ((object)upgrade).ToString();
}
catch
{
return ((upgrade != null) ? upgrade.APIName : null) ?? "???";
}
}
}
namespace MoreStackables
{
public static class MyPluginInfo
{
public const string PLUGIN_GUID = "MoreStackables";
public const string PLUGIN_NAME = "MoreStackables";
public const string PLUGIN_VERSION = "1.0.2";
}
}
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
internal sealed class IgnoresAccessChecksToAttribute : Attribute
{
public IgnoresAccessChecksToAttribute(string assemblyName)
{
}
}
}