Decompiled source of WorldNamePathFix v1.0.0

WorldNamePathFix.dll

Decompiled 3 weeks ago
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("1.0.0.0")]
namespace WorldNamePathFix;

[BepInPlugin("pascal.worldnamepathfix", "WorldNamePathFix", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	private static ManualLogSource Log;

	private static bool _loggedOnce;

	private void Awake()
	{
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		try
		{
			Type type = AccessTools.TypeByName("AltBiomeWorldData");
			if (type == null)
			{
				Log.LogWarning((object)"AltBiomeWorldData nicht gefunden - Patch nicht noetig/anwendbar.");
				return;
			}
			MethodInfo methodInfo = AccessTools.Method(type, "GetFilePath", new Type[1] { typeof(string) }, (Type[])null);
			if (methodInfo == null)
			{
				Log.LogWarning((object)"AltBiomeWorldData.GetFilePath(string) nicht gefunden.");
				return;
			}
			MethodInfo method = typeof(Plugin).GetMethod("Prefix", BindingFlags.Static | BindingFlags.NonPublic);
			new Harmony("pascal.worldnamepathfix").Patch((MethodBase)methodInfo, new HarmonyMethod(method), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			Log.LogInfo((object)"Patch auf AltBiomeWorldData.GetFilePath aktiv.");
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Patch fehlgeschlagen: " + ex));
		}
	}

	private static void Prefix(ref string worldName)
	{
		if (string.IsNullOrEmpty(worldName))
		{
			return;
		}
		char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
		if (worldName.IndexOfAny(invalidFileNameChars) < 0)
		{
			return;
		}
		char[] array = worldName.ToCharArray();
		for (int i = 0; i < array.Length; i++)
		{
			if (Array.IndexOf(invalidFileNameChars, array[i]) >= 0)
			{
				array[i] = '_';
			}
		}
		string text = new string(array);
		if (!_loggedOnce && Log != null)
		{
			Log.LogInfo((object)("Weltname fuer Cache-Pfad bereinigt: '" + worldName + "' -> '" + text + "'"));
			_loggedOnce = true;
		}
		worldName = text;
	}
}