ConnectionDialog.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #if ENABLE_MONO && (DEVELOPMENT_BUILD || UNITY_EDITOR)
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace SingularityGroup.HotReload {
  5. internal class ConnectionDialog : MonoBehaviour {
  6. [Header("UI controls")]
  7. public Button buttonHide;
  8. [Header("Information")]
  9. public Text textSummary;
  10. public Text textSuggestion;
  11. void Start() {
  12. buttonHide.onClick.AddListener(Hide);
  13. }
  14. public int pendingPatches = 0;
  15. public int patchesApplied = 0;
  16. private void Awake() {
  17. SyncPatchCounts();
  18. }
  19. bool SyncPatchCounts() {
  20. var changed = false;
  21. if (pendingPatches != CodePatcher.I.PendingPatches.Count) {
  22. pendingPatches = CodePatcher.I.PendingPatches.Count;
  23. changed = true;
  24. }
  25. if (patchesApplied != CodePatcher.I.PatchesApplied) {
  26. patchesApplied = CodePatcher.I.PatchesApplied;
  27. changed = true;
  28. }
  29. return changed;
  30. }
  31. /// <param name="summary">One of the <see cref="ConnectionSummary"/> constants</param>
  32. public void SetSummary(string summary = ConnectionSummary.Connected) {
  33. if (textSummary != null) textSummary.text = summary;
  34. isConnected = summary == ConnectionSummary.Connected;
  35. }
  36. private bool isConnected = false;
  37. // assumes that auto-pair already tried for several seconds
  38. void Update() {
  39. textSuggestion.enabled = isConnected;
  40. if (SyncPatchCounts()) {
  41. textSuggestion.text = $"Patches: {pendingPatches} pending, {patchesApplied} applied";
  42. }
  43. }
  44. /// hide this dialog
  45. void Hide() {
  46. gameObject.SetActive(false); // this should disable the Update loop?
  47. }
  48. }
  49. /// <summary>
  50. /// The connection between device and Hot Reload can be summarized in a few words.
  51. /// </summary>
  52. /// <remarks>
  53. /// The summary may be shown for less than a second, as the connection can change without warning.<br/>
  54. /// Therefore, we use short and simple messages.
  55. /// </remarks>
  56. internal static class ConnectionSummary {
  57. public const string Cancelled = "Cancelled";
  58. public const string Connecting = "Connecting ...";
  59. public const string Handshaking = "Handshaking ...";
  60. public const string DifferencesFound = "Differences found";
  61. public const string Connected = "Connected!";
  62. // reconnecting can be shown for a long time, so a longer message is okay
  63. public const string TryingToReconnect = "Trying to reconnect ...";
  64. public const string Disconnected = "Disconnected";
  65. }
  66. }
  67. #endif