Decompiled source of TebConnectInfo v0.1.2

TebConnectInfo.dll

Decompiled 6 months ago
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("TebConnectInfo")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TebConnectInfo")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("19a0596c-30d8-441b-89fb-2de5f011f2bb")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Tebbeh.TebConnectInfo;

[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("com.tebbeh.mod.TebConnectInfo", "TebConnectInfo", "0.1.0")]
public class TebConnectInfo : BaseUnityPlugin
{
	public class GetConnectInfo
	{
		[HarmonyPatch(typeof(Chat), "OnNewChatMessage")]
		private class OnConnect
		{
			private static void Prefix(ref UserInfo user, ref Type type, ref string text)
			{
				if ((int)type == 2)
				{
					string name = user.Name;
					if (!string.IsNullOrEmpty(name))
					{
						string text2 = GetjoinMessage();
						text2 = text2.Replace("{player}", name);
						PostToDiscord(text2);
					}
				}
			}
		}

		[HarmonyPatch(typeof(ZNet), "RPC_Disconnect")]
		private class RPC_Disconnect
		{
			private static void Prefix(ZRpc rpc)
			{
				ZNetPeer peer = ZNet.instance.GetPeer(rpc);
				if (peer != null)
				{
					string playerName = peer.m_playerName;
					if (playerName.Length > 0)
					{
						string text = GetleaveMessage();
						text = text.Replace("{player}", playerName);
						PostToDiscord(text);
					}
				}
			}
		}
	}

	private const string ModName = "TebConnectInfo";

	private const string ModVersion = "0.1.0";

	private const string Author = "com.tebbeh.mod";

	private const string ModGUID = "com.tebbeh.mod.TebConnectInfo";

	private static string ConfigFileName = "com.tebbeh.mod.TebConnectInfo.cfg";

	private static string ConfigFileFullPath;

	private readonly Harmony HarmonyInstance = new Harmony("com.tebbeh.mod.TebConnectInfo");

	public static readonly ManualLogSource TebLogger;

	internal static ConfigEntry<string> WebhookURL;

	internal static ConfigEntry<string> WebhookName;

	internal static ConfigEntry<string> joinMessage;

	internal static ConfigEntry<string> leaveMessage;

	private readonly ConfigurationManagerAttributes AdminConfig = new ConfigurationManagerAttributes
	{
		IsAdminOnly = true
	};

	private readonly ConfigurationManagerAttributes ClientConfig = new ConfigurationManagerAttributes
	{
		IsAdminOnly = false
	};

	public static string GetWebhookURL()
	{
		return WebhookURL.Value;
	}

	public static string GetWebhookName()
	{
		return WebhookName.Value;
	}

	public static string GetjoinMessage()
	{
		return joinMessage.Value;
	}

	public static string GetleaveMessage()
	{
		return leaveMessage.Value;
	}

	private void AddConfig<T>(string key, string section, string description, bool synced, T value, ref ConfigEntry<T> configEntry)
	{
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Expected O, but got Unknown
		string extendedDescription = GetExtendedDescription(description, synced);
		configEntry = ((BaseUnityPlugin)this).Config.Bind<T>(section, key, value, new ConfigDescription(extendedDescription, (AcceptableValueBase)null, new object[1] { synced ? AdminConfig : ClientConfig }));
	}

	public string GetExtendedDescription(string description, bool synchronizedSetting)
	{
		return description + (synchronizedSetting ? " [Synced with Server]" : " [Not Synced with Server]");
	}

	private void Awake()
	{
		AddConfig("WebhookURL", "General", "Webhook url (string).", synced: true, "", ref WebhookURL);
		AddConfig("WebhookName", "General", "Webhook send as username (string).", synced: true, "TebConnectInfo", ref WebhookName);
		AddConfig("JoinMessage", "Messages", "Join message to post with webhook, manipulate after your needs. Standard is for Discord! (string).", synced: true, ":green_circle: **{player}** has come online!", ref joinMessage);
		AddConfig("LeaveMessage", "Messages", "Leave message to post with webhook, manipulate after your needs. Standard is for Discord! (string).", synced: true, ":red_circle: **{player}** has gone offline!", ref leaveMessage);
		Assembly executingAssembly = Assembly.GetExecutingAssembly();
		HarmonyInstance.PatchAll(executingAssembly);
		SetupWatcher();
	}

	private void OnDestroy()
	{
		((BaseUnityPlugin)this).Config.Save();
	}

	private void SetupWatcher()
	{
		FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
		fileSystemWatcher.Changed += ReadConfigValues;
		fileSystemWatcher.Created += ReadConfigValues;
		fileSystemWatcher.Renamed += ReadConfigValues;
		fileSystemWatcher.IncludeSubdirectories = true;
		fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
		fileSystemWatcher.EnableRaisingEvents = true;
	}

	private void ReadConfigValues(object sender, FileSystemEventArgs e)
	{
		if (!File.Exists(ConfigFileFullPath))
		{
			return;
		}
		try
		{
			TebLogger.LogDebug((object)"Attempting to reload configuration...");
			((BaseUnityPlugin)this).Config.Reload();
		}
		catch
		{
			TebLogger.LogError((object)("There was an issue loading " + ConfigFileName));
		}
	}

	private static void PostToDiscord(string content)
	{
		string value = WebhookURL.Value;
		string webhookUsername = WebhookName.Value;
		if (content == "" || value == "")
		{
			return;
		}
		WebRequest discordAPI = WebRequest.Create(value);
		discordAPI.Method = "POST";
		discordAPI.ContentType = "application/json";
		discordAPI.GetRequestStreamAsync().ContinueWith(delegate(Task<Stream> t)
		{
			using StreamWriter streamWriter = new StreamWriter(t.Result);
			string value2 = "{\"content\":\"" + escape(content) + "\"" + ((webhookUsername == "") ? "" : (", \"username\":\"" + escape(webhookUsername) + "\"")) + "}";
			streamWriter.WriteAsync(value2).ContinueWith((Task _) => discordAPI.GetResponseAsync());
		});
		static string escape(string s)
		{
			return s.Replace("\\", "\\\\").Replace("\"", "\\\"");
		}
	}

	static TebConnectInfo()
	{
		string configPath = Paths.ConfigPath;
		char directorySeparatorChar = Path.DirectorySeparatorChar;
		ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
		TebLogger = Logger.CreateLogSource("TebConnectInfo");
		WebhookURL = null;
		WebhookName = null;
		joinMessage = null;
		leaveMessage = null;
	}
}