Decompiled source of RunReportSanitizer v1.0.3

RoR2NameSanitizer.dll

Decompiled 3 months ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
using BepInEx;
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("RoR2NameSanitizer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RoR2NameSanitizer")]
[assembly: AssemblyTitle("RoR2NameSanitizer")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 RoR2NameSanitizer
{
	[BepInPlugin("gm.ror2.namesanitizer", "RoR2 Name Sanitizer", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private Harmony _harmony;

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			_harmony = new Harmony("gm.ror2.namesanitizer");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[RoR2 Name Sanitizer] Loaded and patching.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	internal static class NameSan
	{
		private static readonly Regex DotSpace = new Regex("\\.\\s+");

		private static readonly Regex Spaces = new Regex("\\s+");

		private static readonly Regex BadChars = new Regex("[^A-Za-z0-9_.:\\-]");

		public static string SanitizeLocalName(string s)
		{
			if (string.IsNullOrEmpty(s))
			{
				return s;
			}
			s = DotSpace.Replace(s, ".");
			s = Spaces.Replace(s, "_");
			s = BadChars.Replace(s, "_");
			return s;
		}

		public static void SanitizeComposedValueNodes(XElement root)
		{
			foreach (XElement item in from e in ((XContainer)root).Descendants()
				where e.Name.LocalName.Equals("bodyName", StringComparison.OrdinalIgnoreCase) || e.Name.LocalName.Equals("killerBodyName", StringComparison.OrdinalIgnoreCase) || e.Name.LocalName.Equals("gameModeName", StringComparison.OrdinalIgnoreCase)
				select e)
			{
				string text = (string)item;
				string text2 = SanitizeLocalName(text);
				if (!string.Equals(text, text2, StringComparison.Ordinal))
				{
					item.Value = text2;
				}
			}
		}

		public static void SanitizeFieldTagNames(XElement playerInfoElement)
		{
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			XElement val = ((XContainer)playerInfoElement).Element(XName.op_Implicit("statSheet"));
			if (val == null)
			{
				return;
			}
			XElement val2 = ((XContainer)val).Element(XName.op_Implicit("fields"));
			if (val2 == null)
			{
				return;
			}
			foreach (XElement item in ((XContainer)val2).Elements().ToList())
			{
				string localName = item.Name.LocalName;
				string text = SanitizeLocalName(localName);
				if (!string.Equals(localName, text, StringComparison.Ordinal))
				{
					XElement val3 = new XElement(XName.Get(text), new object[2]
					{
						item.Attributes(),
						((XContainer)item).Nodes()
					});
					((XNode)item).ReplaceWith((object)val3);
				}
			}
		}
	}
	[HarmonyPatch]
	internal static class PlayerInfo_FromXml_Prefix
	{
		private static MethodBase TargetMethod()
		{
			Type type = AccessTools.TypeByName("RoR2.RunReport+PlayerInfo");
			return AccessTools.Method(type, "FromXml", new Type[2]
			{
				typeof(XElement),
				type.MakeByRefType()
			}, (Type[])null);
		}

		private static void Prefix(XElement element)
		{
			try
			{
				NameSan.SanitizeComposedValueNodes(element);
				NameSan.SanitizeFieldTagNames(element);
			}
			catch
			{
			}
		}
	}
	[HarmonyPatch]
	internal static class StatSheet_FromXml_Finalizer
	{
		private static MethodBase TargetMethod()
		{
			Type type = AccessTools.TypeByName("RoR2.Stats.StatSheet");
			return AccessTools.Method(type, "FromXml", new Type[2]
			{
				typeof(XElement),
				type.MakeByRefType()
			}, (Type[])null);
		}

		private static Exception Finalizer(Exception __exception)
		{
			if (__exception is XmlException ex && ex.Message.Contains("0x20") && ex.Message.Contains("cannot be included in a name"))
			{
				return null;
			}
			return __exception;
		}
	}
}