Decompiled source of No More Crashes v1.5.0

NoMoreCrashes.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using Steamworks;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("NoMoreCrashes")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NoMoreCrashes")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("90D94205-30B6-48FB-B626-3163135BF670")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.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 NoMoreCrashes
{
	[BepInPlugin("kg.NoMoreCrashes", "kg.NoMoreCrashes", "1.5.0")]
	public class NoMoreCrashes : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(ZNet), "Start")]
		private static class FejdStartup_Start_Patch
		{
			[UsedImplicitly]
			private static void Postfix()
			{
				if (IsServer)
				{
					Application.targetFrameRate = ((BaseUnityPlugin)thistype).Config.Bind<int>("General", "Target FPS", Application.targetFrameRate, "Target FPS").Value;
				}
			}
		}

		[HarmonyPatch]
		private static class PatchMethods
		{
			[HarmonyTargetMethods]
			private static IEnumerable<MethodBase> Target()
			{
				yield return AccessTools.Method(typeof(ZSteamSocket), "Send", new Type[1] { typeof(ZPackage) }, (Type[])null);
				yield return AccessTools.Method(typeof(ZSteamSocket), "Flush", (Type[])null, (Type[])null);
				yield return AccessTools.Method(typeof(ZSteamSocket), "Update", (Type[])null, (Type[])null);
			}

			[HarmonyTranspiler]
			private static IEnumerable<CodeInstruction> FixingIssue(IEnumerable<CodeInstruction> code)
			{
				//IL_0079: Unknown result type (might be due to invalid IL or missing references)
				//IL_0083: Expected O, but got Unknown
				List<CodeInstruction> list = new List<CodeInstruction>(code);
				MethodInfo methodInfo = AccessTools.Method(typeof(NoMoreCrashes), IsServer ? "Replacement_Server" : "Replacement_Client", (Type[])null, (Type[])null);
				MethodInfo targetMethod = AccessTools.Method(typeof(ZSteamSocket), "SendQueuedPackages", (Type[])null, (Type[])null);
				int num = list.FindIndex((CodeInstruction ins) => ins.opcode == OpCodes.Call && ins.operand == targetMethod);
				if (num == -1)
				{
					Debug.LogError((object)"NoMoreCrashes: Failed to find callvirt to ZSteamSocket.SendQueuedPackages");
					return list;
				}
				list[num] = new CodeInstruction(OpCodes.Call, (object)methodInfo);
				return list;
			}
		}

		private static NoMoreCrashes thistype;

		private static bool IsServer => (int)SystemInfo.graphicsDeviceType == 4;

		private void Awake()
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			thistype = this;
			new Harmony("kg.NoMoreCrashes").PatchAll();
		}

		private unsafe static void Replacement_Client(ZSteamSocket socket)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//The blocks IL_0009, IL_0040 are reachable both inside and outside the pinned region starting at IL_001c. ILSpy has duplicated these blocks in order to place them both within and outside the `fixed` statement.
			if (!socket.IsConnected() || socket.m_sendQueue.Count <= 0)
			{
				return;
			}
			byte[] array = socket.m_sendQueue.Dequeue();
			long num = default(long);
			while (true)
			{
				fixed (byte* ptr = &array[0])
				{
					SteamNetworkingSockets.SendMessageToConnection(socket.m_con, (IntPtr)(nint)ptr, (uint)array.Length, 8, ref num);
					socket.m_totalSent += array.Length;
					if (socket.m_sendQueue.Count > 0)
					{
						array = socket.m_sendQueue.Dequeue();
						continue;
					}
					break;
				}
			}
		}

		private unsafe static void Replacement_Server(ZSteamSocket socket)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//The blocks IL_0009, IL_0040 are reachable both inside and outside the pinned region starting at IL_001c. ILSpy has duplicated these blocks in order to place them both within and outside the `fixed` statement.
			if (!socket.IsConnected() || socket.m_sendQueue.Count <= 0)
			{
				return;
			}
			byte[] array = socket.m_sendQueue.Dequeue();
			long num = default(long);
			while (true)
			{
				fixed (byte* ptr = &array[0])
				{
					SteamGameServerNetworkingSockets.SendMessageToConnection(socket.m_con, (IntPtr)(nint)ptr, (uint)array.Length, 8, ref num);
					socket.m_totalSent += array.Length;
					if (socket.m_sendQueue.Count > 0)
					{
						array = socket.m_sendQueue.Dequeue();
						continue;
					}
					break;
				}
			}
		}
	}
}