SDKMgr.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using UnityEngine;
  6. public partial class SDKMgr : MonoBehaviour
  7. {
  8. internal string httpCheckUrl = string.Empty;
  9. internal GameObject sdkMgrObj;
  10. public void SetHttpCheckUrl(string value)
  11. {
  12. if (!string.IsNullOrEmpty(value))
  13. {
  14. httpCheckUrl = value;
  15. }
  16. }
  17. protected static SDKMgr instance;
  18. public static SDKMgr Instance
  19. {
  20. get
  21. {
  22. if (instance == null)
  23. {
  24. //初次调用时,因为java回调需要一个实例对象,
  25. GameObject a = new GameObject("SDKMgr");
  26. instance = a.AddComponent<SDKMgr>();
  27. DontDestroyOnLoad(a);
  28. }
  29. return instance;
  30. }
  31. }
  32. public SDKBase sdk;
  33. private ISDKCallBack listener;
  34. //TODO:后续将与回调一样的写法
  35. private SDKReportBase m_SDKReport;
  36. public SDKReportBase SDKReport
  37. {
  38. get
  39. {
  40. #if UNITY_ANDROID && NTSDK && !UNITY_EDITOR
  41. if (m_SDKReport == null)
  42. {
  43. m_SDKReport = new NTReport();
  44. }
  45. #endif
  46. return m_SDKReport;
  47. }
  48. }
  49. /// <summary>
  50. /// sdk初始化 同时初始化回调事件 需要在初始化完成后在回调方法中设置初始化状态为true
  51. /// </summary>
  52. public void Init()
  53. {
  54. if (sdk == null)
  55. {
  56. #if UNITY_EDITOR || NULLSDK
  57. //listener = new NULLSDKCBListener();
  58. sdk = new NULLSDKAndroid();
  59. //sdk.Init(listener, "");
  60. //#elif UNITY_ANDROID && NBSDK
  61. // listener = new NBSDKCBListener();
  62. // sdk = new NBSDKAndroid();
  63. // sdk.Init(listener , "SDKNB");
  64. //#elif UNITY_ANDROID && NTSDK
  65. // listener = new NTSDKCBListener();
  66. // sdk = new NTSDKAndroid();
  67. // sdk.Init(listener, "SDKUni");
  68. #else
  69. var types = Assembly.GetExecutingAssembly().GetTypes();
  70. var baseType = typeof(SDKBase);
  71. List<SDKBase> processors = new List<SDKBase>();
  72. foreach (var t in types)
  73. {
  74. var tmp = t.BaseType;
  75. while (tmp != null)
  76. {
  77. if (tmp == baseType && Type.GetType(t.FullName, true) != typeof(NULLSDKAndroid))
  78. {
  79. //Debug.Log(t.BaseType);
  80. SDKBase obj = MethodMaker.CreateObject(t.FullName) as SDKBase;
  81. if (obj != null)
  82. {
  83. processors.Add(obj);
  84. sdk = obj;
  85. }
  86. break;
  87. }
  88. else
  89. {
  90. tmp = tmp.BaseType;
  91. }
  92. }
  93. }
  94. #endif
  95. }
  96. if (sdk == null)
  97. return;
  98. if (sdk.GetInited())
  99. {
  100. sdk.CallInitSuccessCB();
  101. }
  102. else
  103. {
  104. sdk.Init();
  105. }
  106. }
  107. private void OnApplicationFocus(bool focusStatus)
  108. {
  109. if (sdk == null) return;
  110. sdk.OnApplicationFocus(focusStatus);
  111. }
  112. /// <summary>
  113. /// sdk登录 登陆后需要在回调中设置登陆状态为true
  114. /// </summary>
  115. public void Login()
  116. {
  117. ReportLoginUI(GetInt64TimeStamp());
  118. if (sdk == null) return;
  119. if (sdk.GetLogined())
  120. {
  121. sdk.CallLoginSuccessCB();
  122. return;
  123. }
  124. sdk.Login();
  125. }
  126. public void Logout()
  127. {
  128. if (sdk == null) return;
  129. sdk.Logout();
  130. }
  131. public void Pay(int goodsId, string goodsName, string goodsDesc,
  132. int count, float amount,
  133. string cpOrderId, string extrasParams)
  134. {
  135. if (sdk == null) return;
  136. if (m_GameRoleInfo == null) return;
  137. OrderExtraInfo data = new OrderExtraInfo()
  138. {
  139. subPlatform = GetChannelName(),
  140. serverId = m_GameRoleInfo.serverId,
  141. platform = ((sdk == null || string.IsNullOrEmpty(sdk.SDKName)) ? "" : sdk.SDKName),
  142. };
  143. try
  144. {
  145. string extraParams = JsonUtility.ToJson(data);
  146. sdk.Pay(goodsId, goodsName, goodsDesc,
  147. count, amount,
  148. cpOrderId, extraParams);
  149. }
  150. catch (Exception e)
  151. {
  152. Debug.LogException(e);
  153. Debug.LogError("Pay Fail");
  154. }
  155. }
  156. public void EnterGame()
  157. {
  158. SyncInfoToFairGuard();
  159. SyncInfoToBugly();
  160. if (sdk == null) return;
  161. sdk.EnterGame();
  162. }
  163. public void CreateRole()
  164. {
  165. if (sdk == null) return;
  166. sdk.CreateRole();
  167. }
  168. public void ExitGame()
  169. {
  170. if (sdk == null) return;
  171. sdk.ExitGame();
  172. }
  173. public void UpdateRoleLv()
  174. {
  175. if (sdk == null) return;
  176. sdk.UpdateRoleLv();
  177. }
  178. public void SwitchAccount()
  179. {
  180. if (sdk == null) return;
  181. sdk.SwitchAccount();
  182. }
  183. public bool Exit()
  184. {
  185. if (sdk == null) return false;
  186. return sdk.Exit();
  187. }
  188. public bool Quit()
  189. {
  190. if (sdk == null) return false;
  191. return sdk.Quit();
  192. }
  193. public bool IsReportAction()
  194. {
  195. if (sdk == null) return false;
  196. return sdk.IsReportAction();
  197. }
  198. public void ReportAction(Dictionary<object, object> datas)
  199. {
  200. if (sdk == null) return;
  201. sdk.ReportAction(datas);
  202. }
  203. public string GetSDKName()
  204. {
  205. if (sdk == null) return "";
  206. return sdk.GetSDKName();
  207. }
  208. public void SetLogined(bool value)
  209. {
  210. if (sdk == null) return;
  211. sdk.SetLogined(value);
  212. }
  213. /// <summary>
  214. /// 设置sdk的初始化状态显示
  215. /// </summary>
  216. /// <param name="value"></param>
  217. public void SetInited(bool value)
  218. {
  219. if (sdk == null) return;
  220. sdk.SetInited(value);
  221. }
  222. /// <summary>
  223. /// 获取sdk的登陆状态
  224. /// </summary>
  225. /// <returns></returns>
  226. public bool GetLogined()
  227. {
  228. if (sdk == null) return false;
  229. return sdk.GetLogined();
  230. }
  231. /// <summary>
  232. /// 获取sdk的初始化状态
  233. /// </summary>
  234. /// <returns></returns>
  235. public bool GetInited()
  236. {
  237. if (sdk == null) return false;
  238. return sdk.GetInited();
  239. }
  240. /// <summary>
  241. /// 检查屏蔽言论,具体需要的逻辑由各sdk内部重写
  242. /// </summary>
  243. /// <param name="level">玩家等级</param>
  244. /// <param name="channel">聊天频道</param>
  245. /// <param name="cont">言论内容</param>
  246. /// <returns></returns>
  247. public bool CheckSpeech(string level, ChannelType channel, string cont)
  248. {
  249. if (sdk == null) return true;
  250. return sdk.EnvReviewLan(level, channel.ToString(), cont);
  251. }
  252. /// <summary>
  253. /// 检查屏蔽昵称,具体需要的逻辑由各sdk内部重写
  254. /// </summary>
  255. /// <param name="name"></param>
  256. /// <returns></returns>
  257. public bool CheckName(string name)
  258. {
  259. if (sdk == null) return true;
  260. return sdk.EnvReviewName(name);
  261. }
  262. /// <summary>
  263. /// 此为服务器回的信息的解码,具体需要的逻辑由各sdk内部重写
  264. /// </summary>
  265. /// <param name="code"></param>
  266. public void Decode(string code)
  267. {
  268. if (sdk == null) return;
  269. sdk.Decode(code);
  270. }
  271. /// <summary>
  272. /// 检查sdk中是否包含此功能模块,用于判断游戏中是否需要打开UI
  273. /// </summary>
  274. /// <param name="needCheckModulType"></param>
  275. /// <returns></returns>
  276. public bool CheckHasModul(SDKModulType needCheckModulType)
  277. {
  278. if (sdk == null) return false;
  279. return sdk.CheckHasModul(needCheckModulType);
  280. }
  281. /// <summary>
  282. /// 用于游戏内的UI打开SDK中的功能模块
  283. /// </summary>
  284. public void OpenModul(SDKModulType sDKModulType)
  285. {
  286. if (sdk == null) return;
  287. sdk.OpenModul(sDKModulType);
  288. }
  289. /// <summary>
  290. /// 打开sdk中的网页
  291. /// </summary>
  292. /// <param name="url">网址</param>
  293. public void OpenWebview(string url)
  294. {
  295. if (sdk == null) return;
  296. sdk.OpenWebview(url);
  297. }
  298. /// <summary>
  299. /// 打开客服页面
  300. /// </summary>
  301. public void OpenGM()
  302. {
  303. if (sdk == null) return;
  304. sdk.OpenGM();
  305. }
  306. /// <summary>
  307. /// 关闭客服页面
  308. /// </summary>
  309. public void CloseGM()
  310. {
  311. if (sdk == null) return;
  312. sdk.CloseGM();
  313. }
  314. ///// <summary>
  315. ///// 打开实名认证倒计时
  316. ///// </summary>
  317. //public void OpenRealNameCountDown()
  318. //{
  319. // if (sdk == null) return;
  320. // sdk.OpenReakNameCountDown();
  321. //}
  322. ///// <summary>
  323. ///// 关闭实名认证倒计时
  324. ///// </summary>
  325. //public void CloseRealNameCountDown()
  326. //{
  327. // if (sdk == null) return;
  328. // sdk.CloseRealNameCountDown();
  329. //}
  330. /// <summary>
  331. /// 打开用户协议
  332. /// </summary>
  333. /// <param name="showState">false 为一次性 true 为可无限打开</param>
  334. public void OpenCompactView(bool showState)
  335. {
  336. if (sdk == null) return;
  337. sdk.OpenCompactView(showState);
  338. }
  339. public bool HasCanEnterServerJudge()
  340. {
  341. if (sdk == null) return false;
  342. return sdk.HasCanEnterServerJudge();
  343. }
  344. public void CanEnterServerJudge(string serverId, string serverName)
  345. {
  346. if (sdk == null) return;
  347. sdk.CanEnterServerJudge(serverId, serverName);
  348. }
  349. public Int64 GetInt64TimeStamp()
  350. {
  351. return DateTime.Now.Ticks;
  352. }
  353. private string m_ChannelName = string.Empty;
  354. public string GetChannelName()
  355. {
  356. if (string.IsNullOrEmpty(m_ChannelName))
  357. {
  358. m_ChannelName = Wenting.Lebian.LeBianSDK.instance.GetClientChId();
  359. }
  360. return m_ChannelName;
  361. }
  362. private DeviceSystemData m_DeviceSystemData;
  363. public DeviceSystemData GetDeviceSystemData()
  364. {
  365. if (sdk == null) return null;
  366. DeviceSystemData deviceSystemData = sdk.GetDeviceSystemData();
  367. if (deviceSystemData != null)
  368. {
  369. return deviceSystemData;
  370. }
  371. if (m_DeviceSystemData == null)
  372. {
  373. m_DeviceSystemData = new DeviceSystemData()
  374. {
  375. deviceModel = SystemInfo.deviceModel,
  376. // deviceWidth = 0,
  377. // deviceHeight = 0,
  378. osName = SystemInfo.operatingSystemFamily.ToString(),
  379. osVer = SystemInfo.operatingSystem,
  380. // macAddr = "",
  381. // udid = "",
  382. // isp = "",
  383. // network = "",
  384. // appChannel = "",
  385. // appVer = "",
  386. // transId = "",
  387. deviceId = SystemInfo.deviceUniqueIdentifier,
  388. // isEmulator = 0,
  389. // isRoot = 0,
  390. // accountId = "",
  391. // oldAccountId = "",
  392. // imei = "",
  393. // countryCode = "",
  394. // oaid = "",
  395. engineVer = Application.unityVersion,
  396. };
  397. }
  398. return m_DeviceSystemData;
  399. }
  400. private GameRoleInfo m_GameRoleInfo;
  401. public GameRoleInfo GetGameRoleInfo()
  402. {
  403. if (sdk == null && SDKReport == null) return null;
  404. if (m_GameRoleInfo == null)
  405. {
  406. m_GameRoleInfo = new GameRoleInfo();
  407. }
  408. return m_GameRoleInfo;
  409. }
  410. private class FairGuardData
  411. {
  412. public string openId;
  413. public string subPlatform;
  414. public int serverId;
  415. public string serverName;
  416. public string roleId;
  417. public string deviceId;
  418. public string platform;
  419. }
  420. private void SyncInfoToFairGuard()
  421. {
  422. if (m_GameRoleInfo != null)
  423. {
  424. DeviceSystemData systemData = GetDeviceSystemData();
  425. FairGuardData data = new FairGuardData()
  426. {
  427. openId = m_GameRoleInfo.openId,
  428. subPlatform = GetChannelName(),
  429. serverId = m_GameRoleInfo.serverId,
  430. serverName = m_GameRoleInfo.serverName,
  431. roleId = m_GameRoleInfo.roleId,
  432. deviceId = (systemData != null ? systemData.deviceId : SystemInfo.deviceUniqueIdentifier),
  433. platform = ((sdk == null || string.IsNullOrEmpty(sdk.SDKName)) ? "" : sdk.SDKName),
  434. };
  435. string extraStr = JsonUtility.ToJson(data);
  436. FairGuard.Init();
  437. FairGuard.setUserInfoEx(m_GameRoleInfo.roleName, m_GameRoleInfo.openId, m_GameRoleInfo.roleId, m_GameRoleInfo.serverName, GetChannelName(), extraStr);
  438. }
  439. }
  440. private void SyncInfoToBugly()
  441. {
  442. #if BUGLY
  443. if (m_GameRoleInfo == null) return;
  444. string userId = "";
  445. if (!string.IsNullOrEmpty (m_GameRoleInfo.openId))
  446. {
  447. userId = userId + m_GameRoleInfo.openId;
  448. }
  449. userId = userId + "_" + m_GameRoleInfo.serverId;
  450. BuglyAgent.SetUserId (userId);
  451. #endif
  452. }
  453. }
  454. public class MethodMaker
  455. {
  456. /// <summary>
  457. /// 创建对象(当前程序集)
  458. /// </summary>
  459. /// <param name="typeName">类型名</param>
  460. /// <returns>创建的对象,失败返回 null</returns>
  461. public static object CreateObject(string typeName)
  462. {
  463. object obj = null;
  464. try
  465. {
  466. Type objType = Type.GetType(typeName, true);
  467. obj = Activator.CreateInstance(objType);
  468. }
  469. catch (Exception ex)
  470. {
  471. Debug.Log(ex);
  472. }
  473. return obj;
  474. }
  475. /// <summary>
  476. /// 创建对象(外部程序集)
  477. /// </summary>
  478. /// <param name="path"></param>
  479. /// <param name="typeName">类型名</param>
  480. /// <returns>创建的对象,失败返回 null</returns>
  481. public static object CreateObject(string path, string typeName)
  482. {
  483. object obj = null;
  484. try
  485. {
  486. obj = Assembly.Load(path).CreateInstance(typeName);
  487. }
  488. catch (Exception ex)
  489. {
  490. //Debug.Write(ex);
  491. }
  492. return obj;
  493. }
  494. }