Auxiliary.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 GetUnit(Unit unit, int value)
  254. //{
  255. // if (value == 1)
  256. // {
  257. // }
  258. // else
  259. // {
  260. // }
  261. //}
  262. public static string GetStreamPath()
  263. {
  264. if (Application.isEditor)
  265. {
  266. return "file://" + Application.streamingAssetsPath;
  267. }
  268. else if (Application.platform == RuntimePlatform.IPhonePlayer)
  269. {
  270. return "file://" + Application.streamingAssetsPath;
  271. }
  272. else if (Application.isMobilePlatform)
  273. {
  274. return Application.streamingAssetsPath;
  275. }
  276. else
  277. {
  278. throw new Exception();
  279. }
  280. }
  281. public void DelayCall(UnityAction function, int frame)
  282. {
  283. StartCoroutine(IDelayCall(function, frame));
  284. }
  285. public void DelayCall(UnityAction function, float time)
  286. {
  287. StartCoroutine(IDelayCall(function, time));
  288. }
  289. public static IEnumerator IDelayCall(UnityAction function, int frame)
  290. {
  291. int count = 0;
  292. while (count < frame)
  293. {
  294. count++;
  295. yield return null;
  296. }
  297. function.Invoke();
  298. }
  299. public static IEnumerator IDelayCall(UnityAction function, float time)
  300. {
  301. yield return new WaitForSeconds(time);
  302. function.Invoke();
  303. }
  304. public static string FmlParse1(string str)
  305. {
  306. int index = 0;
  307. int openIndex = 0;
  308. bool flag = false;
  309. bool group = false;
  310. for (int i = 0; i < str.Length; i++)
  311. {
  312. if (group)
  313. {
  314. if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
  315. {
  316. str = str.Replace(openIndex, i - 1, FmlParse2(str[index], str.Between(openIndex, index - 1), str.Between(index + 1, i - 1)));
  317. i = 0;
  318. group = false;
  319. }
  320. else if (i == str.Length - 1)
  321. {
  322. str = str.Replace(openIndex, i, FmlParse2(str[index], str.Between(openIndex, index - 1), str.Between(index + 1, i)));
  323. break;
  324. }
  325. }
  326. else
  327. {
  328. if (str[i] == '+' || str[i] == '-')
  329. {
  330. flag = true;
  331. openIndex = i + 1;
  332. }
  333. else if (str[i] == '*' || str[i] == '/')
  334. {
  335. index = i;
  336. group = true;
  337. if (!flag)
  338. {
  339. openIndex = 0;
  340. }
  341. }
  342. }
  343. }
  344. group = false;
  345. for (int i = 0; i < str.Length; i++)
  346. {
  347. if (group)
  348. {
  349. if (str[i] == '+' || str[i] == '-')
  350. {
  351. str = str.Replace(0, i - 1, FmlParse2(str[index], str.Between(0, index - 1), str.Between(index + 1, i - 1)));
  352. i = -1;
  353. group = false;
  354. }
  355. else if (i == str.Length - 1)
  356. {
  357. return FmlParse2(str[index], str.Between(0, index - 1), str.Between(index + 1, i));
  358. }
  359. }
  360. else
  361. {
  362. if (str[i] == '+' || str[i] == '-')
  363. {
  364. index = i;
  365. group = true;
  366. }
  367. }
  368. }
  369. return str;
  370. }
  371. public static string FmlParse2(char op, string str1, string str2)
  372. {
  373. double var1 = double.Parse(str1);
  374. double var2 = double.Parse(str2);
  375. if (op == '+')
  376. {
  377. return (var1 + var2).ToString();
  378. }
  379. else if (op == '-')
  380. {
  381. return (var1 - var2).ToString();
  382. }
  383. else if (op == '*')
  384. {
  385. return (var1 * var2).ToString();
  386. }
  387. else if (op == '/')
  388. {
  389. return (var1 / var2).ToString();
  390. }
  391. else
  392. {
  393. throw new Exception(op.ToString());
  394. }
  395. }
  396. public static double FmlParse(string str, params string[] strings)
  397. {
  398. for (int i = 0; i < strings.Length; i += 2)
  399. {
  400. if (str.Contains(strings[i]))
  401. {
  402. str = str.Replace(strings[i], strings[i + 1]);
  403. }
  404. else
  405. {
  406. //Debug.Log("No such var " + strings[i + 1]);
  407. }
  408. }
  409. int openIndex = 0;
  410. for (int i = 0; i < str.Length; i++)
  411. {
  412. if (str[i] == '(')
  413. {
  414. openIndex = i;
  415. }
  416. else if (str[i] == ')')
  417. {
  418. str = str.Replace(openIndex, i, FmlParse1(str.Between(openIndex + 1, i - 1)));
  419. i = -1;
  420. }
  421. }
  422. return double.Parse(FmlParse1(str));
  423. }
  424. public static string OmitNumberStr(int accuray, string str)
  425. {
  426. int index = str.IndexOf('.');
  427. if (index == -1)
  428. {
  429. return str;
  430. }
  431. if (accuray == 0)
  432. {
  433. return str.Substring(0, index);
  434. }
  435. else
  436. {
  437. return str.Substring(0, index + accuray + 1);
  438. }
  439. }
  440. public static long ShrinkNumber(double value, int accuracy = 1)
  441. {
  442. long longValue = (long) value;
  443. if (ManaLan.CurrentLan == Lan.English)
  444. {
  445. if (longValue < Amount.千)
  446. {
  447. return longValue;
  448. }
  449. else if (longValue < Amount.百万)
  450. {
  451. long temp = (long) Amount.百;
  452. return (longValue/ temp) * temp;
  453. }
  454. else if (longValue < Amount.十亿)
  455. {
  456. long temp = (long)Amount.十万;
  457. return (longValue / temp) * temp;
  458. }
  459. else if (longValue < Amount.万亿)
  460. {
  461. long temp = (long)Amount.亿;
  462. return (longValue / temp) * temp;
  463. }
  464. else if (longValue < Amount.千万亿)
  465. {
  466. long temp = (long)Amount.千亿;
  467. return (longValue / temp) * temp;
  468. }
  469. }
  470. else if (ManaLan.CurrentLan == Lan.ChineseSimplified || ManaLan.CurrentLan == Lan.ChineseTraditional)
  471. {
  472. if (longValue < Amount.万)
  473. {
  474. return longValue;
  475. }
  476. else if (longValue < Amount.亿)
  477. {
  478. long temp = (long)Amount.千;
  479. return (longValue / temp) * temp;
  480. }
  481. else if (longValue < Amount.万亿)
  482. {
  483. long temp = (long)Amount.千万;
  484. return (longValue / temp) * temp;
  485. }
  486. else if (longValue < Amount.万万亿)
  487. {
  488. long temp = (long)Amount.千亿;
  489. return (longValue / temp) * temp;
  490. }
  491. }
  492. throw new Exception();
  493. }
  494. public static string ShrinkNumberStr(double value, int accuracy = 1)
  495. {
  496. long longValue = (long) value;
  497. if (ManaLan.CurrentLan == Lan.English)
  498. {
  499. if (longValue < Amount.千)
  500. {
  501. return longValue.ToString();
  502. }
  503. else if (longValue < Amount.百万)
  504. {
  505. string str = (longValue / Amount.千).ToString();
  506. return OmitNumberStr(accuracy, str) + "K";
  507. }
  508. else if (longValue < Amount.十亿)
  509. {
  510. string str = (longValue / Amount.百万).ToString();
  511. return OmitNumberStr(accuracy, str) + "M";
  512. }
  513. else if (longValue < Amount.万亿)
  514. {
  515. string str = (longValue / Amount.十亿).ToString();
  516. return OmitNumberStr(accuracy, str) + "B";
  517. }
  518. else if (longValue < Amount.千万亿)
  519. {
  520. string str = (longValue / Amount.万亿).ToString();
  521. return OmitNumberStr(accuracy, str) + "T";
  522. }
  523. }
  524. else if (ManaLan.CurrentLan == Lan.ChineseSimplified)
  525. {
  526. if (longValue < Amount.万)
  527. {
  528. return longValue.ToString();
  529. }
  530. else if (longValue < Amount.亿)
  531. {
  532. string str = (longValue/Amount.万).ToString();
  533. return OmitNumberStr(accuracy, str) + "万";
  534. }
  535. else if (longValue < Amount.万亿)
  536. {
  537. string str = (longValue / Amount.亿).ToString();
  538. return OmitNumberStr(accuracy, str) + "亿";
  539. }
  540. else if (longValue < Amount.万万亿)
  541. {
  542. string str = (longValue / Amount.万亿).ToString();
  543. return OmitNumberStr(accuracy, str) + "万亿";
  544. }
  545. }
  546. else if (ManaLan.CurrentLan == Lan.ChineseTraditional)
  547. {
  548. if (longValue < Amount.万)
  549. {
  550. return longValue.ToString();
  551. }
  552. else if (longValue < Amount.亿)
  553. {
  554. string str = (longValue / Amount.万).ToString();
  555. return OmitNumberStr(accuracy, str) + "萬";
  556. }
  557. else if (longValue < Amount.万亿)
  558. {
  559. string str = (longValue / Amount.亿).ToString();
  560. return OmitNumberStr(accuracy, str) + "億";
  561. }
  562. else if (longValue < Amount.万万亿)
  563. {
  564. string str = (longValue / Amount.万亿).ToString();
  565. return OmitNumberStr(accuracy, str) + "萬億";
  566. }
  567. }
  568. throw new Exception();
  569. }
  570. public static int IntParse(string str, int defaultValue)
  571. {
  572. if (string.IsNullOrEmpty(str))
  573. {
  574. return defaultValue;
  575. }
  576. else
  577. {
  578. return int.Parse(str);
  579. }
  580. }
  581. public static long LongParse(string str, long defaultValue)
  582. {
  583. if (string.IsNullOrEmpty(str))
  584. {
  585. return defaultValue;
  586. }
  587. else
  588. {
  589. return long.Parse(str);
  590. }
  591. }
  592. public static bool BoolParse(string str, bool defaultValue)
  593. {
  594. if (string.IsNullOrEmpty(str))
  595. {
  596. return defaultValue;
  597. }
  598. else
  599. {
  600. return Convert.ToBoolean(int.Parse(str));
  601. }
  602. }
  603. public static float FloatParse(string str, float defaultValue)
  604. {
  605. if (string.IsNullOrEmpty(str))
  606. {
  607. return defaultValue;
  608. }
  609. else
  610. {
  611. return float.Parse(str);
  612. }
  613. }
  614. public static double DoubleParse(string str, double defaultValue)
  615. {
  616. if (string.IsNullOrEmpty(str))
  617. {
  618. return defaultValue;
  619. }
  620. else
  621. {
  622. return float.Parse(str);
  623. }
  624. }
  625. public static List<int> IntListParse(char cah, string str, List<int> defaultValue)
  626. {
  627. if (string.IsNullOrEmpty(str))
  628. {
  629. return defaultValue;
  630. }
  631. else
  632. {
  633. string[] strings = str.Split(cah);
  634. List<int> list = new List<int>();
  635. for (int i = 0; i < strings.Length; i++)
  636. {
  637. list.Add(int.Parse(strings[i]));
  638. }
  639. return list;
  640. }
  641. }
  642. public static List<string> StringListParse(char cah, string str, List<string> defaultValue)
  643. {
  644. if (string.IsNullOrEmpty(str))
  645. {
  646. return defaultValue;
  647. }
  648. else
  649. {
  650. string[] strings = str.Split(cah);
  651. List<string> list = new List<string>();
  652. for (int i = 0; i < strings.Length; i++)
  653. {
  654. list.Add(strings[i]);
  655. }
  656. return list;
  657. }
  658. }
  659. public static List<double> DoubleListParse(char cah, string str, List<double> defaultValue)
  660. {
  661. if (string.IsNullOrEmpty(str))
  662. {
  663. return defaultValue;
  664. }
  665. else
  666. {
  667. string[] strings = str.Split(cah);
  668. List<double> list = new List<double>();
  669. for (int i = 0; i < strings.Length; i++)
  670. {
  671. list.Add(double.Parse(strings[i]));
  672. }
  673. return list;
  674. }
  675. }
  676. }