/******************************************************************************* Copyright © 2015-2022 PICO Technology Co., Ltd.All rights reserved. NOTICE:All information contained herein is, and remains the property of PICO Technology Co., Ltd. The intellectual and technical concepts contained herein are proprietary to PICO Technology Co., Ltd. and may be covered by patents, patents in process, and are protected by trade secret or copyright law. Dissemination of this information or reproduction of this material is strictly forbidden unless prior written permission is obtained from PICO Technology Co., Ltd. *******************************************************************************/ using System; using System.IO; using System.Runtime.CompilerServices; using Pico.Platform.Framework; using Unity.XR.PXR; using UnityEngine; [assembly: InternalsVisibleTo("Assembly-CSharp-Editor")] namespace Pico.Platform { /** * \defgroup Platform Services */ /** * \ingroup Platform * */ public static class CoreService { public static bool Initialized = false; public static string NotInitializedError = "Platform SDK has not been initialized!"; /// Gets whether the Platform SDK has been initialized. /// /// * `true`: initialized /// * `false`: not initialized /// public static bool IsInitialized() { return Initialized; } /// /// Gets the app ID for the current app. /// /// The app ID. /// If the app ID cannot be found, this exception will be thrown. public static string GetAppID(string appId = null) { string configAppID = PXR_PlatformSetting.Instance.appID.Trim(); if (!string.IsNullOrWhiteSpace(appId) && !string.IsNullOrWhiteSpace(configAppID) && appId != configAppID) { throw new UnityException("The parameter appId is inconsistent with the configured appId"); } if (!string.IsNullOrWhiteSpace(appId)) { return appId; } if (!string.IsNullOrWhiteSpace(configAppID)) { return configAppID; } throw new UnityException("Cannot find appId"); } /// /// Initializes the Platform SDK asynchronously. /// /// The app ID for the Platform SDK. If not provided, Unity editor configuration will be applied. /// The initialization result. /// If the input app ID is null or empty or if the initialization fails, this exception will be thrown. /// If the current platform is not supported, this exception will be thrown. public static Task AsyncInitialize(string appId = null) { if (Initialized) { return new Task(0); } appId = GetAppID(appId); if (String.IsNullOrWhiteSpace(appId)) { throw new UnityException("AppID cannot be null or empty"); } Task task; if (Application.platform == RuntimePlatform.Android) { AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity"); var requestId = CLIB.ppf_InitializeAndroidAsynchronous(appId, activity.GetRawObject(), IntPtr.Zero); if (requestId == 0) { throw new Exception("PICO PlatformSDK failed to initialize"); } task = new Task(requestId); } else if ((Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)) { var config = Resources.Load("PicoSdkPCConfig"); var logDirectory = Path.GetFullPath("Logs"); if (config == null) { throw new UnityException($"cannot find PC config file Resources/PicoSdkPCConfig"); } if (!Directory.Exists(logDirectory)) { Directory.CreateDirectory(logDirectory); } var requestId = CLIB.ppf_PcInitAsynchronousWrapper(appId, config.text, logDirectory); if (requestId == 0) { throw new Exception("PICO PlatformSDK failed to initialize"); } else { task = new Task(requestId); } } else { throw new NotImplementedException("PICO platform is not implemented on this platform yet."); } Initialized = true; Runner.RegisterGameObject(); return task; } /// /// Initializes the Platform SDK synchronously. /// /// The app ID for the Platform SDK. If not provided, Unity editor configuration will be applied. /// If the current platform is not supported, this exception will be thrown. /// If the initialization fails, this exception will be thrown. public static void Initialize(string appId = null) { if (Initialized) { return; } appId = GetAppID(appId); if (String.IsNullOrWhiteSpace(appId)) { throw new UnityException("AppID must not be null or empty"); } PlatformInitializeResult initializeResult; if (Application.platform == RuntimePlatform.Android) { AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity"); initializeResult = CLIB.ppf_InitializeAndroid(appId, activity.GetRawObject(), IntPtr.Zero); if (initializeResult == PlatformInitializeResult.Success || initializeResult == PlatformInitializeResult.AlreadyInitialized) { Initialized = true; } } else if ((Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)) { var config = Resources.Load("PicoSdkPCConfig"); if (config == null) { throw new UnityException($"cannot find PC config file Resources/PicoSdkPCConfig"); } var logDirectory = Path.GetFullPath("Logs"); if (!Directory.Exists(logDirectory)) { Directory.CreateDirectory(logDirectory); } initializeResult = CLIB.ppf_PcInitWrapper(appId, config.text, logDirectory); if (initializeResult == PlatformInitializeResult.Success || initializeResult == PlatformInitializeResult.AlreadyInitialized) { Initialized = true; } } else { throw new NotImplementedException("PICO platform is not implemented on this platform yet."); } if (!Initialized) { throw new UnityException($"PICO Platform failed to initialize:{initializeResult}."); } Runner.RegisterGameObject(); } /** * \overload Task GameInitialize(string accessToken) */ /// /// Initializes game-related modules, such as room, matchmaking, and network. /// /// The access token of Platform SDK. You can get the access token by calling `UserService.GetAccessToken()`. public static Task GameInitialize(string accessToken) { if (Initialized) { return new Task(CLIB.ppf_Game_InitializeWithToken(accessToken)); } Debug.LogError(NotInitializedError); return null; } /** * \overload Task GameInitialize() */ /// /// Initializes modules without token related with game, such as room, matchmaking, and net. /// public static Task GameInitialize() { if (Initialized) { return new Task(CLIB.ppf_Game_InitializeAuto()); } Debug.LogError(NotInitializedError); return null; } /// /// Uninitializes game-related modules, such as room, matchmaking, and network. /// /// /// * `true`: success /// * `false`: failure /// public static bool GameUninitialize() { if (Initialized) { return CLIB.ppf_Game_UnInitialize(); } return false; } } }