Decompiled source of LazyMoons v0.1.0

BepInEx/patchers/LazyMoons/LazyMoons.dll

Decompiled 41 minutes 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.Text;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Mono.Cecil;

[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("LazyMoons")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+97dadafda8309e147f33dec906813f263af4a4f6")]
[assembly: AssemblyProduct("LazyMoons")]
[assembly: AssemblyTitle("LazyMoons")]
[assembly: AssemblyVersion("0.1.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 LazyMoons
{
	internal static class BundleHeader
	{
		private const int MaxHeader = 16777216;

		public static List<string> Scenes(string path)
		{
			byte[] array;
			using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
			{
				using BinaryReader binaryReader = new BinaryReader(fileStream);
				if (Text(binaryReader) != "UnityFS")
				{
					throw new InvalidDataException("not a UnityFS bundle");
				}
				uint num = U32(binaryReader);
				Text(binaryReader);
				Text(binaryReader);
				binaryReader.ReadBytes(8);
				uint num2 = U32(binaryReader);
				uint num3 = U32(binaryReader);
				uint num4 = U32(binaryReader);
				if (num2 > 16777216 || num3 > 16777216)
				{
					throw new InvalidDataException("header too big");
				}
				if (num >= 7)
				{
					fileStream.Position = (fileStream.Position + 15) / 16 * 16;
				}
				if ((num4 & 0x80) != 0)
				{
					fileStream.Position = fileStream.Length - num2;
				}
				array = binaryReader.ReadBytes((int)num2);
				if (array.Length != num2)
				{
					throw new InvalidDataException("file ends early");
				}
				switch (num4 & 0x3F)
				{
				case 2u:
				case 3u:
					array = Lz4(array, (int)num3);
					break;
				default:
					throw new InvalidDataException("header compression " + (num4 & 0x3F));
				case 0u:
					break;
				}
			}
			List<string> list = new List<string>();
			using BinaryReader binaryReader2 = new BinaryReader(new MemoryStream(array));
			binaryReader2.ReadBytes(16);
			long num5 = U32(binaryReader2);
			binaryReader2.BaseStream.Position += num5 * 10;
			uint num6 = U32(binaryReader2);
			for (uint num7 = 0u; num7 < num6; num7++)
			{
				binaryReader2.BaseStream.Position += 20L;
				string text = Text(binaryReader2);
				if (text.StartsWith("BuildPlayer-", StringComparison.Ordinal) && !text.EndsWith(".sharedAssets", StringComparison.Ordinal) && !text.EndsWith(".resource", StringComparison.Ordinal) && !text.EndsWith(".resS", StringComparison.Ordinal))
				{
					list.Add(text.Substring("BuildPlayer-".Length));
				}
			}
			return list;
		}

		private static uint U32(BinaryReader r)
		{
			byte[] array = r.ReadBytes(4);
			if (array.Length < 4)
			{
				throw new EndOfStreamException();
			}
			return (uint)((array[0] << 24) | (array[1] << 16) | (array[2] << 8) | array[3]);
		}

		private static string Text(BinaryReader r)
		{
			List<byte> list = new List<byte>();
			byte item;
			while ((item = r.ReadByte()) != 0)
			{
				list.Add(item);
				if (list.Count > 4096)
				{
					throw new InvalidDataException("text too long");
				}
			}
			return Encoding.UTF8.GetString(list.ToArray());
		}

		private static byte[] Lz4(byte[] src, int size)
		{
			byte[] array = new byte[size];
			int num = 0;
			int num2 = 0;
			while (num < src.Length)
			{
				int num3 = src[num++];
				int num4 = num3 >> 4;
				if (num4 == 15)
				{
					int num5;
					do
					{
						num5 = src[num++];
						num4 += num5;
					}
					while (num5 == 255);
				}
				Buffer.BlockCopy(src, num, array, num2, num4);
				num += num4;
				num2 += num4;
				if (num >= src.Length)
				{
					break;
				}
				int num6 = src[num] | (src[num + 1] << 8);
				num += 2;
				if (num6 == 0 || num6 > num2)
				{
					throw new InvalidDataException("bad LZ4 data");
				}
				num4 = num3 & 0xF;
				if (num4 == 15)
				{
					int num7;
					do
					{
						num7 = src[num++];
						num4 += num7;
					}
					while (num7 == 255);
				}
				num4 += 4;
				while (num4 > 0)
				{
					array[num2] = array[num2 - num6];
					num4--;
					num2++;
				}
			}
			if (num2 != size)
			{
				throw new InvalidDataException("bad LZ4 length");
			}
			return array;
		}
	}
	public static class Patcher
	{
		private const string ManifestVersion = "2";

		private static readonly ManualLogSource Log = Logger.CreateLogSource("LazyMoons");

		public static IEnumerable<string> TargetDLLs { get; } = new string[0];

		public static void Patch(AssemblyDefinition _)
		{
		}

		public static void Initialize()
		{
			try
			{
				Run();
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("Left LLL's list alone: " + ex));
			}
		}

		private static void Run()
		{
			Stopwatch stopwatch = Stopwatch.StartNew();
			string text = Path.Combine(Paths.CachePath, "scenebundles.txt");
			Dictionary<string, string[]> dictionary = new Dictionary<string, string[]>(StringComparer.Ordinal);
			if (File.Exists(text))
			{
				string[] array = File.ReadAllLines(text);
				if (array.Length != 0 && array[0].Trim() != "2")
				{
					Log.LogWarning((object)("LLL's list is format '" + array[0] + "', not 2. Leaving it alone."));
					return;
				}
				foreach (string item in array.Skip(1))
				{
					string[] array2 = item.Split(';');
					if (array2.Length == 4 && !dictionary.ContainsKey(array2[0]))
					{
						dictionary[array2[0]] = array2;
					}
				}
			}
			int num = 0;
			int num2 = 0;
			foreach (IGrouping<string, string> item2 in Directory.GetFiles(Paths.PluginPath, "*.lethalbundle", SearchOption.AllDirectories).GroupBy(Path.GetFileName))
			{
				if (item2.Count() > 1)
				{
					Log.LogWarning((object)(item2.Key + " is there more than once. Leaving it to LLL."));
					continue;
				}
				string path = item2.First();
				string key = item2.Key;
				string text2 = File.GetLastWriteTime(path).Ticks.ToString(CultureInfo.InvariantCulture);
				if (dictionary.TryGetValue(key, out var value) && value[2] == text2)
				{
					continue;
				}
				List<string> list;
				try
				{
					list = BundleHeader.Scenes(path);
				}
				catch (Exception ex)
				{
					num2++;
					Log.LogDebug((object)(key + ": " + ex.Message + ". Leaving it to LLL."));
					continue;
				}
				if (list.Count != 0 && !key.Contains(';') && !list.Any((string s) => s.IndexOfAny(new char[4] { ';', ',', '/', '\\' }) >= 0))
				{
					dictionary[key] = new string[4]
					{
						key,
						(value != null) ? value[1] : key,
						text2,
						string.Join(",", list.Select((string s) => s + ".unity"))
					};
					num++;
				}
			}
			if (num > 0)
			{
				Directory.CreateDirectory(Paths.CachePath);
				string text3 = text + ".lazymoons";
				File.WriteAllLines(text3, new string[1] { "2" }.Concat(dictionary.Values.Select((string[] p) => string.Join(";", p))), Encoding.UTF8);
				if (File.Exists(text))
				{
					File.Replace(text3, text, null);
				}
				else
				{
					File.Move(text3, text);
				}
			}
			Log.LogInfo((object)$"Added {num} new or updated moon files to LLL's list without loading them ({num2} left to LLL, {stopwatch.ElapsedMilliseconds} ms).");
		}
	}
}