Skip to content

Commit

Permalink
Fix lobby UI
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Dec 21, 2023
1 parent c60039c commit fd7ed2a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 19 deletions.
23 changes: 17 additions & 6 deletions NebulaPatcher/Patches/Dynamic/UIGalaxySelect_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public static void _OnOpen_Postfix(UIGalaxySelect __instance)
{
var galaxySelectRect = __instance.gameObject.GetComponent<RectTransform>();
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)
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<RectTransform>();

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>().text =
"Start Game";
__instance.startButtonText.text = "开始游戏".Translate();
}

[HarmonyPrefix]
Expand Down
27 changes: 17 additions & 10 deletions NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private static void AddMultiplayerJoinMenu()
passwordTransform.localPosition += new Vector3(0, -36, 0);
passwordTransform.GetComponent<Text>().text = "Password (optional)".Translate();
passwordTransform.name = "Password (optional)";

passwordInput = passwordTransform.GetComponentInChildren<InputField>();
passwordInput.contentType = InputField.ContentType.Password;
passwordInput.text = "";
Expand Down Expand Up @@ -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;
}
}
54 changes: 54 additions & 0 deletions NebulaPatcher/Patches/Transpilers/UIGalaxySelect_Transpiler.cs
Original file line number Diff line number Diff line change
@@ -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<CodeInstruction> _OnUpdate_Transpiler(IEnumerable<CodeInstruction> 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<Func<string, string>>((originalBtnName) =>
{
if (UIVirtualStarmap_Transpiler.customBirthPlanet == -1)
return originalBtnName;
return "开始游戏".Translate() + '(' + UIVirtualStarmap_Transpiler.customBirthPlanetName + ')';
}));

return matcher.InstructionEnumeration();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.text = $"Start Game at {pData.displayName}";
var text = UIRoot.instance.galaxySelect.startButtonText;
text.horizontalOverflow = HorizontalWrapMode.Overflow;

if (pData.data == null)
Expand Down Expand Up @@ -587,6 +586,7 @@ public static IEnumerable<CodeInstruction> 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
Expand Down

0 comments on commit fd7ed2a

Please sign in to comment.