Core.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*******************************************************************************
  2. Copyright © 2015-2022 PICO Technology Co., Ltd.All rights reserved.
  3. NOTICE:All information contained herein is, and remains the property of
  4. PICO Technology Co., Ltd. The intellectual and technical concepts
  5. contained herein are proprietary to PICO Technology Co., Ltd. and may be
  6. covered by patents, patents in process, and are protected by trade secret or
  7. copyright law. Dissemination of this information or reproduction of this
  8. material is strictly forbidden unless prior written permission is obtained from
  9. PICO Technology Co., Ltd.
  10. *******************************************************************************/
  11. using System;
  12. using System.IO;
  13. using System.Runtime.CompilerServices;
  14. using Pico.Platform.Framework;
  15. using Unity.XR.PXR;
  16. using UnityEngine;
  17. [assembly: InternalsVisibleTo("Assembly-CSharp-Editor")]
  18. namespace Pico.Platform
  19. {
  20. /**
  21. * \defgroup Platform Services
  22. */
  23. /**
  24. * \ingroup Platform
  25. *
  26. */
  27. public static class CoreService
  28. {
  29. public static bool Initialized = false;
  30. public static string NotInitializedError = "Platform SDK has not been initialized!";
  31. /// <summary>Gets whether the Platform SDK has been initialized.</summary>
  32. /// <returns>
  33. /// * `true`: initialized
  34. /// * `false`: not initialized
  35. /// </returns>
  36. public static bool IsInitialized()
  37. {
  38. return Initialized;
  39. }
  40. /// <summary>
  41. /// Gets the app ID for the current app.
  42. /// </summary>
  43. /// <returns>The app ID.</returns>
  44. /// <exception cref="UnityException">If the app ID cannot be found, this exception will be thrown.</exception>
  45. public static string GetAppID(string appId = null)
  46. {
  47. string configAppID = PXR_PlatformSetting.Instance.appID.Trim();
  48. if (!string.IsNullOrWhiteSpace(appId) && !string.IsNullOrWhiteSpace(configAppID) && appId != configAppID)
  49. {
  50. throw new UnityException("The parameter appId is inconsistent with the configured appId");
  51. }
  52. if (!string.IsNullOrWhiteSpace(appId))
  53. {
  54. return appId;
  55. }
  56. if (!string.IsNullOrWhiteSpace(configAppID))
  57. {
  58. return configAppID;
  59. }
  60. throw new UnityException("Cannot find appId");
  61. }
  62. /// <summary>
  63. /// Initializes the Platform SDK asynchronously.
  64. /// </summary>
  65. /// <param name="appId">The app ID for the Platform SDK. If not provided, Unity editor configuration will be applied.</param>
  66. /// <returns>The initialization result.</returns>
  67. /// <exception cref="UnityException">If the input app ID is null or empty or if the initialization fails, this exception will be thrown.</exception>
  68. /// <exception cref="NotImplementedException">If the current platform is not supported, this exception will be thrown.</exception>
  69. public static Task<PlatformInitializeResult> AsyncInitialize(string appId = null)
  70. {
  71. if (Initialized)
  72. {
  73. return new Task<PlatformInitializeResult>(0);
  74. }
  75. appId = GetAppID(appId);
  76. if (String.IsNullOrWhiteSpace(appId))
  77. {
  78. throw new UnityException("AppID cannot be null or empty");
  79. }
  80. Task<PlatformInitializeResult> task;
  81. if (Application.platform == RuntimePlatform.Android)
  82. {
  83. AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
  84. var requestId = CLIB.ppf_InitializeAndroidAsynchronous(appId, activity.GetRawObject(), IntPtr.Zero);
  85. if (requestId == 0)
  86. {
  87. throw new Exception("PICO PlatformSDK failed to initialize");
  88. }
  89. task = new Task<PlatformInitializeResult>(requestId);
  90. }
  91. else if ((Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor))
  92. {
  93. var config = Resources.Load<TextAsset>("PicoSdkPCConfig");
  94. var logDirectory = Path.GetFullPath("Logs");
  95. if (config == null)
  96. {
  97. throw new UnityException($"cannot find PC config file Resources/PicoSdkPCConfig");
  98. }
  99. if (!Directory.Exists(logDirectory))
  100. {
  101. Directory.CreateDirectory(logDirectory);
  102. }
  103. var requestId = CLIB.ppf_PcInitAsynchronousWrapper(appId, config.text, logDirectory);
  104. if (requestId == 0)
  105. {
  106. throw new Exception("PICO PlatformSDK failed to initialize");
  107. }
  108. else
  109. {
  110. task = new Task<PlatformInitializeResult>(requestId);
  111. }
  112. }
  113. else
  114. {
  115. throw new NotImplementedException("PICO platform is not implemented on this platform yet.");
  116. }
  117. Initialized = true;
  118. Runner.RegisterGameObject();
  119. return task;
  120. }
  121. /// <summary>
  122. /// Initializes the Platform SDK synchronously.
  123. /// </summary>
  124. /// <param name="appId">The app ID for the Platform SDK. If not provided, Unity editor configuration will be applied.</param>
  125. /// <exception cref="NotImplementedException">If the current platform is not supported, this exception will be thrown.</exception>
  126. /// <exception cref="UnityException">If the initialization fails, this exception will be thrown.</exception>
  127. public static void Initialize(string appId = null)
  128. {
  129. if (Initialized)
  130. {
  131. return;
  132. }
  133. appId = GetAppID(appId);
  134. if (String.IsNullOrWhiteSpace(appId))
  135. {
  136. throw new UnityException("AppID must not be null or empty");
  137. }
  138. PlatformInitializeResult initializeResult;
  139. if (Application.platform == RuntimePlatform.Android)
  140. {
  141. AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
  142. initializeResult = CLIB.ppf_InitializeAndroid(appId, activity.GetRawObject(), IntPtr.Zero);
  143. if (initializeResult == PlatformInitializeResult.Success ||
  144. initializeResult == PlatformInitializeResult.AlreadyInitialized)
  145. {
  146. Initialized = true;
  147. }
  148. }
  149. else if ((Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor))
  150. {
  151. var config = Resources.Load<TextAsset>("PicoSdkPCConfig");
  152. if (config == null)
  153. {
  154. throw new UnityException($"cannot find PC config file Resources/PicoSdkPCConfig");
  155. }
  156. var logDirectory = Path.GetFullPath("Logs");
  157. if (!Directory.Exists(logDirectory))
  158. {
  159. Directory.CreateDirectory(logDirectory);
  160. }
  161. initializeResult = CLIB.ppf_PcInitWrapper(appId, config.text, logDirectory);
  162. if (initializeResult == PlatformInitializeResult.Success ||
  163. initializeResult == PlatformInitializeResult.AlreadyInitialized)
  164. {
  165. Initialized = true;
  166. }
  167. }
  168. else
  169. {
  170. throw new NotImplementedException("PICO platform is not implemented on this platform yet.");
  171. }
  172. if (!Initialized)
  173. {
  174. throw new UnityException($"PICO Platform failed to initialize:{initializeResult}.");
  175. }
  176. Runner.RegisterGameObject();
  177. }
  178. /**
  179. * \overload Task<GameInitializeResult> GameInitialize(string accessToken)
  180. */
  181. /// <summary>
  182. /// Initializes game-related modules, such as room, matchmaking, and network.
  183. /// </summary>
  184. /// <param name="accessToken">The access token of Platform SDK. You can get the access token by calling `UserService.GetAccessToken()`.</param>
  185. public static Task<GameInitializeResult> GameInitialize(string accessToken)
  186. {
  187. if (Initialized)
  188. {
  189. return new Task<GameInitializeResult>(CLIB.ppf_Game_InitializeWithToken(accessToken));
  190. }
  191. Debug.LogError(NotInitializedError);
  192. return null;
  193. }
  194. /**
  195. * \overload Task<GameInitializeResult> GameInitialize()
  196. */
  197. /// <summary>
  198. /// Initializes modules without token related with game, such as room, matchmaking, and net.
  199. /// </summary>
  200. public static Task<GameInitializeResult> GameInitialize()
  201. {
  202. if (Initialized)
  203. {
  204. return new Task<GameInitializeResult>(CLIB.ppf_Game_InitializeAuto());
  205. }
  206. Debug.LogError(NotInitializedError);
  207. return null;
  208. }
  209. /// <summary>
  210. /// Uninitializes game-related modules, such as room, matchmaking, and network.
  211. /// </summary>
  212. /// <returns>
  213. /// * `true`: success
  214. /// * `false`: failure
  215. /// </returns>
  216. public static bool GameUninitialize()
  217. {
  218. if (Initialized)
  219. {
  220. return CLIB.ppf_Game_UnInitialize();
  221. }
  222. return false;
  223. }
  224. }
  225. }