using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace undead_universal_patch
{
public static class ConfigUtils
{
public static string GetGameserverProtocol()
{
return GameserverConfig.SecureProtocol.Value ? "https" : "http";
}
public static string GetGameserverSocketProtocol()
{
return GameserverConfig.SecureSocketProtocol.Value ? "wss" : "ws";
}
public static string GetGameserverBaseUrl()
{
string protocol = GetGameserverProtocol();
return $"{protocol}://{GameserverConfig.Host.Value}";
}
public static string GetGameserverSocketBaseUrl()
{
string protocol = GameserverConfig.SecureProtocol.Value ? "wss" : "ws";
if (GameserverConfig.UseSocketHost.Value) return $"{protocol}://{GameserverConfig.SocketHost.Value}";
else return $"{GetGameserverProtocol()}://${GameserverConfig.Host.Value}";
}
public static string GetNameserverProtocol()
{
return NameserverConfig.SecureProtocol.Value ? "https" : "http";
}
}
public static class GenericUtils
{
public static void PrintAllNamespaces()
{
// ChatGPT GPT-4o: Is it possible to list all namespaces available in the context?
/*
* Response:
* Listing all namespaces directly isn't straightforward because .NET and C# don't have a built-in method to enumerate all namespaces.
* However, you can list all types within the currently loaded assemblies and then deduce the namespaces from these types.
*/
HashSet<string> namespaces = [];
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
try
{
foreach (var type in assembly.GetTypes())
if (!string.IsNullOrEmpty(type.Namespace)) namespaces.Add(type.Namespace);
}
catch (ReflectionTypeLoadException ex)
{
// Handle the case where some types cannot be loaded.
foreach (var type in ex.Types)
if (type != null && !string.IsNullOrEmpty(type.Namespace)) namespaces.Add(type.Namespace);
}
}
Plugin.Log.LogDebug("=====================");
Plugin.Log.LogDebug($"Available namespaces:");
foreach (var ns in namespaces.OrderBy(ns => ns))
Plugin.Log.LogDebug($"- '{ns}'");
Plugin.Log.LogDebug("=====================");
}
public static Uri RewriteUri(string originalUrl, string newHost, bool secure, string path, string scheme)
{
return RewriteUri(new Uri(originalUrl), newHost, secure, path, scheme);
}
public static Uri RewriteUri(Uri original, string newHost, bool secure, string path, string scheme)
{
UriBuilder newUri = new(original);
string host;
int port;
if (newHost.Contains(":"))
{
string[] host_port = newHost.Split(':');
if (host_port.Length > 2)
{
Plugin.Log.LogFatal("Could not rewrite URL: The new target host was not valid!");
return null;
}
host = host_port[0];
port = int.Parse(host_port[1]);
}
else
{
host = newHost;
if (secure) port = 443;
else port = 80;
}
newUri.Host = host;
newUri.Path = path;
newUri.Port = port;
newUri.Scheme = scheme;
return newUri.Uri;
}
}
public static class PatchUtils
{
public static Uri RewriteUrlForNameserver(Uri targetUri)
{
if (targetUri.Host.Contains("ns.rec.net") && NameserverConfig.UseNameserverHost.Value)
{
return GenericUtils.RewriteUri(targetUri, NameserverConfig.Host.Value,
NameserverConfig.SecureProtocol.Value, NameserverConfig.Path.Value,
ConfigUtils.GetNameserverProtocol());
}
return targetUri;
}
public static Uri RewriteUrlForGameserver(Uri targetUri)
{
UriBuilder parsedUrl = new(targetUri);
// Rewrites
if (GameserverConfig.UseGameserverHost.Value &&
(parsedUrl.Host.Contains("azurewebsites.net") || parsedUrl.Host.Contains("againstgrav.com")))
{
return GenericUtils.RewriteUri(targetUri, GameserverConfig.Host.Value,
GameserverConfig.SecureProtocol.Value, targetUri.AbsolutePath,
ConfigUtils.GetGameserverProtocol());
}
return parsedUrl.Uri;
}
public static Uri RewriteUrlForGameserver(string targetUrl)
{
return RewriteUrlForGameserver(new Uri(targetUrl));
}
public static Uri RewriteUrlForSocket(string targetUrl)
{
return RewriteUrlForSocket(new Uri(targetUrl));
}
public static Uri RewriteUrlForSocket(Uri targetUri)
{
UriBuilder parsedUrl = new(targetUri);
string SocketHost = GameserverConfig.Host.Value;
string SocketScheme = ConfigUtils.GetGameserverSocketProtocol();
bool SocketSecure = GameserverConfig.SecureProtocol.Value;
if (GameserverConfig.UseSocketHost.Value)
{
SocketHost = GameserverConfig.SocketHost.Value;
SocketSecure = GameserverConfig.SecureSocketProtocol.Value;
}
// Rewrites
if (parsedUrl.Path.Contains("api/notification/v2") && !GenericConfig.IsNameserver.Value)
{
return GenericUtils.RewriteUri(targetUri, SocketHost, SocketSecure, targetUri.AbsolutePath, SocketScheme);
}
return targetUri;
}
}
}