Decompiled source of PersistentData v1.1.1

BepInEx/plugins/lammas123.PersistentData.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
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 BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.IL2CPP;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using SteamworksNative;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.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 PersistentData
{
	public static class Api
	{
		public static string PersistentDataPath { get; internal set; } = Path.Combine(Paths.ConfigPath, "lammas123.PersistentData", "Data");


		public static string PersistentClientDataPath { get; internal set; } = Path.Combine(Paths.ConfigPath, "lammas123.PersistentData", "ClientData");


		public static HashSet<ulong> PersistentClientDataIds { get; internal set; } = new HashSet<ulong>();


		public static DataFile GetDataFile(string fileId)
		{
			DataFile result = default(DataFile);
			result.data = new Dictionary<string, string>();
			result.Id = fileId;
			result.FilePath = Path.Combine(PersistentDataPath, fileId + ".txt");
			result.ReadFile();
			return result;
		}

		public static ClientDataFile GetClientDataFile(ulong clientId)
		{
			ClientDataFile result = default(ClientDataFile);
			result.data = new Dictionary<string, string>();
			result.ClientId = clientId;
			result.FilePath = Path.Combine(PersistentClientDataPath, $"{clientId}.txt");
			result.ReadFile();
			return result;
		}
	}
	public struct DataFile
	{
		internal Dictionary<string, string> data;

		public string Id { get; internal set; }

		public string FilePath { get; internal set; }

		public readonly string[] GetKeys()
		{
			return data.Keys.ToArray();
		}

		public readonly bool ContainsKey(string key)
		{
			return data.ContainsKey(key);
		}

		public readonly string Get(string key)
		{
			if (!ContainsKey(key))
			{
				return null;
			}
			return data[key];
		}

		public readonly bool Set(string key, string value)
		{
			if (string.IsNullOrEmpty(key) || key.StartsWith('#') || key.Contains(':') || key.Contains('\n') || string.IsNullOrEmpty(value) || value.Contains('\n'))
			{
				return false;
			}
			data[key] = value;
			return true;
		}

		public readonly bool Remove(string key)
		{
			return data.Remove(key);
		}

		public readonly bool ReadFile()
		{
			if (!File.Exists(FilePath))
			{
				return false;
			}
			data.Clear();
			string[] array = File.ReadAllLines(FilePath);
			foreach (string text in array)
			{
				if (text.Length != 0 && !text.StartsWith('#'))
				{
					int num = text.IndexOf(':');
					if (num != -1)
					{
						Dictionary<string, string> dictionary = data;
						string key = text.Substring(0, num).Trim(' ');
						string text2 = text;
						int num2 = num + 1;
						dictionary[key] = text2.Substring(num2, text2.Length - num2).Trim(' ');
					}
				}
			}
			return true;
		}

		public readonly void SaveFile()
		{
			string[] array = new string[data.Count];
			int num = 0;
			foreach (string key in data.Keys)
			{
				array[num++] = key + ":" + data[key];
			}
			Directory.CreateDirectory(Path.GetDirectoryName(FilePath));
			File.WriteAllLines(FilePath, array);
		}

		public readonly bool DeleteFile()
		{
			bool num = File.Exists(FilePath);
			if (num)
			{
				File.Delete(FilePath);
			}
			return num;
		}
	}
	public struct ClientDataFile
	{
		internal Dictionary<string, string> data;

		public ulong ClientId { get; internal set; }

		public string FilePath { get; internal set; }

		public readonly string[] GetKeys()
		{
			return data.Keys.ToArray();
		}

		public readonly bool ContainsKey(string key)
		{
			return data.ContainsKey(key);
		}

		public readonly string Get(string key)
		{
			if (!ContainsKey(key))
			{
				return null;
			}
			return data[key];
		}

		public readonly bool Set(string key, string value)
		{
			if (string.IsNullOrEmpty(key) || key.StartsWith('#') || key.Contains(':') || key.Contains('\n') || string.IsNullOrEmpty(value) || value.Contains('\n'))
			{
				return false;
			}
			data[key] = value;
			return true;
		}

		public readonly bool Remove(string key)
		{
			return data.Remove(key);
		}

		public readonly bool ReadFile()
		{
			if (!File.Exists(FilePath))
			{
				Api.PersistentClientDataIds.Remove(ClientId);
				return false;
			}
			data.Clear();
			Api.PersistentClientDataIds.Add(ClientId);
			string[] array = File.ReadAllLines(FilePath);
			foreach (string text in array)
			{
				if (text.Length != 0 && !text.StartsWith('#'))
				{
					int num = text.IndexOf(':');
					if (num != -1)
					{
						Dictionary<string, string> dictionary = data;
						string key = text.Substring(0, num).Trim(' ');
						string text2 = text;
						int num2 = num + 1;
						dictionary[key] = text2.Substring(num2, text2.Length - num2).Trim(' ');
					}
				}
			}
			return true;
		}

		public readonly void SaveFile()
		{
			string[] array = new string[data.Count];
			int num = 0;
			foreach (string key in data.Keys)
			{
				array[num++] = key + ":" + data[key];
			}
			Directory.CreateDirectory(Path.GetDirectoryName(FilePath));
			File.WriteAllLines(FilePath, array);
			Api.PersistentClientDataIds.Add(ClientId);
		}

		public readonly bool DeleteFile()
		{
			bool num = File.Exists(FilePath);
			if (num)
			{
				File.Delete(FilePath);
			}
			Api.PersistentClientDataIds.Remove(ClientId);
			return num;
		}
	}
	internal static class Patches
	{
		[HarmonyPatch(typeof(MonoBehaviourPublicGataInefObInUnique), "Method_Private_Void_GameObject_Boolean_Vector3_Quaternion_0")]
		[HarmonyPatch(typeof(MonoBehaviourPublicCSDi2UIInstObUIloDiUnique), "Method_Private_Void_0")]
		[HarmonyPrefix]
		internal static bool PreBepinexDetection()
		{
			return false;
		}

		[HarmonyPatch(typeof(MonoBehaviourPublicCSDi2UIInstObUIloDiUnique), "StartLobby")]
		[HarmonyPatch(typeof(MonoBehaviourPublicCSDi2UIInstObUIloDiUnique), "StartPracticeLobby")]
		[HarmonyPostfix]
		internal static void PostLobbyManagerStartLobby()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			ClientDataFile clientDataFile = Api.GetClientDataFile(SteamUser.GetSteamID().m_SteamID);
			clientDataFile.Set("Username", SteamFriends.GetFriendPersonaName(SteamUser.GetSteamID()));
			clientDataFile.SaveFile();
		}

		[HarmonyPatch(typeof(MonoBehaviourPublicCSDi2UIInstObUIloDiUnique), "OnPlayerJoinLeaveUpdate")]
		[HarmonyPostfix]
		internal static void PostLobbyManagerOnPlayerJoinLeaveUpdate(CSteamID param_1, bool param_2)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			if (MonoBehaviourPublicObInUIgaStCSBoStcuCSUnique.Instance.IsLobbyOwner() && param_2)
			{
				ClientDataFile clientDataFile = Api.GetClientDataFile(param_1.m_SteamID);
				clientDataFile.Set("Username", SteamFriends.GetFriendPersonaName(param_1));
				clientDataFile.SaveFile();
			}
		}
	}
	[BepInPlugin("lammas123.PersistentData", "PersistentData", "1.1.1")]
	public sealed class PersistentData : BasePlugin
	{
		public override void Load()
		{
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Expected O, but got Unknown
			CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
			CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
			CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
			CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
			Directory.CreateDirectory(Api.PersistentDataPath);
			Directory.CreateDirectory(Api.PersistentClientDataPath);
			string[] files = Directory.GetFiles(Api.PersistentClientDataPath);
			Api.PersistentClientDataIds.EnsureCapacity(files.Length);
			string[] array = files;
			for (int i = 0; i < array.Length; i++)
			{
				if (ulong.TryParse(Path.GetFileNameWithoutExtension(Path.GetFileName(array[i])), NumberStyles.None, CultureInfo.InvariantCulture.NumberFormat, out var result))
				{
					Api.PersistentClientDataIds.Add(result);
				}
			}
			new Harmony("PersistentData").PatchAll(typeof(Patches));
			ManualLogSource log = ((BasePlugin)this).Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(15, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Initialized [");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("PersistentData");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("1.1.1");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("]");
			}
			log.LogInfo(val);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "lammas123.PersistentData";

		public const string PLUGIN_NAME = "PersistentData";

		public const string PLUGIN_VERSION = "1.1.1";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}