Decompiled source of LethalASD v1.0.1

dev.ammir.lethalasd.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TerminalApi;
using TerminalApi.Classes;
using TerminalApi.Events;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: IgnoresAccessChecksTo("TerminalApi")]
[assembly: AssemblyCompany("dev.ammir.lethalasd")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LethalASD")]
[assembly: AssemblyTitle("dev.ammir.lethalasd")]
[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.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;
		}
	}
}
public class MatrixSorter
{
	public static readonly string SOURCE_CODE = "<color=#DCDCDC>#include <stdio.h></color>\r\n<color=#DCDCDC>#include <stdlib.h></color>\r\n<color=#DCDCDC>#include <time.h></color>\r\n<color=#DCDCDC>#include <unistd.h></color>\r\n\r\n<color=#FF6347>const</color> <color=#D3D3D3>int</color> SHELL_GAPS[3] = {7, 3, 1};\r\n\r\n<color=#FF6347>void</color> swap(<color=#D3D3D3>int</color> *a, <color=#D3D3D3>int</color> *b) {\r\n    <color=#D3D3D3>int</color> temp = *a;\r\n    *a = *b;\r\n    *b = temp;\r\n}\r\n\r\n<color=#FF6347>void</color> shell_sort(<color=#D3D3D3>int</color> size, <color=#D3D3D3>int</color> **array) {\r\n    <color=#FF6347>for</color> (<color=#D3D3D3>int</color> g = 0; g < sizeof(SHELL_GAPS) / sizeof(<color=#D3D3D3>int</color>); g++) {\r\n        <color=#D3D3D3>int</color> gap = SHELL_GAPS[g];\r\n        <color=#FF6347>for</color> (<color=#D3D3D3>int</color> i = gap; i < size; i++) { <color=#808080>// if gap is greater than size, we skip it</color>\r\n            <color=#FF6347>for</color> (<color=#D3D3D3>int</color> j = i - gap; j >= 0; j -= gap) {\r\n                <color=#D3D3D3>int</color> *diagonal_j      = &array[j][size - j - 1];\r\n                <color=#D3D3D3>int</color> *diagonal_j_next = &array[j + gap][size - (j + gap) - 1];\r\n                \r\n                <color=#FF6347>if</color> (*diagonal_j > *diagonal_j_next) { <color=#808080>// Condition that defines order</color>\r\n                    swap(diagonal_j, diagonal_j_next);\r\n                }\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\n<color=#FF6347>void</color> print_matrix(<color=#D3D3D3>int</color> size, <color=#D3D3D3>int</color> **array) {\r\n    <color=#FF6347>for</color> (<color=#D3D3D3>int</color> y = 0; y < size; y++) {\r\n        <color=#FF6347>for</color> (<color=#D3D3D3>int</color> x = 0; x < size; x++) {\r\n            <color=#FF6347>if</color> (x == size - y - 1) {\r\n                <color=#D3D3D3>printf</color>(<color=#FFFF00>\"\\033[31m%3d\\033[0m \"</color>, array[y][x]); <color=#808080>// print in red</color>\r\n            } <color=#FF6347>else</color> {\r\n                <color=#D3D3D3>printf</color>(<color=#FFFF00>\"%3d \"</color>, array[y][x]);\r\n            }\r\n        }\r\n        <color=#D3D3D3>printf</color>(<color=#FFFF00>\"\\n\"</color>);\r\n    }\r\n}\r\n\r\n<color=#FF6347>int</color> main(<color=#D3D3D3>int</color> argc, <color=#D3D3D3>char</color> **argv) {\r\n    <color=#D3D3D3>srand</color>(clock());\r\n\r\n    <color=#D3D3D3>int</color> size;\r\n    <color=#D3D3D3>int</color> **matrix;\r\n\r\n    <color=#FF6347>if</color> (argc < 3) {\r\n        <color=#FF6347>if</color> (argc == 1) {\r\n            <color=#D3D3D3>printf</color>(<color=#FFFF00>\"Enter the size of the matrix: \"</color>);\r\n            <color=#D3D3D3>scanf</color>(<color=#FFFF00>\"%d\"</color>, &size);\r\n        } <color=#FF6347>else</color> {\r\n            size = <color=#D3D3D3>atoi</color>(argv[1]);\r\n        }\r\n        \r\n        matrix = <color=#D3D3D3>malloc</color>(sizeof(<color=#D3D3D3>int</color> *) * size);\r\n        <color=#FF6347>for</color> (<color=#D3D3D3>int</color> y = 0; y < size; y++) {\r\n            matrix[y] = <color=#D3D3D3>malloc</color>(size * sizeof(<color=#D3D3D3>int</color>));\r\n            <color=#FF6347>for</color> (<color=#D3D3D3>int</color> x = 0; x < size; x++) {\r\n                matrix[y][x] = rand() % 100 - 50;\r\n            }\r\n        }\r\n    } <color=#FF6347>else</color> {\r\n        size = <color=#D3D3D3>atoi</color>(argv[1]);\r\n        <color=#FF6347>if</color> (argc < size * size + 2) {\r\n            <color=#D3D3D3>printf</color>(<color=#FFFF00>\"Invalid argument count for matrix size %d\\n\"</color>, size);\r\n            <color=#D3D3D3>return</color> 0;\r\n        }\r\n        \r\n        matrix = <color=#D3D3D3>malloc</color>(sizeof(<color=#D3D3D3>int</color> *) * size);\r\n        <color=#FF6347>for</color> (<color=#D3D3D3>int</color> i = 0; i < size*size; i++) {\r\n            <color=#D3D3D3>int</color> y = i / size;\r\n            <color=#D3D3D3>int</color> x = i % size;\r\n            <color=#FF6347>if</color> (x == 0) {\r\n                matrix[y] = <color=#D3D3D3>malloc</color>(size * sizeof(<color=#D3D3D3>int</color>));\r\n            }\r\n            matrix[y][x] = <color=#D3D3D3>atoi</color>(argv[i + 2]);\r\n        }\r\n    }\r\n\r\n    <color=#D3D3D3>printf</color>(<color=#FFFF00>\"\\nInput matrix:\\n\"</color>);\r\n    print_matrix(size, matrix);\r\n    \r\n    shell_sort(size, matrix);\r\n    \r\n    <color=#D3D3D3>printf</color>(<color=#FFFF00>\"\\nSorted matrix:\\n\"</color>);\r\n    print_matrix(size, matrix);\r\n    \r\n    <color=#D3D3D3>free</color>(matrix);\r\n    <color=#D3D3D3>return</color> 0;\r\n}\r\n\r\n// <color=#808080>vim: ts=4: sw=4:</color>\r\n";

	private static readonly int[] ShellGaps = new int[3] { 7, 3, 1 };

	public string Run(int size = 10, int[,]? initialMatrix = null)
	{
		int[,] matrix;
		if (initialMatrix != null)
		{
			matrix = initialMatrix;
			size = initialMatrix.GetLength(0);
		}
		else
		{
			matrix = GenerateRandomMatrix(size);
		}
		string text = "Input matrix:\n" + MatrixToString(matrix) + "\n";
		ShellSortAntidiagonal(matrix, size);
		return text + "Sorted matrix:\n" + MatrixToString(matrix) + "\n";
	}

	public string RunFromString(string? input = null)
	{
		if (input == null)
		{
			return Run();
		}
		string[] array = input.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
		if (array.Length == 0)
		{
			return Run();
		}
		if (array.Length == 1)
		{
			if (!int.TryParse(array[0], out var result))
			{
				return "<color=#FF0000>Error: Invalid size argument.</color>\n\n";
			}
			return Run(result);
		}
		if (!int.TryParse(array[0], out var result2))
		{
			return "<color=#FF0000>Error: Invalid size argument.</color>\n\n";
		}
		if (array.Length < result2 * result2 + 1)
		{
			return $"<color=#FF0000>Error: The number of matrix elements does not match the expected size. Expected {result2}x{result2} matrix, but got {array.Length - 1} elements.</color>\n\n";
		}
		int[,] array2 = new int[result2, result2];
		for (int i = 0; i < result2 * result2; i++)
		{
			if (!int.TryParse(array[i + 1], out array2[i / result2, i % result2]))
			{
				return "<color=#FF0000>Error: Invalid matrix element '" + array[i + 1] + "'.</color>\n\n";
			}
		}
		return Run(result2, array2);
	}

	private void ShellSortAntidiagonal(int[,] matrix, int size)
	{
		int[] shellGaps = ShellGaps;
		foreach (int num in shellGaps)
		{
			if (num >= size)
			{
				continue;
			}
			for (int j = num; j < size; j++)
			{
				for (int num2 = j - num; num2 >= 0; num2 -= num)
				{
					int num3 = num2;
					int num4 = num2 + num;
					int num5 = matrix[num3, size - num3 - 1];
					int num6 = matrix[num4, size - num4 - 1];
					if (num5 > num6)
					{
						matrix[num3, size - num3 - 1] = num6;
						matrix[num4, size - num4 - 1] = num5;
					}
				}
			}
		}
	}

	private static int[,] GenerateRandomMatrix(int size)
	{
		Random random = new Random();
		int[,] array = new int[size, size];
		for (int i = 0; i < size; i++)
		{
			for (int j = 0; j < size; j++)
			{
				array[i, j] = random.Next(-50, 50);
			}
		}
		return array;
	}

	private static string MatrixToString(int[,] matrix)
	{
		int length = matrix.GetLength(0);
		string text = string.Empty;
		for (int i = 0; i < length; i++)
		{
			for (int j = 0; j < length; j++)
			{
				text = ((j != length - i - 1) ? (text + $"{matrix[i, j],3} ") : (text + $"<color=#FF0000>{matrix[i, j],3}</color> "));
			}
			text += "\n";
		}
		return text;
	}
}
namespace LethalASD
{
	[BepInPlugin("dev.ammir.lethalasd", "LethalASD", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class LethalASD : BaseUnityPlugin
	{
		public static MatrixSorter sorter = new MatrixSorter();

		public static TerminalNode runNode = TerminalApi.CreateTerminalNode("", true, "");

		public static LethalASD Instance { get; private set; } = null;


		internal static ManualLogSource Logger { get; private set; } = null;


		internal static Harmony? Harmony { get; set; }

		private void Awake()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected O, but got Unknown
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			Patch();
			Events.TerminalParsedSentence += new TerminalParseSentenceEventHandler(OnTerminalCommandSend);
			runNode.maxCharactersToType = 500;
			TerminalKeyword val = TerminalApi.CreateTerminalKeyword("run", true, runNode);
			TerminalApi.AddTerminalKeyword(val, new CommandInfo
			{
				Description = "[size] [matrix ]\nSorts a matrix",
				Category = "Other"
			});
			TerminalKeyword val2 = TerminalApi.CreateTerminalKeyword("cat", true, (TerminalNode)null);
			TerminalKeyword val3 = TerminalApi.CreateTerminalKeyword("main.c", false, (TerminalNode)null);
			TerminalNode val4 = TerminalApi.CreateTerminalNode(MatrixSorter.SOURCE_CODE, true, "");
			val2 = TerminalExtenstionMethods.AddCompatibleNoun(val2, val3, val4);
			TerminalApi.AddTerminalKeyword(val2);
			TerminalApi.AddTerminalKeyword(val3);
			Logger.LogInfo((object)"dev.ammir.lethalasd v1.0.0 has loaded!");
		}

		private void OnTerminalCommandSend(object sender, TerminalParseSentenceEventArgs e)
		{
			string terminalInput = TerminalApi.GetTerminalInput();
			if (terminalInput == "run")
			{
				runNode.displayText = sorter.RunFromString();
			}
			else if (terminalInput.StartsWith("run ") && terminalInput.Length >= 5)
			{
				string input = terminalInput.Split(" ", 2, StringSplitOptions.RemoveEmptyEntries)[1];
				runNode.displayText = sorter.RunFromString(input);
			}
		}

		internal static void Patch()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("dev.ammir.lethalasd");
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Finished patching!");
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogDebug((object)"Finished unpatching!");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "dev.ammir.lethalasd";

		public const string PLUGIN_NAME = "LethalASD";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace LethalASD.Patches
{
	[HarmonyPatch(typeof(Terminal))]
	public class TerminalPatch
	{
		[HarmonyPatch("ParsePlayerSentence")]
		[HarmonyPostfix]
		private static void HandleAllRunSubcommands(ref TerminalNode __result)
		{
			string terminalInput = TerminalApi.GetTerminalInput();
			if (terminalInput.StartsWith("run"))
			{
				__result = LethalASD.runNode;
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}