Decompiled source of LogKeeper v1.0.1

LogKeeper.dll

Decompiled 7 months ago
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
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 JetBrains.Annotations;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("LogKeeper")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Azumatt")]
[assembly: AssemblyProduct("LogKeeper")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4358610B-F3F4-4843-B7AF-98B7BC60DCDE")]
[assembly: AssemblyFileVersion("1.0.1")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace LogKeeper
{
	[BepInPlugin("Azumatt.LogKeeper", "LogKeeper", "1.0.1")]
	public class LogKeeperPlugin : BaseUnityPlugin
	{
		private class ConfigurationManagerAttributes
		{
			[UsedImplicitly]
			public int? Order;

			[UsedImplicitly]
			public bool? Browsable;

			[UsedImplicitly]
			public string? Category;

			[UsedImplicitly]
			public Action<ConfigEntryBase>? CustomDrawer;
		}

		internal const string ModName = "LogKeeper";

		internal const string ModVersion = "1.0.1";

		internal const string Author = "Azumatt";

		private const string ModGuid = "Azumatt.LogKeeper";

		private const string ConfigFileName = "Azumatt.LogKeeper.cfg";

		private static readonly string ConfigFileFullPath;

		private static readonly string LOGOutputPath;

		private static readonly string LOGKeeperLogsPath;

		private readonly Harmony _harmony = new Harmony("Azumatt.LogKeeper");

		private static readonly ManualLogSource LogKeeperLogger;

		private static ConfigEntry<int> _howManyToKeep;

		public void Awake()
		{
			_howManyToKeep = config("1 - General", "How Many To Keep", 5, "How many log files to keep in the LogKeeperLogs folder.");
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			_harmony.PatchAll(executingAssembly);
			SetupWatcher();
		}

		private void OnDestroy()
		{
			DoTheThing();
		}

		private void OnApplicationQuit()
		{
			DoTheThing();
		}

		private void DoTheThing()
		{
			((BaseUnityPlugin)this).Config.Save();
			if (!Directory.Exists(LOGKeeperLogsPath))
			{
				Directory.CreateDirectory(LOGKeeperLogsPath);
			}
			string text = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
			string path = "LogOutput_" + text + ".log";
			string destFileName = Path.Combine(LOGKeeperLogsPath, path);
			File.Copy(LOGOutputPath, destFileName, overwrite: true);
			string[] files = Directory.GetFiles(LOGKeeperLogsPath, "*.log");
			int num = files.Length - _howManyToKeep.Value;
			if (num <= 0)
			{
				return;
			}
			foreach (string item in files.OrderBy((string f) => new FileInfo(f).CreationTime).Take(num))
			{
				File.Delete(item);
			}
		}

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, "Azumatt.LogKeeper.cfg");
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				LogKeeperLogger.LogDebug((object)"ReadConfigValues called");
				((BaseUnityPlugin)this).Config.Reload();
			}
			catch
			{
				LogKeeperLogger.LogError((object)"There was an issue loading your Azumatt.LogKeeper.cfg");
				LogKeeperLogger.LogError((object)"Please check your config entries for spelling and format!");
			}
		}

		private ConfigEntry<T> config<T>(string group, string name, T value, ConfigDescription description)
		{
			return ((BaseUnityPlugin)this).Config.Bind<T>(group, name, value, description);
		}

		private ConfigEntry<T> config<T>(string group, string name, T value, string description)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			return config(group, name, value, new ConfigDescription(description, (AcceptableValueBase)null, Array.Empty<object>()));
		}

		static LogKeeperPlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + "Azumatt.LogKeeper.cfg";
			string bepInExRootPath = Paths.BepInExRootPath;
			directorySeparatorChar = Path.DirectorySeparatorChar;
			LOGOutputPath = bepInExRootPath + directorySeparatorChar + "LogOutput.log";
			string bepInExRootPath2 = Paths.BepInExRootPath;
			directorySeparatorChar = Path.DirectorySeparatorChar;
			LOGKeeperLogsPath = bepInExRootPath2 + directorySeparatorChar + "LogKeeperLogs";
			LogKeeperLogger = Logger.CreateLogSource("LogKeeper");
			_howManyToKeep = null;
		}
	}
}