Decompiled source of QuickStackStorePersonalData v0.1.0

plugins\QuickStackStorePersonalData\QuickStackStorePersonalData.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Daishi11.QuickStackStorePersonalData;

[BepInPlugin("daishi11.quickstackstorepersonaldata", "QuickStackStore Personal Data", "0.1.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public sealed class Plugin : BaseUnityPlugin
{
	public const string PluginGuid = "daishi11.quickstackstorepersonaldata";

	public const string PluginName = "QuickStackStore Personal Data";

	public const string PluginVersion = "0.1.0";

	public const string QuickStackStoreGuid = "goldenrevolver.quick_stack_store";

	internal static ManualLogSource Log;

	private Harmony _harmony;

	private void Awake()
	{
		Log = ((BaseUnityPlugin)this).Logger;
		try
		{
			Directory.CreateDirectory(Storage.PersistentDirectory);
			Storage.MigrateExistingFiles();
			Type type = AccessTools.TypeByName("QuickStackStore.UserConfig");
			if (type == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"QuickStackStore.UserConfig was not found. No patch was applied.");
				return;
			}
			ConstructorInfo constructorInfo = AccessTools.Constructor(type, new Type[1] { typeof(long) }, false);
			if (constructorInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"QuickStackStore.UserConfig(long) was not found. No patch was applied.");
				return;
			}
			_harmony = Harmony.CreateAndPatchAll(typeof(Plugin).Assembly, "daishi11.quickstackstorepersonaldata");
			((BaseUnityPlugin)this).Logger.LogInfo((object)("QuickStackStore per-player data redirected to: " + Storage.PersistentDirectory));
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Failed to initialize QuickStackStore Personal Data: " + ex));
		}
	}

	private void OnDestroy()
	{
		if (_harmony != null)
		{
			_harmony.UnpatchSelf();
		}
	}
}
internal static class Storage
{
	internal static readonly string PersistentDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Daishi11", "Valheim", "QuickStackStorePersonalData");

	internal static string GetPersistentConfigPath()
	{
		Directory.CreateDirectory(PersistentDirectory);
		return PersistentDirectory;
	}

	internal static void MigrateExistingFiles()
	{
		if (!Directory.Exists(Paths.ConfigPath))
		{
			return;
		}
		Directory.CreateDirectory(PersistentDirectory);
		string[] files = Directory.GetFiles(Paths.ConfigPath, "QuickStackStore_player_*.dat", SearchOption.TopDirectoryOnly);
		int num = 0;
		string[] array = files;
		foreach (string text in array)
		{
			try
			{
				if (new FileInfo(text).Length > 0)
				{
					string text2 = Path.Combine(PersistentDirectory, Path.GetFileName(text));
					if (!File.Exists(text2))
					{
						File.Copy(text, text2, overwrite: false);
						num++;
					}
				}
			}
			catch (Exception ex)
			{
				if (Plugin.Log != null)
				{
					Plugin.Log.LogWarning((object)("Could not migrate " + text + ": " + ex.Message));
				}
			}
		}
		if (num > 0 && Plugin.Log != null)
		{
			Plugin.Log.LogInfo((object)("Migrated " + num + " existing QuickStackStore player data file(s) out of BepInEx/config."));
		}
	}
}
[HarmonyPatch]
internal static class Patches
{
	private static MethodBase TargetMethod()
	{
		Type type = AccessTools.TypeByName("QuickStackStore.UserConfig");
		if (type == null)
		{
			return null;
		}
		return AccessTools.Constructor(type, new Type[1] { typeof(long) }, false);
	}

	[HarmonyTranspiler]
	internal static IEnumerable<CodeInstruction> UserConfigConstructorTranspiler(IEnumerable<CodeInstruction> instructions)
	{
		MethodInfo originalGetter = AccessTools.PropertyGetter(typeof(Paths), "ConfigPath");
		MethodInfo replacement = AccessTools.Method(typeof(Storage), "GetPersistentConfigPath", (Type[])null, (Type[])null);
		int replacements = 0;
		foreach (CodeInstruction instruction in instructions)
		{
			if ((instruction.opcode == OpCodes.Call || instruction.opcode == OpCodes.Callvirt) && instruction.operand is MethodInfo && (MethodInfo)instruction.operand == originalGetter)
			{
				instruction.opcode = OpCodes.Call;
				instruction.operand = replacement;
				replacements++;
			}
			yield return instruction;
		}
		if (Plugin.Log != null)
		{
			switch (replacements)
			{
			case 1:
				Plugin.Log.LogInfo((object)"Patched QuickStackStore UserConfig path successfully.");
				break;
			case 0:
				Plugin.Log.LogError((object)"QuickStackStore UserConfig path pattern was not found. Personal data redirection is inactive.");
				break;
			default:
				Plugin.Log.LogWarning((object)("Patched " + replacements + " QuickStackStore ConfigPath accesses; expected exactly one."));
				break;
			}
		}
	}
}