Decompiled source of LycansCustomCode v1.0.0

plugins/LycansCustomCode.dll

Decompiled 3 months ago
using System;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Logging;
using Fusion.Photon.Realtime;
using LycansCustomCode.Hooks;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using On;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("LycansCustomCode")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+497249915070839646bbf2b401aa1121210fff98")]
[assembly: AssemblyProduct("LycansCustomCode")]
[assembly: AssemblyTitle("LycansCustomCode")]
[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 LycansCustomCode
{
	internal static class Log
	{
		private static ManualLogSource _logSource;

		internal static void Init(ManualLogSource logSource)
		{
			_logSource = logSource;
		}

		internal static void Debug(object data)
		{
			if (_logSource != null)
			{
				_logSource.LogDebug(data);
			}
			else
			{
				Debug.Log((object)("[LycansCustomCode][Debug] " + data));
			}
		}

		internal static void Error(object data)
		{
			if (_logSource != null)
			{
				_logSource.LogError(data);
			}
			else
			{
				Debug.LogError((object)("[LycansCustomCode][Error] " + data));
			}
		}

		internal static void Fatal(object data)
		{
			if (_logSource != null)
			{
				_logSource.LogFatal(data);
			}
			else
			{
				Debug.LogError((object)("[LycansCustomCode][Fatal] " + data));
			}
		}

		internal static void Info(object data)
		{
			if (_logSource != null)
			{
				_logSource.LogInfo(data);
			}
			else
			{
				Debug.Log((object)("[LycansCustomCode][Info] " + data));
			}
		}

		internal static void Message(object data)
		{
			if (_logSource != null)
			{
				_logSource.LogMessage(data);
			}
			else
			{
				Debug.Log((object)("[LycansCustomCode][Message] " + data));
			}
		}

		internal static void Warning(object data)
		{
			if (_logSource != null)
			{
				_logSource.LogWarning(data);
			}
			else
			{
				Debug.LogWarning((object)("[LycansCustomCode][Warning] " + data));
			}
		}
	}
	[BepInPlugin("Knard.LycansCustomCode", "LycansCustomCode", "1.0.0")]
	[BepInProcess("Lycans.exe")]
	public class LycansCustomCode : BaseUnityPlugin
	{
		public const string PLUGIN_GUID = "Knard.LycansCustomCode";

		public const string PLUGIN_AUTHOR = "Knard";

		public const string PLUGIN_NAME = "LycansCustomCode";

		public const string PLUGIN_VERSION = "1.0.0";

		public static string SteamRoomCode;

		private const string STEAM_ROOM_CODE_URL = "https://lycansmods.goneworlds.ovh/api/getSteamRoomCode/";

		public static LycansCustomCode instance { get; private set; }

		private void Awake()
		{
			instance = this;
			Log.Init(((BaseUnityPlugin)this).Logger);
			new RoomCodeCreate();
			Log.Info("Mod Started : LycansCustomCode by Knard(1.0.0)");
			deleayedLoading();
		}

		public async void deleayedLoading()
		{
			Log.Info("[main] Starting delayed loading of Steam room code, waiting 3 seconds...");
			await Task.Delay(3000);
			Log.Info("[main] Attempting to load Steam room code after delay");
			LoadSteamRoomCodeOnStartup();
		}

		private static async void LoadSteamRoomCodeOnStartup()
		{
			string steamId;
			try
			{
				Log.Info("[main] Attempting to read SteamID for room code lookup");
				AuthenticationValues authValues = SteamAuth.Instance.InitToken();
				Log.Info("[main] Steam AuthenticationValues obtained, UserId=" + authValues.UserId);
				steamId = authValues.UserId;
				Log.Info("[main] SteamID read successfully: " + steamId);
			}
			catch (Exception ex2)
			{
				Exception ex = ex2;
				Log.Warning("[main] Steam roomCode lookup skipped: failed to read SteamID: " + ex.Message);
				return;
			}
			if (!IsValidSteamId(steamId))
			{
				Log.Warning("[main] Steam roomCode lookup skipped: invalid SteamID");
				return;
			}
			string roomCode = await GetSteamRoomCodeAsync(steamId);
			if (string.IsNullOrWhiteSpace(roomCode))
			{
				Log.Info("[main] No Steam roomCode found for this SteamID");
				return;
			}
			SteamRoomCode = roomCode.Trim();
			Log.Info("[main] Steam roomCode loaded");
		}

		private static bool IsValidSteamId(string steamId)
		{
			if (string.IsNullOrWhiteSpace(steamId))
			{
				return false;
			}
			steamId = steamId.Trim();
			if (steamId == "0")
			{
				return false;
			}
			string text = steamId;
			foreach (char c in text)
			{
				if (!char.IsLetterOrDigit(c) && c != '-')
				{
					return false;
				}
			}
			return true;
		}

		public static async Task<string> GetSteamRoomCodeAsync(string steamId)
		{
			if (string.IsNullOrWhiteSpace(steamId))
			{
				Log.Warning("[main] GetSteamRoomCodeAsync aborted: steamId is empty");
				return null;
			}
			steamId = steamId.Trim();
			string url = "https://lycansmods.goneworlds.ovh/api/getSteamRoomCode/" + Uri.EscapeDataString(steamId);
			Log.Info("[main] GetSteamRoomCodeAsync fetching roomCode for steamId=" + steamId);
			try
			{
				string roomCode = await LoadJsonStringAsync(url);
				if (string.IsNullOrWhiteSpace(roomCode))
				{
					Log.Info("[main] GetSteamRoomCodeAsync: no roomCode found for steamId=" + steamId);
					return null;
				}
				roomCode = roomCode.Trim();
				Log.Info("[main] GetSteamRoomCodeAsync: roomCode found for steamId=" + steamId);
				return roomCode;
			}
			catch (Exception ex)
			{
				Log.Error("[main] GetSteamRoomCodeAsync failed: " + ex);
				return null;
			}
		}

		public static async Task<string> LoadJsonStringAsync(string url)
		{
			if (string.IsNullOrWhiteSpace(url))
			{
				return null;
			}
			ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
			using WebClient client = new WebClient();
			client.Headers[HttpRequestHeader.Accept] = "application/json";
			client.Headers[HttpRequestHeader.CacheControl] = "no-cache";
			client.Headers[HttpRequestHeader.UserAgent] = "LycansDuckModpackV2";
			try
			{
				return JsonConvert.DeserializeObject<string>(await client.DownloadStringTaskAsync(new Uri(url)));
			}
			catch (WebException ex2)
			{
				WebException ex = ex2;
				if (ex.Response is HttpWebResponse response && response.StatusCode == HttpStatusCode.NotFound)
				{
					return null;
				}
				throw;
			}
		}

		private void Start()
		{
		}

		private void OnDestroy()
		{
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "LycansCustomCode";

		public const string PLUGIN_NAME = "LycansCustomCode";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace LycansCustomCode.Hooks
{
	internal class RoomCodeCreate
	{
		public static RoomCodeCreate Instance;

		public RoomCodeCreate()
		{
			Instance = this;
			Hooks();
		}

		public void Hooks()
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			RoomCode.Create += new hook_Create(RoomCode_Create);
			Log.Info("[RoomCodeCreate] Hooks applied.");
		}

		private string RoomCode_Create(orig_Create orig, int length)
		{
			if (!string.IsNullOrWhiteSpace(LycansCustomCode.SteamRoomCode))
			{
				Log.Info("[RoomCodeCreate] Returning custom room code: " + LycansCustomCode.SteamRoomCode);
				return LycansCustomCode.SteamRoomCode;
			}
			Log.Info("[RoomCodeCreate] No custom room code set, calling original method.");
			return orig.Invoke(length);
		}
	}
}