UnityPlatformIOS.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #if (UNITY_IOS)
  2. using UnityEngine;
  3. using System.Runtime.InteropServices;
  4. using System;
  5. using CommonUI_Unity3D.Platform;
  6. using CommonLang.Concurrent;
  7. using System.Threading;
  8. using MPQ.Updater;
  9. using System.IO;
  10. namespace CommonUI_Unity3D_IOS
  11. {
  12. public class UnityPlatformIOS : IUnityPlatform
  13. {
  14. public void Assert(string msg)
  15. {
  16. Debug.LogError(msg);
  17. }
  18. //----------------------------------------------------------------------------------------------------------
  19. #region Native
  20. /// <summary>
  21. /// inflate
  22. /// </summary>
  23. /// <param name="srcFile"></param>
  24. /// <param name="dstFile"></param>
  25. /// <returns></returns>
  26. [DllImport("__Internal")]
  27. public static extern bool _Decompress_z(string srcFile, string dstFile);
  28. [DllImport("__Internal")]
  29. public static extern int _Decompress_bytes();
  30. [DllImport("__Internal")]
  31. public static extern bool _Decompress_z_mem(byte[] src, int s_offset, int s_end, byte[] dst, int dst_offset, int dst_end);
  32. [DllImport("__Internal")]
  33. public static extern bool _SysFontTest(
  34. string pText,
  35. string fontName,
  36. int fontStyle,
  37. int fontSize,
  38. int bgCount,
  39. int expectSizeW,
  40. int expectSizeH,
  41. ref int outW,
  42. ref int outH);
  43. [DllImport("__Internal")]
  44. public static extern bool _SysFontTexture2(
  45. string pText,
  46. string fontName,
  47. int fontStyle,
  48. int fontSize,
  49. int fontColorRGBA,
  50. int bgCount,
  51. int bgColorRGBA,
  52. int expectSizeW,
  53. int expectSizeH,
  54. int glTextureID);
  55. [DllImport("__Internal")]
  56. public static extern bool _SysFontTexture2_Ptr(
  57. string pText,
  58. string fontName,
  59. int fontStyle,
  60. int fontSize,
  61. int fontColorRGBA,
  62. int bgCount,
  63. int bgColorRGBA,
  64. int expectSizeW,
  65. int expectSizeH,
  66. IntPtr glTextureID_Ptr);
  67. [DllImport("__Internal")]
  68. public static extern bool _SysFontGetPixels(
  69. string pText,
  70. string fontName,
  71. int fontStyle,
  72. int fontSize,
  73. int fontColorRGBA,
  74. int bgCount,
  75. int bgColorRGBA,
  76. int pixelW,
  77. int pixelH,
  78. byte[] pixels);
  79. [DllImport("__Internal")]
  80. public static extern bool _SysFontGetPixels_Color32(
  81. string pText,
  82. string fontName,
  83. int fontStyle,
  84. int fontSize,
  85. int fontColorRGBA,
  86. int bgCount,
  87. int bgColorRGBA,
  88. int pixelW,
  89. int pixelH,
  90. Color32[] pixels);
  91. [DllImport("__Internal")]
  92. public static extern bool _Md5_CheckFile(string srcFile, byte[] dst);
  93. #endregion
  94. //----------------------------------------------------------------------------------------------------------
  95. #region TEXT
  96. public const string FontName = "Helvetica-Bold";
  97. public Texture2D SysFontTexture(
  98. string text,
  99. bool readable,
  100. CommonUI.Display.FontStyle style,
  101. int fontSize,
  102. uint fontColor,
  103. int borderTime,
  104. uint borderColor,
  105. CommonUI.Gemo.Size2D expectSize,
  106. out int boundW,
  107. out int boundH)
  108. {
  109. try
  110. {
  111. text = text + "";
  112. int _pixelW = 8;
  113. int _pixelH = 8;
  114. int _expectW = 0;
  115. int _expectH = 0;
  116. if (expectSize != null)
  117. {
  118. _pixelW = (int)expectSize.width;
  119. _pixelH = (int)expectSize.height;
  120. _expectW = (int)_pixelW;
  121. _expectH = (int)_pixelH;
  122. }
  123. if (_SysFontTest(
  124. text,
  125. FontName,
  126. (int)style,
  127. (int)fontSize,
  128. borderTime,
  129. (int)_expectW,
  130. (int)_expectH,
  131. ref _pixelW,
  132. ref _pixelH))
  133. {
  134. boundW = _pixelW;
  135. boundH = _pixelH;
  136. byte[] rgba = new byte[_pixelW * _pixelH * 4];
  137. _SysFontGetPixels(
  138. text,
  139. FontName,
  140. (int)style,
  141. (int)fontSize,
  142. (int)fontColor,
  143. (int)borderTime,
  144. (int)borderColor,
  145. (int)_pixelW,
  146. (int)_pixelH,
  147. rgba);
  148. Texture2D mTexture = new UnityEngine.Texture2D(_pixelW, _pixelH, TextureFormat.RGBA32, false, true);
  149. mTexture.filterMode = FilterMode.Bilinear;
  150. mTexture.wrapMode = TextureWrapMode.Clamp;
  151. mTexture.anisoLevel = 0;
  152. mTexture.mipMapBias = 0;
  153. mTexture.LoadRawTextureData(rgba);
  154. if (readable)
  155. {
  156. mTexture.Apply(false, false);
  157. }
  158. else
  159. {
  160. mTexture.Apply(false, true);
  161. }
  162. return mTexture;
  163. }
  164. }
  165. catch (Exception) { }
  166. Texture2D tex = new UnityEngine.Texture2D(8, 8, TextureFormat.RGBA32, false, true);
  167. tex.filterMode = FilterMode.Point;
  168. tex.wrapMode = TextureWrapMode.Clamp;
  169. boundW = 8;
  170. boundH = 8;
  171. tex.Apply(false, true);
  172. return tex;
  173. }
  174. public bool TestTextLineBreak(string text, float size, CommonUI.Display.FontStyle style,
  175. int borderTime,
  176. float testWidth,
  177. out float realWidth,
  178. out float realHeight)
  179. {
  180. int tw = 0;
  181. int th = 0;
  182. try
  183. {
  184. _SysFontTest(text, FontName, (int)style, (int)size, borderTime, 0, 0, ref tw, ref th);
  185. realWidth = tw;
  186. realHeight = th;
  187. if (realWidth > testWidth)
  188. {
  189. _SysFontTest(text, FontName, (int)style, (int)size, borderTime, (int)testWidth, 0, ref tw, ref th);
  190. realWidth = tw;
  191. return true;
  192. }
  193. }
  194. catch (Exception)
  195. {
  196. realWidth = tw;
  197. realHeight = th;
  198. }
  199. return false;
  200. }
  201. private static void InnRect(Texture2D src, ref int sx, ref int sy)
  202. {
  203. if (sx < 0)
  204. sx = 0;
  205. if (sx >= src.width)
  206. sx = src.width - 1;
  207. if (sy < 0)
  208. sy = 0;
  209. if (sy >= src.height)
  210. sy = src.height - 1;
  211. }
  212. public void CopyPixels(Texture2D src, int sx, int sy, int sw, int sh, Texture2D dst, int dx, int dy)
  213. {
  214. int sx2 = sx + sw;
  215. int sy2 = sy + sh;
  216. int dx2 = dx + sw;
  217. int dy2 = dy + sh;
  218. InnRect(src, ref sx, ref sy);
  219. InnRect(src, ref sx2, ref sy2);
  220. InnRect(dst, ref dx, ref dy);
  221. InnRect(dst, ref dx2, ref dy2);
  222. sw = Mathf.Min(sx2 - sx, dx2 - dx);
  223. sh = Mathf.Min(sy2 - sy, dy2 - dy);
  224. try
  225. {
  226. if (sw > 0 && sh > 0)
  227. {
  228. UnityEngine.Color[] colors = src.GetPixels(sx, sy, sw, sh);
  229. dst.SetPixels(dx, dy, sw, sh, colors);
  230. dst.Apply();
  231. }
  232. }
  233. catch (Exception err)
  234. {
  235. Debug.LogError(err.Message);
  236. Debug.LogException(err);
  237. }
  238. }
  239. #endregion
  240. //----------------------------------------------------------------------------------------------------------
  241. #region IME
  242. #endregion
  243. //----------------------------------------------------------------------------------------------------------
  244. #region MPQ
  245. public bool IsNativeUnzip { get { return true; } }
  246. public long GetAvaliableSpace(string path)
  247. {
  248. try
  249. {
  250. DriveInfo drive = new DriveInfo(Directory.GetDirectoryRoot(path));
  251. return drive.AvailableFreeSpace;
  252. }
  253. catch (Exception)
  254. {
  255. return long.MaxValue;
  256. }
  257. }
  258. public long GetTotalSpace(string path)
  259. {
  260. try
  261. {
  262. DriveInfo drive = new DriveInfo(Directory.GetDirectoryRoot(path));
  263. return drive.TotalSize;
  264. }
  265. catch (Exception)
  266. {
  267. return long.MaxValue;
  268. }
  269. }
  270. private class DecompressTask
  271. {
  272. private bool result;
  273. private bool isDone;
  274. public bool Run(string src, string dst, AtomicLong process)
  275. {
  276. isDone = false;
  277. var task = new Thread(() =>
  278. {
  279. try
  280. {
  281. result = _Decompress_z(src, dst);
  282. }
  283. catch (Exception err)
  284. {
  285. Console.WriteLine(err.Message + "\n" + err.StackTrace);
  286. result = false;
  287. }
  288. finally
  289. {
  290. isDone = true;
  291. }
  292. });
  293. task.Name = "_Decompress_z";
  294. task.Start();
  295. long total = 0;
  296. while (!isDone)
  297. {
  298. Thread.Sleep(100);
  299. long bytes = _Decompress_bytes();
  300. if (total < bytes)
  301. {
  302. process += (bytes - total);
  303. total = bytes;
  304. }
  305. }
  306. return this.result;
  307. }
  308. }
  309. public bool NativeDecompressFile(MPQUpdater updater, MPQUpdater.RemoteFileInfo zip_file, MPQUpdater.RemoteFileInfo mpq_file, AtomicLong current_unzip_bytes)
  310. {
  311. if (zip_file.file.Name.EndsWith(".z"))
  312. {
  313. var task = new DecompressTask();
  314. return task.Run(zip_file.file.FullName, mpq_file.file.FullName, current_unzip_bytes);
  315. }
  316. else
  317. {
  318. throw new Exception("iOSÖ»Ö§³Ö.z¸ñʽNative½âѹËõ£¡");
  319. }
  320. }
  321. public bool NativeDecompressMemory(byte[] src, int s_start, int s_end, byte[] dst, int dst_start, int dst_end)
  322. {
  323. return _Decompress_z_mem(src, s_start, s_end, dst, dst_start, dst_end);
  324. }
  325. public bool NativeDecompressMemory(ArraySegment<byte> src, ArraySegment<byte> dst)
  326. {
  327. return _Decompress_z_mem(src.Array, src.Offset, src.Offset + src.Count, dst.Array, dst.Offset, dst.Offset + dst.Count);
  328. }
  329. public bool NativeGetFileMD5(string fullname, out string md5string)
  330. {
  331. byte[] md5 = new byte[32];
  332. md5string = string.Empty;
  333. if (_Md5_CheckFile(fullname, md5))
  334. {
  335. for (int i = 0; i < md5.Length; i++)
  336. {
  337. md5string += (char)md5[i];
  338. }
  339. return true;
  340. }
  341. return false;
  342. }
  343. #endregion
  344. //----------------------------------------------------------------------------------------------------------
  345. }
  346. }
  347. #endif