UtilTools.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Net;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. #if !COMBAT_SERVER
  8. using UnityEngine;
  9. #endif
  10. using System.IO;
  11. using System.Runtime.Serialization.Formatters.Binary;
  12. #if !COMBAT_SERVER
  13. using UnityEngine.Profiling;
  14. #endif
  15. using System.Security.Cryptography;
  16. using Excel2Json;
  17. using Fort23.UTool;
  18. namespace Utility
  19. {
  20. public static class UtilTools
  21. {
  22. #if !COMBAT_SERVER
  23. public static List<GameObject> allDontDestroyOnLoad = new List<GameObject>();
  24. #endif
  25. private static int m_seed = 0;
  26. private static StringBuilder sb;
  27. public static long oneDay = 86400000;
  28. public static int GenerateSeed()
  29. {
  30. m_seed++;
  31. return m_seed;
  32. }
  33. public static float FrameToDuration(int frame)
  34. {
  35. return frame * 0.02f;
  36. }
  37. public static string GetGUID()
  38. {
  39. System.Guid guid = new Guid();
  40. guid = Guid.NewGuid();
  41. string str = guid.ToString();
  42. return str;
  43. }
  44. #if !COMBAT_SERVER
  45. public static void SetDontDestroyOnLoad(GameObject go)
  46. {
  47. if (allDontDestroyOnLoad.Contains(go))
  48. {
  49. return;
  50. }
  51. GameObject.DontDestroyOnLoad(go);
  52. allDontDestroyOnLoad.Add(go);
  53. }
  54. public static int DurationToFrame(float duration)
  55. {
  56. return (int)(duration / Time.fixedDeltaTime);
  57. }
  58. #endif
  59. // public static int RoundToInt(float value)
  60. // {
  61. // return (int)System.Math.Round(value);
  62. // }
  63. #if !COMBAT_SERVER
  64. public static string CombineString(params string[] strs)
  65. {
  66. StringBuilder sb = new StringBuilder();
  67. for (int i = 0; i < strs.Length; i++)
  68. {
  69. sb.Append(strs[i]);
  70. }
  71. return sb.ToString();
  72. }
  73. public static string CombineString(params object[] strs)
  74. {
  75. if (sb == null)
  76. {
  77. sb = new StringBuilder();
  78. }
  79. sb.Clear();
  80. //StringBuilder sb = new StringBuilder();
  81. for (int i = 0; i < strs.Length; i++)
  82. {
  83. sb.Append(strs[i]);
  84. }
  85. return sb.ToString();
  86. }
  87. // /// <summary>
  88. // /// 获取描述文本
  89. // /// </summary>
  90. // /// <param name="a"></param>
  91. // /// <param name="b"></param>
  92. // /// <returns></returns>
  93. // public static string GetDscString(string a, float[] b)
  94. // {
  95. // if (a == "")
  96. // return "";
  97. // for (int i = 0; i < b?.Length; i++)
  98. // {
  99. // string d = b[i].ToString();
  100. // string c = "{" + i + "}";
  101. // a = a.Replace(c, d);
  102. // }
  103. //
  104. // return a;
  105. // }
  106. /// <summary>
  107. /// 获取描述文本
  108. /// </summary>
  109. /// <param name="a"></param>
  110. /// <param name="b"></param>
  111. /// <returns></returns>
  112. public static string GetDscString(string a, float[] b)
  113. {
  114. if (a == "")
  115. return "";
  116. for (int i = 0; i < b?.Length; i++)
  117. {
  118. string d = b[i].ToString("0.##");
  119. string c = "{" + i + "}";
  120. a = a.Replace(c, d);
  121. }
  122. return a;
  123. }
  124. /// <summary>
  125. /// 获取描述文本
  126. /// </summary>
  127. /// <param name="a"></param>
  128. /// <param name="b"></param>
  129. /// <returns></returns>
  130. public static string GetDscString(string a, string[] b)
  131. {
  132. if (a == "")
  133. return "";
  134. for (int i = 0; i < b?.Length; i++)
  135. {
  136. string d = b[i].ToString();
  137. string c = "{" + i + "}";
  138. a = a.Replace(c, d);
  139. }
  140. return a;
  141. }
  142. public static string GetSignaturesInfo()
  143. {
  144. #if UNITY_ANDROID&&!UNITY_EDITOR
  145. AndroidJavaClass contextClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  146. AndroidJavaObject context = contextClass.GetStatic<AndroidJavaObject>("currentActivity");
  147. AndroidJavaObject packageManager = context.Call<AndroidJavaObject>("getPackageManager");
  148. string packageName = context.Call<string>("getPackageName");
  149. int flags = packageManager.GetStatic<int>("GET_SIGNATURES");
  150. AndroidJavaObject packageInfo =
  151. packageManager.Call<AndroidJavaObject>("getPackageInfo", packageName, flags);
  152. AndroidJavaObject[] signatures = packageInfo.Get<AndroidJavaObject[]>("signatures");
  153. // Get the first signature (assuming only one signature)
  154. AndroidJavaObject signature = signatures[0];
  155. // Convert signature to bytes
  156. byte[] signatureBytes = signature.Call<byte[]>("toByteArray");
  157. // Compute SHA256 hash
  158. SHA256 sha256 = SHA256.Create();
  159. byte[] hashBytes = sha256.ComputeHash(signatureBytes);
  160. // Convert bytes to hex string
  161. StringBuilder builder = new StringBuilder();
  162. for (int i = 0; i < hashBytes.Length; i++)
  163. {
  164. // Debug.Log(hashBytes[i].ToString("x2").ToUpperInvariant());
  165. builder.Append(hashBytes[i].ToString("X2"));
  166. if (i < hashBytes.Length - 1)
  167. {
  168. builder.Append(":");
  169. }
  170. }
  171. string sha256Signature = builder.ToString();
  172. return sha256Signature;
  173. #else
  174. return "";
  175. #endif
  176. }
  177. /// <summary>
  178. /// 正则匹配
  179. /// </summary>
  180. /// <param name="str"></param>
  181. /// <param name="data">"mydata|mydata|"</param>
  182. public static bool MatchingMouna(ref string str, string data)
  183. {
  184. MatchCollection m = Regex.Matches(str, "(" + data + ")", RegexOptions.Multiline);
  185. for (int i = 0; i < m.Count; i++)
  186. {
  187. string m1 = Regex.Replace(m[i].ToString(), "\\$", "\\$");
  188. m1 = Regex.Replace(m1, "\\^", "\\^");
  189. string xingxing = "";
  190. for (int j = 0; j < m1.Length; j++)
  191. xingxing += "*";
  192. str = Regex.Replace(str, m1, xingxing);
  193. }
  194. if (m.Count > 0)
  195. {
  196. return true;
  197. }
  198. else
  199. {
  200. return false;
  201. }
  202. //LogManager.Instance.Log(str + " " + data);
  203. }
  204. /// <summary>
  205. /// 设置对象的层
  206. /// </summary>
  207. /// <param name="layerName"></param>
  208. /// <param name="obj"></param>
  209. /// <param name="isChild"></param>
  210. public static void SetGameObjctLayer(string layerName, GameObject obj, bool isChild = true)
  211. {
  212. int layerIdx = LayerMask.NameToLayer(layerName);
  213. if (isChild)
  214. SetChildLayer(layerIdx, obj.transform);
  215. else
  216. {
  217. obj.layer = layerIdx;
  218. }
  219. }
  220. /// <summary>
  221. /// 设置对象的层
  222. /// </summary>
  223. /// <param name="layerName"></param>
  224. /// <param name="obj"></param>
  225. /// <param name="isChild"></param>
  226. public static void SetGameObjctLayer(int layer, GameObject obj, bool isChild = true)
  227. {
  228. //int layerIdx = LayerMask.NameToLayer(layerName);
  229. if (isChild)
  230. SetChildLayer(layer, obj.transform);
  231. else
  232. {
  233. obj.layer = layer;
  234. }
  235. }
  236. private static void SetChildLayer(int layerIdx, Transform obj)
  237. {
  238. obj.gameObject.layer = layerIdx;
  239. for (int i = 0; i < obj.childCount; i++)
  240. {
  241. SetChildLayer(layerIdx, obj.GetChild(i));
  242. }
  243. }
  244. public static IPEndPoint CombineIPAddress(string host, int port)
  245. {
  246. return new IPEndPoint(IPAddress.Parse(host), port);
  247. }
  248. public static bool IsStringLetterAndNumber(string content)
  249. {
  250. if (content.Length == 0)
  251. {
  252. return false;
  253. }
  254. int asciiCount = Regex.Matches(content, "[a-zA-Z0-9]").Count;
  255. return (content.Length > asciiCount) ? false : true;
  256. }
  257. public static bool IsArrayValid(object array)
  258. {
  259. if (array == null || !(array is Array) || (array as Array).Length == 0)
  260. {
  261. return false;
  262. }
  263. return true;
  264. }
  265. public static Texture2D GetTexture(string path)
  266. {
  267. if (string.IsNullOrEmpty(path))
  268. return null;
  269. if (!File.Exists(path))
  270. return null;
  271. byte[] data = File.ReadAllBytes(path);
  272. Texture2D textrue = new Texture2D(512, 512);
  273. textrue.LoadImage(data);
  274. return textrue;
  275. }
  276. private static string m_bannedWorlds;
  277. public static bool IsBannedWord(string rolename)
  278. {
  279. return false;
  280. }
  281. #if UNITY_EDITOR
  282. /// <summary>
  283. /// 获取场景中全部对象
  284. /// </summary>
  285. /// <typeparam name="T"></typeparam>
  286. /// <returns></returns>
  287. public static List<T> GetAllSceneObj<T>()
  288. {
  289. List<T> ts = new List<T>();
  290. //HierarchyProperty.
  291. //EditorSceneManager.
  292. GameObject[] go = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().GetRootGameObjects();
  293. //LogTool.Log(go.Length);
  294. AddObjcet(ts, go);
  295. go = Utility.UtilTools.allDontDestroyOnLoad.ToArray();
  296. AddObjcet(ts, go);
  297. return ts;
  298. }
  299. //public static string GetModelName(GlobalConfig.LevelType _type, MapUnit unit)
  300. //{
  301. // string str =FixationString.empty;
  302. // switch (unit)
  303. // {
  304. // case MapUnit.Wall:
  305. // str = _type.SplitToString() + "_Wall_";
  306. // break;
  307. // case MapUnit.Coin:
  308. // return _type.SplitToString() + "_Seed";
  309. // case MapUnit.Gate:
  310. // return _type.SplitToString() + "_Gate";
  311. // case MapUnit.Player:
  312. // return "Player";
  313. // case MapUnit.WallVisible:
  314. // return _type.SplitToString() + "_WallInvisible";
  315. // case MapUnit.CoinVisible:
  316. // return _type.SplitToString() + "SeedInvisible";
  317. // case MapUnit.BlockLight:
  318. // case MapUnit.BlockBalance:
  319. // case MapUnit.BlockNormal:
  320. // case MapUnit.BlockSleep:
  321. // case MapUnit.BlockGravity:
  322. // return "Block" + _type.SplitToString();
  323. // case MapUnit.Wall_DestroyAble:
  324. // return "Wall_DestroyAble";
  325. // }
  326. // return str;
  327. //}
  328. /// <summary>
  329. private static void AddObjcet<T>(List<T> ts, GameObject[] go)
  330. {
  331. for (int i = 0; i < go.Length; i++)
  332. {
  333. if (!go[i])
  334. continue;
  335. //if (!go[i].gameObject)
  336. // continue;
  337. T[] objs = go[i].transform.GetComponentsInChildren<T>(true);
  338. for (int j = 0; j < objs.Length; j++)
  339. {
  340. ts.Add(objs[j]);
  341. }
  342. }
  343. }
  344. #endif
  345. public static string GetMD5(string inStr)
  346. {
  347. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  348. byte[] InBytes = Encoding.UTF8.GetBytes(inStr);
  349. byte[] OutBytes = md5.ComputeHash(InBytes);
  350. string OutString = "";
  351. for (int i = 0; i < OutBytes.Length; i++)
  352. {
  353. OutString += OutBytes[i].ToString("x2");
  354. }
  355. //LogTool.Log(OutString);
  356. if (OutString.Length > 8) OutString = OutString.Substring(0, 8);
  357. return OutString;
  358. }
  359. // DES加密
  360. public static string DESEncrypt(string pToEncrypt, string sKey)
  361. {
  362. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  363. byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
  364. // if (sKey.Length > 8) sKey = sKey.Substring(0, 8);
  365. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  366. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  367. MemoryStream ms = new MemoryStream();
  368. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  369. cs.Write(inputByteArray, 0, inputByteArray.Length);
  370. cs.FlushFinalBlock();
  371. StringBuilder ret = new StringBuilder();
  372. foreach (byte b in ms.ToArray())
  373. {
  374. ret.AppendFormat("{0:X2}", b);
  375. }
  376. ret.ToString();
  377. //LogTool.Log(inputByteArray.Length + " 加密后" + ret.SplitToString().Length);
  378. return ret.ToString();
  379. }
  380. // DES解密
  381. public static string DESDecrypt(string pToDecrypt, string sKey)
  382. {
  383. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  384. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  385. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  386. {
  387. int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  388. inputByteArray[x] = (byte)i;
  389. }
  390. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  391. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  392. MemoryStream ms = new MemoryStream();
  393. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  394. cs.Write(inputByteArray, 0, inputByteArray.Length);
  395. cs.FlushFinalBlock();
  396. StringBuilder ret = new StringBuilder();
  397. return System.Text.Encoding.UTF8.GetString(ms.ToArray());
  398. }
  399. public static string BASE64_Decode(string data)
  400. {
  401. byte[] bytes = Convert.FromBase64String(data);
  402. return Encoding.UTF8.GetString(bytes);
  403. }
  404. public static string GetMD5HashFromFile(byte[] data)
  405. {
  406. try
  407. {
  408. // FileStream file = new FileStream(filename,FileMode.Open);
  409. System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  410. byte[] retVal = md5.ComputeHash(data);
  411. StringBuilder sb = new StringBuilder();
  412. for (int i = 0; i < retVal.Length; i++)
  413. {
  414. sb.Append(retVal[i].ToString("x2"));
  415. }
  416. return sb.ToString();
  417. }
  418. catch (System.Exception ex)
  419. {
  420. LogTool.Log("错误信息:" + ex);
  421. return null;
  422. }
  423. }
  424. public static string FormatTwoTime(int totalSeconds)
  425. {
  426. int minutes = totalSeconds / 60;
  427. string mm = minutes < 10f ? "0" + minutes : minutes.ToString();
  428. int seconds = (totalSeconds - (minutes * 60));
  429. string ss = seconds < 10 ? "0" + seconds : seconds.ToString();
  430. return string.Format("{0}:{1}", mm, ss);
  431. }
  432. // public static string ToStringEx(this long i)
  433. // {
  434. // // if (isShowLongEx)
  435. // {
  436. // if (i >= 10000000000)
  437. // return (i / 1000000000) + "B";
  438. // else if (i >= 10000000)
  439. // return (i / 1000000) + "M";
  440. // else if (i >= 10000)
  441. // return (i / 1000) + "K";
  442. // }
  443. //
  444. // return i.ToString();
  445. // }
  446. /// <summary>
  447. /// 获取目标小数位的str
  448. /// </summary>
  449. /// <param name="n"></param>
  450. /// <returns></returns>
  451. public static string GetTargetDecimalPlaceStr(int n)
  452. {
  453. string str = "";
  454. if (n >= 100) //整数大于两位数保留一位小数
  455. {
  456. int n1 = (int)(n * 10);
  457. n = n1 / 10;
  458. str = n.ToString(CultureInfo.InvariantCulture);
  459. }
  460. else
  461. {
  462. //保留两位小数
  463. int n1 = (int)(n * 100);
  464. n = n1 / 100;
  465. str = n.ToString(CultureInfo.InvariantCulture);
  466. }
  467. return str;
  468. }
  469. public static long Round(this float number)
  470. {
  471. return (long)Math.Ceiling(number);
  472. }
  473. /// <summary>
  474. /// 四舍五入到整数
  475. /// </summary>
  476. /// <param name="value"></param>
  477. /// <returns></returns>
  478. public static int RoundToInt(this float value)
  479. {
  480. // float tempValue = value.Round(0);
  481. // int finalValue = Mathf.FloorToInt(tempValue);
  482. Math.Ceiling(value);
  483. return (int)Math.Ceiling(value);
  484. ;
  485. }
  486. public static string ToStringEx(this long number)
  487. {
  488. // if (number > 1000000000000)
  489. // {
  490. // float n = number / 1000000000000f;
  491. // string str = "";
  492. // str = GetTargetDecimalPlaceStr((int)n);
  493. // return str + "t";
  494. // }
  495. //
  496. // if (number > 1000000000)
  497. // {
  498. // float n = number / 1000000000f;
  499. // string str = "";
  500. // str = GetTargetDecimalPlaceStr((int)n);
  501. // return str + "b";
  502. // }
  503. if (number > 1000000)
  504. {
  505. float n = number / 10000f;
  506. string str = "";
  507. str = GetTargetDecimalPlaceStr((int)n);
  508. return str + "m";
  509. }
  510. if (number > 100000)
  511. {
  512. float n = number / 1000f;
  513. string str = "";
  514. str = GetTargetDecimalPlaceStr((int)n);
  515. return str + "k";
  516. }
  517. return number.ToString();
  518. }
  519. public static string ToStringEx(this int number)
  520. {
  521. // if (number > 1000000000000)
  522. // {
  523. // float n = number / 1000000000000f;
  524. // string str = "";
  525. // str = GetTargetDecimalPlaceStr((int)n);
  526. // return str + "t";
  527. // }
  528. //
  529. // if (number > 1000000000)
  530. // {
  531. // float n = number / 1000000000f;
  532. // string str = "";
  533. // str = GetTargetDecimalPlaceStr((int)n);
  534. // return str + "b";
  535. // }
  536. if (number > 1000000)
  537. {
  538. float n = number / 10000f;
  539. string str = "";
  540. str = GetTargetDecimalPlaceStr((int)n);
  541. return str + "m";
  542. }
  543. if (number > 100000)
  544. {
  545. float n = number / 1000f;
  546. string str = "";
  547. str = GetTargetDecimalPlaceStr((int)n);
  548. return str + "k";
  549. }
  550. return number.ToString();
  551. // // if (isShowLongEx)
  552. // {
  553. // if (i >= 10000000000)
  554. // return (i / 1000000000) + "B";
  555. // else if (i >= 10000000)
  556. // return (i / 1000000) + "M";
  557. // else if (i >= 10000)
  558. // return (i / 1000) + "K";
  559. // }
  560. //
  561. // return i.ToString();
  562. }
  563. #endif
  564. /// <summary>
  565. /// 时间(秒)转换位小时分钟秒
  566. /// </summary>
  567. /// <param name="miao"></param>
  568. /// <returns></returns>
  569. public static string TimeToHSM(long miao)
  570. {
  571. long fen = miao / 60;
  572. miao = miao % 60;
  573. long xiaoShi = fen / 60;
  574. long tian = xiaoShi / 24;
  575. xiaoShi = xiaoShi % 24;
  576. fen = fen % 60;
  577. if (tian > 0)
  578. {
  579. return $"{tian}天{xiaoShi}小时";
  580. }
  581. else
  582. {
  583. if (xiaoShi <= 0 && fen <= 59)
  584. {
  585. return $"{fen}分{miao}秒";
  586. }
  587. else
  588. {
  589. return $"{xiaoShi}小时{fen}分";
  590. }
  591. }
  592. }
  593. /// <summary>
  594. /// 时间(秒)转换位小时分钟秒
  595. /// </summary>
  596. /// <param name="miao"></param>
  597. /// <returns></returns>
  598. public static string TimeToHSM1(long miao)
  599. {
  600. long fen = miao / 60;
  601. miao = miao % 60;
  602. long xiaoShi = fen / 60;
  603. long tian = xiaoShi / 24;
  604. xiaoShi = xiaoShi % 24;
  605. fen = fen % 60;
  606. if (tian <= 0 && xiaoShi <= 0)
  607. {
  608. return $"{fen}分钟";
  609. }
  610. else if (tian <= 0 && xiaoShi > 0)
  611. {
  612. return $"{xiaoShi}小时";
  613. }
  614. else if (tian >= 1 )
  615. {
  616. return $"{tian}天";
  617. }
  618. return "";
  619. }
  620. public static string GeiLxTime(long miao)
  621. {
  622. long fen = miao / 60;
  623. miao = miao % 60;
  624. long xiaoShi = fen / 60;
  625. long tian = xiaoShi / 24;
  626. xiaoShi = xiaoShi % 24;
  627. fen = fen % 60;
  628. if (tian <= 0 && xiaoShi <= 0)
  629. {
  630. return $"离线{fen}分钟";
  631. }
  632. else if (tian <= 0 && xiaoShi > 0)
  633. {
  634. return $"离线{xiaoShi}小时";
  635. }
  636. else if (tian >= 1 && tian <= 7)
  637. {
  638. return $"离线{tian}天";
  639. }
  640. else if (tian > 7)
  641. {
  642. return "离线七天以上";
  643. }
  644. return "";
  645. }
  646. /// <summary>
  647. /// 类型1 :小于1天后显示分和秒
  648. /// </summary>
  649. /// <param name="miao"></param>
  650. /// <param name="type1"></param>
  651. /// <returns></returns>
  652. public static string TimeToHSM(long miao, int type1)
  653. {
  654. long fen = miao / 60;
  655. miao = miao % 60;
  656. long xiaoShi = fen / 60;
  657. long tian = xiaoShi / 24;
  658. xiaoShi = xiaoShi % 24;
  659. fen = fen % 60;
  660. if (tian > 0)
  661. {
  662. return $"{tian}天{xiaoShi}小时";
  663. }
  664. else
  665. {
  666. string key = "";
  667. switch (type1)
  668. {
  669. case 1:
  670. return $"{fen}分{miao}秒";
  671. break;
  672. default:
  673. break;
  674. }
  675. return key;
  676. }
  677. }
  678. /// <summary>获取秒级别时间戳(10位)</summary>
  679. public static long GetTimestampToSeconds()
  680. {
  681. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  682. return Convert.ToInt64(ts.TotalSeconds);
  683. }
  684. /// <summary>获取毫秒级别时间戳(13位)</summary>
  685. public static long GetTimeStampToMilliseconds()
  686. {
  687. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  688. return Convert.ToInt64(ts.TotalMilliseconds);
  689. }
  690. public static long GetTimestampToSeconds(int value)
  691. {
  692. DateTime dateTime = new DateTime(value, DateTimeKind.Utc);
  693. TimeSpan ts = dateTime - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  694. return Convert.ToInt64(ts.TotalSeconds);
  695. }
  696. /// <summary>
  697. /// 时间戳转时间结构 秒
  698. /// </summary>
  699. /// <param name="unixTimestamp"></param>
  700. /// <returns></returns>
  701. public static DateTime ConvertUnixTimestamp(double unixTimestamp)
  702. {
  703. DateTime unixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
  704. DateTime convertedTime = unixStartTime.AddSeconds(unixTimestamp).ToLocalTime();
  705. return convertedTime;
  706. }
  707. /// <summary>
  708. ///时间戳转时间结构 毫秒
  709. /// </summary>
  710. /// <param name="unixTimestamp"></param>
  711. /// <returns></returns>
  712. public static DateTime ConvertUnixTimestampForMillis(double unixTimestamp)
  713. {
  714. DateTime unixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
  715. DateTime convertedTime = unixStartTime.AddMilliseconds(unixTimestamp).ToLocalTime();
  716. return convertedTime;
  717. }
  718. public static string NumberToArab(int number)
  719. {
  720. switch (number)
  721. {
  722. case 0: return "0";
  723. case 1: return "I";
  724. case 2: return "II";
  725. case 3: return "III";
  726. case 4: return "IV";
  727. case 5: return "V";
  728. case 6: return "VI";
  729. }
  730. return "";
  731. }
  732. /// <summary>
  733. /// 获取凌晨时间戳 单位s
  734. /// </summary>
  735. /// <param name="kind"></param>
  736. /// <returns></returns>
  737. public static long GetMidnightTimestamp(DateTimeKind kind = DateTimeKind.Local)
  738. {
  739. DateTime now = DateTime.Now;
  740. DateTime midnight = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, kind);
  741. DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  742. TimeSpan timeSpan = midnight.ToUniversalTime() - epoch;
  743. return (long)timeSpan.TotalSeconds;
  744. }
  745. /// <summary>
  746. /// 获取每日更新倒计时
  747. /// </summary>
  748. /// <returns></returns>
  749. public static string GetEveryDayUpdateCountdown(int day)
  750. {
  751. DateTime dateTime1 = ConvertUnixTimestampForMillis(UtilTools.GetTimeStampToMilliseconds());
  752. int h = ConfigComponent.Instance.Get<GameConstantConfig>(1).refreshTime;
  753. long targetTime;
  754. if (dateTime1.Hour >= h) //如果已经过更新时间就获取明天的更新时间
  755. {
  756. int add = (24 + h) * 60 * 60 + day * 24 * 60 * 60;
  757. targetTime = GetMidnightTimestamp() + add; //获取明天凌晨5点的时间戳
  758. }
  759. else
  760. {
  761. int add = h * 60 * 60 + day * 24 * 60 * 60;
  762. targetTime = GetMidnightTimestamp() + add;
  763. }
  764. long r = targetTime - GetTimestampToSeconds(); //剩余时间
  765. return TimeToHSM(r);
  766. }
  767. /// <summary>
  768. /// 获取每日更新倒计时(显示天)
  769. /// </summary>
  770. /// <returns></returns>
  771. public static string GetEveryDayUpdateCountdown1(int day)
  772. {
  773. DateTime dateTime1 = ConvertUnixTimestampForMillis(UtilTools.GetTimeStampToMilliseconds());
  774. int h = ConfigComponent.Instance.Get<GameConstantConfig>(1).refreshTime;
  775. long targetTime;
  776. if (dateTime1.Hour >= h) //如果已经过更新时间就获取明天的更新时间
  777. {
  778. int add = (24 + h) * 60 * 60 + day * 24 * 60 * 60;
  779. targetTime = GetMidnightTimestamp() + add; //获取明天凌晨5点的时间戳
  780. }
  781. else
  782. {
  783. int add = h * 60 * 60 + day * 24 * 60 * 60;
  784. targetTime = GetMidnightTimestamp() + add;
  785. }
  786. long r = targetTime - GetTimestampToSeconds(); //剩余时间
  787. return TimeToHSM1(r);
  788. }
  789. /// <summary>
  790. /// 获取每日更新倒计时(秒)
  791. /// </summary>
  792. /// <returns></returns>
  793. public static long GetEveryDayUpdateCountdown2(int day)
  794. {
  795. DateTime dateTime1 = ConvertUnixTimestampForMillis(UtilTools.GetTimeStampToMilliseconds());
  796. int h = ConfigComponent.Instance.Get<GameConstantConfig>(1).refreshTime;
  797. long targetTime;
  798. if (dateTime1.Hour >= h) //如果已经过更新时间就获取明天的更新时间
  799. {
  800. int add = (24 + h) * 60 * 60 + day * 24 * 60 * 60;
  801. targetTime = GetMidnightTimestamp() + add; //获取明天凌晨5点的时间戳
  802. }
  803. else
  804. {
  805. int add = h * 60 * 60 + day * 24 * 60 * 60;
  806. targetTime = GetMidnightTimestamp() + add;
  807. }
  808. long r = targetTime - GetTimestampToSeconds(); //剩余时间
  809. return r;
  810. }
  811. public static T DeepCopy<T>(T obj)
  812. {
  813. using (var memoryStream = new MemoryStream())
  814. {
  815. var formatter = new BinaryFormatter();
  816. formatter.Serialize(memoryStream, obj);
  817. memoryStream.Seek(0, SeekOrigin.Begin);
  818. return (T)formatter.Deserialize(memoryStream);
  819. }
  820. }
  821. }
  822. //UtilTools
  823. }