Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

1563 linhas
77 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using UnityEditor;
  9. using UnityEngine.Networking;
  10. using UnityEngine.UDP.Editor.Analytics;
  11. #if (UNITY_5_6_OR_NEWER && !UNITY_5_6_0)
  12. namespace UnityEngine.UDP.Editor
  13. {
  14. [CustomEditor(typeof(AppStoreSettings))]
  15. public class AppStoreSettingsEditor : UnityEditor.Editor
  16. {
  17. [MenuItem("Window/Unity Distribution Portal/Settings", false, 111)]
  18. public static void CreateAppStoreSettingsAsset()
  19. {
  20. if (File.Exists(AppStoreSettings.appStoreSettingsAssetPath))
  21. {
  22. AppStoreSettings existedAppStoreSettings = CreateInstance<AppStoreSettings>();
  23. existedAppStoreSettings =
  24. (AppStoreSettings) AssetDatabase.LoadAssetAtPath(AppStoreSettings.appStoreSettingsAssetPath,
  25. typeof(AppStoreSettings));
  26. EditorUtility.FocusProjectWindow();
  27. Selection.activeObject = existedAppStoreSettings;
  28. return;
  29. }
  30. if (!Directory.Exists(AppStoreSettings.appStoreSettingsAssetFolder))
  31. Directory.CreateDirectory(AppStoreSettings.appStoreSettingsAssetFolder);
  32. var appStoreSettings = CreateInstance<AppStoreSettings>();
  33. AssetDatabase.CreateAsset(appStoreSettings, AppStoreSettings.appStoreSettingsAssetPath);
  34. EditorUtility.FocusProjectWindow();
  35. Selection.activeObject = appStoreSettings;
  36. }
  37. [MenuItem("Window/Unity Distribution Portal/Settings", true)]
  38. public static bool CheckUnityOAuthValidation()
  39. {
  40. return enableOAuth;
  41. }
  42. private UnityClientInfo unityClientInfo;
  43. private string clientSecret_in_memory;
  44. private string callbackUrl_in_memory;
  45. private string existed_clientId_in_memory;
  46. private bool ownerAuthed = false;
  47. private static readonly bool enableOAuth = Utils.FindTypeByName("UnityEditor.Connect.UnityOAuth") != null;
  48. private string callbackUrl_last;
  49. private string existedClientId = "";
  50. private const string STEP_GET_CLIENT = "get_client";
  51. private const string STEP_UPDATE_CLIENT = "update_client";
  52. private const string STEP_UPDATE_CLIENT_SECRET = "update_client_secret";
  53. private static List<TestAccount> testAccounts = new List<TestAccount>();
  54. private TestAccount testAccount = new TestAccount();
  55. private RolePermission _rolePermission = new RolePermission();
  56. private AppItem currentAppItem;
  57. private bool _checkLink = true;
  58. private bool _canLink = false;
  59. private string targetStep;
  60. public struct ReqStruct
  61. {
  62. public string currentStep;
  63. public string targetStep;
  64. public string eventName;
  65. public UnityWebRequest request;
  66. public GeneralResponse resp;
  67. }
  68. private Queue<ReqStruct> requestQueue = new Queue<ReqStruct>();
  69. private class AppStoreStyles
  70. {
  71. public const string kNoUnityProjectIDErrorMessage =
  72. "Unity Project ID doesn't exist, please go to Window/Services to create one.";
  73. public const int kUnityProjectIDBoxHeight = 24;
  74. public const int kUnityProjectLinkBoxHeight = 50;
  75. public const int kUnityClientBoxHeight = 110;
  76. public const int kUnityAppItemBoxHeight = 60;
  77. public const int kUnityClientIDButtonWidth = 160;
  78. public const int kSaveButtonWidth = 120;
  79. public const int kClientLabelWidth = 140;
  80. public const int kClientLabelHeight = 16;
  81. public const int kClientLabelWidthShort = 80;
  82. public const int kClientLabelHeightShort = 15;
  83. public static int kTestAccountBoxHeight = 25;
  84. public const int kTestAccountTextWidth = 110;
  85. }
  86. private SerializedProperty unityProjectID;
  87. private SerializedProperty unityClientID;
  88. private SerializedProperty unityClientKey;
  89. private SerializedProperty unityClientRSAPublicKey;
  90. private SerializedProperty appName;
  91. private SerializedProperty appSlug;
  92. private SerializedProperty appItemId;
  93. private SerializedProperty permission;
  94. private bool isOperationRunning = false;
  95. void OnEnable()
  96. {
  97. // For unity client settings.
  98. unityProjectID = serializedObject.FindProperty("UnityProjectID");
  99. unityClientID = serializedObject.FindProperty("UnityClientID");
  100. unityClientKey = serializedObject.FindProperty("UnityClientKey");
  101. unityClientRSAPublicKey = serializedObject.FindProperty("UnityClientRSAPublicKey");
  102. appName = serializedObject.FindProperty("AppName");
  103. appSlug = serializedObject.FindProperty("AppSlug");
  104. appItemId = serializedObject.FindProperty("AppItemId");
  105. permission = serializedObject.FindProperty("Permission");
  106. testAccounts = new List<TestAccount>();
  107. currentAppItem = new AppItem();
  108. EditorApplication.update += CheckUpdate;
  109. if (enableOAuth && !String.IsNullOrEmpty(Application.cloudProjectId))
  110. {
  111. InitializeSecrets();
  112. }
  113. }
  114. public override void OnInspectorGUI()
  115. {
  116. serializedObject.Update();
  117. EditorGUI.BeginDisabledGroup(isOperationRunning);
  118. GUILayout.BeginHorizontal();
  119. // Unity project id.
  120. EditorGUILayout.LabelField(new GUIContent("Unity Project ID"));
  121. GUILayout.FlexibleSpace();
  122. if (GUILayout.Button("Copy to Clipboard", GUILayout.Height(AppStoreStyles.kClientLabelHeight)))
  123. {
  124. TextEditor te = new TextEditor();
  125. te.text = Application.cloudProjectId;
  126. te.SelectAll();
  127. te.Copy();
  128. }
  129. GUILayout.EndHorizontal();
  130. GUILayout.BeginVertical();
  131. GUILayout.Space(2);
  132. GUILayout.EndVertical();
  133. using (new EditorGUILayout.VerticalScope("OL Box",
  134. GUILayout.Height(AppStoreStyles.kUnityProjectIDBoxHeight)))
  135. {
  136. GUILayout.FlexibleSpace();
  137. if (!String.IsNullOrEmpty(this.unityProjectID.stringValue) &&
  138. !String.IsNullOrEmpty(Application.cloudProjectId) &&
  139. !this.unityProjectID.stringValue.Equals(Application.cloudProjectId))
  140. {
  141. this.unityProjectID.stringValue = "";
  142. this.unityClientID.stringValue = "";
  143. this.unityClientKey.stringValue = "";
  144. this.unityClientRSAPublicKey.stringValue = "";
  145. this.appName.stringValue = "";
  146. this.appSlug.stringValue = "";
  147. this.appItemId.stringValue = "";
  148. this.permission.stringValue = "";
  149. clientSecret_in_memory = "";
  150. callbackUrl_in_memory = "";
  151. ownerAuthed = false;
  152. callbackUrl_last = "";
  153. existedClientId = "";
  154. currentAppItem = new AppItem();
  155. testAccounts = new List<TestAccount>();
  156. testAccount = new TestAccount();
  157. AppStoreStyles.kTestAccountBoxHeight = 25;
  158. AssetDatabase.SaveAssets();
  159. if (!EditorUtility.DisplayDialog("Hint",
  160. "Your Project ID has changed.\nYou need to generate a new client first. If you want to link this project to your existed client, "
  161. + "please open UDP portal in browser to update your client with this new project ID" +
  162. "(Warning: Make sure you finish operations in UDP portal before clicking 'Generate Unity Client' button!).",
  163. "I'd like to generate a new client, Go Ahead",
  164. "Open UDP portal in Browser"))
  165. {
  166. Application.OpenURL(BuildConfig.CONNECT_ENDPOINT);
  167. }
  168. else
  169. {
  170. _checkLink = true;
  171. unityClientID.stringValue = null;
  172. }
  173. }
  174. string unityProjectID = Application.cloudProjectId;
  175. if (String.IsNullOrEmpty(unityProjectID))
  176. {
  177. EditorGUILayout.LabelField(new GUIContent(AppStoreStyles.kNoUnityProjectIDErrorMessage));
  178. GUILayout.FlexibleSpace();
  179. return;
  180. }
  181. EditorGUILayout.LabelField(new GUIContent(Application.cloudProjectId));
  182. GUILayout.FlexibleSpace();
  183. }
  184. if (String.IsNullOrEmpty(this.unityClientID.stringValue) &&
  185. !String.IsNullOrEmpty(Application.cloudProjectId) && _checkLink)
  186. {
  187. isOperationRunning = true;
  188. _checkLink = false;
  189. targetStep = "LinkProject";
  190. UnityWebRequest newRequest = AppStoreOnboardApi.GetUnityClientInfo(Application.cloudProjectId);
  191. UnityClientResponseWrapper clientRespWrapper = new UnityClientResponseWrapper();
  192. ReqStruct newReqStruct = new ReqStruct();
  193. newReqStruct.request = newRequest;
  194. newReqStruct.resp = clientRespWrapper;
  195. newReqStruct.targetStep = targetStep;
  196. requestQueue.Enqueue(newReqStruct);
  197. }
  198. if (String.IsNullOrEmpty(this.unityClientID.stringValue) &&
  199. !String.IsNullOrEmpty(Application.cloudProjectId) && _canLink)
  200. {
  201. GUILayout.BeginVertical();
  202. GUILayout.Space(10);
  203. GUILayout.EndVertical();
  204. EditorGUILayout.BeginHorizontal();
  205. EditorGUILayout.LabelField(new GUIContent("Link Project to Existed Client"));
  206. EditorGUILayout.EndHorizontal();
  207. GUILayout.BeginVertical();
  208. GUILayout.Space(2);
  209. GUILayout.EndVertical();
  210. using (new EditorGUILayout.VerticalScope("OL Box",
  211. GUILayout.Height(AppStoreStyles.kUnityProjectLinkBoxHeight)))
  212. {
  213. GUILayout.FlexibleSpace();
  214. GUILayout.BeginHorizontal();
  215. GUILayout.Label("Client ID", GUILayout.Width(AppStoreStyles.kClientLabelWidth));
  216. existed_clientId_in_memory =
  217. EditorGUILayout.TextField(String.IsNullOrEmpty(existed_clientId_in_memory)
  218. ? ""
  219. : existed_clientId_in_memory);
  220. GUILayout.EndHorizontal();
  221. if (GUILayout.Button("Link") && !String.IsNullOrEmpty(existed_clientId_in_memory))
  222. {
  223. isOperationRunning = true;
  224. UnityWebRequest newRequest =
  225. AppStoreOnboardApi.GetUnityClientInfoByClientId(existed_clientId_in_memory);
  226. UnityClientResponse unityClientResponse = new UnityClientResponse();
  227. ReqStruct newReqStruct = new ReqStruct();
  228. newReqStruct.request = newRequest;
  229. newReqStruct.resp = unityClientResponse;
  230. newReqStruct.targetStep = "LinkProject";
  231. requestQueue.Enqueue(newReqStruct);
  232. }
  233. }
  234. }
  235. GUILayout.BeginVertical();
  236. GUILayout.Space(10);
  237. GUILayout.EndVertical();
  238. //control by UnityOAuth
  239. EditorGUI.BeginDisabledGroup(!enableOAuth);
  240. // Unity client settings.
  241. EditorGUILayout.BeginHorizontal();
  242. EditorGUILayout.LabelField(new GUIContent("UDP Client Settings"));
  243. EditorGUILayout.EndHorizontal();
  244. GUILayout.BeginVertical();
  245. GUILayout.Space(2);
  246. GUILayout.EndVertical();
  247. using (new EditorGUILayout.VerticalScope("OL Box", GUILayout.Height(AppStoreStyles.kUnityClientBoxHeight)))
  248. {
  249. GUILayout.FlexibleSpace();
  250. EditorGUILayout.BeginHorizontal();
  251. if (String.IsNullOrEmpty(unityClientID.stringValue))
  252. {
  253. EditorGUILayout.LabelField("Client ID", GUILayout.Width(AppStoreStyles.kClientLabelWidth));
  254. EditorGUILayout.LabelField("None");
  255. }
  256. else
  257. {
  258. EditorGUILayout.LabelField("Client ID", GUILayout.Width(AppStoreStyles.kClientLabelWidth));
  259. EditorGUILayout.LabelField(strPrefix(unityClientID.stringValue),
  260. GUILayout.Width(AppStoreStyles.kClientLabelWidthShort));
  261. GUILayout.FlexibleSpace();
  262. if (GUILayout.Button("Copy to Clipboard",
  263. GUILayout.Width(AppStoreStyles.kUnityClientIDButtonWidth)))
  264. {
  265. TextEditor te = new TextEditor();
  266. te.text = unityClientID.stringValue;
  267. te.SelectAll();
  268. te.Copy();
  269. }
  270. }
  271. EditorGUILayout.EndHorizontal();
  272. GUILayout.FlexibleSpace();
  273. EditorGUILayout.BeginHorizontal();
  274. if (String.IsNullOrEmpty(unityClientKey.stringValue))
  275. {
  276. EditorGUILayout.LabelField("Client Key", GUILayout.Width(AppStoreStyles.kClientLabelWidth));
  277. EditorGUILayout.LabelField("None");
  278. }
  279. else
  280. {
  281. EditorGUILayout.LabelField("Client Key", GUILayout.Width(AppStoreStyles.kClientLabelWidth));
  282. EditorGUILayout.LabelField(strPrefix(unityClientKey.stringValue),
  283. GUILayout.Width(AppStoreStyles.kClientLabelWidthShort));
  284. GUILayout.FlexibleSpace();
  285. if (GUILayout.Button("Copy to Clipboard",
  286. GUILayout.Width(AppStoreStyles.kUnityClientIDButtonWidth)))
  287. {
  288. TextEditor te = new TextEditor();
  289. te.text = unityClientKey.stringValue;
  290. te.SelectAll();
  291. te.Copy();
  292. }
  293. }
  294. EditorGUILayout.EndHorizontal();
  295. GUILayout.FlexibleSpace();
  296. EditorGUILayout.BeginHorizontal();
  297. if (String.IsNullOrEmpty(unityClientRSAPublicKey.stringValue))
  298. {
  299. EditorGUILayout.LabelField("Client RSA Public Key",
  300. GUILayout.Width(AppStoreStyles.kClientLabelWidth));
  301. EditorGUILayout.LabelField("None");
  302. }
  303. else
  304. {
  305. EditorGUILayout.LabelField("Client RSA Public Key",
  306. GUILayout.Width(AppStoreStyles.kClientLabelWidth));
  307. EditorGUILayout.LabelField(strPrefix(unityClientRSAPublicKey.stringValue),
  308. GUILayout.Width(AppStoreStyles.kClientLabelWidthShort));
  309. GUILayout.FlexibleSpace();
  310. if (GUILayout.Button("Copy to Clipboard",
  311. GUILayout.Width(AppStoreStyles.kUnityClientIDButtonWidth)))
  312. {
  313. TextEditor te = new TextEditor();
  314. te.text = unityClientRSAPublicKey.stringValue;
  315. te.SelectAll();
  316. te.Copy();
  317. }
  318. }
  319. EditorGUILayout.EndHorizontal();
  320. GUILayout.FlexibleSpace();
  321. EditorGUILayout.BeginHorizontal();
  322. if (String.IsNullOrEmpty(clientSecret_in_memory))
  323. {
  324. EditorGUILayout.LabelField("Client Secret", GUILayout.Width(AppStoreStyles.kClientLabelWidth));
  325. EditorGUILayout.LabelField("None");
  326. }
  327. else
  328. {
  329. EditorGUILayout.LabelField("Client Secret", GUILayout.Width(AppStoreStyles.kClientLabelWidth));
  330. if (_rolePermission.manager || _rolePermission.owner)
  331. {
  332. EditorGUILayout.LabelField(strPrefix(clientSecret_in_memory),
  333. GUILayout.Width(AppStoreStyles.kClientLabelWidthShort));
  334. }
  335. else
  336. {
  337. EditorGUILayout.LabelField("********",
  338. GUILayout.Width(AppStoreStyles.kClientLabelWidthShort));
  339. }
  340. GUILayout.FlexibleSpace();
  341. EditorGUI.BeginDisabledGroup(!(_rolePermission.manager || _rolePermission.owner));
  342. if (GUILayout.Button("Copy to Clipboard",
  343. GUILayout.Width(AppStoreStyles.kUnityClientIDButtonWidth)))
  344. {
  345. TextEditor te = new TextEditor();
  346. te.text = clientSecret_in_memory;
  347. te.SelectAll();
  348. te.Copy();
  349. }
  350. EditorGUI.EndDisabledGroup();
  351. }
  352. EditorGUILayout.EndHorizontal();
  353. GUILayout.FlexibleSpace();
  354. GUILayout.BeginHorizontal();
  355. GUILayout.Label("Callback URL", GUILayout.Width(AppStoreStyles.kClientLabelWidth));
  356. callbackUrl_in_memory =
  357. EditorGUILayout.TextField(String.IsNullOrEmpty(callbackUrl_in_memory) ? "" : callbackUrl_in_memory);
  358. GUILayout.EndHorizontal();
  359. GUILayout.FlexibleSpace();
  360. }
  361. GUILayout.BeginVertical();
  362. GUILayout.Space(2);
  363. GUILayout.EndVertical();
  364. bool clientNotExists = String.IsNullOrEmpty(unityClientID.stringValue);
  365. string buttonLabelString = "Generate Unity Client";
  366. bool isButtonActive = false;
  367. string target = STEP_GET_CLIENT;
  368. if (!clientNotExists)
  369. {
  370. if (String.IsNullOrEmpty(clientSecret_in_memory) || !AppStoreOnboardApi.loaded)
  371. {
  372. buttonLabelString = "Load Unity Client";
  373. AppStoreStyles.kTestAccountBoxHeight = 25;
  374. testAccounts = new List<TestAccount>();
  375. testAccount = new TestAccount();
  376. }
  377. else
  378. {
  379. buttonLabelString = "Refresh Unity Client";
  380. // target = STEP_UPDATE_CLIENT_SECRET;
  381. isButtonActive = false;
  382. }
  383. }
  384. EditorGUILayout.BeginHorizontal();
  385. EditorGUI.BeginDisabledGroup(isButtonActive);
  386. if (GUILayout.Button(buttonLabelString, GUILayout.Width(AppStoreStyles.kUnityClientIDButtonWidth)))
  387. {
  388. isOperationRunning = true;
  389. if (target == STEP_UPDATE_CLIENT_SECRET)
  390. {
  391. clientSecret_in_memory = null;
  392. }
  393. targetStep = target;
  394. callApiAsync();
  395. serializedObject.ApplyModifiedProperties();
  396. this.Repaint();
  397. AssetDatabase.SaveAssets();
  398. }
  399. EditorGUI.EndDisabledGroup();
  400. GUILayout.FlexibleSpace();
  401. EditorGUI.BeginDisabledGroup(!(_rolePermission.manager || _rolePermission.owner));
  402. if (GUILayout.Button("Update Client Settings", GUILayout.Width(AppStoreStyles.kUnityClientIDButtonWidth)))
  403. {
  404. isOperationRunning = true;
  405. if (clientNotExists)
  406. {
  407. EditorUtility.DisplayDialog("Error",
  408. "Please get/generate Unity Client first.",
  409. "OK");
  410. isOperationRunning = false;
  411. }
  412. else
  413. {
  414. if (callbackUrl_last != callbackUrl_in_memory)
  415. {
  416. if (String.IsNullOrEmpty(callbackUrl_in_memory))
  417. {
  418. if (EditorUtility.DisplayDialog("Warning",
  419. "Are you sure to clear Callback URL?",
  420. "Clear", "Do Not Clear"))
  421. {
  422. targetStep = STEP_UPDATE_CLIENT;
  423. callApiAsync();
  424. }
  425. }
  426. else if (CheckURL(callbackUrl_in_memory))
  427. {
  428. targetStep = STEP_UPDATE_CLIENT;
  429. callApiAsync();
  430. }
  431. else
  432. {
  433. EditorUtility.DisplayDialog("Error",
  434. "Callback URL is invalid. (http/https is required)",
  435. "OK");
  436. isOperationRunning = false;
  437. }
  438. }
  439. else
  440. {
  441. isOperationRunning = false;
  442. }
  443. }
  444. serializedObject.ApplyModifiedProperties();
  445. this.Repaint();
  446. AssetDatabase.SaveAssets();
  447. }
  448. EditorGUI.EndDisabledGroup();
  449. EditorGUILayout.EndHorizontal();
  450. GUILayout.BeginVertical();
  451. GUILayout.Space(10);
  452. GUILayout.EndVertical();
  453. // Unity client settings.
  454. EditorGUILayout.BeginHorizontal();
  455. EditorGUILayout.BeginVertical();
  456. EditorGUILayout.LabelField(new GUIContent("Game Settings"));
  457. EditorGUILayout.EndVertical();
  458. EditorGUI.BeginDisabledGroup(!(_rolePermission.manager || _rolePermission.owner));
  459. if (GUILayout.Button("Update Game", GUILayout.Width(AppStoreStyles.kUnityClientIDButtonWidth)))
  460. {
  461. if (currentAppItem.name == null || currentAppItem.slug == null
  462. || currentAppItem.name == "" || currentAppItem.slug == "")
  463. {
  464. EditorUtility.DisplayDialog("Error",
  465. "Please fill in Game Title and Game Id fields.",
  466. "OK");
  467. }
  468. else
  469. {
  470. isOperationRunning = true;
  471. currentAppItem.status = "STAGE";
  472. UnityWebRequest newRequest = AppStoreOnboardApi.UpdateAppItem(currentAppItem);
  473. AppItemResponse appItemResponse = new AppItemResponse();
  474. ReqStruct newReqStruct = new ReqStruct();
  475. newReqStruct.request = newRequest;
  476. newReqStruct.resp = appItemResponse;
  477. requestQueue.Enqueue(newReqStruct);
  478. }
  479. }
  480. EditorGUILayout.EndHorizontal();
  481. GUILayout.BeginVertical();
  482. GUILayout.Space(2);
  483. GUILayout.EndVertical();
  484. GUILayout.BeginVertical();
  485. using (new EditorGUILayout.VerticalScope("OL Box", GUILayout.Height(AppStoreStyles.kUnityAppItemBoxHeight)))
  486. {
  487. GUILayout.FlexibleSpace();
  488. EditorGUI.BeginDisabledGroup(true);
  489. EditorGUILayout.BeginHorizontal();
  490. var slugRect = EditorGUILayout.GetControlRect(true);
  491. currentAppItem.slug = EditorGUI.TextField(slugRect, "Game Id:", currentAppItem.slug);
  492. EditorGUILayout.EndHorizontal();
  493. EditorGUI.EndDisabledGroup();
  494. var nameRect = EditorGUILayout.GetControlRect(true);
  495. currentAppItem.name = EditorGUI.TextField(nameRect, "Game Title:", currentAppItem.name);
  496. EditorGUI.BeginDisabledGroup(true);
  497. var clientIdRect = EditorGUILayout.GetControlRect(true);
  498. EditorGUI.TextField(clientIdRect, "Game Revision:", currentAppItem.revision);
  499. EditorGUI.EndDisabledGroup();
  500. }
  501. EditorGUI.EndDisabledGroup();
  502. GUILayout.EndVertical();
  503. GUILayout.BeginVertical();
  504. GUILayout.Space(10);
  505. GUILayout.EndVertical();
  506. EditorGUILayout.LabelField(new GUIContent("Test Account Settings"));
  507. GUILayout.BeginVertical();
  508. GUILayout.Space(2);
  509. GUILayout.EndVertical();
  510. using (new EditorGUILayout.VerticalScope("OL Box", GUILayout.Height(AppStoreStyles.kTestAccountBoxHeight)))
  511. {
  512. for (int i = 0; i < testAccounts.Count; i++)
  513. {
  514. GUILayout.FlexibleSpace();
  515. GUILayout.BeginHorizontal();
  516. EditorGUI.BeginDisabledGroup(true);
  517. EditorGUILayout.TextField(testAccounts[i].email,
  518. GUILayout.Width(AppStoreStyles.kTestAccountTextWidth));
  519. EditorGUI.EndDisabledGroup();
  520. GUILayout.Space(10);
  521. EditorGUI.BeginDisabledGroup(!testAccounts[i].isUpdate);
  522. if (testAccounts[i].isUpdate)
  523. {
  524. testAccounts[i].password = EditorGUILayout.TextField(testAccounts[i].password,
  525. GUILayout.Width(AppStoreStyles.kTestAccountTextWidth));
  526. }
  527. else
  528. {
  529. EditorGUILayout.TextField(testAccounts[i].password,
  530. GUILayout.Width(AppStoreStyles.kTestAccountTextWidth));
  531. }
  532. EditorGUI.EndDisabledGroup();
  533. GUILayout.FlexibleSpace();
  534. EditorGUI.BeginDisabledGroup(!(_rolePermission.manager || _rolePermission.owner));
  535. string buttonString = testAccounts[i].isUpdate ? "Save" : "Update";
  536. if (GUILayout.Button(buttonString, GUILayout.Width(EditorGUIUtility.singleLineHeight * 3),
  537. GUILayout.Height(EditorGUIUtility.singleLineHeight)))
  538. {
  539. if (testAccounts[i].isUpdate)
  540. {
  541. if (testAccounts[i].password.Length < 6)
  542. {
  543. EditorUtility.DisplayDialog("Error", "The min length of password is 6", "OK");
  544. }
  545. else
  546. {
  547. testAccounts[i].isUpdate = !testAccounts[i].isUpdate;
  548. PlayerChangePasswordRequest player = new PlayerChangePasswordRequest();
  549. player.password = testAccounts[i].password;
  550. player.playerId = testAccounts[i].playerId;
  551. UnityWebRequest request = AppStoreOnboardApi.UpdateTestAccount(player);
  552. PlayerChangePasswordResponse playerDeleteResponse = new PlayerChangePasswordResponse();
  553. ReqStruct reqStruct = new ReqStruct();
  554. reqStruct.request = request;
  555. reqStruct.resp = playerDeleteResponse;
  556. reqStruct.targetStep = null;
  557. requestQueue.Enqueue(reqStruct);
  558. isOperationRunning = true;
  559. }
  560. }
  561. else
  562. {
  563. testAccounts[i].password = "";
  564. testAccounts[i].isUpdate = !testAccounts[i].isUpdate;
  565. }
  566. }
  567. if (testAccounts[i].isUpdate)
  568. {
  569. if (GUILayout.Button("Cancel", GUILayout.Width(EditorGUIUtility.singleLineHeight * 3),
  570. GUILayout.Height(EditorGUIUtility.singleLineHeight)))
  571. {
  572. testAccounts[i].password = "******";
  573. testAccounts[i].isUpdate = !testAccounts[i].isUpdate;
  574. }
  575. }
  576. if (GUILayout.Button("-", GUILayout.Width(EditorGUIUtility.singleLineHeight),
  577. GUILayout.Height(EditorGUIUtility.singleLineHeight))
  578. && EditorUtility.DisplayDialog("Delete Test Account?",
  579. "Are you sure you want to delete this test account?",
  580. "Delete",
  581. "Do Not Delete"))
  582. {
  583. UnityWebRequest request = AppStoreOnboardApi.DeleteTestAccount(testAccounts[i].playerId);
  584. PlayerDeleteResponse playerDeleteResponse = new PlayerDeleteResponse();
  585. ReqStruct reqStruct = new ReqStruct();
  586. reqStruct.request = request;
  587. reqStruct.resp = playerDeleteResponse;
  588. reqStruct.targetStep = null;
  589. requestQueue.Enqueue(reqStruct);
  590. isOperationRunning = true;
  591. }
  592. EditorGUI.EndDisabledGroup();
  593. GUILayout.EndHorizontal();
  594. }
  595. GUILayout.FlexibleSpace();
  596. GUILayout.BeginHorizontal();
  597. EditorGUI.BeginDisabledGroup(!(_rolePermission.manager || _rolePermission.owner));
  598. GUILayout.MinWidth(AppStoreStyles.kTestAccountTextWidth * 2 + EditorGUIUtility.singleLineHeight * 2);
  599. testAccount.email = EditorGUILayout.TextField(
  600. String.IsNullOrEmpty(testAccount.email) ? "Email" : testAccount.email,
  601. GUILayout.Width(AppStoreStyles.kTestAccountTextWidth));
  602. GUILayout.Space(10);
  603. testAccount.password = EditorGUILayout.TextField(
  604. String.IsNullOrEmpty(testAccount.password) ? "Password" : testAccount.password,
  605. GUILayout.Width(AppStoreStyles.kTestAccountTextWidth));
  606. GUILayout.FlexibleSpace();
  607. if (GUILayout.Button("+", GUILayout.Width(EditorGUIUtility.singleLineHeight * 2),
  608. GUILayout.Height(EditorGUIUtility.singleLineHeight)))
  609. {
  610. bool existed = false;
  611. foreach (var TA in testAccounts)
  612. {
  613. if (TA.email.Equals(testAccount.email))
  614. {
  615. existed = true;
  616. break;
  617. }
  618. }
  619. if (testAccount.email == "Email")
  620. {
  621. EditorUtility.DisplayDialog("Error", "You must fill in Email of the Test Account", "OK");
  622. }
  623. else if (testAccount.password == "Password")
  624. {
  625. EditorUtility.DisplayDialog("Error", "You must fill in Password of the Test Account", "OK");
  626. }
  627. else if (testAccount.password.Length < 6)
  628. {
  629. EditorUtility.DisplayDialog("Error", "The min length of password is 6", "OK");
  630. }
  631. else if (!CheckEmailAddr(testAccount.email))
  632. {
  633. EditorUtility.DisplayDialog("Error", "Email is not valid", "OK");
  634. }
  635. else if (existed)
  636. {
  637. EditorUtility.DisplayDialog("Error", "Email already existed", "OK");
  638. }
  639. else
  640. {
  641. if (clientNotExists)
  642. {
  643. EditorUtility.DisplayDialog("Error", "Please get the Unity Client first", "OK");
  644. }
  645. else
  646. {
  647. Player player = new Player();
  648. player.email = testAccount.email;
  649. player.password = testAccount.password;
  650. UnityWebRequest request =
  651. AppStoreOnboardApi.SaveTestAccount(player, unityClientID.stringValue);
  652. PlayerResponse playerResponse = new PlayerResponse();
  653. ReqStruct reqStruct = new ReqStruct();
  654. reqStruct.request = request;
  655. reqStruct.resp = playerResponse;
  656. reqStruct.targetStep = null;
  657. requestQueue.Enqueue(reqStruct);
  658. isOperationRunning = true;
  659. }
  660. }
  661. }
  662. EditorGUI.EndDisabledGroup();
  663. GUILayout.EndHorizontal();
  664. GUILayout.FlexibleSpace();
  665. }
  666. GUILayout.BeginVertical();
  667. GUILayout.Space(10);
  668. GUILayout.EndVertical();
  669. serializedObject.ApplyModifiedProperties();
  670. this.Repaint();
  671. EditorGUI.EndDisabledGroup();
  672. //control by UnityOAuth
  673. EditorGUI.EndDisabledGroup();
  674. if (GUILayout.Button("Edit Game Information on Portal"))
  675. {
  676. Application.OpenURL(BuildConfig.CONSOLE_URL);
  677. }
  678. }
  679. string strPrefix(string str)
  680. {
  681. var preIndex = str.Length < 5 ? str.Length : 5;
  682. return str.Substring(0, preIndex) + "...";
  683. }
  684. public void Perform<T>(T response)
  685. {
  686. var authCodePropertyInfo = response.GetType().GetProperty("AuthCode");
  687. var exceptionPropertyInfo = response.GetType().GetProperty("Exception");
  688. string authCode = (string) authCodePropertyInfo.GetValue(response, null);
  689. Exception exception = (Exception) exceptionPropertyInfo.GetValue(response, null);
  690. if (authCode != null)
  691. {
  692. UnityWebRequest request = AppStoreOnboardApi.GetAccessToken(authCode);
  693. TokenInfo tokenInfoResp = new TokenInfo();
  694. ReqStruct reqStruct = new ReqStruct();
  695. reqStruct.request = request;
  696. reqStruct.resp = tokenInfoResp;
  697. reqStruct.targetStep = targetStep;
  698. requestQueue.Enqueue(reqStruct);
  699. }
  700. else
  701. {
  702. EditorUtility.DisplayDialog("Error", "Failed: " + exception.ToString(), "OK");
  703. isOperationRunning = false;
  704. }
  705. }
  706. void callApiAsync()
  707. {
  708. if (AppStoreOnboardApi.tokenInfo.access_token == null)
  709. {
  710. Type unityOAuthType = Utils.FindTypeByName("UnityEditor.Connect.UnityOAuth");
  711. Type authCodeResponseType = unityOAuthType.GetNestedType("AuthCodeResponse", BindingFlags.Public);
  712. var performMethodInfo =
  713. typeof(AppStoreSettingsEditor).GetMethod("Perform").MakeGenericMethod(authCodeResponseType);
  714. var actionT =
  715. typeof(Action<>).MakeGenericType(authCodeResponseType); // Action<UnityOAuth.AuthCodeResponse>
  716. var getAuthorizationCodeAsyncMethodInfo = unityOAuthType.GetMethod("GetAuthorizationCodeAsync");
  717. var performDelegate = Delegate.CreateDelegate(actionT, this, performMethodInfo);
  718. try
  719. {
  720. getAuthorizationCodeAsyncMethodInfo.Invoke(null,
  721. new object[] {AppStoreOnboardApi.oauthClientId, performDelegate});
  722. }
  723. catch (TargetInvocationException ex)
  724. {
  725. if (ex.InnerException is InvalidOperationException)
  726. {
  727. EditorUtility.DisplayDialog("Error", "You must login with Unity ID first.", "OK");
  728. isOperationRunning = false;
  729. }
  730. }
  731. }
  732. else
  733. {
  734. UnityWebRequest request = AppStoreOnboardApi.GetUserId();
  735. UserIdResponse userIdResp = new UserIdResponse();
  736. ReqStruct reqStruct = new ReqStruct();
  737. reqStruct.request = request;
  738. reqStruct.resp = userIdResp;
  739. reqStruct.targetStep = targetStep;
  740. requestQueue.Enqueue(reqStruct);
  741. }
  742. }
  743. void OnDestroy()
  744. {
  745. EditorApplication.update -= CheckUpdate;
  746. }
  747. void CheckUpdate()
  748. {
  749. CheckRequestUpdate();
  750. }
  751. bool CheckEmailAddr(String email)
  752. {
  753. string pattern =
  754. @"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$";
  755. return new Regex(pattern, RegexOptions.IgnoreCase).IsMatch(email);
  756. }
  757. bool CheckURL(String URL)
  758. {
  759. // string pattern = @"^(http|https):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$";
  760. string pattern =
  761. @"^(https?://[\w\-]+(\.[\w\-]+)+(:\d+)?((/[\w\-]*)?)*(\?[\w\-]+=[\w\-]+((&[\w\-]+=[\w\-]+)?)*)?)?$";
  762. return new Regex(pattern, RegexOptions.IgnoreCase).IsMatch(URL);
  763. }
  764. void InitializeSecrets()
  765. {
  766. // No need to initialize for invalid client settings.
  767. if (String.IsNullOrEmpty(unityClientID.stringValue))
  768. {
  769. return;
  770. }
  771. if (!String.IsNullOrEmpty(clientSecret_in_memory))
  772. {
  773. return;
  774. }
  775. // Start initialization.
  776. isOperationRunning = true;
  777. _checkLink = false;
  778. UnityWebRequest newRequest = AppStoreOnboardApi.GetUnityClientInfo(Application.cloudProjectId);
  779. UnityClientResponseWrapper clientRespWrapper = new UnityClientResponseWrapper();
  780. ReqStruct newReqStruct = new ReqStruct();
  781. newReqStruct.request = newRequest;
  782. newReqStruct.resp = clientRespWrapper;
  783. newReqStruct.targetStep = "CheckUpdate";
  784. requestQueue.Enqueue(newReqStruct);
  785. }
  786. private string SHA256(String data)
  787. {
  788. var crypt = new System.Security.Cryptography.SHA256Managed();
  789. var hash = new System.Text.StringBuilder();
  790. byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(data));
  791. foreach (byte theByte in crypto)
  792. {
  793. hash.Append(theByte.ToString("x2"));
  794. }
  795. return hash.ToString();
  796. }
  797. private void saveGameSettingsProps(String clientId)
  798. {
  799. if (!Directory.Exists(AppStoreSettings.appStoreSettingsPropFolder))
  800. Directory.CreateDirectory(AppStoreSettings.appStoreSettingsPropFolder);
  801. StreamWriter writter = new StreamWriter(AppStoreSettings.appStoreSettingsPropPath, false);
  802. String warningMessage = "*** DO NOT DELETE OR MODIFY THIS FILE !! ***";
  803. writter.WriteLine(warningMessage);
  804. writter.WriteLine(clientId);
  805. writter.WriteLine(warningMessage);
  806. writter.Close();
  807. }
  808. void CheckRequestUpdate()
  809. {
  810. if (requestQueue.Count <= 0)
  811. {
  812. return;
  813. }
  814. ReqStruct reqStruct = requestQueue.Dequeue();
  815. UnityWebRequest request = reqStruct.request;
  816. GeneralResponse resp = reqStruct.resp;
  817. if (request != null && request.isDone)
  818. {
  819. if (request.error != null || request.responseCode / 100 != 2)
  820. {
  821. if (request.downloadHandler.text.Contains(AppStoreOnboardApi.invalidAccessTokenInfo)
  822. || request.downloadHandler.text.Contains(AppStoreOnboardApi.forbiddenInfo)
  823. || request.downloadHandler.text.Contains(AppStoreOnboardApi.expiredAccessTokenInfo))
  824. {
  825. UnityWebRequest newRequest = AppStoreOnboardApi.RefreshToken();
  826. TokenInfo tokenInfoResp = new TokenInfo();
  827. ReqStruct newReqStruct = new ReqStruct();
  828. newReqStruct.request = newRequest;
  829. newReqStruct.resp = tokenInfoResp;
  830. newReqStruct.targetStep = reqStruct.targetStep;
  831. requestQueue.Enqueue(newReqStruct);
  832. }
  833. else if (request.downloadHandler.text.Contains(AppStoreOnboardApi.invalidRefreshTokenInfo)
  834. || request.downloadHandler.text.Contains(AppStoreOnboardApi.expiredRefreshTokenInfo))
  835. {
  836. targetStep = STEP_GET_CLIENT;
  837. AppStoreOnboardApi.tokenInfo.access_token = null;
  838. AppStoreOnboardApi.tokenInfo.refresh_token = null;
  839. callApiAsync();
  840. }
  841. else
  842. {
  843. isOperationRunning = false;
  844. ErrorResponse response = JsonUtility.FromJson<ErrorResponse>(request.downloadHandler.text);
  845. #region Analytics Fails
  846. if (resp.GetType() == typeof(EventRequestResponse))
  847. {
  848. // Debug.Log("[Debug] Event Request Failed: " + reqStruct.eventName);
  849. return; // Do not show error dialog
  850. }
  851. if (resp.GetType() == typeof(UnityClientResponse))
  852. {
  853. string eventName = null;
  854. switch (request.method)
  855. {
  856. case UnityWebRequest.kHttpVerbPOST:
  857. eventName = EditorAnalyticsApi.k_ClientCreateEventName;
  858. break;
  859. case UnityWebRequest.kHttpVerbPUT:
  860. eventName = EditorAnalyticsApi.k_ClientUpdateEventName;
  861. break;
  862. default:
  863. eventName = null;
  864. break;
  865. }
  866. if (eventName != null)
  867. {
  868. UnityWebRequest analyticsRequest =
  869. EditorAnalyticsApi.ClientEvent(eventName, null, response.message);
  870. ReqStruct analyticsReqStruct = new ReqStruct
  871. {
  872. request = analyticsRequest,
  873. resp = new EventRequestResponse(),
  874. eventName = eventName,
  875. };
  876. requestQueue.Enqueue(analyticsReqStruct);
  877. }
  878. }
  879. if (resp.GetType() == typeof(AppItemResponse))
  880. {
  881. string eventName;
  882. switch (request.method)
  883. {
  884. case UnityWebRequest.kHttpVerbPOST:
  885. eventName = EditorAnalyticsApi.k_AppCreateEventName;
  886. break;
  887. case UnityWebRequest.kHttpVerbPUT:
  888. eventName = EditorAnalyticsApi.k_AppUpdateEventName;
  889. break;
  890. default:
  891. eventName = null;
  892. break;
  893. }
  894. if (eventName != null)
  895. {
  896. UnityWebRequest analyticsRequest =
  897. EditorAnalyticsApi.AppEvent(eventName, unityClientID.stringValue, null, response.message);
  898. ReqStruct analyticsRequestStruct = new ReqStruct
  899. {
  900. request = analyticsRequest,
  901. resp = new EventRequestResponse(),
  902. eventName = eventName,
  903. };
  904. requestQueue.Enqueue(analyticsRequestStruct);
  905. }
  906. }
  907. #endregion
  908. if (response != null && response.message != null && response.details != null &&
  909. response.details.Length != 0)
  910. {
  911. EditorUtility.DisplayDialog("Error",
  912. response.details[0].field + ": " + response.message,
  913. "OK");
  914. this.Repaint();
  915. }
  916. else if (response != null && response.message != null)
  917. {
  918. EditorUtility.DisplayDialog("Error",
  919. response.message,
  920. "OK");
  921. this.Repaint();
  922. }
  923. else
  924. {
  925. EditorUtility.DisplayDialog("Error",
  926. "Unknown error",
  927. "OK");
  928. this.Repaint();
  929. }
  930. }
  931. // Error Recovery
  932. if (resp.GetType() == typeof(PlayerResponseWrapper))
  933. {
  934. listPlayers();
  935. }
  936. }
  937. else
  938. {
  939. if (resp.GetType() == typeof(TokenInfo))
  940. {
  941. resp = JsonUtility.FromJson<TokenInfo>(request.downloadHandler.text);
  942. AppStoreOnboardApi.tokenInfo.access_token = ((TokenInfo) resp).access_token;
  943. AppStoreOnboardApi.tokenInfo.refresh_token = ((TokenInfo) resp).refresh_token;
  944. UnityWebRequest newRequest = AppStoreOnboardApi.GetUserId();
  945. UserIdResponse userIdResp = new UserIdResponse();
  946. ReqStruct newReqStruct = new ReqStruct();
  947. newReqStruct.request = newRequest;
  948. newReqStruct.resp = userIdResp;
  949. newReqStruct.targetStep = reqStruct.targetStep;
  950. requestQueue.Enqueue(newReqStruct);
  951. }
  952. else if (resp.GetType() == typeof(UserIdResponse))
  953. {
  954. resp = JsonUtility.FromJson<UserIdResponse>(request.downloadHandler.text);
  955. AppStoreOnboardApi.userId = ((UserIdResponse) resp).sub;
  956. UnityWebRequest newRequest = AppStoreOnboardApi.GetOrgId(Application.cloudProjectId);
  957. OrgIdResponse orgIdResp = new OrgIdResponse();
  958. ReqStruct newReqStruct = new ReqStruct();
  959. newReqStruct.request = newRequest;
  960. newReqStruct.resp = orgIdResp;
  961. newReqStruct.targetStep = reqStruct.targetStep;
  962. requestQueue.Enqueue(newReqStruct);
  963. }
  964. else if (resp.GetType() == typeof(OrgIdResponse))
  965. {
  966. resp = JsonUtility.FromJson<OrgIdResponse>(request.downloadHandler.text);
  967. AppStoreOnboardApi.orgId = ((OrgIdResponse) resp).org_foreign_key;
  968. UnityWebRequest newRequest = AppStoreOnboardApi.GetOrgRoles();
  969. OrgRoleResponse orgRoleResp = new OrgRoleResponse();
  970. ReqStruct newReqStruct = new ReqStruct();
  971. newReqStruct.request = newRequest;
  972. newReqStruct.resp = orgRoleResp;
  973. newReqStruct.targetStep = reqStruct.targetStep;
  974. requestQueue.Enqueue(newReqStruct);
  975. }
  976. else if (resp.GetType() == typeof(OrgRoleResponse))
  977. {
  978. resp = JsonUtility.FromJson<OrgRoleResponse>(request.downloadHandler.text);
  979. List<string> roles = ((OrgRoleResponse) resp).roles;
  980. if (roles.Contains("owner"))
  981. {
  982. ownerAuthed = true;
  983. permission.stringValue = "owner";
  984. _rolePermission.owner = true;
  985. if (reqStruct.targetStep == STEP_GET_CLIENT)
  986. {
  987. UnityWebRequest newRequest =
  988. AppStoreOnboardApi.GetUnityClientInfo(Application.cloudProjectId);
  989. UnityClientResponseWrapper clientRespWrapper = new UnityClientResponseWrapper();
  990. ReqStruct newReqStruct = new ReqStruct();
  991. newReqStruct.request = newRequest;
  992. newReqStruct.resp = clientRespWrapper;
  993. newReqStruct.targetStep = reqStruct.targetStep;
  994. requestQueue.Enqueue(newReqStruct);
  995. }
  996. else if (reqStruct.targetStep == STEP_UPDATE_CLIENT)
  997. {
  998. UnityClientInfo unityClientInfo = new UnityClientInfo();
  999. unityClientInfo.ClientId = unityClientID.stringValue;
  1000. string callbackUrl = callbackUrl_in_memory;
  1001. UnityWebRequest newRequest =
  1002. AppStoreOnboardApi.UpdateUnityClient(Application.cloudProjectId, unityClientInfo,
  1003. callbackUrl);
  1004. UnityClientResponse clientResp = new UnityClientResponse();
  1005. ReqStruct newReqStruct = new ReqStruct();
  1006. newReqStruct.request = newRequest;
  1007. newReqStruct.resp = clientResp;
  1008. newReqStruct.targetStep = reqStruct.targetStep;
  1009. requestQueue.Enqueue(newReqStruct);
  1010. }
  1011. else if (reqStruct.targetStep == STEP_UPDATE_CLIENT_SECRET)
  1012. {
  1013. string clientId = unityClientID.stringValue;
  1014. UnityWebRequest newRequest = AppStoreOnboardApi.UpdateUnityClientSecret(clientId);
  1015. UnityClientResponse clientResp = new UnityClientResponse();
  1016. ReqStruct newReqStruct = new ReqStruct();
  1017. newReqStruct.request = newRequest;
  1018. newReqStruct.resp = clientResp;
  1019. newReqStruct.targetStep = reqStruct.targetStep;
  1020. requestQueue.Enqueue(newReqStruct);
  1021. }
  1022. else if (reqStruct.targetStep == "LinkProject")
  1023. {
  1024. UnityWebRequest newRequest =
  1025. AppStoreOnboardApi.GetUnityClientInfoByClientId(existedClientId);
  1026. UnityClientResponse unityClientResponse = new UnityClientResponse();
  1027. ReqStruct newReqStruct = new ReqStruct();
  1028. newReqStruct.request = newRequest;
  1029. newReqStruct.resp = unityClientResponse;
  1030. newReqStruct.targetStep = reqStruct.targetStep;
  1031. requestQueue.Enqueue(newReqStruct);
  1032. }
  1033. }
  1034. else if (roles.Contains("user") || roles.Contains("manager"))
  1035. {
  1036. ownerAuthed = false;
  1037. if (roles.Contains("manager"))
  1038. {
  1039. permission.stringValue = "mananger";
  1040. _rolePermission.manager = true;
  1041. }
  1042. else if (roles.Contains("user"))
  1043. {
  1044. permission.stringValue = "user";
  1045. _rolePermission.user = true;
  1046. }
  1047. if (reqStruct.targetStep == STEP_GET_CLIENT)
  1048. {
  1049. UnityWebRequest newRequest =
  1050. AppStoreOnboardApi.GetUnityClientInfo(Application.cloudProjectId);
  1051. UnityClientResponseWrapper clientRespWrapper = new UnityClientResponseWrapper();
  1052. ReqStruct newReqStruct = new ReqStruct();
  1053. newReqStruct.request = newRequest;
  1054. newReqStruct.resp = clientRespWrapper;
  1055. newReqStruct.targetStep = reqStruct.targetStep;
  1056. requestQueue.Enqueue(newReqStruct);
  1057. }
  1058. else
  1059. {
  1060. EditorUtility.DisplayDialog("Error",
  1061. "Permission denied.",
  1062. "OK");
  1063. isOperationRunning = false;
  1064. }
  1065. }
  1066. else
  1067. {
  1068. EditorUtility.DisplayDialog("Error",
  1069. "Permission denied.",
  1070. "OK");
  1071. permission.stringValue = "none";
  1072. isOperationRunning = false;
  1073. }
  1074. serializedObject.ApplyModifiedProperties();
  1075. AssetDatabase.SaveAssets();
  1076. }
  1077. else if (resp.GetType() == typeof(UnityClientResponseWrapper))
  1078. {
  1079. string raw = "{ \"array\": " + request.downloadHandler.text + "}";
  1080. resp = JsonUtility.FromJson<UnityClientResponseWrapper>(raw);
  1081. // only one element in the list
  1082. if (((UnityClientResponseWrapper) resp).array.Length > 0)
  1083. {
  1084. if (reqStruct.targetStep != null && reqStruct.targetStep == "CheckUpdate")
  1085. {
  1086. targetStep = STEP_GET_CLIENT;
  1087. callApiAsync();
  1088. }
  1089. else
  1090. {
  1091. UnityClientResponse unityClientResp = ((UnityClientResponseWrapper) resp).array[0];
  1092. AppStoreOnboardApi.tps = unityClientResp.channel.thirdPartySettings;
  1093. unityClientID.stringValue = unityClientResp.client_id;
  1094. unityClientKey.stringValue = unityClientResp.client_secret;
  1095. unityClientRSAPublicKey.stringValue = unityClientResp.channel.publicRSAKey;
  1096. unityProjectID.stringValue = unityClientResp.channel.projectGuid;
  1097. clientSecret_in_memory = unityClientResp.channel.channelSecret;
  1098. callbackUrl_in_memory = unityClientResp.channel.callbackUrl;
  1099. callbackUrl_last = callbackUrl_in_memory;
  1100. AppStoreOnboardApi.updateRev = unityClientResp.rev;
  1101. AppStoreOnboardApi.loaded = true;
  1102. serializedObject.ApplyModifiedProperties();
  1103. this.Repaint();
  1104. AssetDatabase.SaveAssets();
  1105. saveGameSettingsProps(unityClientResp.client_id);
  1106. UnityWebRequest newRequest = AppStoreOnboardApi.GetAppItem(unityClientID.stringValue);
  1107. AppItemResponseWrapper appItemResponseWrapper = new AppItemResponseWrapper();
  1108. ReqStruct newReqStruct = new ReqStruct();
  1109. newReqStruct.request = newRequest;
  1110. newReqStruct.resp = appItemResponseWrapper;
  1111. requestQueue.Enqueue(newReqStruct);
  1112. }
  1113. }
  1114. else
  1115. {
  1116. if (reqStruct.targetStep != null &&
  1117. (reqStruct.targetStep == "LinkProject" || reqStruct.targetStep == "CheckUpdate"))
  1118. {
  1119. _canLink = true;
  1120. isOperationRunning = false;
  1121. }
  1122. // no client found, generate one.
  1123. else if (ownerAuthed)
  1124. {
  1125. UnityClientInfo unityClientInfo = new UnityClientInfo();
  1126. string callbackUrl = callbackUrl_in_memory;
  1127. UnityWebRequest newRequest =
  1128. AppStoreOnboardApi.GenerateUnityClient(Application.cloudProjectId, unityClientInfo,
  1129. callbackUrl);
  1130. UnityClientResponse clientResp = new UnityClientResponse();
  1131. ReqStruct newReqStruct = new ReqStruct();
  1132. newReqStruct.request = newRequest;
  1133. newReqStruct.resp = clientResp;
  1134. newReqStruct.targetStep = reqStruct.targetStep;
  1135. requestQueue.Enqueue(newReqStruct);
  1136. }
  1137. else
  1138. {
  1139. EditorUtility.DisplayDialog("Error",
  1140. "Permission denied.",
  1141. "OK");
  1142. isOperationRunning = false;
  1143. }
  1144. }
  1145. }
  1146. else if (resp.GetType() == typeof(UnityClientResponse))
  1147. {
  1148. resp = JsonUtility.FromJson<UnityClientResponse>(request.downloadHandler.text);
  1149. unityClientID.stringValue = ((UnityClientResponse) resp).client_id;
  1150. unityClientKey.stringValue = ((UnityClientResponse) resp).client_secret;
  1151. unityClientRSAPublicKey.stringValue = ((UnityClientResponse) resp).channel.publicRSAKey;
  1152. unityProjectID.stringValue = ((UnityClientResponse) resp).channel.projectGuid;
  1153. clientSecret_in_memory = ((UnityClientResponse) resp).channel.channelSecret;
  1154. callbackUrl_in_memory = ((UnityClientResponse) resp).channel.callbackUrl;
  1155. callbackUrl_last = callbackUrl_in_memory;
  1156. AppStoreOnboardApi.tps = ((UnityClientResponse) resp).channel.thirdPartySettings;
  1157. AppStoreOnboardApi.updateRev = ((UnityClientResponse) resp).rev;
  1158. serializedObject.ApplyModifiedProperties();
  1159. this.Repaint();
  1160. AssetDatabase.SaveAssets();
  1161. saveGameSettingsProps(((UnityClientResponse) resp).client_id);
  1162. if (request.method == UnityWebRequest.kHttpVerbPOST) // Generated Client
  1163. {
  1164. UnityWebRequest analyticsRequest =
  1165. EditorAnalyticsApi.ClientEvent(EditorAnalyticsApi.k_ClientCreateEventName,
  1166. ((UnityClientResponse) resp).client_id, null);
  1167. ReqStruct analyticsReqStruct = new ReqStruct
  1168. {
  1169. request = analyticsRequest,
  1170. resp = new EventRequestResponse(),
  1171. eventName = EditorAnalyticsApi.k_ClientCreateEventName,
  1172. };
  1173. requestQueue.Enqueue(analyticsReqStruct);
  1174. }
  1175. else if (request.method == UnityWebRequest.kHttpVerbPUT) // Updated Client
  1176. {
  1177. UnityWebRequest analyticsRequest =
  1178. EditorAnalyticsApi.ClientEvent(EditorAnalyticsApi.k_ClientUpdateEventName,
  1179. ((UnityClientResponse) resp).client_id, null);
  1180. ReqStruct analyticsReqStruct = new ReqStruct
  1181. {
  1182. request = analyticsRequest,
  1183. resp = new EventRequestResponse(),
  1184. eventName = EditorAnalyticsApi.k_ClientUpdateEventName,
  1185. };
  1186. requestQueue.Enqueue(analyticsReqStruct);
  1187. }
  1188. if (reqStruct.targetStep == "LinkProject")
  1189. {
  1190. UnityClientInfo unityClientInfo = new UnityClientInfo();
  1191. unityClientInfo.ClientId = unityClientID.stringValue;
  1192. UnityWebRequest newRequest =
  1193. AppStoreOnboardApi.UpdateUnityClient(Application.cloudProjectId, unityClientInfo,
  1194. callbackUrl_in_memory);
  1195. UnityClientResponse clientResp = new UnityClientResponse();
  1196. ReqStruct newReqStruct = new ReqStruct();
  1197. newReqStruct.request = newRequest;
  1198. newReqStruct.resp = clientResp;
  1199. newReqStruct.targetStep = "GetRole";
  1200. requestQueue.Enqueue(newReqStruct);
  1201. }
  1202. else if (reqStruct.targetStep == "GetRole")
  1203. {
  1204. UnityWebRequest newRequest = AppStoreOnboardApi.GetUserId();
  1205. UserIdResponse userIdResp = new UserIdResponse();
  1206. ReqStruct newReqStruct = new ReqStruct();
  1207. newReqStruct.request = newRequest;
  1208. newReqStruct.resp = userIdResp;
  1209. newReqStruct.targetStep = STEP_GET_CLIENT;
  1210. requestQueue.Enqueue(newReqStruct);
  1211. }
  1212. else
  1213. {
  1214. if (reqStruct.targetStep == STEP_UPDATE_CLIENT)
  1215. {
  1216. EditorUtility.DisplayDialog("Hint",
  1217. "Unity Client updated successfully.",
  1218. "OK");
  1219. }
  1220. if (currentAppItem.status == "STAGE")
  1221. {
  1222. UnityWebRequest newRequest = AppStoreOnboardApi.UpdateAppItem(currentAppItem);
  1223. AppItemResponse appItemResponse = new AppItemResponse();
  1224. ReqStruct newReqStruct = new ReqStruct();
  1225. newReqStruct.request = newRequest;
  1226. newReqStruct.resp = appItemResponse;
  1227. requestQueue.Enqueue(newReqStruct);
  1228. }
  1229. else
  1230. {
  1231. UnityWebRequest newRequest = AppStoreOnboardApi.GetAppItem(unityClientID.stringValue);
  1232. AppItemResponseWrapper appItemResponseWrapper = new AppItemResponseWrapper();
  1233. ReqStruct newReqStruct = new ReqStruct();
  1234. newReqStruct.request = newRequest;
  1235. newReqStruct.resp = appItemResponseWrapper;
  1236. requestQueue.Enqueue(newReqStruct);
  1237. }
  1238. }
  1239. }
  1240. else if (resp.GetType() == typeof(AppItemResponse))
  1241. {
  1242. resp = JsonUtility.FromJson<AppItemResponse>(request.downloadHandler.text);
  1243. appItemId.stringValue = ((AppItemResponse) resp).id;
  1244. appName.stringValue = ((AppItemResponse) resp).name;
  1245. appSlug.stringValue = ((AppItemResponse) resp).slug;
  1246. currentAppItem.id = ((AppItemResponse) resp).id;
  1247. currentAppItem.name = ((AppItemResponse) resp).name;
  1248. currentAppItem.slug = ((AppItemResponse) resp).slug;
  1249. currentAppItem.ownerId = ((AppItemResponse) resp).ownerId;
  1250. currentAppItem.ownerType = ((AppItemResponse) resp).ownerType;
  1251. currentAppItem.status = ((AppItemResponse) resp).status;
  1252. currentAppItem.type = ((AppItemResponse) resp).type;
  1253. currentAppItem.clientId = ((AppItemResponse) resp).clientId;
  1254. currentAppItem.packageName = ((AppItemResponse) resp).packageName;
  1255. currentAppItem.revision = ((AppItemResponse) resp).revision;
  1256. serializedObject.ApplyModifiedProperties();
  1257. AssetDatabase.SaveAssets();
  1258. #region Analytics
  1259. string eventName = null;
  1260. if (request.method == UnityWebRequest.kHttpVerbPOST)
  1261. {
  1262. eventName = EditorAnalyticsApi.k_AppCreateEventName;
  1263. }
  1264. else if (request.method == UnityWebRequest.kHttpVerbPUT)
  1265. {
  1266. eventName = EditorAnalyticsApi.k_AppUpdateEventName;
  1267. }
  1268. if (eventName != null)
  1269. {
  1270. ReqStruct analyticsReqStruct = new ReqStruct
  1271. {
  1272. eventName = eventName,
  1273. request = EditorAnalyticsApi.AppEvent(eventName, currentAppItem.clientId,
  1274. (AppItemResponse) resp, null),
  1275. resp = new EventRequestResponse(),
  1276. };
  1277. requestQueue.Enqueue(analyticsReqStruct);
  1278. }
  1279. #endregion
  1280. this.Repaint();
  1281. publishApp(appItemId.stringValue);
  1282. }
  1283. else if (resp.GetType() == typeof(AppItemPublishResponse))
  1284. {
  1285. AppStoreOnboardApi.loaded = true;
  1286. resp = JsonUtility.FromJson<AppItemPublishResponse>(request.downloadHandler.text);
  1287. currentAppItem.revision = ((AppItemPublishResponse) resp).revision;
  1288. currentAppItem.status = "PUBLIC";
  1289. listPlayers();
  1290. }
  1291. else if (resp.GetType() == typeof(AppItemResponseWrapper))
  1292. {
  1293. resp = JsonUtility.FromJson<AppItemResponseWrapper>(request.downloadHandler.text);
  1294. if (((AppItemResponseWrapper) resp).total < 1)
  1295. {
  1296. // generate app
  1297. currentAppItem.clientId = unityClientID.stringValue;
  1298. currentAppItem.name = unityProjectID.stringValue;
  1299. currentAppItem.slug = Guid.NewGuid().ToString();
  1300. currentAppItem.ownerId = AppStoreOnboardApi.orgId;
  1301. UnityWebRequest newRequest = AppStoreOnboardApi.CreateAppItem(currentAppItem);
  1302. AppItemResponse appItemResponse = new AppItemResponse();
  1303. ReqStruct newReqStruct = new ReqStruct();
  1304. newReqStruct.request = newRequest;
  1305. newReqStruct.resp = appItemResponse;
  1306. requestQueue.Enqueue(newReqStruct);
  1307. }
  1308. else
  1309. {
  1310. var appItemResp = ((AppItemResponseWrapper) resp).results[0];
  1311. appName.stringValue = appItemResp.name;
  1312. appSlug.stringValue = appItemResp.slug;
  1313. appItemId.stringValue = appItemResp.id;
  1314. currentAppItem.id = appItemResp.id;
  1315. currentAppItem.name = appItemResp.name;
  1316. currentAppItem.slug = appItemResp.slug;
  1317. currentAppItem.ownerId = appItemResp.ownerId;
  1318. currentAppItem.ownerType = appItemResp.ownerType;
  1319. currentAppItem.status = appItemResp.status;
  1320. currentAppItem.type = appItemResp.type;
  1321. currentAppItem.clientId = appItemResp.clientId;
  1322. currentAppItem.packageName = appItemResp.packageName;
  1323. currentAppItem.revision = appItemResp.revision;
  1324. serializedObject.ApplyModifiedProperties();
  1325. AssetDatabase.SaveAssets();
  1326. this.Repaint();
  1327. if (appItemResp.status != "PUBLIC")
  1328. {
  1329. publishApp(appItemResp.id);
  1330. }
  1331. else
  1332. {
  1333. AppStoreOnboardApi.loaded = true;
  1334. listPlayers();
  1335. }
  1336. }
  1337. }
  1338. else if (resp.GetType() == typeof(PlayerResponse))
  1339. {
  1340. resp = JsonUtility.FromJson<PlayerResponse>(request.downloadHandler.text);
  1341. var playerId = ((PlayerResponse) resp).id;
  1342. UnityWebRequest newRequest = AppStoreOnboardApi.VerifyTestAccount(playerId);
  1343. PlayerVerifiedResponse playerVerifiedResponse = new PlayerVerifiedResponse();
  1344. ReqStruct newReqStruct = new ReqStruct();
  1345. newReqStruct.request = newRequest;
  1346. newReqStruct.resp = playerVerifiedResponse;
  1347. newReqStruct.targetStep = null;
  1348. requestQueue.Enqueue(newReqStruct);
  1349. }
  1350. else if (resp.GetType() == typeof(PlayerResponseWrapper))
  1351. {
  1352. resp = JsonUtility.FromJson<PlayerResponseWrapper>(request.downloadHandler.text);
  1353. testAccounts = new List<TestAccount>();
  1354. AppStoreStyles.kTestAccountBoxHeight = 25;
  1355. if (((PlayerResponseWrapper) resp).total > 0)
  1356. {
  1357. var exists = ((PlayerResponseWrapper) resp).results;
  1358. for (int i = 0; i < exists.Length; i++)
  1359. {
  1360. TestAccount existed = new TestAccount();
  1361. existed.email = exists[i].nickName;
  1362. existed.password = "******";
  1363. existed.playerId = exists[i].id;
  1364. testAccounts.Add(existed);
  1365. AppStoreStyles.kTestAccountBoxHeight += 22;
  1366. }
  1367. this.Repaint();
  1368. }
  1369. testAccount = new TestAccount();
  1370. testAccount.email = "Email";
  1371. testAccount.password = "Password";
  1372. this.Repaint();
  1373. isOperationRunning = false;
  1374. }
  1375. else if (resp.GetType() == typeof(PlayerVerifiedResponse))
  1376. {
  1377. listPlayers();
  1378. }
  1379. else if (resp.GetType() == typeof(PlayerChangePasswordResponse))
  1380. {
  1381. EditorUtility.DisplayDialog("Hint",
  1382. "Password changed successfully.",
  1383. "OK");
  1384. listPlayers();
  1385. }
  1386. else if (resp.GetType() == typeof(PlayerDeleteResponse))
  1387. {
  1388. EditorUtility.DisplayDialog("Hint",
  1389. "Test account deleted successfully.",
  1390. "OK");
  1391. listPlayers();
  1392. }
  1393. }
  1394. }
  1395. else
  1396. {
  1397. requestQueue.Enqueue(reqStruct);
  1398. }
  1399. }
  1400. private void listPlayers()
  1401. {
  1402. UnityWebRequest newRequest = AppStoreOnboardApi.GetTestAccount(unityClientID.stringValue);
  1403. PlayerResponseWrapper playerResponseWrapper = new PlayerResponseWrapper();
  1404. ReqStruct newReqStruct = new ReqStruct();
  1405. newReqStruct.request = newRequest;
  1406. newReqStruct.resp = playerResponseWrapper;
  1407. newReqStruct.targetStep = null;
  1408. requestQueue.Enqueue(newReqStruct);
  1409. }
  1410. private void publishApp(String appItemId)
  1411. {
  1412. UnityWebRequest newRequest = AppStoreOnboardApi.PublishAppItem(appItemId);
  1413. AppItemPublishResponse appItemPublishResponse = new AppItemPublishResponse();
  1414. ReqStruct newReqStruct = new ReqStruct();
  1415. newReqStruct.request = newRequest;
  1416. newReqStruct.resp = appItemPublishResponse;
  1417. requestQueue.Enqueue(newReqStruct);
  1418. }
  1419. }
  1420. }
  1421. #endif