diff --git a/NebulaPatcher/Patches/Dynamic/UIGalaxySelect_Patch.cs b/NebulaPatcher/Patches/Dynamic/UIGalaxySelect_Patch.cs index 927e8e156..e165e5765 100644 --- a/NebulaPatcher/Patches/Dynamic/UIGalaxySelect_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/UIGalaxySelect_Patch.cs @@ -28,7 +28,13 @@ public static void _OnOpen_Postfix(UIGalaxySelect __instance) { var galaxySelectRect = __instance.gameObject.GetComponent(); galaxySelectRect.Find("random-button").gameObject.SetActive(false); - galaxySelectRect.Find("setting-group").gameObject.SetActive(false); + var settingGroupRect = galaxySelectRect.Find("setting-group"); + for (var i = 0; i < settingGroupRect.childCount; i++) + { + var childObject = settingGroupRect.GetChild(i).gameObject; + if (childObject.name != "top-title" && childObject.name != "galaxy-seed") + childObject.SetActive(false); + } } if (!Multiplayer.IsActive) { @@ -74,7 +80,7 @@ public static void _OnOpen_Postfix(UIGalaxySelect __instance) [HarmonyPatch(nameof(UIGalaxySelect.EnterGame))] public static bool EnterGame_Prefix(UIGalaxySelect __instance) { - if (!Multiplayer.IsInMultiplayerMenu) + if (!Multiplayer.IsInMultiplayerMenu || __instance.uiCombat.active) { return true; } @@ -131,18 +137,23 @@ public static void _OnClose_Prefix(UIGalaxySelect __instance) // cant check anymore if we are in multiplayer or not, so just do this without check. will not do any harm C: var galaxySelectRect = __instance.gameObject.GetComponent(); - galaxySelectRect.Find("setting-group").gameObject.SetActive(true); galaxySelectRect.Find("random-button").gameObject.SetActive(true); + var settingGroupRect = galaxySelectRect.Find("setting-group"); + for (var i = 0; i < settingGroupRect.childCount; i++) + { + var childObject = settingGroupRect.GetChild(i).gameObject; + if (childObject.name != "top-title" && childObject.name != "galaxy-seed") + childObject.SetActive(true); + } } [HarmonyPrefix] [HarmonyPatch(nameof(UIGalaxySelect.Rerand))] - public static void Rerand_Prefix() + public static void Rerand_Prefix(UIGalaxySelect __instance) { UIVirtualStarmap_Transpiler.customBirthStar = -1; UIVirtualStarmap_Transpiler.customBirthPlanet = -1; - GameObject.Find("UI Root/Overlay Canvas/Galaxy Select/start-button/start-text").GetComponent().text = - "Start Game"; + __instance.startButtonText.text = "开始游戏".Translate(); } [HarmonyPrefix] diff --git a/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs b/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs index df1c0ff27..491efbfed 100644 --- a/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs @@ -250,7 +250,7 @@ private static void AddMultiplayerJoinMenu() passwordTransform.localPosition += new Vector3(0, -36, 0); passwordTransform.GetComponent().text = "Password (optional)".Translate(); passwordTransform.name = "Password (optional)"; - + passwordInput = passwordTransform.GetComponentInChildren(); passwordInput.contentType = InputField.ContentType.Password; passwordInput.text = ""; @@ -362,19 +362,26 @@ private static void OnJoinGameBackButtonClick() private static bool ConnectToServer(string connectionString, int serverPort, bool isIP, string password) { - if (isIP) + try { - Multiplayer.JoinGame(new Client(new IPEndPoint(IPAddress.Parse(connectionString), serverPort), password)); + if (isIP) + { + Multiplayer.JoinGame(new Client(new IPEndPoint(IPAddress.Parse(connectionString), serverPort), password)); + return true; + } + + //trying to resolve as uri + if (!Uri.TryCreate(connectionString, UriKind.RelativeOrAbsolute, out _)) + { + return false; + } + Multiplayer.JoinGame(new Client(connectionString, serverPort, password)); return true; } - - //trying to resolve as uri - if (!Uri.TryCreate(connectionString, UriKind.RelativeOrAbsolute, out _)) + catch (Exception e) { - return false; + Log.Error("ConnectToServer error:\n" + e); } - Multiplayer.JoinGame(new Client(connectionString, serverPort, password)); - return true; - + return false; } } diff --git a/NebulaPatcher/Patches/Transpilers/UIGalaxySelect_Transpiler.cs b/NebulaPatcher/Patches/Transpilers/UIGalaxySelect_Transpiler.cs new file mode 100644 index 000000000..2cdfc53de --- /dev/null +++ b/NebulaPatcher/Patches/Transpilers/UIGalaxySelect_Transpiler.cs @@ -0,0 +1,54 @@ +#region + +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Reflection; +using System.Reflection.Emit; +using HarmonyLib; +using NebulaModel.Logger; +using NebulaWorld; +using UnityEngine; +using UnityEngine.UI; + +#endregion + +namespace NebulaPatcher.Patches.Transpilers; + +[HarmonyPatch(typeof(UIGalaxySelect))] +internal class UIGalaxySelect_Transpiler +{ + [HarmonyTranspiler] + [HarmonyPatch(nameof(UIGalaxySelect._OnUpdate))] + public static IEnumerable _OnUpdate_Transpiler(IEnumerable instructions) + { + var codeInstructions = instructions as CodeInstruction[] ?? instructions.ToArray(); + /* + from: + this.startButtonText.text = (this.uiCombat.active ? "黑雾设置页面返回" : "开始游戏").Translate(); + + to: + this.startButtonText.text = UIVirtualStarmap_Transpiler.customBirthPlanet == -1 ? + (this.uiCombat.active ? "黑雾设置页面返回" : "开始游戏").Translate() : UIVirtualStarmap_Transpiler.customBirthPlanetName; + */ + var matcher = new CodeMatcher(codeInstructions) + .End() + .MatchBack(false, new CodeMatch(i => i.opcode == OpCodes.Callvirt && ((MethodInfo)i.operand).Name == "set_text")); + if (matcher.IsInvalid) + { + Log.Warn("UIGalaxySelect_OnUpdate_Transpiler could not find injection point, not patching!"); + return codeInstructions; + } + + matcher.Insert( + HarmonyLib.Transpilers.EmitDelegate>((originalBtnName) => + { + if (UIVirtualStarmap_Transpiler.customBirthPlanet == -1) + return originalBtnName; + return "开始游戏".Translate() + '(' + UIVirtualStarmap_Transpiler.customBirthPlanetName + ')'; + })); + + return matcher.InstructionEnumeration(); + } +} diff --git a/NebulaPatcher/Patches/Transpilers/UIVirtualStarmap_Transpiler.cs b/NebulaPatcher/Patches/Transpilers/UIVirtualStarmap_Transpiler.cs index 0272d7970..b2e3356b3 100644 --- a/NebulaPatcher/Patches/Transpilers/UIVirtualStarmap_Transpiler.cs +++ b/NebulaPatcher/Patches/Transpilers/UIVirtualStarmap_Transpiler.cs @@ -123,11 +123,10 @@ ShowSolarsystemDetails delegate (own logic) customBirthStar = starData.id; customBirthPlanet = pData.id; + customBirthPlanetName = pData.displayName; Log.Info($"set birth planet{pData.id} {pData.displayName}"); - var text = GameObject.Find("UI Root/Overlay Canvas/Galaxy Select/start-button/start-text") - .GetComponent(); - text.text = $"Start Game at {pData.displayName}"; + var text = UIRoot.instance.galaxySelect.startButtonText; text.horizontalOverflow = HorizontalWrapMode.Overflow; if (pData.data == null) @@ -587,6 +586,7 @@ public static IEnumerable OnGalaxyDataReset_Transpiler(IEnumera public static int customBirthStar = -1; public static int customBirthPlanet = -1; + public static string customBirthPlanetName = ""; private static readonly int s_tintColor = Shader.PropertyToID("_TintColor"); private static readonly int s_lineColorA = Shader.PropertyToID("_LineColorA"); #pragma warning restore IDE1006