SDKMgr.cs 16 KB

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