XDevicePluginOpCenter.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Collections.Generic;
  4. //#if UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID || UNITY_WIN || UNITY_MAC
  5. #if true
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. using XDebug = UnityEngine.Debug;
  9. #else
  10. using XDebug = System.Diagnostics.Debug;
  11. #endif // UNITY_EDITOR
  12. using NativeHandle = System.IntPtr;
  13. using NativeExHandle = System.IntPtr;
  14. using AOT;
  15. namespace Ximmerse.XR
  16. {
  17. public partial class XDevicePlugin
  18. {
  19. public const string xopc_pluginName = "xopc_client";
  20. #region Native
  21. [DllImport(xopc_pluginName, CallingConvention = CallingConvention.Cdecl)]
  22. private static extern int xopc_on_mission_begin();
  23. [DllImport(xopc_pluginName, CallingConvention = CallingConvention.Cdecl)]
  24. private static extern int xopc_on_mission_end();
  25. private delegate int opc_message_handle_callback_t(IntPtr message);
  26. /// Set OPCenter message handle callback.
  27. [DllImport(xopc_pluginName, CallingConvention = CallingConvention.Cdecl)]
  28. private static extern void xopc_set_message_callback(opc_message_handle_callback_t cb);
  29. #endregion
  30. private class OpcMessage
  31. {
  32. public string type;
  33. }
  34. [MonoPInvokeCallback(typeof(opc_message_handle_callback_t))]
  35. static private int OpcMessageHandleCallback(IntPtr message)
  36. {
  37. int ret = 0;
  38. if (OpcEventsDelegates == null)
  39. return ret;
  40. var msgJson = Marshal.PtrToStringAnsi(message);
  41. var msg = JsonUtility.FromJson<OpcMessage>(msgJson);
  42. if (msg.type != null)
  43. {
  44. if (msg.type.Equals("begin_mission"))
  45. {
  46. if (OpcEventsDelegates.OnRequestBeginMission != null)
  47. {
  48. OpcEventsDelegates.OnRequestBeginMission(null);
  49. }
  50. }
  51. else if (msg.type.Equals("end_mission"))
  52. {
  53. if (OpcEventsDelegates.OnRequestEndMission != null)
  54. {
  55. OpcEventsDelegates.OnRequestEndMission(null);
  56. }
  57. }
  58. else if (msg.type.Equals("exit_app"))
  59. {
  60. if (OpcEventsDelegates.OnRequestExitApp != null)
  61. {
  62. OpcEventsDelegates.OnRequestExitApp(null);;
  63. }
  64. }
  65. else
  66. {
  67. ret = -1;
  68. }
  69. }
  70. return ret;
  71. }
  72. /// Delegates for handling difference messages from OP Center
  73. public class OpCenterMessagesDelegates
  74. {
  75. /// Invoked with mission key string argument while receiving begin mission message from OP Center.
  76. public Action<string> OnRequestBeginMission;
  77. /// Invoked with mission key string argument while receiving begin mission message from OP Center.
  78. public Action<string> OnRequestEndMission;
  79. /// Invoked with string argument while receiving begin mission message from OP Center.
  80. public Action<string> OnRequestExitApp;
  81. }
  82. static OpCenterMessagesDelegates OpcEventsDelegates;
  83. /// Set delegates to handle OpCenter messages
  84. public static void SetOpCenterMessageDelegates(OpCenterMessagesDelegates delegates)
  85. {
  86. OpcEventsDelegates = delegates;
  87. if (delegates != null)
  88. xopc_set_message_callback(OpcMessageHandleCallback);
  89. else
  90. xopc_set_message_callback(null);
  91. }
  92. //public void ExitOpCenter()
  93. //{
  94. //}
  95. /// \brief developer need to call this method before beginning the game mission,
  96. /// then start mission or not according the return value;
  97. /// NOTICE: this method may block the thread.
  98. /// \returns Return negative value for error, otherwise return seconds for how long time allowed to play;
  99. public static int OnMissionBegin()
  100. {
  101. return xopc_on_mission_begin();
  102. }
  103. /// \brief Developer need to call this method before ending the game mission.
  104. /// \returns Return negative value for error.
  105. public static int OnMissionEnd()
  106. {
  107. return xopc_on_mission_end();
  108. }
  109. //public static void OnGameStateChange()
  110. //{
  111. // return 0;
  112. //}
  113. static AndroidJavaClass OpServiceApiClass = null;
  114. static bool isOpcInit = false;
  115. static public bool OpcInit(String gameId) {
  116. if (isOpcInit) {
  117. Debug.LogError("Opc already Init");
  118. return true;
  119. }
  120. Debug.Log("Opc Init...");
  121. OpServiceApiClass = new AndroidJavaClass("com.ximmerse.opcenter.OpServiceApi");
  122. AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  123. AndroidJavaObject currentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
  124. AndroidJavaObject obj = OpServiceApiClass.CallStatic<AndroidJavaObject>("createDefault", currentActivity, gameId);
  125. if (obj != null && OpServiceApiClass!=null)
  126. {
  127. isOpcInit = true;
  128. }
  129. else {
  130. Debug.LogError("Opc Init error...");
  131. isOpcInit = false;
  132. }
  133. return isOpcInit;
  134. }
  135. static public void OpcExit() {
  136. Debug.Log("Opc Exit...");
  137. if (isOpcInit) {
  138. OpServiceApiClass.CallStatic("releaseDefault");
  139. }
  140. isOpcInit = false;
  141. }
  142. /**
  143. * Get OPService version code
  144. */
  145. public static int GetOpServiceVersionCode()
  146. {
  147. if (isOpcInit) {
  148. return OpServiceApiClass.CallStatic<int>("getOpServiceVersionCode");
  149. }
  150. Debug.LogError("OpService not inited");
  151. return -1;
  152. }
  153. /**
  154. * Is Remote OPConsole server connected.
  155. */
  156. public static bool IsOpConsoleServerConnected()
  157. {
  158. if (isOpcInit)
  159. {
  160. return OpServiceApiClass.CallStatic<bool>("isOpConsoleServerConnected");
  161. }
  162. Debug.LogError("OpService not inited");
  163. return false;
  164. }
  165. /**
  166. *
  167. * \returns Return OPConsole server IP address, return null or empty if not exists.
  168. */
  169. public static string GetOpConsoleServerAddress()
  170. {
  171. if (isOpcInit)
  172. {
  173. return OpServiceApiClass.CallStatic<string>("getOpConsoleServerAddress");
  174. }
  175. Debug.LogError("OpService not inited");
  176. return null;
  177. }
  178. // String[] GetGameServerAddresses(String gameId);
  179. // String[] GetGameServerUUIDs(String gameId);
  180. /**
  181. * \returns return true if bound game server exists
  182. */
  183. public static bool HasBoundGameServer()
  184. {
  185. if (isOpcInit)
  186. {
  187. return OpServiceApiClass.CallStatic<bool>("hasBoundGameServer");
  188. }
  189. Debug.LogError("OpService not inited");
  190. return false;
  191. }
  192. /**
  193. * Bound game server is online
  194. * \returns return true if bound game server exists and online, otherwise return false.
  195. */
  196. public static bool IsBoundGameServerOnline()
  197. {
  198. if (isOpcInit)
  199. {
  200. return OpServiceApiClass.CallStatic<bool>("isBoundGameServerOnline");
  201. }
  202. Debug.LogError("OpService not inited");
  203. return false;
  204. }
  205. /**
  206. * Get bound game server address(IP or URL)
  207. * \returns return null or empty if not exists.
  208. */
  209. public static string GetBoundGameServerAddress()
  210. {
  211. if (isOpcInit)
  212. {
  213. return OpServiceApiClass.CallStatic<string>("getBoundGameServerAddress");
  214. }
  215. Debug.LogError("OpService not inited");
  216. return null;
  217. }
  218. /**
  219. * Get bound game server GameID
  220. * \returns return null or empty if not exists.
  221. */
  222. public static string GetBoundGameServerGameID()
  223. {
  224. if (isOpcInit)
  225. {
  226. return OpServiceApiClass.CallStatic<string>("getBoundGameServerGameID");
  227. }
  228. Debug.LogError("OpService not inited");
  229. return null;
  230. }
  231. /**
  232. * Get bound game server UUID
  233. * \returns return null or empty if not exists.
  234. */
  235. public static string GetBoundGameServerUUID()
  236. {
  237. if (isOpcInit)
  238. {
  239. return OpServiceApiClass.CallStatic<string>("getBoundGameServerUUID");
  240. }
  241. Debug.LogError("OpService not inited");
  242. return null;
  243. }
  244. }
  245. }