Decompiled source of RotatingLogs v0.1.0

RotatingLogs.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
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.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("RotatingLogs")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+51e313fced0ccaa93c0f9973657e6550617db92e")]
[assembly: AssemblyProduct("RotatingLogs")]
[assembly: AssemblyTitle("RotatingLogs")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/flibber-hk/Silksong.RotatingLogs")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.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.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;
		}
	}
	[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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace RotatingLogs
{
	[BepInPlugin("io.github.flibber-hk.rotatinglogs", "RotatingLogs", "0.1.0")]
	public class RotatingLogsPlugin : BaseUnityPlugin
	{
		private string logPath = Path.Combine(Paths.BepInExRootPath, "LogOutput.log");

		private string backupDir = Path.Combine(Paths.BepInExRootPath, "OldLogs");

		private string? logBackup;

		private ConfigEntry<int> MaxLogs;

		public const string Id = "io.github.flibber-hk.rotatinglogs";

		public static string Name => "RotatingLogs";

		public static string Version => "0.1.0";

		private void Awake()
		{
			MaxLogs = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxLogs", 20, "The number of old logs to keep. This value is only used at startup.");
			if (!File.Exists(logPath))
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Not rotating logs: Log path not found");
				return;
			}
			Directory.CreateDirectory(backupDir);
			logBackup = Path.Combine(backupDir, $"LogOutput_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.log");
			CleanupOldLogs();
			BackupLog();
			AddDiskLog();
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Plugin " + Name + " (io.github.flibber-hk.rotatinglogs) has loaded!"));
		}

		private void AddDiskLog()
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Expected O, but got Unknown
			LogLevel val = (LogLevel)63;
			bool flag = true;
			DiskLogListener item = new DiskLogListener(logBackup, val, true, flag);
			Logger.Listeners.Add((ILogListener)(object)item);
		}

		private void CleanupOldLogs()
		{
			try
			{
				List<FileInfo> list = (from f in Directory.GetFiles(backupDir, "LogOutput_*.log")
					select new FileInfo(f) into f
					orderby f.CreationTime descending
					select f).ToList();
				if (list.Count <= MaxLogs.Value)
				{
					return;
				}
				foreach (FileInfo item in list.Skip(MaxLogs.Value))
				{
					try
					{
						item.Delete();
					}
					catch
					{
					}
				}
			}
			catch
			{
			}
		}

		private void FlushLogs()
		{
			try
			{
				foreach (DiskLogListener item in Logger.Listeners.OfType<DiskLogListener>())
				{
					item.LogWriter.Flush();
				}
			}
			catch
			{
			}
		}

		private void BackupLog()
		{
			if (string.IsNullOrEmpty(logBackup))
			{
				return;
			}
			try
			{
				FlushLogs();
				File.Copy(logPath, logBackup, overwrite: true);
			}
			catch
			{
			}
		}
	}
}