Decompiled source of PoolTrim v1.0.0

PoolTrim.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("PoolTrim")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+43d84ec353616da5af8431d9a9d3c7e9f434e31f")]
[assembly: AssemblyProduct("PoolTrim")]
[assembly: AssemblyTitle("PoolTrim")]
[assembly: AssemblyVersion("1.0.0.0")]
[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;
		}
	}
}
namespace PoolTrim
{
	[BepInPlugin("fyyy.dsp.pooltrim", "PoolTrim", "1.0.0")]
	public class PoolTrimPlugin : BaseUnityPlugin
	{
		public const string GUID = "fyyy.dsp.pooltrim";

		public const string NAME = "PoolTrim";

		public const string VERSION = "1.0.0";

		internal static ManualLogSource Log;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony.CreateAndPatchAll(typeof(Patches), "fyyy.dsp.pooltrim");
			Log.LogInfo((object)"PoolTrim ready.");
		}
	}
	internal static class Patches
	{
		private static readonly FieldRef<CargoPath, int> BufferLength = AccessTools.FieldRefAccess<CargoPath, int>("bufferLength");

		private static bool _errorLogged;

		internal static long TrimmedPoints;

		[HarmonyPostfix]
		[HarmonyPatch(typeof(CargoPath), "Import")]
		private static void TrimAfterImport(CargoPath __instance)
		{
			try
			{
				int num = BufferLength.Invoke(__instance);
				if (__instance.buffer != null && __instance.buffer.Length > num)
				{
					TrimmedPoints += __instance.buffer.Length - num;
					__instance.SetCapacity(num);
				}
			}
			catch (Exception ex)
			{
				if (!_errorLogged)
				{
					_errorLogged = true;
					PoolTrimPlugin.Log.LogWarning((object)("PoolTrim failed (further errors muted): " + ex));
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameSave), "LoadCurrentGame")]
		private static void ReportAfterLoad()
		{
			if (TrimmedPoints > 0)
			{
				PoolTrimPlugin.Log.LogInfo((object)$"PoolTrim: trimmed {TrimmedPoints} cargo path points, ~{(double)TrimmedPoints * 29.0 / 1073741824.0:F2} GB saved");
				TrimmedPoints = 0L;
			}
		}
	}
}