AssetsObscureUtil.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using UnityEngine;
  6. public class AssetsObscureUtil
  7. {
  8. private const string c_ObscureKey = "AssetsObscureKey";
  9. private const string c_ObscureOffsetMin = "AssetsObscureOffsetMin";
  10. private const string c_ObscureOffsetValues = "AssetsObscureOffsetValues";
  11. private static string s_ObscureKey = string.Empty;
  12. private static uint s_ObscureOffsetMin = 0;
  13. private static int[] s_ObscureOffsetValues = null;
  14. public static string GetObscureKey()
  15. {
  16. return s_ObscureKey;
  17. }
  18. public static uint GetObscureOffsetMin()
  19. {
  20. return s_ObscureOffsetMin;
  21. }
  22. public static int[] GetObscureOffsetValues()
  23. {
  24. return s_ObscureOffsetValues;
  25. }
  26. internal static void InitAssetsObscureConfig(ref Dictionary<string, string> assetsMappingDict)
  27. {
  28. string key = c_ObscureKey.ToLower();
  29. if (assetsMappingDict.ContainsKey(key))
  30. {
  31. s_ObscureKey = assetsMappingDict[key];
  32. assetsMappingDict.Remove(key);
  33. }
  34. key = c_ObscureOffsetMin.ToLower();
  35. if (assetsMappingDict.ContainsKey(key))
  36. {
  37. if (!uint.TryParse(assetsMappingDict[key], out s_ObscureOffsetMin))
  38. {
  39. s_ObscureOffsetMin = 0;
  40. }
  41. assetsMappingDict.Remove(key);
  42. }
  43. key = c_ObscureOffsetValues.ToLower();
  44. if (assetsMappingDict.ContainsKey(key))
  45. {
  46. string content = assetsMappingDict[key];
  47. if (!string.IsNullOrEmpty(content))
  48. {
  49. string[] values = content.Split('/');
  50. List<int> ls = new List<int>();
  51. for (int i = 0, iMax = values.Length; i < iMax; i++)
  52. {
  53. int value;
  54. if (int.TryParse(values[i], out value))
  55. {
  56. ls.Add(value);
  57. }
  58. }
  59. if (ls.Count > 0)
  60. {
  61. s_ObscureOffsetValues = ls.ToArray();
  62. }
  63. }
  64. assetsMappingDict.Remove(key);
  65. }
  66. }
  67. public static void WriteInfoData(ref StringBuilder stringBuilder)
  68. {
  69. if (stringBuilder == null) return;
  70. if (!string.IsNullOrEmpty(s_ObscureKey))
  71. {
  72. stringBuilder.Append(c_ObscureKey);
  73. stringBuilder.Append(",");
  74. stringBuilder.Append(s_ObscureKey);
  75. stringBuilder.Append("\r\n");
  76. }
  77. if (s_ObscureOffsetMin != 0)
  78. {
  79. stringBuilder.Append(c_ObscureOffsetMin);
  80. stringBuilder.Append(",");
  81. stringBuilder.Append(s_ObscureOffsetMin);
  82. stringBuilder.Append("\r\n");
  83. }
  84. if (s_ObscureOffsetValues != null && s_ObscureOffsetValues.Length > 0)
  85. {
  86. stringBuilder.Append(c_ObscureOffsetValues);
  87. stringBuilder.Append(",");
  88. for (int i = 0, iMax = s_ObscureOffsetValues.Length; i < iMax; i++)
  89. {
  90. if (i != 0)
  91. {
  92. stringBuilder.Append("/");
  93. }
  94. stringBuilder.Append(s_ObscureOffsetValues[i]);
  95. }
  96. stringBuilder.Append("\r\n");
  97. }
  98. }
  99. public static bool IsObscure()
  100. {
  101. return !(string.IsNullOrEmpty(s_ObscureKey));
  102. }
  103. public static string GetABFileName(string abName)
  104. {
  105. if (IsObscure())
  106. {
  107. try
  108. {
  109. using(var md5 = new MD5CryptoServiceProvider())
  110. {
  111. UTF8Encoding encoding = new UTF8Encoding(false);
  112. byte[] bytes = encoding.GetBytes((abName + s_ObscureKey).ToLower());
  113. bytes = md5.ComputeHash(bytes);
  114. StringBuilder sb = new StringBuilder();
  115. for (int i = 0; i < bytes.Length; i++)
  116. {
  117. sb.Append(bytes[i].ToString("x2"));
  118. }
  119. return sb.ToString();
  120. }
  121. }
  122. catch(Exception e)
  123. {
  124. Debug.LogException(e);
  125. }
  126. }
  127. #if UNITY_EDITOR
  128. #else
  129. string language = AssetsMgr.Instance.GetReslanguage();
  130. if (!string.IsNullOrEmpty(language) && language != "base")
  131. {
  132. abName = AssetsMgr.Instance.GetlanguageAbName_EN(abName);
  133. }
  134. #endif
  135. return abName;
  136. }
  137. public static ulong GetABOffset(string abFileName)
  138. {
  139. if (IsObscure())
  140. {
  141. if (string.IsNullOrEmpty(abFileName))
  142. {
  143. return 0;
  144. }
  145. try
  146. {
  147. int length = abFileName.Length;
  148. if (length != 32)
  149. {
  150. return 0;
  151. }
  152. if (s_ObscureOffsetValues == null) return 0;
  153. ulong offset = 0;
  154. for (int i = 0, iMax = s_ObscureOffsetValues.Length; i < iMax; i++)
  155. {
  156. int pos = ConvertHexDigit(abFileName[s_ObscureOffsetValues[i]]);
  157. if (pos < 0 || pos > 16)
  158. {
  159. return 0;
  160. }
  161. offset = offset + (uint)(pos * Mathf.Pow(16, i));
  162. }
  163. if (offset <= s_ObscureOffsetMin)
  164. {
  165. offset = s_ObscureOffsetMin + offset;
  166. }
  167. return offset;
  168. }
  169. catch(Exception e)
  170. {
  171. Debug.LogException(e);
  172. }
  173. }
  174. return 0;
  175. }
  176. private static int ConvertHexDigit(char val)
  177. {
  178. if (val <= '9' && val >= '0')
  179. {
  180. return val - 48;
  181. }
  182. if (val >= 'a' && val <= 'f')
  183. {
  184. return val - 97 + 10;
  185. }
  186. if (val >= 'A' && val <= 'F')
  187. {
  188. return val - 65 + 10;
  189. }
  190. return 0;
  191. }
  192. public static string GetUniqueValue()
  193. {
  194. if (string.IsNullOrEmpty(s_ObscureKey))
  195. {
  196. return string.Empty;
  197. }
  198. string value = s_ObscureKey + s_ObscureOffsetMin;
  199. if (s_ObscureOffsetValues != null && s_ObscureOffsetValues.Length > 0)
  200. {
  201. for (int i = 0, iMax = s_ObscureOffsetValues.Length; i < iMax; i++)
  202. {
  203. value = value + s_ObscureOffsetValues[i];
  204. }
  205. }
  206. return value;
  207. }
  208. }