PhotonEditor.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="PhotonEditor.cs" company="Exit Games GmbH">
  3. // PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // MenuItems and in-Editor scripts for PhotonNetwork.
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // ----------------------------------------------------------------------------
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Reflection;
  14. using UnityEditor;
  15. using UnityEditor.Callbacks;
  16. using UnityEditor.Compilation;
  17. using UnityEngine;
  18. namespace Photon.Pun
  19. {
  20. using Realtime;
  21. public class PunWizardText
  22. {
  23. public string WindowTitle = "PUN Wizard";
  24. public string SetupWizardWarningTitle = "Warning";
  25. public string SetupWizardWarningMessage = "You have not yet run the Photon setup wizard! Your game won't be able to connect. See Windows -> Photon Unity Networking.";
  26. public string MainMenuButton = "Main Menu";
  27. public string SetupWizardTitle = "PUN Setup";
  28. public string SetupWizardInfo = "Thanks for importing Photon Unity Networking.\nThis window should set you up.\n\n<b>-</b> To use an existing Photon Cloud App, enter your AppId.\n<b>-</b> To register an account or access an existing one, enter the account's mail address.\n<b>-</b> To use Photon OnPremise, skip this step.";
  29. public string EmailOrAppIdLabel = "AppId or Email";
  30. public string AlreadyRegisteredInfo = "The email is registered so we can't fetch your AppId (without password).\n\nPlease login online to get your AppId and paste it above.";
  31. public string SkipRegistrationInfo = "Skipping? No problem:\nEdit your server settings in the PhotonServerSettings file.";
  32. public string RegisteredNewAccountInfo = "We created a (free) account and fetched you an AppId.\nWelcome. Your PUN project is setup.";
  33. public string AppliedToSettingsInfo = "Your AppId is now applied to this project.";
  34. public string SetupCompleteInfo = "<b>Done!</b>\nAll connection settings can be edited in the <b>PhotonServerSettings</b> now.\nHave a look.";
  35. public string CloseWindowButton = "Close";
  36. public string SkipButton = "Skip";
  37. public string SetupButton = "Setup Project";
  38. public string CancelButton = "Cancel";
  39. public string PUNWizardLabel = "PUN Wizard";
  40. public string SettingsButton = "Settings:";
  41. public string SetupServerCloudLabel = "Setup wizard for setting up your own server or the cloud.";
  42. public string WarningPhotonDisconnect = "Disconnecting PUN due to recompile. Exit PlayMode.";
  43. public string StartButton = "Start";
  44. public string LocateSettingsButton = "Locate PhotonServerSettings";
  45. public string SettingsHighlightLabel = "Highlights the used photon settings file in the project.";
  46. public string DocumentationLabel = "Documentation:";
  47. public string OpenPDFText = "Reference PDF";
  48. public string OpenPDFTooltip = "Opens the local documentation pdf.";
  49. public string OpenDevNetText = "Doc Pages / Manual";
  50. public string OpenDevNetTooltip = "Online documentation for Photon.";
  51. public string OpenCloudDashboardText = "Cloud Dashboard Login";
  52. public string OpenCloudDashboardTooltip = "Review Cloud App information and statistics.";
  53. public string OpenForumText = "Open Forum";
  54. public string OpenForumTooltip = "Online support for Photon.";
  55. public string OkButton = "Ok";
  56. public string OwnHostCloudCompareLabel = "How 'my own host' compares to 'cloud'.";
  57. public string ComparisonPageButton = "Cloud versus OnPremise";
  58. public string ConnectionTitle = "Connecting";
  59. public string ConnectionInfo = "Connecting to the account service...";
  60. public string ErrorTextTitle = "Error";
  61. public string IncorrectRPCListTitle = "Warning: RPC-list becoming incompatible!";
  62. public string IncorrectRPCListLabel = "Your project's RPC-list is full, so we can't add some RPCs just compiled.\n\nBy removing outdated RPCs, the list will be long enough but incompatible with older client builds!\n\nMake sure you change the game version where you use PhotonNetwork.ConnectUsingSettings().";
  63. public string RemoveOutdatedRPCsLabel = "Remove outdated RPCs";
  64. public string FullRPCListTitle = "Warning: RPC-list is full!";
  65. public string FullRPCListLabel = "Your project's RPC-list is too long for PUN.\n\nYou can change PUN's source to use short-typed RPC index. Look for comments 'LIMITS RPC COUNT'\n\nAlternatively, remove some RPC methods (use more parameters per RPC maybe).\n\nAfter a RPC-list refresh, make sure you change the game version where you use PhotonNetwork.ConnectUsingSettings().";
  66. public string SkipRPCListUpdateLabel = "Skip RPC-list update";
  67. public string PUNNameReplaceTitle = "Warning: RPC-list Compatibility";
  68. public string PUNNameReplaceLabel = "PUN replaces RPC names with numbers by using the RPC-list. All clients must use the same list for that.\n\nClearing it most likely makes your client incompatible with previous versions! Change your game version or make sure the RPC-list matches other clients.";
  69. public string RPCListCleared = "Clear RPC-list";
  70. public string ServerSettingsCleanedWarning = "Cleared the PhotonServerSettings.RpcList, which breaks compatibility with older builds. You should update the \"App Version\" in the PhotonServerSettings to avoid issues.";
  71. public string WizardMainWindowInfo = "This window should help you find important settings for PUN, as well as documentation.";
  72. }
  73. public class PhotonEditor : EditorWindow
  74. {
  75. protected static Type WindowType = typeof(PhotonEditor);
  76. protected Vector2 scrollPos = Vector2.zero;
  77. private readonly Vector2 preferredSize = new Vector2(350, 400);
  78. private static Texture2D BackgroundImage;
  79. public static PunWizardText CurrentLang = new PunWizardText();
  80. /// <summary>
  81. /// third parties custom token
  82. /// </summary>
  83. public static string CustomToken = null;
  84. /// <summary>
  85. /// third parties custom context
  86. /// </summary>
  87. public static string CustomContext = null;
  88. protected static string DocumentationLocation = "Assets/Photon/PhotonNetworking-Documentation.pdf";
  89. protected static string UrlFreeLicense = "https://dashboard.photonengine.com/en-US/SelfHosted";
  90. public const string UrlDevNet = "https://doc.photonengine.com/en-us/pun/v2";
  91. protected static string UrlForum = "https://forum.photonengine.com";
  92. protected static string UrlCompare = "https://doc.photonengine.com/en-us/realtime/current/getting-started/onpremise-or-saas";
  93. protected static string UrlHowToSetup = "https://doc.photonengine.com/en-us/onpremise/current/getting-started/photon-server-in-5min";
  94. protected static string UrlAppIDExplained = "https://doc.photonengine.com/en-us/realtime/current/getting-started/obtain-your-app-id";
  95. public const string UrlCloudDashboard = "https://dashboard.photonengine.com/en-US/account/signin?email=";
  96. public const string UrlPunSettings = "https://doc.photonengine.com/en-us/pun/v2/getting-started/initial-setup"; // the SeverSettings class has this url directly in it's HelpURL attribute.
  97. private enum PhotonSetupStates
  98. {
  99. MainUi,
  100. RegisterForPhotonCloud,
  101. EmailAlreadyRegistered,
  102. GoEditPhotonServerSettings,
  103. EmailRegistrationPending
  104. }
  105. private bool isSetupWizard = false;
  106. private PhotonSetupStates photonSetupState = PhotonSetupStates.RegisterForPhotonCloud;
  107. private bool minimumInput = false;
  108. private bool useMail = false;
  109. private bool useAppId = false;
  110. private bool useSkip = false;
  111. private bool highlightedSettings = false;
  112. private bool close = false;
  113. private string mailOrAppId = string.Empty;
  114. private static double lastWarning = 0;
  115. private static bool postInspectorUpdate;
  116. [MenuItem("Window/Photon Unity Networking/PUN Wizard &p", false, 0)]
  117. protected static void MenuItemOpenWizard()
  118. {
  119. PhotonEditor win = GetWindow<PhotonEditor>(false, CurrentLang.WindowTitle, true);
  120. if (win == null)
  121. {
  122. return;
  123. }
  124. win.photonSetupState = PhotonSetupStates.MainUi;
  125. win.isSetupWizard = false;
  126. }
  127. [MenuItem("Window/Photon Unity Networking/Highlight Server Settings %#&p", false, 1)]
  128. protected static void MenuItemHighlightSettings()
  129. {
  130. HighlightSettings();
  131. }
  132. [UnityEditor.InitializeOnLoadMethod]
  133. public static void InitializeOnLoadMethod()
  134. {
  135. //Debug.Log("InitializeOnLoadMethod()");
  136. EditorApplication.delayCall += OnDelayCall;
  137. }
  138. // used to register for various events (post-load)
  139. private static void OnDelayCall()
  140. {
  141. //Debug.Log("OnDelayCall()");
  142. postInspectorUpdate = true;
  143. EditorApplication.playModeStateChanged -= PlayModeStateChanged;
  144. EditorApplication.playModeStateChanged += PlayModeStateChanged;
  145. #if UNITY_2021_1_OR_NEWER
  146. CompilationPipeline.compilationStarted -= OnCompileStarted21;
  147. CompilationPipeline.compilationStarted += OnCompileStarted21;
  148. #else
  149. CompilationPipeline.assemblyCompilationStarted -= OnCompileStarted;
  150. CompilationPipeline.assemblyCompilationStarted += OnCompileStarted;
  151. #endif
  152. #if (UNITY_2018 || UNITY_2018_1_OR_NEWER)
  153. EditorApplication.projectChanged -= OnProjectChanged;
  154. EditorApplication.projectChanged += OnProjectChanged;
  155. #else
  156. EditorApplication.projectWindowChanged -= OnProjectChanged;
  157. EditorApplication.projectWindowChanged += OnProjectChanged;
  158. #endif
  159. if (!EditorApplication.isPlaying && !EditorApplication.isPlayingOrWillChangePlaymode)
  160. {
  161. OnProjectChanged(); // call this initially from here, as the project change events happened earlier (on start of the Editor)
  162. PhotonEditor.UpdateRpcList();
  163. }
  164. }
  165. // called in editor, opens wizard for initial setup, keeps scene PhotonViews up to date and closes connections when compiling (to avoid issues)
  166. private static void OnProjectChanged()
  167. {
  168. // Prevent issues with Unity Cloud Builds where ServerSettings are not found.
  169. // Also, within the context of a Unity Cloud Build, ServerSettings is already present anyway.
  170. #if UNITY_CLOUD_BUILD
  171. return;
  172. #else
  173. if (PhotonNetwork.PhotonServerSettings == null)
  174. {
  175. // the PhotonServerSettings are loaded or created. If both fails, the Editor should probably not run (anymore).
  176. return;
  177. }
  178. PunSceneSettings.SanitizeSceneSettings();
  179. // serverSetting is null when the file gets deleted. otherwise, the wizard should only run once and only if hosting option is not (yet) set
  180. if (!PhotonNetwork.PhotonServerSettings.DisableAutoOpenWizard)
  181. {
  182. ShowRegistrationWizard();
  183. PhotonNetwork.PhotonServerSettings.DisableAutoOpenWizard = true;
  184. PhotonEditor.SaveSettings();
  185. }
  186. #endif
  187. }
  188. #if UNITY_2021_1_OR_NEWER
  189. private static void OnCompileStarted21(object obj)
  190. {
  191. OnCompileStarted(obj as string);
  192. }
  193. #endif
  194. private static void OnCompileStarted(string obj)
  195. {
  196. if (PhotonNetwork.IsConnected)
  197. {
  198. // log warning, unless there was one recently
  199. if (EditorApplication.timeSinceStartup - lastWarning > 3)
  200. {
  201. Debug.LogWarning(CurrentLang.WarningPhotonDisconnect);
  202. lastWarning = EditorApplication.timeSinceStartup;
  203. }
  204. PhotonNetwork.Disconnect();
  205. PhotonNetwork.NetworkingClient.LoadBalancingPeer.DispatchIncomingCommands();
  206. #if UNITY_2019_4_OR_NEWER && UNITY_EDITOR
  207. EditorApplication.ExitPlaymode();
  208. #endif
  209. }
  210. }
  211. [DidReloadScripts]
  212. private static void OnDidReloadScripts()
  213. {
  214. //Debug.Log("OnDidReloadScripts() postInspectorUpdate: "+postInspectorUpdate + " isPlayingOrWillChangePlaymode: "+EditorApplication.isPlayingOrWillChangePlaymode);
  215. if (postInspectorUpdate && !EditorApplication.isPlayingOrWillChangePlaymode)
  216. {
  217. PhotonEditor.UpdateRpcList(); // could be called when compilation finished (instead of when reload / compile starts)
  218. }
  219. }
  220. private static void PlayModeStateChanged(PlayModeStateChange state)
  221. {
  222. //Debug.Log("PlayModeStateChanged");
  223. if (EditorApplication.isPlaying || !EditorApplication.isPlayingOrWillChangePlaymode)
  224. {
  225. return;
  226. }
  227. if (string.IsNullOrEmpty(PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime) && !PhotonNetwork.PhotonServerSettings.AppSettings.IsMasterServerAddress)
  228. {
  229. EditorUtility.DisplayDialog(CurrentLang.SetupWizardWarningTitle, CurrentLang.SetupWizardWarningMessage, CurrentLang.OkButton);
  230. }
  231. }
  232. #region GUI and Wizard
  233. // setup per window
  234. public PhotonEditor()
  235. {
  236. this.minSize = this.preferredSize;
  237. }
  238. protected void Awake()
  239. {
  240. // check if some appid is set. if so, we can avoid registration calls.
  241. if (PhotonNetwork.PhotonServerSettings != null && PhotonNetwork.PhotonServerSettings.AppSettings != null && !string.IsNullOrEmpty(PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime))
  242. {
  243. this.mailOrAppId = PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime;
  244. }
  245. }
  246. /// <summary>Creates an Editor window, showing the cloud-registration wizard for Photon (entry point to setup PUN).</summary>
  247. protected static void ShowRegistrationWizard()
  248. {
  249. PhotonEditor win = GetWindow(WindowType, false, CurrentLang.WindowTitle, true) as PhotonEditor;
  250. if (win == null)
  251. {
  252. return;
  253. }
  254. win.photonSetupState = PhotonSetupStates.RegisterForPhotonCloud;
  255. win.isSetupWizard = true;
  256. }
  257. // Window Update() callback. On-demand, when Window is open
  258. protected void Update()
  259. {
  260. if (this.close)
  261. {
  262. this.Close();
  263. }
  264. }
  265. protected virtual void OnGUI()
  266. {
  267. if (BackgroundImage == null)
  268. {
  269. string[] paths = AssetDatabase.FindAssets("PunGradient t:Texture2D");
  270. if (paths != null && paths.Length > 0)
  271. {
  272. BackgroundImage = AssetDatabase.LoadAssetAtPath<Texture2D>(AssetDatabase.GUIDToAssetPath(paths[0]));
  273. }
  274. }
  275. PhotonSetupStates oldGuiState = this.photonSetupState; // used to fix an annoying Editor input field issue: wont refresh until focus is changed.
  276. GUI.SetNextControlName(string.Empty);
  277. this.scrollPos = GUILayout.BeginScrollView(this.scrollPos);
  278. if (this.photonSetupState == PhotonSetupStates.MainUi)
  279. {
  280. this.UiMainWizard();
  281. }
  282. else
  283. {
  284. EditorGUI.BeginDisabledGroup(this.photonSetupState == PhotonSetupStates.EmailRegistrationPending);
  285. this.UiSetupApp();
  286. EditorGUI.EndDisabledGroup();
  287. }
  288. GUILayout.EndScrollView();
  289. if (oldGuiState != this.photonSetupState)
  290. {
  291. GUI.FocusControl(string.Empty);
  292. }
  293. }
  294. private string emailSentToAccount;
  295. private bool emailSentToAccountIsRegistered;
  296. protected virtual void UiSetupApp()
  297. {
  298. GUI.skin.label.wordWrap = true;
  299. if (!this.isSetupWizard)
  300. {
  301. GUILayout.BeginHorizontal();
  302. GUILayout.FlexibleSpace();
  303. if (GUILayout.Button(CurrentLang.MainMenuButton, GUILayout.ExpandWidth(false)))
  304. {
  305. this.photonSetupState = PhotonSetupStates.MainUi;
  306. }
  307. GUILayout.EndHorizontal();
  308. }
  309. // setup header
  310. this.UiTitleBox(CurrentLang.SetupWizardTitle, BackgroundImage);
  311. // setup info text
  312. GUI.skin.label.richText = true;
  313. GUILayout.Label(CurrentLang.SetupWizardInfo);
  314. // input of appid or mail
  315. EditorGUILayout.Separator();
  316. GUILayout.Label(CurrentLang.EmailOrAppIdLabel);
  317. this.minimumInput = false;
  318. this.useMail = false;
  319. this.useAppId = false;
  320. this.mailOrAppId = EditorGUILayout.TextField(this.mailOrAppId);
  321. if (!string.IsNullOrEmpty(this.mailOrAppId))
  322. {
  323. this.mailOrAppId = this.mailOrAppId.Trim(); // note: we trim all input
  324. if (AccountService.IsValidEmail(this.mailOrAppId))
  325. {
  326. // input should be a mail address
  327. this.useMail = true;
  328. // check if the current input equals earlier input, which is known to be registered already
  329. this.minimumInput = !this.mailOrAppId.Equals(this.emailSentToAccount) || !this.emailSentToAccountIsRegistered;
  330. }
  331. else if (ServerSettings.IsAppId(this.mailOrAppId))
  332. {
  333. // this should be an appId
  334. this.minimumInput = true;
  335. this.useAppId = true;
  336. }
  337. }
  338. // button to skip setup
  339. GUILayout.BeginHorizontal();
  340. GUILayout.FlexibleSpace();
  341. if (GUILayout.Button(CurrentLang.SkipButton, GUILayout.Width(100)))
  342. {
  343. this.photonSetupState = PhotonSetupStates.GoEditPhotonServerSettings;
  344. this.useSkip = true;
  345. this.useMail = false;
  346. this.useAppId = false;
  347. }
  348. // SETUP button
  349. EditorGUI.BeginDisabledGroup(!this.minimumInput);
  350. if (GUILayout.Button(CurrentLang.SetupButton, GUILayout.Width(100)))
  351. {
  352. this.useSkip = false;
  353. GUIUtility.keyboardControl = 0;
  354. if (this.useMail)
  355. {
  356. this.RegisterWithEmail(this.mailOrAppId); // sets state
  357. }
  358. else if (this.useAppId)
  359. {
  360. this.photonSetupState = PhotonSetupStates.GoEditPhotonServerSettings;
  361. Undo.RecordObject(PhotonNetwork.PhotonServerSettings, "Update PhotonServerSettings for PUN");
  362. PhotonNetwork.PhotonServerSettings.UseCloud(this.mailOrAppId);
  363. PhotonEditor.SaveSettings();
  364. }
  365. }
  366. EditorGUI.EndDisabledGroup();
  367. GUILayout.FlexibleSpace();
  368. GUILayout.EndHorizontal();
  369. // existing account needs to fetch AppId online
  370. if (this.photonSetupState == PhotonSetupStates.EmailAlreadyRegistered)
  371. {
  372. // button to open dashboard and get the AppId
  373. GUILayout.Space(15);
  374. GUILayout.Label(CurrentLang.AlreadyRegisteredInfo);
  375. GUILayout.BeginHorizontal();
  376. GUILayout.FlexibleSpace();
  377. if (GUILayout.Button(new GUIContent(CurrentLang.OpenCloudDashboardText, CurrentLang.OpenCloudDashboardTooltip), GUILayout.Width(205)))
  378. {
  379. Application.OpenURL(string.Concat(UrlCloudDashboard, Uri.EscapeUriString(this.mailOrAppId)));
  380. this.mailOrAppId = string.Empty;
  381. }
  382. GUILayout.FlexibleSpace();
  383. GUILayout.EndHorizontal();
  384. }
  385. else if (this.photonSetupState == PhotonSetupStates.GoEditPhotonServerSettings)
  386. {
  387. if (!this.highlightedSettings)
  388. {
  389. this.highlightedSettings = true;
  390. HighlightSettings();
  391. }
  392. GUILayout.Space(15);
  393. if (this.useSkip)
  394. {
  395. GUILayout.Label(CurrentLang.SkipRegistrationInfo);
  396. }
  397. else if (this.useMail)
  398. {
  399. GUILayout.Label(CurrentLang.RegisteredNewAccountInfo);
  400. }
  401. else if (this.useAppId)
  402. {
  403. GUILayout.Label(CurrentLang.AppliedToSettingsInfo);
  404. }
  405. // setup-complete info
  406. GUILayout.Space(15);
  407. GUILayout.Label(CurrentLang.SetupCompleteInfo);
  408. // close window (done)
  409. GUILayout.BeginHorizontal();
  410. GUILayout.FlexibleSpace();
  411. if (GUILayout.Button(CurrentLang.CloseWindowButton, GUILayout.Width(205)))
  412. {
  413. this.close = true;
  414. }
  415. GUILayout.FlexibleSpace();
  416. GUILayout.EndHorizontal();
  417. }
  418. GUI.skin.label.richText = false;
  419. }
  420. private void UiTitleBox(string title, Texture2D bgIcon)
  421. {
  422. GUIStyle bgStyle = EditorGUIUtility.isProSkin ? new GUIStyle(GUI.skin.GetStyle("Label")) : new GUIStyle(GUI.skin.GetStyle("WhiteLabel"));
  423. bgStyle.padding = new RectOffset(10, 10, 10, 10);
  424. bgStyle.fontSize = 22;
  425. bgStyle.fontStyle = FontStyle.Bold;
  426. if (bgIcon != null)
  427. {
  428. bgStyle.normal.background = bgIcon;
  429. }
  430. EditorGUILayout.BeginHorizontal();
  431. GUILayout.FlexibleSpace();
  432. EditorGUILayout.EndHorizontal();
  433. Rect scale = GUILayoutUtility.GetLastRect();
  434. scale.height = 44;
  435. GUI.Label(scale, title, bgStyle);
  436. GUILayout.Space(scale.height + 5);
  437. }
  438. protected virtual void UiMainWizard()
  439. {
  440. GUILayout.Space(15);
  441. // title
  442. this.UiTitleBox(CurrentLang.PUNWizardLabel, BackgroundImage);
  443. EditorGUILayout.BeginVertical(new GUIStyle() { padding = new RectOffset(10, 10, 10, 10) });
  444. // wizard info text
  445. GUILayout.Label(CurrentLang.WizardMainWindowInfo, new GUIStyle("Label") { wordWrap = true });
  446. GUILayout.Space(15);
  447. // settings button
  448. GUILayout.Label(CurrentLang.SettingsButton, EditorStyles.boldLabel);
  449. if (GUILayout.Button(new GUIContent(CurrentLang.LocateSettingsButton, CurrentLang.SettingsHighlightLabel)))
  450. {
  451. HighlightSettings();
  452. }
  453. if (GUILayout.Button(new GUIContent(CurrentLang.OpenCloudDashboardText, CurrentLang.OpenCloudDashboardTooltip)))
  454. {
  455. Application.OpenURL(UrlCloudDashboard + Uri.EscapeUriString(this.mailOrAppId));
  456. }
  457. if (GUILayout.Button(new GUIContent(CurrentLang.SetupButton, CurrentLang.SetupServerCloudLabel)))
  458. {
  459. this.photonSetupState = PhotonSetupStates.RegisterForPhotonCloud;
  460. }
  461. GUILayout.Space(15);
  462. // documentation
  463. GUILayout.Label(CurrentLang.DocumentationLabel, EditorStyles.boldLabel);
  464. if (GUILayout.Button(new GUIContent(CurrentLang.OpenPDFText, CurrentLang.OpenPDFTooltip)))
  465. {
  466. EditorUtility.OpenWithDefaultApp(DocumentationLocation);
  467. }
  468. if (GUILayout.Button(new GUIContent(CurrentLang.OpenDevNetText, CurrentLang.OpenDevNetTooltip)))
  469. {
  470. Application.OpenURL(UrlDevNet);
  471. }
  472. GUI.skin.label.wordWrap = true;
  473. GUILayout.Label(CurrentLang.OwnHostCloudCompareLabel);
  474. if (GUILayout.Button(CurrentLang.ComparisonPageButton))
  475. {
  476. Application.OpenURL(UrlCompare);
  477. }
  478. if (GUILayout.Button(new GUIContent(CurrentLang.OpenForumText, CurrentLang.OpenForumTooltip)))
  479. {
  480. Application.OpenURL(UrlForum);
  481. }
  482. GUILayout.EndVertical();
  483. }
  484. #endregion
  485. private AccountService serviceClient;
  486. protected virtual void RegisterWithEmail(string email)
  487. {
  488. List<ServiceTypes> types = new List<ServiceTypes>();
  489. types.Add(ServiceTypes.Pun);
  490. if (PhotonEditorUtils.HasChat)
  491. {
  492. types.Add(ServiceTypes.Chat);
  493. }
  494. if (PhotonEditorUtils.HasVoice)
  495. {
  496. types.Add(ServiceTypes.Voice);
  497. }
  498. if (this.serviceClient == null)
  499. {
  500. this.serviceClient = new AccountService();
  501. this.serviceClient.CustomToken = CustomToken;
  502. this.serviceClient.CustomContext = CustomContext;
  503. }
  504. else
  505. {
  506. // while RegisterByEmail will check RequestPendingResult below, it would also display an error message. no needed in this case
  507. if (this.serviceClient.RequestPendingResult)
  508. {
  509. Debug.LogWarning("Registration request is pending a response. Please wait.");
  510. return;
  511. }
  512. }
  513. this.emailSentToAccount = email;
  514. this.emailSentToAccountIsRegistered = false;
  515. if (this.serviceClient.RegisterByEmail(email, types, RegisterWithEmailSuccessCallback, RegisterWithEmailErrorCallback, "PUN"+PhotonNetwork.PunVersion))
  516. {
  517. this.photonSetupState = PhotonSetupStates.EmailRegistrationPending;
  518. EditorUtility.DisplayProgressBar(CurrentLang.ConnectionTitle, CurrentLang.ConnectionInfo, 0.5f);
  519. }
  520. else
  521. {
  522. this.DisplayErrorMessage("Email registration request could not be sent. Retry again or check error logs and contact support.");
  523. }
  524. }
  525. private void RegisterWithEmailSuccessCallback(AccountServiceResponse res)
  526. {
  527. EditorUtility.ClearProgressBar();
  528. this.emailSentToAccountIsRegistered = true; // email is either registered now, or was already
  529. if (res.ReturnCode == AccountServiceReturnCodes.Success)
  530. {
  531. string key = ((int) ServiceTypes.Pun).ToString();
  532. string appId;
  533. if (res.ApplicationIds.TryGetValue(key, out appId))
  534. {
  535. this.mailOrAppId = appId;
  536. PhotonNetwork.PhotonServerSettings.UseCloud(this.mailOrAppId, null);
  537. key = ((int) ServiceTypes.Chat).ToString();
  538. if (res.ApplicationIds.TryGetValue(key, out appId))
  539. {
  540. PhotonNetwork.PhotonServerSettings.AppSettings.AppIdChat = appId;
  541. }
  542. else if (PhotonEditorUtils.HasChat)
  543. {
  544. Debug.LogWarning("Registration successful but no Chat AppId returned");
  545. }
  546. key = ((int) ServiceTypes.Voice).ToString();
  547. if (res.ApplicationIds.TryGetValue(key, out appId))
  548. {
  549. PhotonNetwork.PhotonServerSettings.AppSettings.AppIdVoice = appId;
  550. }
  551. else if (PhotonEditorUtils.HasVoice)
  552. {
  553. Debug.LogWarning("Registration successful but no Voice AppId returned");
  554. }
  555. PhotonEditor.SaveSettings();
  556. this.photonSetupState = PhotonSetupStates.GoEditPhotonServerSettings;
  557. }
  558. else
  559. {
  560. DisplayErrorMessage("Registration successful but no PUN AppId returned");
  561. }
  562. }
  563. else
  564. {
  565. PhotonEditor.SaveSettings();
  566. if (res.ReturnCode == AccountServiceReturnCodes.EmailAlreadyRegistered)
  567. {
  568. this.photonSetupState = PhotonSetupStates.EmailAlreadyRegistered;
  569. }
  570. else
  571. {
  572. DisplayErrorMessage(res.Message);
  573. }
  574. }
  575. }
  576. private void RegisterWithEmailErrorCallback(string error)
  577. {
  578. EditorUtility.ClearProgressBar();
  579. DisplayErrorMessage(error);
  580. }
  581. private void DisplayErrorMessage(string error)
  582. {
  583. EditorUtility.DisplayDialog(CurrentLang.ErrorTextTitle, error, CurrentLang.OkButton);
  584. this.photonSetupState = PhotonSetupStates.RegisterForPhotonCloud;
  585. }
  586. // Pings PhotonServerSettings and makes it selected (show in Inspector)
  587. private static void HighlightSettings()
  588. {
  589. ServerSettings serverSettings = (ServerSettings)Resources.Load(PhotonNetwork.ServerSettingsFileName, typeof(ServerSettings));
  590. Selection.objects = new UnityEngine.Object[] { serverSettings };
  591. EditorGUIUtility.PingObject(serverSettings);
  592. }
  593. // Marks settings object as dirty, so it gets saved.
  594. // unity 5.3 changes the usecase for SetDirty(). but here we don't modify a scene object! so it's ok to use
  595. private static void SaveSettings()
  596. {
  597. EditorUtility.SetDirty(PhotonNetwork.PhotonServerSettings);
  598. }
  599. #region RPC List Handling
  600. public static void UpdateRpcList()
  601. {
  602. //Debug.Log("UpdateRpcList()");
  603. if (PhotonNetwork.PhotonServerSettings == null)
  604. {
  605. Debug.LogWarning("UpdateRpcList() wasn not able to access the PhotonServerSettings. Not updating the RPCs.");
  606. return;
  607. }
  608. // check all "script assemblies" for methods with PunRPC attribute
  609. List<string> additionalRpcs = new List<string>(); // not yet listed rpc-method names go here
  610. List<string> allRpcs = new List<string>();
  611. #if UNITY_2019_2_OR_NEWER
  612. // we can make use of the new TypeCache to find methods with PunRPC attribute
  613. var extractedMethods = TypeCache.GetMethodsWithAttribute<PunRPC>();
  614. foreach (var methodInfo in extractedMethods)
  615. {
  616. allRpcs.Add(methodInfo.Name);
  617. if (!PhotonNetwork.PhotonServerSettings.RpcList.Contains(methodInfo.Name) && !additionalRpcs.Contains(methodInfo.Name))
  618. {
  619. additionalRpcs.Add(methodInfo.Name);
  620. }
  621. }
  622. #else
  623. System.Reflection.Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies().Where(a => !(a.ManifestModule is System.Reflection.Emit.ModuleBuilder)).ToArray();
  624. foreach (var assembly in assemblies)
  625. {
  626. if (!assembly.Location.Contains("ScriptAssemblies") || assembly.FullName.StartsWith("Assembly-CSharp-Editor"))
  627. {
  628. continue;
  629. }
  630. var types = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(MonoBehaviour)));
  631. var methodInfos = types.SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance));
  632. var methodNames = methodInfos.Where(m => m.IsDefined(typeof(PunRPC), false)).Select(mi => mi.Name).ToArray();
  633. var additional = methodNames.Where(n => !PhotonNetwork.PhotonServerSettings.RpcList.Contains(n) && !additionalRpcs.Contains(n));
  634. allRpcs.AddRange(methodNames);
  635. additionalRpcs.AddRange(additional);
  636. }
  637. #endif
  638. if (additionalRpcs.Count <= 0)
  639. {
  640. //Debug.Log("UpdateRPCs did not found new.");
  641. return;
  642. }
  643. if (additionalRpcs.Count + PhotonNetwork.PhotonServerSettings.RpcList.Count >= byte.MaxValue)
  644. {
  645. if (allRpcs.Count <= byte.MaxValue)
  646. {
  647. bool clearList = EditorUtility.DisplayDialog(CurrentLang.IncorrectRPCListTitle, CurrentLang.IncorrectRPCListLabel, CurrentLang.RemoveOutdatedRPCsLabel, CurrentLang.CancelButton);
  648. if (clearList)
  649. {
  650. PhotonNetwork.PhotonServerSettings.RpcList.Clear();
  651. additionalRpcs = allRpcs.Distinct().ToList(); // we add all unique names
  652. }
  653. else
  654. {
  655. return;
  656. }
  657. }
  658. else
  659. {
  660. EditorUtility.DisplayDialog(CurrentLang.FullRPCListTitle, CurrentLang.FullRPCListLabel, CurrentLang.SkipRPCListUpdateLabel);
  661. return;
  662. }
  663. }
  664. additionalRpcs.Sort();
  665. Undo.RecordObject(PhotonNetwork.PhotonServerSettings, "RPC-list update of PUN.");
  666. PhotonNetwork.PhotonServerSettings.RpcList.AddRange(additionalRpcs);
  667. EditorUtility.SetDirty(PhotonNetwork.PhotonServerSettings);
  668. //Debug.Log("Updated RPCs. Added: "+additionalRpcs.Count);
  669. }
  670. public static void ClearRpcList()
  671. {
  672. bool clearList = EditorUtility.DisplayDialog(CurrentLang.PUNNameReplaceTitle, CurrentLang.PUNNameReplaceLabel, CurrentLang.RPCListCleared, CurrentLang.CancelButton);
  673. if (clearList)
  674. {
  675. ServerSettings serverSettings = PhotonNetwork.PhotonServerSettings;
  676. Undo.RecordObject(serverSettings, "RPC-list cleared for PUN.");
  677. serverSettings.RpcList.Clear();
  678. EditorUtility.SetDirty(serverSettings);
  679. Debug.LogWarning(CurrentLang.ServerSettingsCleanedWarning);
  680. }
  681. }
  682. #endregion
  683. }
  684. }