Added config file
This commit is contained in:
1 parent
cfc34528b6
commit
37b73ea162
1 file changed
+211
-32
+211
-32
@@ -1,18 +1,28 @@
|
|||||||
using BepInEx;
|
using System;
|
||||||
using BepInEx.Unity.IL2CPP;
|
|
||||||
using HarmonyLib;
|
|
||||||
using UnityEngine;
|
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
|
using BepInEx;
|
||||||
|
using BepInEx.Unity.IL2CPP;
|
||||||
|
using HarmonyLib;
|
||||||
|
using Il2CppInterop.Runtime.InteropTypes.Arrays;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.SceneManagement;
|
||||||
|
|
||||||
namespace AUModdedRegions
|
namespace AUModdedRegions
|
||||||
{
|
{
|
||||||
[BepInPlugin("com.aumoddedregions", "AUModdedRegions", "1.0.0")]
|
[BepInPlugin(Id, Name, Version)]
|
||||||
|
[BepInProcess("Among Us.exe")]
|
||||||
public class AUModdedRegionsPlugin : BasePlugin
|
public class AUModdedRegionsPlugin : BasePlugin
|
||||||
{
|
{
|
||||||
|
public const string Id = "com.nb1x.aumoddedregions";
|
||||||
|
public const string Name = "AUModdedRegions";
|
||||||
|
public const string Version = "1.3.0";
|
||||||
|
|
||||||
|
public static string ConfigPath => Path.Combine(Paths.ConfigPath, "CustomRegions.cfg");
|
||||||
|
public Harmony Harmony { get; } = new Harmony(Id);
|
||||||
|
|
||||||
private static readonly string[] OfficialDomains = new string[]
|
private static readonly string[] OfficialDomains = new string[]
|
||||||
{
|
{
|
||||||
"innersloth.com",
|
"innersloth.com",
|
||||||
@@ -22,27 +32,91 @@ namespace AUModdedRegions
|
|||||||
"matchmaker-eu.among.us"
|
"matchmaker-eu.among.us"
|
||||||
};
|
};
|
||||||
|
|
||||||
private static readonly List<(string Name, string Host)> DefaultModdedRegions = new()
|
|
||||||
{
|
|
||||||
("Niko233 (EU)", "https://au-eu.niko233.top"),
|
|
||||||
("Niko233 (AS)", "https://au-as.niko233.top"),
|
|
||||||
("Niko233 (NA)", "https://au-us.niko233.top"),
|
|
||||||
("Niko233 (CN)", "https://au-cn.niko233.top"),
|
|
||||||
("Modded NA", "https://aumods.org"),
|
|
||||||
("Modded EU", "https://au-eu.duikbo.at"),
|
|
||||||
("Modded AS", "https://au-as.duikbo.at"),
|
|
||||||
("Skeld.net", "https://play.skeld.net")
|
|
||||||
};
|
|
||||||
|
|
||||||
public override void Load()
|
public override void Load()
|
||||||
{
|
{
|
||||||
ProcessAndMergeRegions();
|
Log.LogInfo($"[AUModdedRegions] Lancement de {Name} v{Version}...");
|
||||||
|
|
||||||
Harmony.CreateAndPatchAll(typeof(PatchCleanAndMerge));
|
EnsureConfigFileExists();
|
||||||
Log.LogInfo("AUModdedRegions chargé avec succès !");
|
|
||||||
|
var parsedRegions = ParseConfigRegions(ConfigPath);
|
||||||
|
ProcessAndCleanRegionJson(parsedRegions);
|
||||||
|
|
||||||
|
Harmony.PatchAll();
|
||||||
|
|
||||||
|
SceneManager.add_sceneLoaded((Action<Scene, LoadSceneMode>)((scene, _) =>
|
||||||
|
{
|
||||||
|
if (scene.name == "MainMenu")
|
||||||
|
{
|
||||||
|
Log.LogInfo("[AUModdedRegions] Scène MainMenu détectée, injection des régions...");
|
||||||
|
InjectRegions(parsedRegions);
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessAndMergeRegions()
|
private static void EnsureConfigFileExists()
|
||||||
|
{
|
||||||
|
if (File.Exists(ConfigPath)) return;
|
||||||
|
|
||||||
|
string defaultConfig = @"[Region 1]
|
||||||
|
Name = Niko_NA
|
||||||
|
Adress = au-us.niko233.top
|
||||||
|
Https = true
|
||||||
|
Dtls = false
|
||||||
|
Port = 443, 22023
|
||||||
|
|
||||||
|
[Region 2]
|
||||||
|
Name = Niko_EU
|
||||||
|
Adress = au-eu.niko233.top
|
||||||
|
Https = true
|
||||||
|
Dtls = false
|
||||||
|
Port = 443, 22023
|
||||||
|
|
||||||
|
[Region 3]
|
||||||
|
Name = Niko_AS
|
||||||
|
Adress = au-as.niko233.top
|
||||||
|
Https = true
|
||||||
|
Dtls = false
|
||||||
|
Port = 443, 22023
|
||||||
|
|
||||||
|
[Region 4]
|
||||||
|
Name = Niko_CN
|
||||||
|
Adress = au-cn.niko233.top
|
||||||
|
Https = true
|
||||||
|
Dtls = false
|
||||||
|
Port = 443, 22023
|
||||||
|
|
||||||
|
[Region 5]
|
||||||
|
Name = Modded NA
|
||||||
|
Adress = aumods.org
|
||||||
|
Https = true
|
||||||
|
Dtls = false
|
||||||
|
Port = 443, 22023
|
||||||
|
|
||||||
|
[Region 6]
|
||||||
|
Name = Modded EU
|
||||||
|
Adress = au-eu.duikbo.at
|
||||||
|
Https = true
|
||||||
|
Dtls = false
|
||||||
|
Port = 443, 22023
|
||||||
|
|
||||||
|
[Region 7]
|
||||||
|
Name = Modded AS
|
||||||
|
Adress = au-as.duikbo.at
|
||||||
|
Https = true
|
||||||
|
Dtls = false
|
||||||
|
Port = 443, 22023
|
||||||
|
|
||||||
|
[Region 8]
|
||||||
|
Name = Default
|
||||||
|
Adress =
|
||||||
|
Https =
|
||||||
|
Dtls =
|
||||||
|
Port = 443, 22023
|
||||||
|
";
|
||||||
|
File.WriteAllText(ConfigPath, defaultConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ProcessAndCleanRegionJson(List<ParsedRegion> parsedRegions)
|
||||||
{
|
{
|
||||||
string regionFilePath = Path.Combine(Application.persistentDataPath, "regionInfo.json");
|
string regionFilePath = Path.Combine(Application.persistentDataPath, "regionInfo.json");
|
||||||
|
|
||||||
@@ -71,20 +145,29 @@ namespace AUModdedRegions
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var (name, host) in DefaultModdedRegions)
|
foreach (var reg in parsedRegions)
|
||||||
{
|
{
|
||||||
|
if (string.Equals(reg.Name, "Default", StringComparison.OrdinalIgnoreCase) && string.IsNullOrWhiteSpace(reg.Adress))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(reg.Name) || string.IsNullOrWhiteSpace(reg.Adress))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
string protocol = reg.Https ? "https://" : "http://";
|
||||||
|
string host = $"{protocol}{reg.Adress}";
|
||||||
|
|
||||||
if (!HasRegion(regionsArray, host))
|
if (!HasRegion(regionsArray, host))
|
||||||
{
|
{
|
||||||
regionsArray.Add(BuildRegionNode(name, host));
|
regionsArray.Add(BuildRegionNode(reg.Name, host, reg.Https ? reg.Port1 : reg.Port2, reg.Dtls));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rootNode["Regions"] = regionsArray;
|
rootNode["Regions"] = regionsArray;
|
||||||
|
|
||||||
var options = new JsonSerializerOptions { WriteIndented = true };
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
||||||
File.WriteAllText(regionFilePath, rootNode.ToJsonString(options));
|
File.WriteAllText(regionFilePath, rootNode.ToJsonString(options));
|
||||||
|
|
||||||
Log.LogInfo("[AUModdedRegions] Fichier regionInfo.json mis à jour ! (Innersloth viré, tes régions custom + régions moddées conservées/ajoutées).");
|
Log.LogInfo("[AUModdedRegions] Fichier regionInfo.json nettoyé et mis à jour !");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -123,7 +206,7 @@ namespace AUModdedRegions
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonNode BuildRegionNode(string name, string host)
|
private JsonNode BuildRegionNode(string name, string host, ushort port, bool dtls)
|
||||||
{
|
{
|
||||||
return new JsonObject
|
return new JsonObject
|
||||||
{
|
{
|
||||||
@@ -136,8 +219,8 @@ namespace AUModdedRegions
|
|||||||
{
|
{
|
||||||
["Name"] = "Http-1",
|
["Name"] = "Http-1",
|
||||||
["Ip"] = host,
|
["Ip"] = host,
|
||||||
["Port"] = 443,
|
["Port"] = port,
|
||||||
["UseDtls"] = false,
|
["UseDtls"] = dtls,
|
||||||
["Players"] = 0,
|
["Players"] = 0,
|
||||||
["ConnectionFailures"] = 0
|
["ConnectionFailures"] = 0
|
||||||
}
|
}
|
||||||
@@ -146,12 +229,108 @@ namespace AUModdedRegions
|
|||||||
["TranslateName"] = 1003
|
["TranslateName"] = 1003
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void InjectRegions(List<ParsedRegion> parsedRegions)
|
||||||
|
{
|
||||||
|
ServerManager serverMngr = DestroyableSingleton<ServerManager>.Instance;
|
||||||
|
if (serverMngr == null) return;
|
||||||
|
|
||||||
|
foreach (var reg in parsedRegions)
|
||||||
|
{
|
||||||
|
if (string.Equals(reg.Name, "Default", StringComparison.OrdinalIgnoreCase) && string.IsNullOrWhiteSpace(reg.Adress))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(reg.Name) || string.IsNullOrWhiteSpace(reg.Adress))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
ushort selectedPort = reg.Https ? reg.Port1 : reg.Port2;
|
||||||
|
string protocol = reg.Https ? "https://" : "http://";
|
||||||
|
string fullUrl = $"{protocol}{reg.Adress}";
|
||||||
|
|
||||||
|
var serverInfo = new ServerInfo("http-1", fullUrl, selectedPort, reg.Dtls);
|
||||||
|
var serversArray = new Il2CppReferenceArray<ServerInfo>(new ServerInfo[] { serverInfo });
|
||||||
|
|
||||||
|
var regionInfo = new StaticHttpRegionInfo(reg.Name, (StringNames)1003, fullUrl, serversArray);
|
||||||
|
|
||||||
|
serverMngr.AddOrUpdateRegion(regionInfo.Cast<IRegionInfo>());
|
||||||
|
Log.LogInfo($"[AUModdedRegions] Région '{reg.Name}' injectée en mémoire.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<ParsedRegion> ParseConfigRegions(string filePath)
|
||||||
|
{
|
||||||
|
var result = new List<ParsedRegion>();
|
||||||
|
if (!File.Exists(filePath)) return result;
|
||||||
|
|
||||||
|
ParsedRegion? current = null;
|
||||||
|
|
||||||
|
foreach (var line in File.ReadAllLines(filePath))
|
||||||
|
{
|
||||||
|
string trimmed = line.Trim();
|
||||||
|
|
||||||
|
if (trimmed.StartsWith("[") && trimmed.EndsWith("]"))
|
||||||
|
{
|
||||||
|
if (current != null) result.Add(current);
|
||||||
|
current = new ParsedRegion();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current == null || !trimmed.Contains('=')) continue;
|
||||||
|
|
||||||
|
var parts = trimmed.Split(new[] { '=' }, 2);
|
||||||
|
string key = parts[0].Trim().ToLowerInvariant();
|
||||||
|
string val = parts[1].Trim();
|
||||||
|
|
||||||
|
switch (key)
|
||||||
|
{
|
||||||
|
case "name":
|
||||||
|
current.Name = val;
|
||||||
|
break;
|
||||||
|
case "adress":
|
||||||
|
case "address":
|
||||||
|
current.Adress = val;
|
||||||
|
break;
|
||||||
|
case "https":
|
||||||
|
bool.TryParse(val, out bool https);
|
||||||
|
current.Https = https;
|
||||||
|
break;
|
||||||
|
case "dtls":
|
||||||
|
bool.TryParse(val, out bool dtls);
|
||||||
|
current.Dtls = dtls;
|
||||||
|
break;
|
||||||
|
case "port":
|
||||||
|
var ports = val.Split(',');
|
||||||
|
if (ports.Length > 0 && ushort.TryParse(ports[0].Trim(), out ushort p1))
|
||||||
|
current.Port1 = p1;
|
||||||
|
if (ports.Length > 1 && ushort.TryParse(ports[1].Trim(), out ushort p2))
|
||||||
|
current.Port2 = p2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current != null) result.Add(current);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ParsedRegion
|
||||||
|
{
|
||||||
|
public string Name { get; set; } = "";
|
||||||
|
public string Adress { get; set; } = "";
|
||||||
|
public bool Https { get; set; } = true;
|
||||||
|
public bool Dtls { get; set; } = false;
|
||||||
|
public ushort Port1 { get; set; } = 443;
|
||||||
|
public ushort Port2 { get; set; } = 22023;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[HarmonyPatch("ServerManager+JsonServerData", "CleanAndMerge")]
|
[HarmonyPatch(typeof(ServerManager.JsonServerData), nameof(ServerManager.JsonServerData.CleanAndMerge))]
|
||||||
public static class PatchCleanAndMerge
|
public static class PatchCleanAndMerge
|
||||||
{
|
{
|
||||||
[HarmonyPrefix]
|
[HarmonyPrefix]
|
||||||
public static bool Prefix() => false;
|
public static bool Prefix()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user