Decompiled source of CWOffline Fixed v1.1.0

plugins/CWOffline.dll

Decompiled 14 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
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 BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
using TMPro;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.PropertyVariants;
using UnityEngine.Localization.Settings;
using UnityEngine.SceneManagement;
using Zorro.Core;
using Zorro.Settings;
using Zorro.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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]
	[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 CWOffline
{
	public static class CWOfflineAPI
	{
		public static bool IsGameOffline()
		{
			return !Plugin.CheckInternet();
		}

		public static bool IsGameOnline()
		{
			return Plugin.CheckInternet();
		}
	}
	[HarmonyPatch(typeof(MainMenuHandler), "Host")]
	internal static class HostPatch
	{
		[HarmonyPrefix]
		private static bool Prefix(MainMenuHandler __instance, int saveIndex)
		{
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Expected O, but got Unknown
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Expected O, but got Unknown
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Expected O, but got Unknown
			//IL_00bf: Expected O, but got Unknown
			//IL_00c0: Expected O, but got Unknown
			if (CWOfflineAPI.IsGameOnline())
			{
				return true;
			}
			if (__instance.m_Hosting)
			{
				return false;
			}
			__instance.m_Hosting = true;
			if (!PhotonNetwork.OfflineMode)
			{
				Plugin.Logger.LogInfo((object)"Switching Photon to offline mode...");
				PhotonNetwork.OfflineMode = true;
			}
			((UIPageHandler)__instance.UIHandler).TransistionToPage<MainMenuLoadingPage>().SetText(LocalizationKeys.GetLocalizedString((Keys)133));
			SaveSystem.SelectCurrentSave(saveIndex);
			SaveSystem.UsingSave(true);
			RoomOptions val = new RoomOptions
			{
				IsVisible = false,
				MaxPlayers = 1
			};
			Hashtable val2 = new Hashtable();
			((Dictionary<object, object>)val2).Add((object)"P", (object)1);
			((Dictionary<object, object>)val2).Add((object)"L", (object)((IntSetting)GameHandler.Instance.SettingsHandler.GetSetting<PreferredLanguageMatchmakingSetting>()).Value);
			((Dictionary<object, object>)val2).Add((object)"M", (object)GameHandler.GetPluginHash());
			val.CustomRoomProperties = val2;
			RoomOptions val3 = val;
			bool num = PhotonNetwork.CreateRoom("OFFLINE", val3, TypedLobby.Default, (string[])null);
			__instance.m_Hosting = false;
			if (!num)
			{
				Plugin.Logger.LogError((object)"Failed to create the offline room");
				Modal.ShowError(LocalizationKeys.GetLocalizedString((Keys)262), "");
				return false;
			}
			Plugin.Logger.LogInfo((object)"Created offline room");
			if (!SaveSystem.HaveCurrentSave && SaveSystem.USING_SAVE)
			{
				SaveSystem.MakeNewSave();
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(MainMenuHandler), "OnCreatedRoom")]
	internal static class CreatedRoomPatch
	{
		[HarmonyPrefix]
		private static bool Prefix(MainMenuHandler __instance)
		{
			if (!PhotonNetwork.OfflineMode)
			{
				return true;
			}
			((StateMachine<ConnectionState>)(object)RetrievableSingleton<ConnectionStateHandler>.Instance.StateMachine).SwitchState<InRoomState>();
			__instance.LoadGameScene();
			return false;
		}
	}
	[HarmonyPatch(typeof(MainMenuMainPage), "OnPageEnter")]
	internal static class SetupMenuPatch
	{
		[HarmonyPostfix]
		private static void Postfix(MainMenuMainPage __instance)
		{
			if (CWOfflineAPI.IsGameOnline())
			{
				return;
			}
			((Component)__instance.joinButton).gameObject.SetActive(false);
			((Component)__instance.joinRoomButton).gameObject.SetActive(false);
			TextMeshProUGUI componentInChildren = ((Component)__instance.hostButton).GetComponentInChildren<TextMeshProUGUI>(true);
			if ((Object)(object)componentInChildren != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)componentInChildren).GetComponent<GameObjectLocalizer>());
				((TMP_Text)componentInChildren).text = (Plugin.IsRussian ? "ИГРАТЬ ОФЛАЙН" : "PLAY OFFLINE");
			}
			Transform parent = ((Component)__instance.hostButton).transform.parent;
			if (!((Object)(object)parent.Find("OfflineNotice") != (Object)null))
			{
				Transform val = FindDeep(((Component)__instance).transform, "HIGH SERVER LOAD");
				if ((Object)(object)val == (Object)null)
				{
					Plugin.Logger.LogWarning((object)"Can't find the server notice text to copy, no offline notice on the menu");
					return;
				}
				GameObject obj = Object.Instantiate<GameObject>(((Component)val).gameObject, parent);
				((Object)obj).name = "OfflineNotice";
				Object.Destroy((Object)(object)obj.GetComponentInChildren<GameObjectLocalizer>());
				((TMP_Text)obj.GetComponentInChildren<TextMeshProUGUI>()).text = (Plugin.IsRussian ? "НЕТ ИНТЕРНЕТА, ИГРА ОФЛАЙН." : "YOU ARE CURRENTLY OFFLINE.");
				obj.transform.SetAsFirstSibling();
				obj.SetActive(true);
			}
		}

		private static Transform FindDeep(Transform parent, string name)
		{
			Transform[] componentsInChildren = ((Component)parent).GetComponentsInChildren<Transform>(true);
			foreach (Transform val in componentsInChildren)
			{
				if (((Object)val).name == name)
				{
					return val;
				}
			}
			return null;
		}
	}
	[HarmonyPatch(typeof(StartGameDoorInteractable), "Interact")]
	internal static class StartGameDoorInteractablePatch
	{
		[HarmonyPrefix]
		private static bool Prefix(StartGameDoorInteractable __instance)
		{
			if (!PhotonNetwork.OfflineMode || (Object)(object)__instance.m_NetworkHandler == (Object)null)
			{
				return true;
			}
			__instance.m_NetworkHandler.RequestStartGame();
			return false;
		}
	}
	[HarmonyPatch(typeof(Player), "Start")]
	internal static class AddPlayerPatch
	{
		[HarmonyPostfix]
		private static IEnumerator Postfix(IEnumerator result)
		{
			if (PhotonNetwork.OfflineMode)
			{
				Scene activeScene = SceneManager.GetActiveScene();
				if (((Scene)(ref activeScene)).name == "SurfaceScene")
				{
					GameObject val = GameObject.Find("Tools/Teleporter");
					if ((Object)(object)val != (Object)null)
					{
						val.SetActive(false);
					}
				}
			}
			while (result.MoveNext())
			{
				yield return result.Current;
			}
		}
	}
	[ContentWarningPlugin("CWOffline", "1.1.0", true)]
	[BepInPlugin("CWOffline", "CWOffline", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ConfigEntry<bool> forceOffline;

		private static int connectionCache;

		internal static ManualLogSource Logger { get; private set; }

		internal static bool IsRussian
		{
			get
			{
				//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)
				try
				{
					Locale selectedLocale = LocalizationSettings.SelectedLocale;
					object obj;
					if (selectedLocale == null)
					{
						obj = null;
					}
					else
					{
						LocaleIdentifier identifier = selectedLocale.Identifier;
						obj = ((LocaleIdentifier)(ref identifier)).Code;
					}
					return ((string)obj)?.StartsWith("ru") ?? false;
				}
				catch
				{
					return false;
				}
			}
		}

		internal static bool CheckInternet()
		{
			if (connectionCache != 0)
			{
				return connectionCache == 2;
			}
			try
			{
				HttpWebRequest obj = (HttpWebRequest)WebRequest.Create("https://www.gstatic.com/generate_204");
				obj.KeepAlive = false;
				obj.Timeout = 2000;
				using (obj.GetResponse())
				{
					connectionCache = 2;
					return true;
				}
			}
			catch
			{
				connectionCache = 1;
				return false;
			}
		}

		private void Awake()
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BaseUnityPlugin)this).Logger;
			forceOffline = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ForceOffline", false, "Force Offline Mode.");
			connectionCache = (forceOffline.Value ? 1 : 0);
			new Harmony("com.redbigz.CWOfflinePatch").PatchAll();
			Logger.LogInfo((object)"Checking if offline...");
			if (CheckInternet())
			{
				Logger.LogInfo((object)"Game is online.");
			}
			else
			{
				Logger.LogWarning((object)(forceOffline.Value ? "Game is offline (ForceOffline)." : "Game is offline."));
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}