ExposeServerOption.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Threading.Tasks;
  3. using SingularityGroup.HotReload.Editor.Cli;
  4. using UnityEditor;
  5. namespace SingularityGroup.HotReload.Editor {
  6. internal sealed class ExposeServerOption : ComputerOptionBase {
  7. public override string ShortSummary => "Allow Devices to Connect";
  8. public override string Summary => "Allow Devices to Connect (WiFi)";
  9. public override void InnerOnGUI() {
  10. string description;
  11. if (GetValue()) {
  12. description = "The HotReload server is reachable from devices on the same Wifi network";
  13. } else {
  14. description = "The HotReload server is available to your computer only. Other devices cannot connect to it.";
  15. }
  16. EditorGUILayout.LabelField(description, HotReloadWindowStyles.WrapStyle);
  17. }
  18. public override bool GetValue() {
  19. return HotReloadPrefs.ExposeServerToLocalNetwork;
  20. }
  21. public override void SetValue(SerializedObject so, bool val) {
  22. // AllowAndroidAppToMakeHttpRequestsOption
  23. if (val == HotReloadPrefs.ExposeServerToLocalNetwork) {
  24. return;
  25. }
  26. HotReloadPrefs.ExposeServerToLocalNetwork = val;
  27. if (val) {
  28. // they allowed this one for mobile builds, so now we allow everything else needed for player build to work with HR
  29. new AllowAndroidAppToMakeHttpRequestsOption().SetValue(so, true);
  30. }
  31. RunTask(() => {
  32. RunOnMainThreadSync(() => {
  33. var isRunningResult = ServerHealthCheck.I.IsServerHealthy;
  34. if (isRunningResult) {
  35. var restartServer = EditorUtility.DisplayDialog("Hot Reload",
  36. $"When changing '{Summary}', the Hot Reload server must be restarted for this to take effect." +
  37. "\nDo you want to restart it now?",
  38. "Restart server", "Don't restart");
  39. if (restartServer) {
  40. CodePatcher.I.ClearPatchedMethods();
  41. EditorCodePatcher.RestartCodePatcher().Forget();
  42. }
  43. }
  44. });
  45. });
  46. }
  47. void RunTask(Action action) {
  48. var token = HotReloadWindow.Current.cancelToken;
  49. Task.Run(() => {
  50. if (token.IsCancellationRequested) return;
  51. try {
  52. action();
  53. } catch (Exception ex) {
  54. ThreadUtility.LogException(ex, token);
  55. }
  56. }, token);
  57. }
  58. void RunOnMainThreadSync(Action action) {
  59. ThreadUtility.RunOnMainThread(action, HotReloadWindow.Current.cancelToken);
  60. }
  61. }
  62. }