MPQResourceLoader.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using CommonLang.IO;
  2. using CommonLang.Log;
  3. using MPQ.FileSystem;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. namespace MPQ.Resource
  10. {
  11. public class MPQResourceLoader : DefaultResourceLoader
  12. {
  13. public const string PREFIX_MPQ = "mpq://";
  14. protected MPQFileSystem mFileSystem;
  15. public MPQResourceLoader(string root = ".", MPQFileSystem fs = null)
  16. : base(root)
  17. {
  18. mFileSystem = fs;
  19. CommonLang.IO.Resource.SetLoader(this);
  20. }
  21. //------------------------------------------------------------------------------
  22. #region MPQ
  23. protected virtual bool _TryLoadFromMPQ(string path, ref byte[] ret)
  24. {
  25. if (mFileSystem != null)
  26. {
  27. ret = mFileSystem.getData(path);
  28. return ret != null;
  29. }
  30. return false;
  31. }
  32. protected virtual bool _TryLoadFromMPQ(string path, ref Stream ret)
  33. {
  34. if (mFileSystem != null)
  35. {
  36. ret = mFileSystem.openStream(path);
  37. return ret != null;
  38. }
  39. return false;
  40. }
  41. protected virtual bool _ExistWithMPQ(string path)
  42. {
  43. if (mFileSystem != null)
  44. {
  45. return mFileSystem.findEntry(path) != null;
  46. }
  47. return false;
  48. }
  49. #endregion
  50. //------------------------------------------------------------------------------
  51. public override bool ExistData(string path)
  52. {
  53. if (path.StartsWith(PREFIX_MPQ))
  54. {
  55. return _ExistWithMPQ(path.Substring(PREFIX_MPQ.Length));
  56. }
  57. if (path.StartsWith(PREFIX_FILE))
  58. {
  59. return _ExistWithFileSystem(path.Substring(PREFIX_FILE.Length));
  60. }
  61. if (path.StartsWith(PREFIX_RES))
  62. {
  63. return false;
  64. }
  65. if (mFileSystem != null && _ExistWithMPQ(path))
  66. {
  67. return true;
  68. }
  69. if (File.Exists(path) && _ExistWithFileSystem(path))
  70. {
  71. return true;
  72. }
  73. return false;
  74. }
  75. public override byte[] LoadData(string path)
  76. {
  77. byte[] ret = null;
  78. // Specify Prefix
  79. if (path.StartsWith(PREFIX_MPQ))
  80. {
  81. _TryLoadFromMPQ(path.Substring(PREFIX_MPQ.Length), ref ret);
  82. return ret;
  83. }
  84. if (path.StartsWith(PREFIX_FILE))
  85. {
  86. _TryLoadFromFileSystem(path.Substring(PREFIX_FILE.Length), ref ret);
  87. return ret;
  88. }
  89. if (path.StartsWith(PREFIX_RES))
  90. {
  91. return ret;
  92. }
  93. // MPQ
  94. if (mFileSystem != null && _TryLoadFromMPQ(path, ref ret))
  95. {
  96. return ret;
  97. }
  98. // File
  99. if (File.Exists(path) && _TryLoadFromFileSystem(path, ref ret))
  100. {
  101. return ret;
  102. }
  103. return ret;
  104. }
  105. public override Stream LoadDataAsStream(string path)
  106. {
  107. Stream ret = null;
  108. // Specify Prefix
  109. if (path.StartsWith(PREFIX_MPQ))
  110. {
  111. _TryLoadFromMPQ(path.Substring(PREFIX_MPQ.Length), ref ret);
  112. return ret;
  113. }
  114. if (path.StartsWith(PREFIX_FILE))
  115. {
  116. _TryLoadFromFileSystem(path.Substring(PREFIX_FILE.Length), ref ret);
  117. return ret;
  118. }
  119. if (path.StartsWith(PREFIX_RES))
  120. {
  121. return ret;
  122. }
  123. // MPQ
  124. if (mFileSystem != null && _TryLoadFromMPQ(path, ref ret))
  125. {
  126. return ret;
  127. }
  128. // File
  129. if (File.Exists(path) && _TryLoadFromFileSystem(path, ref ret))
  130. {
  131. return ret;
  132. }
  133. return ret;
  134. }
  135. }
  136. }