diff --git a/Main.cs b/Main.cs index cbc527175..d85a24ae3 100644 --- a/Main.cs +++ b/Main.cs @@ -14,15 +14,17 @@ using EHR.Modules; using EHR.Patches; using EHR.Roles; using HarmonyLib; -using Newtonsoft.Json; using UnityEngine; using UnityEngine.Networking; + #if IL2CPP using BepInEx.Unity.IL2CPP; using BepInEx.Unity.IL2CPP.Utils.Collections; using Il2CppInterop.Runtime.Injection; +using System.Text.Json; #else using BepInEx.Bootstrap; +using Newtonsoft.Json; #endif // ReSharper disable RedundantNameQualifier @@ -42,7 +44,11 @@ namespace EHR; [BepInDependency(SubmergedCompatibility.SubmergedGuid, BepInDependency.DependencyFlags.SoftDependency)] [BepInProcess("Among Us.exe")] [SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")] +#if IL2CPP public class Main : BasePlugin +#else +public class Main : BaseUnityPlugin +#endif { private const string DebugKeyHash = "c0fd562955ba56af3ae20d7ec9e64c664f0facecef4b3e366e109306adeae29d"; private const string DebugKeySalt = "59687b"; @@ -1066,7 +1072,7 @@ public class Main : BasePlugin private static void HandleRoleColorFiles() { #if IL2CPP - string serialized = System.Text.Json.JsonSerializer.Serialize(RoleHtmlColors, new System.Text.Json.JsonSerializerOptions { WriteIndented = true }); + string serialized = JsonSerializer.Serialize(RoleHtmlColors, new JsonSerializerOptions { WriteIndented = true }); #else string serialized = JsonConvert.SerializeObject(RoleHtmlColors, Formatting.Indented); #endif @@ -1085,7 +1091,11 @@ public class Main : BasePlugin string json = File.ReadAllText(path); if (string.IsNullOrWhiteSpace(json) || json == serialized) return; +#if IL2CPP + Dictionary dictionary = JsonSerializer.Deserialize>(json); +#else Dictionary dictionary = JsonConvert.DeserializeObject>(json); +#endif if (dictionary != null) { diff --git a/Modules/Achievements.cs b/Modules/Achievements.cs index 9b34caef7..4960e6d90 100644 --- a/Modules/Achievements.cs +++ b/Modules/Achievements.cs @@ -1,10 +1,15 @@ using System.Collections; using System.Collections.Generic; using System.Text; -using Newtonsoft.Json; using UnityEngine; using UnityEngine.Networking; +#if IL2CPP +using System.Text.Json; +#else +using Newtonsoft.Json; +#endif + // ReSharper disable InconsistentNaming namespace EHR.Modules; @@ -214,7 +219,7 @@ public static class Achievements private static void SaveAllData() { #if IL2CPP - string json = System.Text.Json.JsonSerializer.Serialize(CompletedAchievements); + string json = JsonSerializer.Serialize(CompletedAchievements); #else string json = JsonConvert.SerializeObject(CompletedAchievements); #endif @@ -237,7 +242,7 @@ public static class Achievements }; #if IL2CPP - string payload = System.Text.Json.JsonSerializer.Serialize(data); + string payload = JsonSerializer.Serialize(data); #else string payload = JsonConvert.SerializeObject(data); #endif @@ -261,7 +266,11 @@ public static class Achievements if (File.Exists(SaveFilePath)) { string json = File.ReadAllText(SaveFilePath); +#if IL2CPP + CompletedAchievements = JsonSerializer.Deserialize>(json); +#else CompletedAchievements = JsonConvert.DeserializeObject>(json); +#endif } else if (Options.StoreCompletedAchievementsOnEHRDatabase.GetBool()) { @@ -285,7 +294,11 @@ public static class Achievements else { string json = request.downloadHandler.text; +#if IL2CPP + CompletedAchievements = JsonSerializer.Deserialize>(json); +#else CompletedAchievements = JsonConvert.DeserializeObject>(json); +#endif File.WriteAllText(SaveFilePath, json); Logger.Info("Achievements loaded successfully.", "Achievements.LoadAllData"); } diff --git a/Modules/DevManager.cs b/Modules/DevManager.cs index e16f0b93a..5b721bf29 100644 --- a/Modules/DevManager.cs +++ b/Modules/DevManager.cs @@ -2,10 +2,15 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using Newtonsoft.Json; using UnityEngine; using UnityEngine.Networking; +#if IL2CPP +using System.Text.Json; +#else +using Newtonsoft.Json; +#endif + namespace EHR; public static class DevManager @@ -46,7 +51,11 @@ public static class DevManager try { string json = request.downloadHandler.text.Trim(); +#if IL2CPP + Tags = JsonSerializer.Deserialize>(json); +#else Tags = JsonConvert.DeserializeObject>(json); +#endif Logger.Info($"Tags successfully fetched: {Tags.Count} tags loaded.", "DevManager.FetchTags"); } catch (Exception ex) { Logger.Error($"Error parsing tags JSON: {ex.Message}", "DevManager.FetchTags"); } diff --git a/Modules/LobbySharingAPI.cs b/Modules/LobbySharingAPI.cs index d0388ef40..917c7701a 100644 --- a/Modules/LobbySharingAPI.cs +++ b/Modules/LobbySharingAPI.cs @@ -5,10 +5,15 @@ using System.Text; using System.Text.RegularExpressions; using HarmonyLib; using InnerNet; -using Newtonsoft.Json.Linq; using UnityEngine; using UnityEngine.Networking; +#if IL2CPP +using System.Text.Json; +#else +using Newtonsoft.Json.Linq; +#endif + namespace EHR.Modules; public static class LobbySharingAPI @@ -70,8 +75,13 @@ public static class LobbySharingAPI string responseText = request.downloadHandler.text; Logger.Msg("Response from server: " + responseText, "LobbyNotifierForDiscord.SendLobbyCreatedRequest"); +#if IL2CPP + using JsonDocument doc = JsonDocument.Parse(responseText); + Token = doc.RootElement.GetProperty("token").GetString(); +#else JObject doc = JObject.Parse(responseText); Token = doc["token"]?.ToString(); +#endif Logger.Msg($"Token for room {roomCode}: {Token}", "LobbyNotifierForDiscord.SendLobbyCreatedRequest"); } diff --git a/Modules/ModUpdater.cs b/Modules/ModUpdater.cs index fbbdbb2dd..b5885a69a 100644 --- a/Modules/ModUpdater.cs +++ b/Modules/ModUpdater.cs @@ -4,13 +4,18 @@ using System.IO; using System.Net.Http; using System.Reflection; using HarmonyLib; -using Newtonsoft.Json.Linq; using TMPro; using Twitch; using UnityEngine; using UnityEngine.Networking; using static EHR.Translator; +#if IL2CPP +using System.Text.Json; +#else +using Newtonsoft.Json.Linq; +#endif + namespace EHR; [HarmonyPatch] @@ -86,13 +91,31 @@ public static class ModUpdater yield break; } +#if IL2CPP + JsonDocument data = JsonDocument.Parse(request.downloadHandler.text); + + LatestTitleModName = data.RootElement.GetProperty("name").GetString(); + + if (beta) + { + LatestTitle = LatestTitleModName; + + // Beta builds still use the same EHR.dll release asset. + DownloadUrl = FindDllAssetUrl(data); + + HasUpdate = LatestTitle != ThisAssembly.Git.Commit; + } + else + { + string versionString = data.RootElement.GetProperty("tag_name").GetString()? +#else JObject data = JObject.Parse(request.downloadHandler.text); LatestTitleModName = data["name"]?.ToString(); if (beta) { - LatestTitle = data["name"]?.ToString(); + LatestTitle = LatestTitleModName; // Beta builds still use the same EHR.dll release asset. DownloadUrl = FindDllAssetUrl(data); @@ -103,6 +126,7 @@ public static class ModUpdater { string versionString = data["tag_name"]? .ToString() +#endif .TrimStart('v'); LatestVersion = new Version(versionString!); @@ -141,7 +165,22 @@ public static class ModUpdater ); } } - + +#if IL2CPP + private static string FindDllAssetUrl(JsonDocument release) + { + if (!release.RootElement.TryGetProperty("assets", out JsonElement assets)) + return null; + + foreach (JsonElement asset in assets.EnumerateArray()) + { + if (asset.GetProperty("name").GetString() == "EHR.dll") + return asset.GetProperty("browser_download_url").GetString(); + } + + return null; + } +#else private static string FindDllAssetUrl(JObject release) { if (release["assets"] is not JArray assets) @@ -157,6 +196,7 @@ public static class ModUpdater return null; } +#endif public static void StartUpdate(string url) { diff --git a/Modules/MonoHelpers/CallerArgumentExpressionAttribute.cs b/Modules/MonoHelpers/CallerArgumentExpressionAttribute.cs index fb41ad216..2ed42f8fd 100644 --- a/Modules/MonoHelpers/CallerArgumentExpressionAttribute.cs +++ b/Modules/MonoHelpers/CallerArgumentExpressionAttribute.cs @@ -1,4 +1,5 @@ -namespace System.Runtime.CompilerServices +#if !IL2CPP +namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Parameter)] internal sealed class CallerArgumentExpressionAttribute : Attribute @@ -9,4 +10,5 @@ } public string ParameterName { get; } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/Modules/MonoHelpers/LinqExtensions.cs b/Modules/MonoHelpers/LinqExtensions.cs index 10b64b3c3..7b90dc6ee 100644 --- a/Modules/MonoHelpers/LinqExtensions.cs +++ b/Modules/MonoHelpers/LinqExtensions.cs @@ -1,4 +1,5 @@ -using System; +#if !IL2CPP +using System; using System.Collections.Generic; // Since EHR was originally made for net6.0 and uses the newer LINQ methods from it, @@ -493,4 +494,5 @@ namespace EHR } } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/Modules/MonoHelpers/LinqHelpers.cs b/Modules/MonoHelpers/LinqHelpers.cs index 98ead9ac0..fc8c69f08 100644 --- a/Modules/MonoHelpers/LinqHelpers.cs +++ b/Modules/MonoHelpers/LinqHelpers.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +#if !IL2CPP +using System.Collections.Generic; using System.Linq; namespace EHR; @@ -11,4 +12,5 @@ public static class LinqHelpers { return first.Zip(second, (f, s) => (First: f, Second: s)); } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/Modules/OnlinePresetsManager.cs b/Modules/OnlinePresetsManager.cs index 408e73d3a..6c8ef94e7 100644 --- a/Modules/OnlinePresetsManager.cs +++ b/Modules/OnlinePresetsManager.cs @@ -4,11 +4,16 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; -using Newtonsoft.Json; using TMPro; using UnityEngine; using UnityEngine.Networking; +#if IL2CPP +using System.Text.Json; +#else +using Newtonsoft.Json; +#endif + namespace EHR.Modules; public static class OnlinePresetsManager @@ -197,7 +202,7 @@ public static class OnlinePresetsManager }; #if IL2CPP - string json = System.Text.Json.JsonSerializer.Serialize(body); + string json = JsonSerializer.Serialize(body); #else string json = JsonConvert.SerializeObject(body); #endif @@ -224,7 +229,11 @@ public static class OnlinePresetsManager try { +#if IL2CPP + response = JsonSerializer.Deserialize(request.downloadHandler.text); +#else response = JsonConvert.DeserializeObject(request.downloadHandler.text); +#endif } catch { @@ -285,7 +294,11 @@ public static class OnlinePresetsManager try { +#if IL2CPP + response = JsonSerializer.Deserialize(request.downloadHandler.text); +#else response = JsonConvert.DeserializeObject(request.downloadHandler.text); +#endif } catch { @@ -324,7 +337,11 @@ public static class OnlinePresetsManager try { +#if IL2CPP + response = JsonSerializer.Deserialize(request.downloadHandler.text.Trim()); +#else response = JsonConvert.DeserializeObject(request.downloadHandler.text.Trim()); +#endif } catch (Exception ex) { diff --git a/Modules/OptionHolder.cs b/Modules/OptionHolder.cs index 69528d36e..01ea23bc9 100644 --- a/Modules/OptionHolder.cs +++ b/Modules/OptionHolder.cs @@ -8,9 +8,14 @@ using EHR.Gamemodes; using EHR.Modules; using EHR.Roles; using HarmonyLib; -using Newtonsoft.Json; using UnityEngine; +#if IL2CPP +using System.Text.Json; +#else +using Newtonsoft.Json; +#endif + // ReSharper disable InconsistentNaming namespace EHR; @@ -1208,7 +1213,7 @@ public static class Options { Directory.CreateDirectory(path); #if IL2CPP - File.WriteAllText(path + "/friendcode#1234.txt", System.Text.Json.JsonSerializer.Serialize(new UserData { Tag = string.Empty }, new System.Text.Json.JsonSerializerOptions { WriteIndented = true })); + File.WriteAllText(path + "/friendcode#1234.txt", JsonSerializer.Serialize(new UserData { Tag = string.Empty }, new JsonSerializerOptions { WriteIndented = true })); #else File.WriteAllText(path + "/friendcode#1234.txt", JsonConvert.SerializeObject(new UserData { Tag = string.Empty }, Formatting.Indented)); #endif @@ -1221,7 +1226,11 @@ public static class Options try { string content = File.ReadAllText(file); +#if IL2CPP + var userData = JsonSerializer.Deserialize(content); +#else var userData = JsonConvert.DeserializeObject(content); +#endif if (userData == null) throw new FormatException($"The data in {file} was not in the correct format."); string fileName = Path.GetFileNameWithoutExtension(file); Main.UserData[fileName] = userData; diff --git a/Modules/OptionSaver.cs b/Modules/OptionSaver.cs index 01b22a029..592700271 100644 --- a/Modules/OptionSaver.cs +++ b/Modules/OptionSaver.cs @@ -1,6 +1,11 @@ using System.Collections.Generic; using System.IO; + +#if IL2CPP +using System.Text.Json; +#else using Newtonsoft.Json; +#endif namespace EHR.Modules; @@ -94,7 +99,7 @@ public static class OptionSaver if (AmongUsClient.Instance && !AmongUsClient.Instance.AmHost) return; #if IL2CPP - string jsonString = System.Text.Json.JsonSerializer.Serialize(GenerateOptionsData(), new System.Text.Json.JsonSerializerOptions { WriteIndented = true }); + string jsonString = JsonSerializer.Serialize(GenerateOptionsData(), new JsonSerializerOptions { WriteIndented = true }); #else string jsonString = JsonConvert.SerializeObject(GenerateOptionsData(), Formatting.Indented); #endif @@ -113,7 +118,11 @@ public static class OptionSaver return; } +#if IL2CPP + LoadOptionsData(JsonSerializer.Deserialize(jsonString)); +#else LoadOptionsData(JsonConvert.DeserializeObject(jsonString)); +#endif } public class SerializableOptionsData diff --git a/Modules/Translator.cs b/Modules/Translator.cs index 11fa70358..86f9e8963 100644 --- a/Modules/Translator.cs +++ b/Modules/Translator.cs @@ -9,6 +9,7 @@ using EHR.Gamemodes; #if IL2CPP using System.Text.Json; #else +using Newtonsoft.Json; using Newtonsoft.Json.Linq; #endif diff --git a/Modules/Utils.cs b/Modules/Utils.cs index abf507e0f..5134ddf3e 100644 --- a/Modules/Utils.cs +++ b/Modules/Utils.cs @@ -19,11 +19,16 @@ using EHR.Roles; using HarmonyLib; using Hazel; using InnerNet; -using Newtonsoft.Json; using UnityEngine; using static EHR.Translator; using Tree = EHR.Roles.Tree; +#if IL2CPP +using System.Text.Json; +#else +using Newtonsoft.Json; +#endif + namespace EHR; /* List of symbols that work in game @@ -408,7 +413,7 @@ public static class Utils }); #if IL2CPP - File.WriteAllText(path, System.Text.Json.JsonSerializer.Serialize(data, new System.Text.Json.JsonSerializerOptions { WriteIndented = true })); + File.WriteAllText(path, JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true })); #else File.WriteAllText(path, JsonConvert.SerializeObject(data, Formatting.Indented)); #endif @@ -433,7 +438,11 @@ public static class Utils { if (!File.Exists(path)) return; +#if IL2CPP + var data = JsonSerializer.Deserialize>>>(File.ReadAllText(path)); +#else var data = JsonConvert.DeserializeObject>>>(File.ReadAllText(path)); +#endif Dictionary>> dict = path.Contains("Always") ? Main.AlwaysSpawnTogetherCombos : Main.NeverSpawnTogetherCombos; dict.Clear(); diff --git a/Patches/AnnouncementPatch.cs b/Patches/AnnouncementPatch.cs index 403222cff..28949fe34 100644 --- a/Patches/AnnouncementPatch.cs +++ b/Patches/AnnouncementPatch.cs @@ -6,11 +6,13 @@ using AmongUs.Data; using AmongUs.Data.Player; using Assets.InnerNet; using HarmonyLib; -using Newtonsoft.Json; using UnityEngine.Networking; #if IL2CPP using Il2CppInterop.Runtime.InteropTypes.Arrays; +using System.Text.Json; +#else +using Newtonsoft.Json; #endif namespace EHR; @@ -47,7 +49,11 @@ public class ModNews public static List FromJson(string json) { +#if IL2CPP + return JsonSerializer.Deserialize>(json); +#else return JsonConvert.DeserializeObject>(json); +#endif } } diff --git a/Patches/Crowded.cs b/Patches/Crowded.cs index ac5b7104b..8a5cd92ec 100644 --- a/Patches/Crowded.cs +++ b/Patches/Crowded.cs @@ -3,6 +3,9 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using AmongUs.GameOptions; using HarmonyLib; +#if IL2CPP +using Il2CppInterop.Runtime.Attributes; +#endif using TMPro; using UnityEngine; using static EHR.GameStates; @@ -439,6 +442,9 @@ public class AbstractPagingBehaviour : MonoBehaviour public class MeetingHudPagingBehaviour : AbstractPagingBehaviour { internal MeetingHud meetingHud = null!; +#if IL2CPP + [HideFromIl2Cpp] +#endif private IEnumerable Targets => meetingHud.playerStates.OrderBy(p => p.AmDead); protected override int MaxPageIndex => (Targets.Count() - 1) / MaxPerPage; @@ -488,6 +494,9 @@ public class ShapeShifterPagingBehaviour : AbstractPagingBehaviour { public ShapeshifterMinigame shapeshifterMinigame = null!; private TextMeshPro PageText = null!; +#if IL2CPP + [HideFromIl2Cpp] +#endif private IEnumerable Targets => shapeshifterMinigame.potentialVictims.ToArray(); protected override int MaxPageIndex => (Targets.Count() - 1) / MaxPerPage; @@ -536,6 +545,9 @@ public class VitalsPagingBehaviour : AbstractPagingBehaviour { public VitalsMinigame vitalsMinigame = null!; private TextMeshPro PageText = null!; +#if IL2CPP + [HideFromIl2Cpp] +#endif private IEnumerable Targets => vitalsMinigame.vitals.ToArray(); protected override int MaxPageIndex => (Targets.Count() - 1) / MaxPerPage; diff --git a/Patches/IntroPatch.cs b/Patches/IntroPatch.cs index 5a0fc2445..d910f6d88 100644 --- a/Patches/IntroPatch.cs +++ b/Patches/IntroPatch.cs @@ -5,6 +5,9 @@ using System.Diagnostics; using System.Linq; using System.Reflection; using AmongUs.GameOptions; +#if IL2CPP +using BepInEx.Unity.IL2CPP.Utils.Collections; +#endif using EHR.Gamemodes; using EHR.Modules; using EHR.Patches; @@ -42,7 +45,11 @@ static class ShowRoleMoveNextPatch [HarmonyPatch(typeof(HudManager), nameof(HudManager.CoShowIntro))] static class CoShowIntroPatch { +#if IL2CPP + public static bool Prefix(HudManager __instance, ref Il2CppSystem.Collections.IEnumerator __result) +#else public static bool Prefix(HudManager __instance, ref IEnumerator __result) +#endif { if (!AmongUsClient.Instance.AmHost || !GameStates.IsModHost) return true; @@ -66,7 +73,11 @@ static class CoShowIntroPatch catch { Logger.Warn($"Game ended? {AmongUsClient.Instance.IsGameOver || GameStates.IsLobby || GameEndChecker.Ended}", "ShipStatus.Begin"); } }, 4f, "Assign Tasks"); +#if IL2CPP + __result = CoShowIntro().WrapToIl2Cpp(); +#else __result = CoShowIntro(); +#endif return false; IEnumerator CoShowIntro() diff --git a/Patches/TextBoxPatch.cs b/Patches/TextBoxPatch.cs index 22413df56..ece844d0b 100644 --- a/Patches/TextBoxPatch.cs +++ b/Patches/TextBoxPatch.cs @@ -22,7 +22,7 @@ public static class TextBoxPatch [HarmonyPrefix] // Use the character provided by IsCharAllowed directly. // Tracking a separate character position desyncs when the caret is moved. - public static bool ValidateChatCharacter(char c, ref bool __result) + public static bool ValidateChatCharacter([HarmonyArgument(0)] char c, ref bool __result) { // Do not interfere with IME composition input. if (!string.IsNullOrEmpty(Input.compositionString)) diff --git a/Roles/Standard/Crewmate/Miscellaneous/Tar.cs b/Roles/Standard/Crewmate/Miscellaneous/Tar.cs index 6b20ed46d..89640580e 100644 --- a/Roles/Standard/Crewmate/Miscellaneous/Tar.cs +++ b/Roles/Standard/Crewmate/Miscellaneous/Tar.cs @@ -2,6 +2,10 @@ using AmongUs.GameOptions; using EHR.Modules.Extensions; +#if IL2CPP +using System.Linq; +#endif + namespace EHR.Roles; public class Tar : RoleBase diff --git a/Roles/Standard/Impostor/Killing/Sniper.cs b/Roles/Standard/Impostor/Killing/Sniper.cs index 1cf622dec..5c5d87fc3 100644 --- a/Roles/Standard/Impostor/Killing/Sniper.cs +++ b/Roles/Standard/Impostor/Killing/Sniper.cs @@ -5,6 +5,10 @@ using Hazel; using UnityEngine; using static EHR.Translator; +#if IL2CPP +using System.Linq; +#endif + namespace EHR.Roles; public class Sniper : RoleBase diff --git a/nuget.config b/nuget.config index cb01a3919..2d4874f7e 100644 --- a/nuget.config +++ b/nuget.config @@ -4,5 +4,6 @@ +