ShareSDKConfigEditor.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using UnityEditor;
  8. using UnityEngine;
  9. using cn.mob.unity3d.sdkporter;
  10. using System.Reflection;
  11. namespace cn.sharesdk.unity3d
  12. {
  13. #if UNITY_IOS
  14. [CustomEditor(typeof(ShareSDK))]
  15. [ExecuteInEditMode]
  16. public class ShareSDKConfigEditor : Editor
  17. {
  18. string appKey = "";
  19. string appSecret = "";
  20. string mobNetLater = "2";
  21. string mobTwitterVer = "2";
  22. Hashtable platformConfList;
  23. List<string> associatedDomains = new List<string>();
  24. public ShareSDKConfigEditor()
  25. {
  26. SetPlatformConfList ();
  27. }
  28. void Awake()
  29. {
  30. Prepare ();
  31. }
  32. //导出时会自动触发一次此方法
  33. public void OnDisable()
  34. {
  35. var obj = target as ShareSDK;
  36. if (obj != null)
  37. {
  38. if (obj.customAssociatedDomains != null && obj.customAssociatedDomains.Count > 0)
  39. {
  40. associatedDomains.AddRange(obj.customAssociatedDomains);
  41. }
  42. appKey = obj.appKey;
  43. appSecret = obj.appSecret;
  44. mobNetLater = obj.mobNetLater;
  45. mobTwitterVer = obj.mobTwitterVer;
  46. Save();
  47. checkPlatforms(obj.devInfo);
  48. }
  49. Debug.LogWarning ("ShareSDK OnDisable");
  50. }
  51. private void SetPlatformConfList()
  52. {
  53. platformConfList = new Hashtable();
  54. platformConfList.Add ((int)PlatformType.AliSocial,"app_id");
  55. platformConfList.Add((int)PlatformType.AliSocialMoments,"app_id");
  56. platformConfList.Add ((int)PlatformType.Tumblr,"consumer_key");
  57. platformConfList.Add ((int)PlatformType.VKontakte,"application_id");
  58. platformConfList.Add ((int)PlatformType.Pinterest,"client_id");
  59. platformConfList.Add ((int)PlatformType.KakaoTalk,"app_key");
  60. platformConfList.Add((int)PlatformType.KakaoStory, "app_key");
  61. platformConfList.Add ((int)PlatformType.KakaoPlatform,"app_key");
  62. platformConfList.Add((int)PlatformType.Pocket, "redirect_uri");
  63. platformConfList.Add((int)PlatformType.Dingding, "authApp_id");
  64. platformConfList.Add((int)PlatformType.DingdingShare, "shareApp_id");
  65. platformConfList.Add ((int)PlatformType.Facebook,"api_key");
  66. platformConfList.Add ((int)PlatformType.FacebookMessenger,"api_key");
  67. platformConfList.Add ((int)PlatformType.QZone,"app_id");
  68. platformConfList.Add ((int)PlatformType.QQ,"app_id");
  69. platformConfList.Add ((int)PlatformType.QQPlatform,"app_id");
  70. platformConfList.Add((int)PlatformType.MeiPai, "app_key");
  71. platformConfList.Add ((int)PlatformType.WeChat,"app_id");
  72. platformConfList.Add ((int)PlatformType.WeChatMoments,"app_id");
  73. platformConfList.Add ((int)PlatformType.WeChatFavorites,"app_id");
  74. platformConfList.Add((int)PlatformType.WechatPlatform, "app_id");
  75. platformConfList.Add((int)PlatformType.GooglePlus, "client_id");
  76. platformConfList.Add ((int)PlatformType.YiXinSession,"app_id");
  77. platformConfList.Add ((int)PlatformType.YiXinTimeline,"app_id");
  78. platformConfList.Add ((int)PlatformType.YiXinFav,"app_id");
  79. platformConfList.Add ((int)PlatformType.YixinPlatform,"app_id");
  80. platformConfList.Add ((int)PlatformType.SinaWeibo,"app_key");
  81. platformConfList.Add((int)PlatformType.CMCC, "app_id");
  82. platformConfList.Add ((int)PlatformType.Twitter,"consumer_key");
  83. platformConfList.Add ((int)PlatformType.Line, "channel_id");
  84. platformConfList.Add((int)PlatformType.FacebookAccount, "app_id");
  85. platformConfList.Add((int)PlatformType.Douyin, "app_key");
  86. platformConfList.Add((int)PlatformType.WeWork, "app_Key");
  87. platformConfList.Add((int)PlatformType.Oasis, "app_key");
  88. platformConfList.Add((int)PlatformType.SnapChat, "client_id");
  89. platformConfList.Add((int)PlatformType.TikTok, "app_key");
  90. platformConfList.Add((int)PlatformType.KuaiShou, "app_id");
  91. platformConfList.Add((int)PlatformType.Youtube, "client_id");
  92. }
  93. private void Prepare()
  94. {
  95. try
  96. {
  97. var files = System.IO.Directory.GetFiles(Application.dataPath , "MOB.keypds", System.IO.SearchOption.AllDirectories);
  98. string filePath = files [0];
  99. FileInfo projectFileInfo = new FileInfo( filePath );
  100. if(projectFileInfo.Exists)
  101. {
  102. StreamReader sReader = projectFileInfo.OpenText();
  103. string contents = sReader.ReadToEnd();
  104. sReader.Close();
  105. sReader.Dispose();
  106. Hashtable datastore = (Hashtable)MiniJSON.jsonDecode( contents );
  107. appKey = (string)datastore["MobAppKey"];
  108. appSecret = (string)datastore["MobAppSecret"];
  109. mobNetLater = (string)datastore["MOBNetLater"];
  110. mobTwitterVer = (string)datastore["MOBTwitterVer"];
  111. }
  112. else
  113. {
  114. Debug.LogWarning ("MOB.keypds no find");
  115. }
  116. }
  117. catch(Exception e)
  118. {
  119. if(appKey.Length == 0)
  120. {
  121. appKey = "moba6b6c6d6";
  122. appSecret = "b89d2427a3bc7ad1aea1e1e8c1d36bf3";
  123. mobNetLater = "2";
  124. mobTwitterVer = "2";
  125. }
  126. Debug.LogException (e);
  127. }
  128. }
  129. private void Save()
  130. {
  131. try
  132. {
  133. var files = System.IO.Directory.GetFiles(Application.dataPath , "MOB.keypds", System.IO.SearchOption.AllDirectories);
  134. string filePath = files [0];
  135. if(File.Exists(filePath))
  136. {
  137. Hashtable datastore = new Hashtable();
  138. datastore["MobAppKey"] = appKey;
  139. datastore["MobAppSecret"] = appSecret;
  140. datastore["MOBNetLater"] = mobNetLater;
  141. var json = MiniJSON.jsonEncode(datastore);
  142. StreamWriter sWriter = new StreamWriter(filePath);
  143. sWriter.WriteLine(json);
  144. sWriter.Close();
  145. sWriter.Dispose();
  146. }
  147. else
  148. {
  149. Debug.LogWarning ("MOB.keypds no find");
  150. }
  151. }
  152. catch(Exception e)
  153. {
  154. Debug.LogWarning ("error");
  155. Debug.LogException (e);
  156. }
  157. }
  158. private void savePlatformInfoWithId(int platformId, DevInfo info, Hashtable deviceInfo) {
  159. if (platformId == (int)PlatformType.Line)
  160. {
  161. Hashtable platformInfo = (Hashtable)deviceInfo[platformId];
  162. if (deviceInfo.ContainsKey(platformId) == false)
  163. {
  164. platformInfo = new Hashtable();
  165. deviceInfo.Add(platformId, platformInfo);
  166. }
  167. string universalLink = (string)info.GetType().GetField("app_universalLink").GetValue(info);
  168. platformInfo.Add("universalLink", universalLink);
  169. } else if (platformId == (int)PlatformType.SnapChat) {
  170. Hashtable platformInfo = (Hashtable)deviceInfo[platformId];
  171. if (deviceInfo.ContainsKey(platformId) == false)
  172. {
  173. platformInfo = new Hashtable();
  174. deviceInfo.Add(platformId, platformInfo);
  175. }
  176. string redirect_uri = (string)info.GetType().GetField("redirect_uri").GetValue(info);
  177. Debug.Log(redirect_uri);
  178. platformInfo.Add("redirect_uri", redirect_uri);
  179. }
  180. }
  181. //shareSDK
  182. private void checkPlatforms(DevInfoSet devInfo)
  183. {
  184. Type type = devInfo.GetType();
  185. FieldInfo[] devInfoFields = type.GetFields();
  186. Hashtable enablePlatforms = new Hashtable();
  187. Hashtable deviceInfoPlatforms = new Hashtable();
  188. foreach (FieldInfo devInfoField in devInfoFields)
  189. {
  190. DevInfo info = (DevInfo) devInfoField.GetValue(devInfo);
  191. if (info.Enable)
  192. {
  193. int platformId = (int) info.GetType().GetField("type").GetValue(info);
  194. string appkey = GetAPPKey (info,platformId);
  195. enablePlatforms.Add (platformId,appkey);
  196. savePlatformInfoWithId(platformId, info, deviceInfoPlatforms);
  197. if (info.GetType().GetField("app_universalLink") != null)
  198. {
  199. string app_universalLink = GetValueByName(info, "app_universalLink");
  200. if (app_universalLink != null && app_universalLink.Length > 0)
  201. {
  202. Uri uri = new Uri(app_universalLink);
  203. var appLinkHost = uri.Host;
  204. string totalLink = "applinks:" + appLinkHost;
  205. if (associatedDomains.Contains(totalLink) == false)
  206. {
  207. associatedDomains.Add(totalLink);
  208. }
  209. }
  210. }
  211. }
  212. }
  213. var files = System.IO.Directory.GetFiles(Application.dataPath , "ShareSDK.mobpds", System.IO.SearchOption.AllDirectories);
  214. string filePath = files [0];
  215. FileInfo projectFileInfo = new FileInfo( filePath );
  216. if (projectFileInfo.Exists)
  217. {
  218. StreamReader sReader = projectFileInfo.OpenText();
  219. string contents = sReader.ReadToEnd();
  220. sReader.Close();
  221. sReader.Dispose();
  222. Hashtable datastore = (Hashtable)MiniJSON.jsonDecode( contents );
  223. if (datastore.ContainsKey ("ShareSDKPlatforms"))
  224. {
  225. datastore["ShareSDKPlatforms"] = enablePlatforms;
  226. }
  227. else
  228. {
  229. datastore.Add ("ShareSDKPlatforms",enablePlatforms);
  230. }
  231. if (datastore.ContainsKey("ShareSDKDeviceInfo"))
  232. {
  233. datastore["ShareSDKDeviceInfo"] = deviceInfoPlatforms;
  234. }
  235. else
  236. {
  237. datastore.Add("ShareSDKDeviceInfo", deviceInfoPlatforms);
  238. }
  239. Debug.LogWarning(associatedDomains.ToArray());
  240. var associatedDomains_t = associatedDomains.Distinct();
  241. if (datastore.ContainsKey("AssociatedDomains"))
  242. {
  243. datastore["AssociatedDomains"] = associatedDomains_t.ToArray();
  244. }
  245. else
  246. {
  247. datastore.Add("AssociatedDomains", associatedDomains_t.ToArray());
  248. }
  249. var json = MiniJSON.jsonEncode(datastore);
  250. StreamWriter sWriter = new StreamWriter(filePath);
  251. sWriter.WriteLine(json);
  252. sWriter.Close();
  253. sWriter.Dispose();
  254. }
  255. }
  256. private string GetValueByName(DevInfo devInfoField,string valueName)
  257. {
  258. return (string)devInfoField.GetType ().GetField (valueName).GetValue (devInfoField);
  259. }
  260. private string GetAPPKey (DevInfo devInfoField, int platformId)
  261. {
  262. string valueName = (string)platformConfList[platformId];
  263. if(valueName == null)
  264. {
  265. return "";
  266. }
  267. return GetValueByName (devInfoField, valueName);
  268. }
  269. }
  270. [CustomEditor(typeof(ShareSDKRestoreScene))]
  271. [ExecuteInEditMode]
  272. public class ShareSDKRestoreSceneEditor : Editor
  273. {
  274. public ShareSDKRestoreSceneEditor()
  275. {
  276. }
  277. void Awake()
  278. {
  279. }
  280. //导出时会自动触发一次此方法
  281. public void OnDisable()
  282. {
  283. var restoreSceneObj = target as ShareSDKRestoreScene;
  284. if (restoreSceneObj != null)
  285. {
  286. checkRestoreScene(restoreSceneObj.restoreSceneConfig);
  287. }
  288. //Debug.LogWarning("ShareSDKRestoreScene OnDisable");
  289. }
  290. private void checkRestoreScene(RestoreSceneConfigure restoreSceneConfig)
  291. {
  292. Hashtable enableRestoreScene = new Hashtable();
  293. if (restoreSceneConfig != null && restoreSceneConfig.Enable)
  294. {
  295. enableRestoreScene.Add("open", "1");
  296. if (restoreSceneConfig.capabilititesAssociatedDomain != null)
  297. {
  298. enableRestoreScene.Add("Capabilitites_AssociatedDomain", restoreSceneConfig.capabilititesAssociatedDomain);
  299. enableRestoreScene.Add("Capabilitites_EntitlementsPath", restoreSceneConfig.capabilititesEntitlementsPathInXcode);
  300. }
  301. else
  302. {
  303. enableRestoreScene.Add("Capabilitites_AssociatedDomain", "");
  304. enableRestoreScene.Add("Capabilitites_EntitlementsPath", "");
  305. }
  306. }
  307. var files = System.IO.Directory.GetFiles(Application.dataPath, "ShareSDK.mobpds", System.IO.SearchOption.AllDirectories);
  308. string filePath = files[0];
  309. FileInfo projectFileInfo = new FileInfo(filePath);
  310. if (projectFileInfo.Exists)
  311. {
  312. StreamReader sReader = projectFileInfo.OpenText();
  313. string contents = sReader.ReadToEnd();
  314. sReader.Close();
  315. sReader.Dispose();
  316. Hashtable datastore = (Hashtable)MiniJSON.jsonDecode(contents);
  317. if (datastore.ContainsKey("ShareSDKRestoreScene"))
  318. {
  319. datastore["ShareSDKRestoreScene"] = enableRestoreScene;
  320. }
  321. else
  322. {
  323. datastore.Add("ShareSDKRestoreScene", enableRestoreScene);
  324. }
  325. var json = MiniJSON.jsonEncode(datastore);
  326. StreamWriter sWriter = new StreamWriter(filePath);
  327. sWriter.WriteLine(json);
  328. sWriter.Close();
  329. sWriter.Dispose();
  330. }
  331. }
  332. }
  333. #endif
  334. }