RetryDialog.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #if ENABLE_MONO && (DEVELOPMENT_BUILD || UNITY_EDITOR)
  2. using JetBrains.Annotations;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SingularityGroup.HotReload {
  6. internal class RetryDialog : MonoBehaviour {
  7. [Header("UI controls")]
  8. public Button buttonHide;
  9. public Button buttonRetryAutoPair;
  10. public Button buttonTroubleshoot;
  11. public Text textSummary;
  12. public Text textSuggestion;
  13. public InputField ipInput;
  14. [Tooltip("Hidden by default")]
  15. public Text textForDebugging;
  16. [Header("For HotReload Devs")]
  17. // In Unity Editor, click checkbox to see info helpful for debugging bugs
  18. public bool enableDebugging;
  19. // [Header("Other")]
  20. // [Tooltip("Used when your project does not create an EventSystem early enough")]
  21. // public GameObject fallbackEventSystem;
  22. private static RetryDialog _I;
  23. public string DebugInfo {
  24. set {
  25. textForDebugging.text = value;
  26. }
  27. }
  28. public bool autoConnect { get; set; }
  29. void Start() {
  30. buttonHide.onClick.AddListener(() => {
  31. Hide();
  32. });
  33. buttonRetryAutoPair.onClick.AddListener(() => {
  34. Hide();
  35. int port;
  36. var ipAndPort = ipInput.textComponent.text.Split(':');
  37. if (ipAndPort.Length != 2 || !int.TryParse(ipAndPort[1], out port)) {
  38. port = PlayerEntrypoint.PlayerBuildInfo?.buildMachinePort ?? RequestHelper.defaultPort;
  39. }
  40. var ip = ipAndPort.Length > 0 ? ipAndPort[0] : string.Empty;
  41. PlayerEntrypoint.TryConnectToIpAndPort(ip, port);
  42. });
  43. buttonTroubleshoot.onClick.AddListener(() => {
  44. Application.OpenURL("https://hotreload.net/documentation#connection-issues");
  45. });
  46. }
  47. [CanBeNull]
  48. public static PatchServerInfo TargetServer { private get; set; } = null;
  49. public static ServerHandshake.Result HandshakeResults { private get; set; } = ServerHandshake.Result.None;
  50. private void OnEnable() {
  51. ipInput.text = $"{PlayerEntrypoint.PlayerBuildInfo?.buildMachineHostName}:{PlayerEntrypoint.PlayerBuildInfo?.buildMachinePort}";
  52. UpdateUI();
  53. }
  54. void Update() {
  55. UpdateUI();
  56. }
  57. void UpdateUI() {
  58. // assumes that auto-pair already tried for several seconds
  59. // suggestions to help the user when auto-pair is failing
  60. var networkText = Application.isMobilePlatform ? "WiFi" : "LAN/WiFi";
  61. var noWifiNetwork = $"Is this device connected to {networkText}?";
  62. var waitForCompiling = "Wait for compiling to finish before trying again";
  63. var targetNetworkIsReachable = $"Make sure you're on the same {networkText} network. Also ensure Hot Reload is running";
  64. if (Application.internetReachability != NetworkReachability.ReachableViaLocalAreaNetwork) {
  65. textSuggestion.text = noWifiNetwork;
  66. } else if (HandshakeResults.HasFlag(ServerHandshake.Result.WaitForCompiling)) {
  67. // Note: Technically the player could do the waiting itself, and handshake again with the server
  68. // only after compiling finishes... Telling the user to do that is easier to implement though.
  69. textSuggestion.text = waitForCompiling;
  70. } else {
  71. textSuggestion.text = targetNetworkIsReachable;
  72. }
  73. textSummary.text = autoConnect ? "Auto-pair encountered an issue" : "Connection failed";
  74. if (enableDebugging && textForDebugging) {
  75. textForDebugging.enabled = true;
  76. textForDebugging.text = $"the target = {TargetServer}";
  77. }
  78. }
  79. /// hide this dialog
  80. void Hide() {
  81. gameObject.SetActive(false); // this should disable the Update loop?
  82. }
  83. }
  84. }
  85. #endif