UnityDriver.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. using System;
  2. using CommonUI.Display;
  3. using UnityEngine;
  4. using CommonLang.IO;
  5. using System.IO;
  6. using CommonLang.Property;
  7. using CommonUI_Unity3D.Platform;
  8. using CommonUI.Gemo;
  9. using System.Collections.Generic;
  10. using System.Threading;
  11. using MPQ.FileSystem;
  12. using MPQ;
  13. using MPQ.Updater;
  14. using CommonLang.Concurrent;
  15. using CommonLang.Log;
  16. namespace CommonUI_Unity3D.Impl
  17. {
  18. public class UnityDriver : Driver, IResourceLoader
  19. {
  20. public static bool IsDebug = false;
  21. private static UnityDriver sInstance;
  22. private static IUnityPlatform sPlatform = new DummyUnityPlatform();
  23. public static IUnityPlatform Platform
  24. {
  25. get { return sPlatform; }
  26. }
  27. public static UnityDriver UnityInstance
  28. {
  29. get
  30. {
  31. if (sInstance == null)
  32. {
  33. sInstance = new UnityDriver();
  34. }
  35. return sInstance;
  36. }
  37. }
  38. public static void SetDirver()
  39. {
  40. if (sInstance == null)
  41. {
  42. sInstance = new UnityDriver();
  43. }
  44. if (IsWin32)
  45. {
  46. SetDirver("CommonUI_Unity3D_Win32.UnityPlatformWin32");
  47. }
  48. else if (IsIOS)
  49. {
  50. SetDirver("CommonUI_Unity3D_IOS.UnityPlatformIOS");
  51. }
  52. else if (IsAndroid)
  53. {
  54. SetDirver("CommonUI_Unity3D_Android.UnityPlatformAndroid");
  55. }
  56. }
  57. public static void SetDirver(string platformDriver)
  58. {
  59. if (sPlatform is DummyUnityPlatform)
  60. {
  61. try
  62. {
  63. Type driver = ReflectionUtil.GetType(platformDriver);
  64. if (driver != null)
  65. {
  66. sPlatform = (IUnityPlatform)ReflectionUtil.CreateInstance(driver);
  67. Debug.Log("- Create Platform Driver : " + platformDriver);
  68. }
  69. else
  70. {
  71. Debug.LogError("- Can Not Create Platform Driver : " + platformDriver);
  72. }
  73. }
  74. catch (Exception err)
  75. {
  76. Debug.LogError(err.Message + "\n" + err.StackTrace);
  77. }
  78. }
  79. }
  80. public static void SetDirver(IUnityPlatform platform)
  81. {
  82. if (sPlatform is DummyUnityPlatform)
  83. {
  84. Debug.Log("- Set Platform Driver : " + platform);
  85. sPlatform = platform;
  86. }
  87. }
  88. private UnityDriver()
  89. {
  90. Resource.SetLoader(this);
  91. UnityShaders.InitShaders();
  92. LoggerFactory.SetFactory(new UnityLoggerFactory());
  93. }
  94. public override void Assert(bool cond, string msg)
  95. {
  96. if (!cond)
  97. {
  98. Debug.LogError("Assert: " + msg);
  99. UnityDriver.Platform.Assert("Assert: " + msg);
  100. }
  101. }
  102. // ---------------------------------------------------------------------------------
  103. #region PLATFORM_MACRO
  104. public static bool IsWin32
  105. {
  106. get
  107. {
  108. return Application.platform == RuntimePlatform.WindowsEditor ||
  109. Application.platform == RuntimePlatform.WindowsPlayer ||
  110. Application.platform == RuntimePlatform.WindowsWebPlayer;
  111. }
  112. }
  113. public static bool IsIOS
  114. {
  115. get
  116. {
  117. return Application.platform == RuntimePlatform.IPhonePlayer;
  118. }
  119. }
  120. public static bool IsAndroid
  121. {
  122. get
  123. {
  124. return Application.platform == RuntimePlatform.Android;
  125. }
  126. }
  127. #endregion
  128. // ---------------------------------------------------------------------------------
  129. #region Mono
  130. #endregion
  131. // ---------------------------------------------------------------------------------
  132. #region GFX
  133. public delegate string RedirectImagePath(string resource);
  134. public RedirectImagePath RedirectImage;
  135. public delegate Image GetDefaultImg(string resource);
  136. public GetDefaultImg OnGetDefaultImg;
  137. public override void ReloadImage(Image img)
  138. {
  139. try
  140. {
  141. UnityImage ret = img as UnityImage;
  142. if (ret != null && !string.IsNullOrEmpty(ret.ResourceStr))
  143. {
  144. string resource = ret.ResourceStr;
  145. if (resource.StartsWith(PREFIX_MPQ))
  146. {
  147. byte[] edata = mFileSystem.getData(resource.Substring(PREFIX_MPQ.Length));
  148. if (edata != null)
  149. {
  150. ret.ResestTexture2D(edata, resource);
  151. }
  152. }
  153. else if (resource.StartsWith(PREFIX_RES))
  154. {
  155. string res_path = resource.Substring(PREFIX_RES.Length);
  156. object obj = LoadObjectFromResources(res_path);
  157. if (obj is Texture2D)
  158. {
  159. ret.ResestTexture2D(obj as Texture2D, resource);
  160. }
  161. if (obj is TextAsset)
  162. {
  163. TextAsset ta = (obj as TextAsset);
  164. ret.ResestTexture2D(ta.bytes, resource);
  165. }
  166. }
  167. else if (resource.StartsWith(PREFIX_FILE))
  168. {
  169. FileInfo finfo = new FileInfo(resource.Substring(PREFIX_FILE.Length));
  170. if (finfo.Exists)
  171. {
  172. byte[] data = File.ReadAllBytes(finfo.FullName);
  173. ret.ResestTexture2D(data, resource);
  174. }
  175. }
  176. else
  177. {
  178. byte[] data = Resource.LoadData(resource);
  179. if (data != null)
  180. {
  181. ret.ResestTexture2D(data, resource);
  182. }
  183. else
  184. {
  185. Texture2D tex = LoadFromResources<Texture2D>(resource);
  186. if (tex != null)
  187. {
  188. ret.ResestTexture2D(tex, resource);
  189. }
  190. }
  191. }
  192. }
  193. }
  194. catch (System.Exception e)
  195. {
  196. UnityEngine.Debug.LogException(e);
  197. }
  198. }
  199. public override Image createImage(string resource)
  200. {
  201. try
  202. {
  203. UnityImage ret = null;
  204. if (RedirectImage != null)
  205. {
  206. resource = RedirectImage(resource);
  207. }
  208. if (resource.StartsWith(PREFIX_MPQ))
  209. {
  210. byte[] edata = mFileSystem.getData(resource.Substring(PREFIX_MPQ.Length));
  211. if (edata != null)
  212. {
  213. ret = new UnityImage(edata, resource, resource);
  214. }
  215. }
  216. else if (resource.StartsWith(PREFIX_RES))
  217. {
  218. string res_path = resource.Substring(PREFIX_RES.Length);
  219. object obj = LoadObjectFromResources(res_path);
  220. if (obj is Texture2D)
  221. {
  222. return new UnityImage(obj as Texture2D, resource, resource);
  223. }
  224. if (obj is TextAsset)
  225. {
  226. TextAsset ta = (obj as TextAsset);
  227. ret = new UnityImage(ta.bytes, resource, resource);
  228. }
  229. }
  230. else if (resource.StartsWith(PREFIX_FILE))
  231. {
  232. FileInfo finfo = new FileInfo(resource.Substring(PREFIX_FILE.Length));
  233. if (finfo.Exists)
  234. {
  235. byte[] data = File.ReadAllBytes(finfo.FullName);
  236. ret = new UnityImage(data, resource, resource);
  237. }
  238. }
  239. else
  240. {
  241. byte[] data = Resource.LoadData(resource);
  242. if (data != null)
  243. {
  244. ret = new UnityImage(data, resource, resource);
  245. }
  246. else
  247. {
  248. Texture2D tex = LoadFromResources<Texture2D>(resource);
  249. if (tex != null)
  250. {
  251. ret = new UnityImage(tex, resource, resource);
  252. }
  253. }
  254. }
  255. return ret;
  256. }
  257. catch (System.Exception e)
  258. {
  259. UnityEngine.Debug.LogError(string.Format("Resource Read Error : {0}\n", resource, e.Message));
  260. UnityEngine.Debug.LogException(e);
  261. }
  262. //Assert(false, string.Format("Resource Read Error : {0}\n", resource));
  263. if (OnGetDefaultImg != null) { return OnGetDefaultImg(resource); }
  264. return null;
  265. }
  266. public override Image createImage(System.IO.Stream stream)
  267. {
  268. if (stream == null)
  269. {
  270. UnityEngine.Debug.Log("Invalid Param : create Image from stream");
  271. return null;
  272. }
  273. try
  274. {
  275. // U3D Texture2D
  276. byte[] imageData = new byte[stream.Length];
  277. IOUtil.ReadToEnd(stream, imageData, 0, imageData.Length);
  278. return new UnityImage(imageData, "createImage(stream)");
  279. }
  280. catch (System.Exception e)
  281. {
  282. UnityEngine.Debug.LogError("Stream Read Error " + e.Message);
  283. UnityEngine.Debug.LogException(e);
  284. }
  285. return null;
  286. }
  287. public override Image createImage(byte[] imageData, int imageOffset, int imageLength)
  288. {
  289. try
  290. {
  291. if (imageLength == imageData.Length)
  292. {
  293. return new UnityImage(imageData, "createImage(byte[])");
  294. }
  295. else
  296. {
  297. byte[] data = new byte[imageLength];
  298. System.Array.Copy(imageData, imageOffset, data, 0, imageLength);
  299. // To UnityImage
  300. return new UnityImage(data, "createImage(byte[])");
  301. }
  302. }
  303. catch (System.Exception e)
  304. {
  305. UnityEngine.Debug.LogError("ImageData Read Error " + e.Message);
  306. UnityEngine.Debug.LogException(e);
  307. }
  308. return null;
  309. }
  310. public override Image createRGBImage(uint[] rgba, int width, int height)
  311. {
  312. UnityEngine.Texture2D destTex = new UnityEngine.Texture2D(width, height, TextureFormat.ARGB32, false, true);
  313. int i = 0;
  314. UnityEngine.Color color = UnityEngine.Color.white;
  315. for (int x = 0; x < width; x++)
  316. {
  317. for (int y = 0; y < height; y++, i++)
  318. {
  319. CommonUI.Display.Color.toRGBAF(rgba[i], out color.r, out color.g, out color.b, out color.a);
  320. destTex.SetPixel(x, y, color);
  321. }
  322. }
  323. destTex.Apply();
  324. return (Image)(new UnityImage(destTex, string.Format("createRGBImage({0},{1})", width, height)));
  325. }
  326. public override Image createRGBImage(int width, int height)
  327. {
  328. UnityEngine.Texture2D destTex = new UnityEngine.Texture2D(width, height, TextureFormat.ARGB32, false, true);
  329. UnityEngine.Color color = new UnityEngine.Color(0, 0, 0, 0);
  330. for (int x = 0; x < width; x++)
  331. {
  332. for (int y = 0; y < height; y++)
  333. {
  334. destTex.SetPixel(x, y, color);
  335. }
  336. }
  337. destTex.Apply();
  338. return (Image)(new UnityImage(destTex, string.Format("createRGBImage({0},{1})", width, height)));
  339. }
  340. public override TextLayer createTextLayer(string text, float size, CommonUI.Display.FontStyle style)
  341. {
  342. return new UnityTextLayer(text, style, size);
  343. }
  344. public override bool testTextLineBreak(string text, float size, CommonUI.Display.FontStyle style,
  345. int borderTime,
  346. float testWidth,
  347. out float realWidth,
  348. out float realHeight)
  349. {
  350. return sPlatform.TestTextLineBreak(text, size, style, borderTime, testWidth, out realWidth, out realHeight);
  351. }
  352. public override VertexBuffer createVertexBuffer(int capacity)
  353. {
  354. return new UnityVertexBuffer(capacity);
  355. }
  356. #endregion
  357. // ---------------------------------------------------------------------------------
  358. #region Resource
  359. static private MPQFileSystem mFileSystem;
  360. static private string TestDataPath = String.Empty;
  361. static public void SetTestDataPath(string path)
  362. {
  363. TestDataPath = path;
  364. }
  365. static public void AddFileSystem(MPQFileSystem fs)
  366. {
  367. mFileSystem = fs;
  368. }
  369. private static T LoadFromResources<T>(string path) where T : UnityEngine.Object
  370. {
  371. int index = path.LastIndexOf(".");
  372. if (index < 0) { return null; }
  373. // Unity TextAsset
  374. string assetpath = path.Substring(0, index);
  375. while (assetpath.StartsWith("/"))
  376. {
  377. assetpath = assetpath.Substring(1);
  378. }
  379. T ta = UnityEngine.Resources.Load<T>(assetpath);
  380. //Debug.Log("LoadFromAsserts ==========> " + assetpath + " --- " + ta);
  381. return ta;
  382. }
  383. private static object LoadObjectFromResources(string path)
  384. {
  385. // Unity TextAsset
  386. string assetpath = path.Substring(0, path.LastIndexOf("."));
  387. while (assetpath.StartsWith("/"))
  388. {
  389. assetpath = assetpath.Substring(1);
  390. }
  391. return UnityEngine.Resources.Load(assetpath);
  392. }
  393. //-----------------------------------------------------------------------------------------------------------------
  394. #region IResourceLoader
  395. #region load
  396. private bool TryLoadFromTest(string path, ref byte[] ret)
  397. {
  398. string fullpath = TestDataPath + "/" + path;
  399. try
  400. {
  401. fullpath = System.IO.Path.GetFullPath(fullpath);
  402. if (System.IO.File.Exists(fullpath))
  403. {
  404. ret = File.ReadAllBytes(fullpath);
  405. if (ret != null)
  406. {
  407. if (IsDebug)
  408. {
  409. Debug.Log("Load Data From Test Path : " + fullpath + " -> " + ret.Length + " (bytes)");
  410. }
  411. return true;
  412. }
  413. }
  414. }
  415. catch (Exception err)
  416. {
  417. Debug.LogError(err.Message);
  418. Debug.LogError("Load Data From Test Path : " + fullpath + " -> " + ret.Length + " (bytes)");
  419. }
  420. return false;
  421. }
  422. private bool TryLoadFromFileSystem(string path, ref byte[] ret)
  423. {
  424. string fullpath = path;
  425. ret = File.ReadAllBytes(fullpath);
  426. if (ret != null)
  427. {
  428. if (IsDebug)
  429. {
  430. Debug.Log("Load Data From Path : " + fullpath + " -> " + ret.Length + " (bytes)");
  431. }
  432. return true;
  433. }
  434. return false;
  435. }
  436. private bool TryLoadFromMPQ(string path, ref byte[] ret)
  437. {
  438. if (mFileSystem != null)
  439. {
  440. ret = mFileSystem.getData(path);
  441. if (ret != null)
  442. {
  443. if (IsDebug)
  444. {
  445. Debug.Log("Load Data From MPQ : " + path + " -> " + ret.Length + " (bytes)");
  446. }
  447. return true;
  448. }
  449. }
  450. return false;
  451. }
  452. private bool TryLoadFromResources(string path, ref byte[] ret)
  453. {
  454. TextAsset data = LoadFromResources<TextAsset>(path);
  455. if (data != null)
  456. {
  457. ret = data.bytes;
  458. if (IsDebug)
  459. {
  460. Debug.Log("Load Data From Unity Resources : " + path + " -> " + ret.Length + " (bytes)");
  461. }
  462. return true;
  463. }
  464. return false;
  465. }
  466. private bool TryLoadFromJAR(string path, ref byte[] ret)
  467. {
  468. var data = CommonUI_Unity3D_Android.WWWHelper.getJavaData(path);
  469. //yield return data;
  470. if (data != null)
  471. {
  472. ret = data;
  473. if (IsDebug)
  474. {
  475. Debug.Log("Load Data From JAR : " + path + " -> " + ret.Length + " (bytes)");
  476. }
  477. return true;
  478. }
  479. return false;
  480. }
  481. #endregion
  482. //------------------------------------------------------------------------------------------------------------------------
  483. #region stream
  484. private bool TryOpenFromTest(string path, ref Stream ret)
  485. {
  486. string fullpath = TestDataPath + "/" + path;
  487. try
  488. {
  489. fullpath = System.IO.Path.GetFullPath(fullpath);
  490. if (System.IO.File.Exists(fullpath))
  491. {
  492. ret = new FileStream(fullpath, FileMode.Open, FileAccess.Read, FileShare.Read);// File.ReadAllBytes(fullpath);
  493. if (ret != null)
  494. {
  495. if (IsDebug)
  496. {
  497. Debug.Log("Load Data From Test Path : " + fullpath + " -> " + ret.Length + " (bytes)");
  498. }
  499. return true;
  500. }
  501. }
  502. }
  503. catch (Exception err)
  504. {
  505. Debug.LogError(err.Message);
  506. Debug.LogError("Load Data From Test Path : " + fullpath + " -> " + ret.Length + " (bytes)");
  507. }
  508. return false;
  509. }
  510. private bool TryOpenFromFileSystem(string path, ref Stream ret)
  511. {
  512. string fullpath = path;
  513. ret = new FileStream(fullpath, FileMode.Open, FileAccess.Read, FileShare.Read); //File.ReadAllBytes(fullpath);
  514. if (ret != null)
  515. {
  516. if (IsDebug)
  517. {
  518. Debug.Log("Load Data From Path : " + fullpath + " -> " + ret.Length + " (bytes)");
  519. }
  520. return true;
  521. }
  522. return false;
  523. }
  524. private bool TryOpenFromMPQ(string path, ref Stream ret)
  525. {
  526. if (mFileSystem != null)
  527. {
  528. ret = mFileSystem.openStream(path);
  529. if (ret != null)
  530. {
  531. if (IsDebug)
  532. {
  533. Debug.Log("Load Data From MPQ : " + path + " -> " + ret.Length + " (bytes)");
  534. }
  535. return true;
  536. }
  537. }
  538. return false;
  539. }
  540. private bool TryOpenFromResources(string path, ref Stream ret)
  541. {
  542. return false;
  543. }
  544. private bool TryOpenFromJAR(string path, ref Stream ret)
  545. {
  546. return false;
  547. }
  548. #endregion
  549. //------------------------------------------------------------------------------------------------------------------------
  550. #region exist
  551. private bool TryExistDataFromMPQ(string path)
  552. {
  553. if (mFileSystem != null)
  554. {
  555. if (mFileSystem.findEntry(path) != null)
  556. {
  557. return true;
  558. }
  559. }
  560. return false;
  561. }
  562. private bool TryExistDataFromFileSystem(string path)
  563. {
  564. if (File.Exists(path))
  565. {
  566. return true;
  567. }
  568. return false;
  569. }
  570. private bool TryExistDataFromTestPath(string path)
  571. {
  572. if (Directory.Exists(TestDataPath))
  573. {
  574. string fullpath = TestDataPath + "/" + path;
  575. try
  576. {
  577. fullpath = System.IO.Path.GetFullPath(fullpath);
  578. if (System.IO.File.Exists(fullpath))
  579. {
  580. return true;
  581. }
  582. }
  583. catch (Exception err) { Assert(false, "ExitDataFromTestPath Error:" + err.ToString() + path.ToString()); }
  584. }
  585. return false;
  586. }
  587. private bool TryExistDataFromJAR(string path)
  588. {
  589. return CommonUI_Unity3D_Android.WWWHelper.isFileExists(path);
  590. }
  591. #endregion
  592. //------------------------------------------------------------------------------------------------------------------------
  593. public const string PREFIX_FILE = "file://";
  594. public const string PREFIX_MPQ = "mpq://";
  595. public const string PREFIX_RES = "res://";
  596. public const string PREFIX_JAR = "jar://";
  597. //"jar:file://" + Application.dataPath + "!/assets/";
  598. public bool ExistData(string path)
  599. {
  600. bool rlt = false;
  601. //From MPQ.
  602. if (path.StartsWith(PREFIX_MPQ))
  603. {
  604. rlt = TryExistDataFromMPQ(path.Substring(PREFIX_MPQ.Length));
  605. return rlt;
  606. }
  607. if (path.StartsWith(PREFIX_FILE))
  608. {
  609. rlt = TryExistDataFromFileSystem(path.Substring(PREFIX_FILE.Length));
  610. return rlt;
  611. }
  612. if (path.StartsWith(PREFIX_JAR))
  613. {
  614. rlt = TryExistDataFromJAR(path.Substring(PREFIX_JAR.Length));
  615. return rlt;
  616. }
  617. if (IsAndroid && path.StartsWith(Application.streamingAssetsPath))
  618. {
  619. rlt = TryExistDataFromJAR(path.Substring(Application.streamingAssetsPath.Length));
  620. return rlt;
  621. }
  622. //From MPQ.
  623. rlt = TryExistDataFromMPQ(path);
  624. if (rlt) { return rlt; }
  625. //From Test.
  626. rlt = TryExistDataFromTestPath(path);
  627. if (rlt) { return rlt; }
  628. //From File.
  629. rlt = TryExistDataFromFileSystem(path);
  630. if (rlt) { return rlt; }
  631. return rlt;
  632. }
  633. public byte[] LoadData(string path)
  634. {
  635. byte[] ret = null;
  636. // Specify Prefix.
  637. if (path.StartsWith(PREFIX_MPQ))
  638. {
  639. TryLoadFromMPQ(path.Substring(PREFIX_MPQ.Length), ref ret);
  640. return ret;
  641. }
  642. if (path.StartsWith(PREFIX_FILE))
  643. {
  644. TryLoadFromFileSystem(path.Substring(PREFIX_FILE.Length), ref ret);
  645. return ret;
  646. }
  647. if (path.StartsWith(PREFIX_RES))
  648. {
  649. TryLoadFromResources(path.Substring(PREFIX_RES.Length), ref ret);
  650. return ret;
  651. }
  652. if (path.StartsWith(PREFIX_JAR))
  653. {
  654. TryLoadFromJAR(path.Substring(PREFIX_JAR.Length), ref ret);
  655. return ret;
  656. }
  657. if (IsAndroid && path.StartsWith(Application.streamingAssetsPath))
  658. {
  659. TryLoadFromJAR(path.Substring(Application.streamingAssetsPath.Length), ref ret);
  660. return ret;
  661. }
  662. // Just Test.
  663. if (Directory.Exists(TestDataPath) && TryLoadFromTest(path, ref ret))
  664. {
  665. return ret;
  666. }
  667. // MPQ.
  668. if (mFileSystem != null && TryLoadFromMPQ(path, ref ret))
  669. {
  670. return ret;
  671. }
  672. // File.
  673. if (File.Exists(path) && TryLoadFromFileSystem(path, ref ret))
  674. {
  675. return ret;
  676. }
  677. // Applicateion Data Path.
  678. if (TryLoadFromResources(path, ref ret))
  679. {
  680. return ret;
  681. }
  682. if (IsDebug)
  683. {
  684. Debug.LogWarning("Can Not Read Resource : " + path);
  685. }
  686. return ret;
  687. }
  688. public virtual Stream LoadDataAsStream(string path)
  689. {
  690. Stream ret = null;
  691. // Specify Prefix.
  692. if (path.StartsWith(PREFIX_MPQ))
  693. {
  694. TryOpenFromMPQ(path.Substring(PREFIX_MPQ.Length), ref ret);
  695. return ret;
  696. }
  697. if (path.StartsWith(PREFIX_FILE))
  698. {
  699. TryOpenFromFileSystem(path.Substring(PREFIX_FILE.Length), ref ret);
  700. return ret;
  701. }
  702. // Just Test.
  703. if (Directory.Exists(TestDataPath) && TryOpenFromTest(path, ref ret))
  704. {
  705. return ret;
  706. }
  707. // MPQ.
  708. if (mFileSystem != null && TryOpenFromMPQ(path, ref ret))
  709. {
  710. return ret;
  711. }
  712. // File.
  713. if (File.Exists(path) && TryOpenFromFileSystem(path, ref ret))
  714. {
  715. return ret;
  716. }
  717. if (IsDebug)
  718. {
  719. Debug.LogWarning("Can Not Read Resource : " + path);
  720. }
  721. return ret;
  722. }
  723. public virtual string[] ListFiles(string path) { throw new NotImplementedException(); }
  724. #endregion
  725. //-----------------------------------------------------------------------------------------------------------------
  726. public AssetBundleCreateRequest LoadAssetBundle(string path, out int size)
  727. {
  728. size = 0;
  729. byte[] bin = LoadData(path);
  730. if (bin != null)
  731. {
  732. size = bin.Length;
  733. try
  734. {
  735. return AssetBundle.LoadFromMemoryAsync(bin);
  736. }
  737. catch (Exception err)
  738. {
  739. Assert(false, "LoadAssetBundle : Error " + path + "\n" + err.Message);
  740. }
  741. }
  742. return null;
  743. }
  744. public AssetBundleCreateRequest LoadAssetBundle(string path)
  745. {
  746. byte[] bin = LoadData(path);
  747. if (bin != null)
  748. {
  749. try
  750. {
  751. return AssetBundle.LoadFromMemoryAsync(bin);
  752. }
  753. catch (Exception err)
  754. {
  755. Assert(false, "LoadAssetBundle : Error " + path + "\n" + err.Message);
  756. }
  757. }
  758. return null;
  759. }
  760. public AssetBundle LoadAssetBundleImmediate(string path)
  761. {
  762. byte[] bin = LoadData(path);
  763. if (bin != null)
  764. {
  765. try
  766. {
  767. return AssetBundle.LoadFromMemory(bin);
  768. }
  769. catch (Exception err)
  770. {
  771. Assert(false, "LoadAssetBundleImmediate : Error " + path + "\n" + err.Message);
  772. }
  773. }
  774. return null;
  775. }
  776. #endregion
  777. // ---------------------------------------------------------------------------------
  778. #region TextInput
  779. #endregion
  780. // ---------------------------------------------------------------------------------
  781. #region MPQ
  782. public class MPQAdapter : MPQDirver
  783. {
  784. public override long GetAvaliableSpace(string path)
  785. {
  786. return sPlatform.GetAvaliableSpace(path);
  787. }
  788. public override long GetTotalSpace(string path)
  789. {
  790. return sPlatform.GetTotalSpace(path);
  791. }
  792. public override bool RunGetFileMD5(string fullname, out string md5)
  793. {
  794. return sPlatform.NativeGetFileMD5(fullname, out md5);
  795. }
  796. public override bool RunUnzipSingle(MPQUpdater updater, MPQUpdater.RemoteFileInfo zip, MPQUpdater.RemoteFileInfo mpq, AtomicLong process)
  797. {
  798. return sPlatform.NativeDecompressFile(updater, zip, mpq, process);
  799. }
  800. }
  801. public static MPQUpdater CreateMPQUpdater(
  802. Uri remote_version_url,
  803. string[] remote_version_prefix,
  804. string version_suffix,
  805. DirectoryInfo local_save_root,
  806. DirectoryInfo local_bundle_root,
  807. bool validate_md5,
  808. MPQUpdaterListener listener)
  809. {
  810. sPlatform.GetAvaliableSpace(local_save_root.FullName);
  811. sPlatform.GetTotalSpace(local_save_root.FullName);
  812. var ret = new MPQUpdater(new MPQAdapter());
  813. ret.Init(
  814. remote_version_url,
  815. remote_version_prefix,
  816. version_suffix,
  817. local_save_root,
  818. local_bundle_root,
  819. "",
  820. validate_md5,
  821. listener);
  822. return ret;
  823. }
  824. public static MPQUpdater CreateMPQUpdater(
  825. string[] remote_version_prefix,
  826. string version_suffix,
  827. DirectoryInfo local_save_root,
  828. DirectoryInfo local_bundle_root,
  829. bool validate_md5,
  830. MPQUpdaterListener listener)
  831. {
  832. sPlatform.GetAvaliableSpace(local_save_root.FullName);
  833. sPlatform.GetTotalSpace(local_save_root.FullName);
  834. var ret = new MPQUpdater(new MPQAdapter());
  835. ret.Init(
  836. new Uri(""),
  837. remote_version_prefix,
  838. version_suffix,
  839. local_save_root,
  840. local_bundle_root,
  841. "",
  842. validate_md5,
  843. listener);
  844. return ret;
  845. }
  846. #endregion
  847. // ---------------------------------------------------------------------------------
  848. public class DummyUnityPlatform : IUnityPlatform
  849. {
  850. public bool IsNativeUnzip { get { return false; } }
  851. public void Assert(string msg) { }
  852. public Texture2D SysFontTexture(string text, bool readable, CommonUI.Display.FontStyle style, int fontSize, uint fontColor, int borderTime, uint borderColor, Size2D expectSize, out int boundW, out int boundH)
  853. {
  854. boundW = 8;
  855. boundH = 8;
  856. return new Texture2D(8, 8, TextureFormat.ARGB32, false, true);
  857. }
  858. public bool TestTextLineBreak(string text, float size, CommonUI.Display.FontStyle style, int borderTime, float testWidth, out float realWidth, out float realHeight)
  859. {
  860. realWidth = 8;
  861. realHeight = 8;
  862. return false;
  863. }
  864. public void CopyPixels(Texture2D src, int sx, int sy, int sw, int sh, Texture2D dst, int dx, int dy) { }
  865. public long GetAvaliableSpace(string path) { return long.MaxValue; }
  866. public long GetTotalSpace(string path) { return long.MaxValue; }
  867. public bool NativeDecompressFile(MPQUpdater updater, MPQUpdater.RemoteFileInfo zip_file, MPQUpdater.RemoteFileInfo mpq_file, AtomicLong current_unzip_bytes)
  868. {
  869. throw new NotImplementedException();
  870. }
  871. public bool NativeDecompressMemory(ArraySegment<byte> src, ArraySegment<byte> dst)
  872. {
  873. throw new NotImplementedException();
  874. }
  875. public bool NativeGetFileMD5(string fullname, out string md5)
  876. {
  877. throw new NotImplementedException();
  878. }
  879. }
  880. }
  881. }