Auxiliary.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. using LitJson;
  2. using DragonBones;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Events;
  6. using System;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net.Mail;
  10. using System.Text;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Net;
  15. using System.Security.Cryptography;
  16. using System.Runtime.Serialization.Formatters.Binary;
  17. using System.Security;
  18. using System.Security.Cryptography.Xml;
  19. using System.Xml;
  20. using UnityEngine.Advertisements;
  21. using Random = UnityEngine.Random;
  22. using Transform = UnityEngine.Transform;
  23. //public enum Unit
  24. //{
  25. // Second
  26. //}
  27. public class Amount
  28. {
  29. public static double 百 = 100;
  30. public static double 千 = 1000;
  31. public static double 万 = 10000;
  32. public static double 十万 = 100000;
  33. public static double 百万 = 1000000;
  34. public static double 千万 = 10000000;
  35. public static double 亿 = 100000000;
  36. public static double 十亿 = 1000000000;
  37. public static double 千亿 = 100000000000;
  38. public static double 万亿 = 1000000000000;
  39. public static double 千万亿 = 1000000000000000;
  40. public static double 万万亿 = 10000000000000000;
  41. }
  42. public class Auxiliary : Regist
  43. {
  44. #region 变量
  45. public string String;
  46. public Font Font;
  47. public GameObject Go;
  48. public List<GameObject> GoList;
  49. public List<SpriteRenderer> SrList;
  50. public static bool AnyKeyUp;
  51. public static bool AnyKeyDown;
  52. public static Auxiliary Instance;
  53. public static MD5 MD5
  54. {
  55. get
  56. {
  57. if (_MD5 == null)
  58. {
  59. _MD5 = new MD5CryptoServiceProvider();
  60. }
  61. return _MD5;
  62. }
  63. set { _MD5 = value; }
  64. }
  65. public static DESCryptoServiceProvider Des
  66. {
  67. get
  68. {
  69. if (_Des == null)
  70. {
  71. _Des = new DESCryptoServiceProvider();
  72. _Des.IV = Encoding.UTF8.GetBytes("19283740");
  73. _Des.Key = Encoding.UTF8.GetBytes("93287123");
  74. }
  75. return _Des;
  76. }
  77. set { _Des = value; }
  78. }
  79. private static MD5 _MD5;
  80. private static DESCryptoServiceProvider _Des;
  81. #endregion
  82. public override bool RegistImmed()
  83. {
  84. if (base.RegistImmed())
  85. {
  86. return true;
  87. }
  88. enabled = true;
  89. return false;
  90. }
  91. private void Awake()
  92. {
  93. Instance = this;
  94. }
  95. private void Update()
  96. {
  97. #region 调试
  98. if (Input.GetKeyDown(KeyCode.Escape))
  99. {
  100. }
  101. if (Input.GetKeyDown(KeyCode.Z))
  102. {
  103. }
  104. if (Input.GetKeyDown(KeyCode.X))
  105. {
  106. }
  107. if (Input.GetKeyDown(KeyCode.C))
  108. {
  109. }
  110. if (Input.GetKeyDown(KeyCode.V))
  111. {
  112. }
  113. //if (Input.GetKeyDown(KeyCode.P))
  114. //{
  115. // ManaData.Level += 20;
  116. //}
  117. //if (Input.GetKeyDown(KeyCode.O))
  118. //{
  119. // for (int i = 0; i < ManaData.SkillList.Count; i++)
  120. // {
  121. // ManaData.SkillList[i].ReceiveCool(1, true, true);
  122. // }
  123. //}
  124. //if (Input.GetKeyDown(KeyCode.I))
  125. //{
  126. // ManaData.Coin += 1000;
  127. // ManaData.Diamond += 1000;
  128. //}
  129. //if (Input.GetKeyDown(KeyCode.U))
  130. //{
  131. // Data.SavePlayerConfig();
  132. // Data.SaveXml();
  133. //}
  134. //if (Input.GetKeyDown(KeyCode.Y))
  135. //{
  136. // Data.ResetPlayerConfig();
  137. // Data.SaveXml();
  138. //}
  139. //if (Input.GetKeyDown(KeyCode.T))
  140. //{
  141. // ManaData.Pause = !ManaData.Pause;
  142. //}
  143. #endregion
  144. #region 输入检测
  145. if (Input.GetKeyDown(KeyCode.Escape))
  146. {
  147. ManaMiniGame.Pause = true;
  148. Bubble.Show(Language.GetStr("Common", "QuitGame"), null, null, Application.Quit, () => { ManaMiniGame.Pause = false; });
  149. }
  150. if (AnyKeyUp)
  151. {
  152. if (Input.anyKey == false)
  153. {
  154. AnyKeyUp = false;
  155. }
  156. }
  157. if (Input.anyKeyDown)
  158. {
  159. AnyKeyDown = true;
  160. }
  161. if (AnyKeyDown)
  162. {
  163. if (Input.anyKey == false)
  164. {
  165. AnyKeyUp = true;
  166. AnyKeyDown = false;
  167. }
  168. }
  169. #endregion
  170. }
  171. public static byte[] GetMD5(object obj)
  172. {
  173. MemoryStream memoryStream = new MemoryStream();
  174. BinaryFormatter binaryFormatter = new BinaryFormatter();
  175. binaryFormatter.Serialize(memoryStream, obj);
  176. byte[] bytes = memoryStream.GetBuffer();
  177. memoryStream.Dispose();
  178. return MD5.ComputeHash(bytes);
  179. }
  180. public static void EncryptXml(XmlDocument doc)
  181. {
  182. EncryptedXml encryptedXml = new EncryptedXml();
  183. XmlElement xmlElement = doc.DocumentElement;
  184. byte[] bytes = encryptedXml.EncryptData(xmlElement, Des, false);
  185. EncryptedData encryptedData = new EncryptedData();
  186. encryptedData.Type = EncryptedXml.XmlEncElementUrl;
  187. encryptedData.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncDESUrl);
  188. encryptedData.CipherData.CipherValue = bytes;
  189. EncryptedXml.ReplaceElement(xmlElement, encryptedData, false);
  190. }
  191. public static void DecryptXml(XmlDocument doc)
  192. {
  193. EncryptedXml encryptedXml = new EncryptedXml();
  194. XmlElement encryptXmlElement = (XmlElement)doc.GetElementsByTagName("EncryptedData")[0];
  195. EncryptedData encryptedData = new EncryptedData();
  196. encryptedData.LoadXml(encryptXmlElement);
  197. byte[] bytes = encryptedXml.DecryptData(encryptedData, Des);
  198. encryptedXml.ReplaceData(encryptXmlElement, bytes);
  199. }
  200. public static byte[] Encrypt(string str)
  201. {
  202. byte[] bytes = Encoding.UTF8.GetBytes(str);
  203. byte[] encryptBytes = Des.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
  204. return encryptBytes;
  205. }
  206. public static byte[] ToBytes(string str)
  207. {
  208. string[] strings = str.Split(' ');
  209. byte[] bytes = new byte[strings.Length];
  210. for (int i = 0; i < bytes.Length; i++)
  211. {
  212. bytes[i] = byte.Parse(strings[i]);
  213. }
  214. return bytes;
  215. }
  216. public static string Decrypt(string str)
  217. {
  218. byte[] encryptBytes = ToBytes(str);
  219. byte[] decryptBytes = Des.CreateDecryptor().TransformFinalBlock(encryptBytes, 0, encryptBytes.Length);
  220. return Encoding.UTF8.GetString(decryptBytes);
  221. }
  222. public static string ToString(byte[] bytes)
  223. {
  224. StringBuilder strB = new StringBuilder();
  225. for (int i = 0; i < bytes.Length; i++)
  226. {
  227. if (i == bytes.Length - 1)
  228. {
  229. strB.Append(bytes[i]);
  230. }
  231. else
  232. {
  233. strB.Append(bytes[i] + " ");
  234. }
  235. }
  236. return strB.ToString();
  237. }
  238. public static void CompileDic(Transform tra, Dictionary<string, Transform> dic)
  239. {
  240. Transform[] transforms = tra.GetComponentsInChildren<Transform>(true);
  241. for (int i = 0; i < transforms.Length; i++)
  242. {
  243. if (dic.ContainsKey(transforms[i].name))
  244. {
  245. throw new Exception(transforms[i].name);
  246. }
  247. else
  248. {
  249. dic.Add(transforms[i].name, transforms[i]);
  250. }
  251. }
  252. }
  253. public static string GetStreamPath()
  254. {
  255. if (Application.isEditor)
  256. {
  257. return "file://" + Application.streamingAssetsPath;
  258. }
  259. else if (Application.platform == RuntimePlatform.IPhonePlayer)
  260. {
  261. return "file://" + Application.streamingAssetsPath;
  262. }
  263. else if (Application.isMobilePlatform)
  264. {
  265. return Application.streamingAssetsPath;
  266. }
  267. else
  268. {
  269. throw new Exception();
  270. }
  271. }
  272. public void DelayCall(UnityAction function, int frame)
  273. {
  274. StartCoroutine(IDelayCall(function, frame));
  275. }
  276. public void DelayCall(UnityAction function, float time)
  277. {
  278. StartCoroutine(IDelayCall(function, time));
  279. }
  280. public static IEnumerator IDelayCall(UnityAction function, int frame)
  281. {
  282. int count = 0;
  283. while (count < frame)
  284. {
  285. count++;
  286. yield return null;
  287. }
  288. function.Invoke();
  289. }
  290. public static IEnumerator IDelayCall(UnityAction function, float time)
  291. {
  292. yield return new WaitForSeconds(time);
  293. function.Invoke();
  294. }
  295. public static string FmlParse1(string str)
  296. {
  297. int index = 0;
  298. int openIndex = 0;
  299. bool flag = false;
  300. bool group = false;
  301. for (int i = 0; i < str.Length; i++)
  302. {
  303. if (group)
  304. {
  305. if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
  306. {
  307. str = str.Replace(openIndex, i - 1, FmlParse2(str[index], str.Between(openIndex, index - 1), str.Between(index + 1, i - 1)));
  308. i = 0;
  309. group = false;
  310. }
  311. else if (i == str.Length - 1)
  312. {
  313. str = str.Replace(openIndex, i, FmlParse2(str[index], str.Between(openIndex, index - 1), str.Between(index + 1, i)));
  314. break;
  315. }
  316. }
  317. else
  318. {
  319. if (str[i] == '+' || str[i] == '-')
  320. {
  321. flag = true;
  322. openIndex = i + 1;
  323. }
  324. else if (str[i] == '*' || str[i] == '/')
  325. {
  326. index = i;
  327. group = true;
  328. if (!flag)
  329. {
  330. openIndex = 0;
  331. }
  332. }
  333. }
  334. }
  335. group = false;
  336. for (int i = 0; i < str.Length; i++)
  337. {
  338. if (group)
  339. {
  340. if (str[i] == '+' || str[i] == '-')
  341. {
  342. str = str.Replace(0, i - 1, FmlParse2(str[index], str.Between(0, index - 1), str.Between(index + 1, i - 1)));
  343. i = -1;
  344. group = false;
  345. }
  346. else if (i == str.Length - 1)
  347. {
  348. return FmlParse2(str[index], str.Between(0, index - 1), str.Between(index + 1, i));
  349. }
  350. }
  351. else
  352. {
  353. if (str[i] == '+' || str[i] == '-')
  354. {
  355. index = i;
  356. group = true;
  357. }
  358. }
  359. }
  360. return str;
  361. }
  362. public static string FmlParse2(char op, string str1, string str2)
  363. {
  364. double var1 = double.Parse(str1);
  365. double var2 = double.Parse(str2);
  366. if (op == '+')
  367. {
  368. return (var1 + var2).ToString();
  369. }
  370. else if (op == '-')
  371. {
  372. return (var1 - var2).ToString();
  373. }
  374. else if (op == '*')
  375. {
  376. return (var1 * var2).ToString();
  377. }
  378. else if (op == '/')
  379. {
  380. return (var1 / var2).ToString();
  381. }
  382. else
  383. {
  384. throw new Exception(op.ToString());
  385. }
  386. }
  387. public static double FmlParse(string str, params string[] strings)
  388. {
  389. for (int i = 0; i < strings.Length; i += 2)
  390. {
  391. if (str.Contains(strings[i]))
  392. {
  393. str = str.Replace(strings[i], strings[i + 1]);
  394. }
  395. else
  396. {
  397. //Debug.Log("No such var " + strings[i + 1]);
  398. }
  399. }
  400. int openIndex = 0;
  401. for (int i = 0; i < str.Length; i++)
  402. {
  403. if (str[i] == '(')
  404. {
  405. openIndex = i;
  406. }
  407. else if (str[i] == ')')
  408. {
  409. str = str.Replace(openIndex, i, FmlParse1(str.Between(openIndex + 1, i - 1)));
  410. i = -1;
  411. }
  412. }
  413. return double.Parse(FmlParse1(str));
  414. }
  415. public static string OmitNumberStr(int accuray, string str)
  416. {
  417. int index = str.IndexOf('.');
  418. if (index == -1)
  419. {
  420. return str;
  421. }
  422. if (accuray == 0)
  423. {
  424. return str.Substring(0, index);
  425. }
  426. else
  427. {
  428. return str.Substring(0, index + accuray + 1);
  429. }
  430. }
  431. public static long ShrinkNumber(double value, int accuracy = 1)
  432. {
  433. long longValue = (long) value;
  434. if (ManaLan.CurrentLan == Lan.English)
  435. {
  436. if (longValue < Amount.千)
  437. {
  438. return longValue;
  439. }
  440. else if (longValue < Amount.百万)
  441. {
  442. long temp = (long) Amount.百;
  443. return (longValue/ temp) * temp;
  444. }
  445. else if (longValue < Amount.十亿)
  446. {
  447. long temp = (long)Amount.十万;
  448. return (longValue / temp) * temp;
  449. }
  450. else if (longValue < Amount.万亿)
  451. {
  452. long temp = (long)Amount.亿;
  453. return (longValue / temp) * temp;
  454. }
  455. else if (longValue < Amount.千万亿)
  456. {
  457. long temp = (long)Amount.千亿;
  458. return (longValue / temp) * temp;
  459. }
  460. }
  461. else if (ManaLan.CurrentLan == Lan.ChineseSimplified || ManaLan.CurrentLan == Lan.ChineseTraditional)
  462. {
  463. if (longValue < Amount.万)
  464. {
  465. return longValue;
  466. }
  467. else if (longValue < Amount.亿)
  468. {
  469. long temp = (long)Amount.千;
  470. return (longValue / temp) * temp;
  471. }
  472. else if (longValue < Amount.万亿)
  473. {
  474. long temp = (long)Amount.千万;
  475. return (longValue / temp) * temp;
  476. }
  477. else if (longValue < Amount.万万亿)
  478. {
  479. long temp = (long)Amount.千亿;
  480. return (longValue / temp) * temp;
  481. }
  482. }
  483. throw new Exception();
  484. }
  485. public static string ShrinkNumberStr(double value, int accuracy = 1)
  486. {
  487. long longValue = (long) value;
  488. if (ManaLan.CurrentLan == Lan.English)
  489. {
  490. if (longValue < Amount.千)
  491. {
  492. return longValue.ToString();
  493. }
  494. else if (longValue < Amount.百万)
  495. {
  496. string str = (longValue / Amount.千).ToString();
  497. return OmitNumberStr(accuracy, str) + "K";
  498. }
  499. else if (longValue < Amount.十亿)
  500. {
  501. string str = (longValue / Amount.百万).ToString();
  502. return OmitNumberStr(accuracy, str) + "M";
  503. }
  504. else if (longValue < Amount.万亿)
  505. {
  506. string str = (longValue / Amount.十亿).ToString();
  507. return OmitNumberStr(accuracy, str) + "B";
  508. }
  509. else if (longValue < Amount.千万亿)
  510. {
  511. string str = (longValue / Amount.万亿).ToString();
  512. return OmitNumberStr(accuracy, str) + "T";
  513. }
  514. }
  515. else if (ManaLan.CurrentLan == Lan.ChineseSimplified)
  516. {
  517. if (longValue < Amount.万)
  518. {
  519. return longValue.ToString();
  520. }
  521. else if (longValue < Amount.亿)
  522. {
  523. string str = (longValue/Amount.万).ToString();
  524. return OmitNumberStr(accuracy, str) + "万";
  525. }
  526. else if (longValue < Amount.万亿)
  527. {
  528. string str = (longValue / Amount.亿).ToString();
  529. return OmitNumberStr(accuracy, str) + "亿";
  530. }
  531. else if (longValue < Amount.万万亿)
  532. {
  533. string str = (longValue / Amount.万亿).ToString();
  534. return OmitNumberStr(accuracy, str) + "万亿";
  535. }
  536. }
  537. else if (ManaLan.CurrentLan == Lan.ChineseTraditional)
  538. {
  539. if (longValue < Amount.万)
  540. {
  541. return longValue.ToString();
  542. }
  543. else if (longValue < Amount.亿)
  544. {
  545. string str = (longValue / Amount.万).ToString();
  546. return OmitNumberStr(accuracy, str) + "萬";
  547. }
  548. else if (longValue < Amount.万亿)
  549. {
  550. string str = (longValue / Amount.亿).ToString();
  551. return OmitNumberStr(accuracy, str) + "億";
  552. }
  553. else if (longValue < Amount.万万亿)
  554. {
  555. string str = (longValue / Amount.万亿).ToString();
  556. return OmitNumberStr(accuracy, str) + "萬億";
  557. }
  558. }
  559. throw new Exception();
  560. }
  561. public static int IntParse(string str, int defaultValue)
  562. {
  563. if (string.IsNullOrEmpty(str))
  564. {
  565. return defaultValue;
  566. }
  567. else
  568. {
  569. return int.Parse(str);
  570. }
  571. }
  572. public static long LongParse(string str, long defaultValue)
  573. {
  574. if (string.IsNullOrEmpty(str))
  575. {
  576. return defaultValue;
  577. }
  578. else
  579. {
  580. return long.Parse(str);
  581. }
  582. }
  583. public static bool BoolParse(string str, bool defaultValue)
  584. {
  585. if (string.IsNullOrEmpty(str))
  586. {
  587. return defaultValue;
  588. }
  589. else
  590. {
  591. return Convert.ToBoolean(int.Parse(str));
  592. }
  593. }
  594. public static float FloatParse(string str, float defaultValue)
  595. {
  596. if (string.IsNullOrEmpty(str))
  597. {
  598. return defaultValue;
  599. }
  600. else
  601. {
  602. return float.Parse(str);
  603. }
  604. }
  605. public static double DoubleParse(string str, double defaultValue)
  606. {
  607. if (string.IsNullOrEmpty(str))
  608. {
  609. return defaultValue;
  610. }
  611. else
  612. {
  613. return float.Parse(str);
  614. }
  615. }
  616. public static List<int> IntListParse(char cah, string str, List<int> defaultValue)
  617. {
  618. if (string.IsNullOrEmpty(str))
  619. {
  620. return defaultValue;
  621. }
  622. else
  623. {
  624. string[] strings = str.Split(cah);
  625. List<int> list = new List<int>();
  626. for (int i = 0; i < strings.Length; i++)
  627. {
  628. list.Add(int.Parse(strings[i]));
  629. }
  630. return list;
  631. }
  632. }
  633. public static List<string> StringListParse(char cah, string str, List<string> defaultValue)
  634. {
  635. if (string.IsNullOrEmpty(str))
  636. {
  637. return defaultValue;
  638. }
  639. else
  640. {
  641. string[] strings = str.Split(cah);
  642. List<string> list = new List<string>();
  643. for (int i = 0; i < strings.Length; i++)
  644. {
  645. list.Add(strings[i]);
  646. }
  647. return list;
  648. }
  649. }
  650. public static List<double> DoubleListParse(char cah, string str, List<double> defaultValue)
  651. {
  652. if (string.IsNullOrEmpty(str))
  653. {
  654. return defaultValue;
  655. }
  656. else
  657. {
  658. string[] strings = str.Split(cah);
  659. List<double> list = new List<double>();
  660. for (int i = 0; i < strings.Length; i++)
  661. {
  662. list.Add(double.Parse(strings[i]));
  663. }
  664. return list;
  665. }
  666. }
  667. }