SP2.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. namespace OpenVIII
  8. {
  9. /// <summary>
  10. /// SP2 is a handler for .sp1 and .sp2 files. They are texture atlas coordinates
  11. /// <para>This stores the entries in Entry objects or EntryGroup objects</para>
  12. /// </summary>
  13. // ReSharper disable once InconsistentNaming
  14. public abstract partial class SP2
  15. {
  16. #region Enums
  17. /// <summary>
  18. /// enum to be added to class when implemented
  19. /// </summary>
  20. // ReSharper disable once InconsistentNaming
  21. public enum ID { NotImplemented }
  22. #endregion Enums
  23. #region Properties
  24. /// <summary>
  25. /// Number of Entries
  26. /// </summary>
  27. public uint Count { get; protected set; }
  28. /// <summary>
  29. /// Number of Palettes
  30. /// </summary>
  31. public uint PaletteCount { get; protected set; }
  32. protected Memory.Archive ArchiveString { get; set; }
  33. /// <summary>
  34. /// Dictionary of Entries
  35. /// </summary>
  36. protected virtual Dictionary<uint, Entry> Entries { get; set; }
  37. /// <summary>
  38. /// Entries per texture,ID MOD EntriesPerTexture to get current entry to use on this texture
  39. /// </summary>
  40. public virtual int EntriesPerTexture { get; protected set; }
  41. /// <summary>
  42. /// If true disable mods and high res textures.
  43. /// </summary>
  44. protected bool ForceOriginal { get; set; } = false;
  45. /// <summary>
  46. /// *.sp1 or *.sp2 that contains the entries or entrygroups. With Rectangle and offset information.
  47. /// </summary>
  48. protected string IndexFilename { get; set; }
  49. protected List<TexProps> Props { get; set; }
  50. /// <summary>
  51. /// Should be Vector2.One unless reading a high res version of textures.
  52. /// </summary>
  53. protected Dictionary<uint, Vector2> Scale { get; set; }
  54. /// <summary>
  55. /// List of textures
  56. /// </summary>
  57. protected virtual List<TextureHandler> Textures { get; set; }
  58. /// <summary>
  59. /// Some textures start with 1 and some start with 0. This is added to the current number in
  60. /// when reading the files in.
  61. /// </summary>
  62. protected int TextureStartOffset { get; set; }
  63. #endregion Properties
  64. #region Indexers
  65. public Entry this[Enum id] => GetEntry(id);
  66. #endregion Indexers
  67. #region Methods
  68. public static T Load<T>() where T : SP2, new()
  69. {
  70. Memory.Log.WriteLine($"{nameof(SP2)} :: {nameof(Load)} :: {typeof(T)} ");
  71. var r = new T();
  72. r.DefaultValues();
  73. r.Init();
  74. return r;
  75. }
  76. /// <summary>
  77. /// Draw Item
  78. /// </summary>
  79. /// <param name="id"></param>
  80. /// <param name="dst"></param>
  81. /// <param name="fade"></param>
  82. public virtual void Draw(Enum id, Rectangle dst, float fade = 1)
  83. {
  84. var src = GetEntry(id).GetRectangle;
  85. var tex = GetTexture(id);
  86. tex.Draw(dst, src, Color.White * fade);
  87. }
  88. public virtual void Draw(Enum id, Rectangle dst, Vector2 fill, float fade = 1)
  89. {
  90. var entry = GetEntry(id);
  91. if (entry != null)
  92. {
  93. var src = entry.GetRectangle;
  94. if (fill == Vector2.UnitX)
  95. {
  96. var r = (float)dst.Height / dst.Width;
  97. src.Height = (int)Math.Round(src.Height * r);
  98. }
  99. else if (fill == Vector2.UnitY)
  100. {
  101. var r = (float)dst.Width / dst.Height;
  102. src.Width = (int)Math.Round(src.Width * r);
  103. }
  104. var tex = GetTexture(id);
  105. tex?.Draw(dst, src, Color.White * fade);
  106. }
  107. }
  108. public virtual Entry GetEntry(Enum id) => GetEntry(Convert.ToUInt32(id));
  109. public virtual Entry GetEntry(uint id)
  110. {
  111. if (EntriesPerTexture <= 0 && Entries.ContainsKey(id))
  112. return Entries[id];
  113. else if (Entries?.ContainsKey((uint)(id % EntriesPerTexture))??false)
  114. return Entries[(uint)(id % EntriesPerTexture)];
  115. return null;
  116. }
  117. public virtual TextureHandler GetTexture(Enum id, int file = -1)
  118. {
  119. var pos = Convert.ToInt32(id);
  120. file = file >= 0 ? file : GetEntry((uint)pos).File;
  121. //check if we set a custom file and we have a pos more then set entries per texture
  122. if (file <= 0)
  123. {
  124. if (EntriesPerTexture > 0 && pos >= EntriesPerTexture)
  125. file = (pos / EntriesPerTexture);
  126. }
  127. if (file <= 0) return Textures.Count > file ? Textures[file] : null;
  128. var j = (int)Props.Sum(x => x.Count);
  129. if (file >= j)
  130. {
  131. file %= j;
  132. }
  133. return Textures.Count>file ? Textures[file] : null;
  134. }
  135. public virtual TextureHandler GetTexture(Enum id, out Vector2 scale)
  136. {
  137. scale = Scale[GetEntry(id).File];
  138. return GetTexture(id);
  139. }
  140. public virtual void Trim(Enum ic, byte pal)
  141. {
  142. var entry = this[ic];
  143. entry.SetTrimNonGroup(Textures[pal]);
  144. }
  145. protected virtual VertexPositionTexture_Texture2D Quad(Enum ic, byte pal, float scale = .25f, Box_Options options = Box_Options.Middle | Box_Options.Center, float z = 0f)
  146. {
  147. Trim(ic, pal);
  148. return Quad(this[ic], Textures[pal], scale, options, z);
  149. }
  150. protected virtual VertexPositionTexture_Texture2D Quad(Entry entry, TextureHandler texture, float scale = .25f, Box_Options options = Box_Options.Middle | Box_Options.Center, float z = 0f)
  151. {
  152. var rectangle = entry.GetRectangle;
  153. var scaleFactor = texture.ScaleFactor;
  154. var offset = options.HasFlag(Box_Options.UseOffset) ? entry.Offset : Vector2.Zero;
  155. var vpt = new VertexPositionTexture[6];
  156. float left, right, bottom, top;
  157. if (options.HasFlag(Box_Options.Left))
  158. {
  159. left = 0;
  160. right = rectangle.Width;
  161. }
  162. else if (options.HasFlag(Box_Options.Right))
  163. {
  164. left = -rectangle.Width;
  165. right = 0;
  166. }
  167. else// (options.HasFlag(Box_Options.Center))
  168. {
  169. left = -rectangle.Width / 2f;
  170. right = rectangle.Width / 2f;
  171. }
  172. if (options.HasFlag(Box_Options.Top))
  173. {
  174. bottom = 0;
  175. top = rectangle.Height;
  176. }
  177. else if (options.HasFlag(Box_Options.Buttom))
  178. {
  179. bottom = -rectangle.Height;
  180. top = 0;
  181. }
  182. else //(options.HasFlag(Box_Options.Middle))
  183. {
  184. bottom = -rectangle.Height / 2f;
  185. top = rectangle.Height / 2f;
  186. }
  187. var v = new VertexPositionTexture[]
  188. {
  189. new VertexPositionTexture(new Vector3(left+offset.X,top+offset.Y,z)*scale,new Vector2(rectangle.Right*scaleFactor.X/texture.Width,rectangle.Top*scaleFactor.Y/texture.Height)),
  190. new VertexPositionTexture(new Vector3(right+offset.X,top+offset.Y,z)*scale,new Vector2(rectangle.Left*scaleFactor.X/texture.Width,rectangle.Top*scaleFactor.Y/texture.Height)),
  191. new VertexPositionTexture(new Vector3(right+offset.X,bottom+offset.Y,z)*scale,new Vector2(rectangle.Left*scaleFactor.X/texture.Width,rectangle.Bottom*scaleFactor.Y/texture.Height)),
  192. new VertexPositionTexture(new Vector3(left+offset.X,bottom+offset.Y,z)*scale,new Vector2(rectangle.Right*scaleFactor.X/texture.Width,rectangle.Bottom*scaleFactor.Y/texture.Height)),
  193. };
  194. vpt[0] = v[0];
  195. vpt[1] = v[1];
  196. vpt[2] = v[3];
  197. vpt[3] = v[1];
  198. vpt[4] = v[2];
  199. vpt[5] = v[3];
  200. return new VertexPositionTexture_Texture2D(vpt, texture);
  201. }
  202. protected virtual void DefaultValues()
  203. {
  204. Count = 0;
  205. PaletteCount = 1;
  206. EntriesPerTexture = 1;
  207. Scale = null;
  208. TextureStartOffset = 0;
  209. IndexFilename = "";
  210. Textures = null;
  211. Entries = null;
  212. ArchiveString = Memory.Archives.A_MENU;
  213. }
  214. protected virtual void Init()
  215. {
  216. if (Entries == null)
  217. {
  218. var aw = ArchiveWorker.Load(ArchiveString);
  219. InitEntries(aw);
  220. InsertCustomEntries();
  221. InitTextures<TEX>(aw);
  222. }
  223. }
  224. protected virtual void InitEntries(ArchiveBase aw = null)
  225. {
  226. if (Entries != null) return;
  227. if (aw == null)
  228. aw = ArchiveWorker.Load(ArchiveString);
  229. MemoryStream ms;
  230. var buffer = aw.GetBinaryFile(IndexFilename);
  231. if (buffer == null) return;
  232. using (var br = new BinaryReader(ms = new MemoryStream(buffer)))
  233. {
  234. Count = br.ReadUInt32();
  235. var locations = new ushort[Count];
  236. Entries = new Dictionary<uint, Entry>((int)Count);
  237. for (uint i = 0; i < Count; i++)
  238. {
  239. locations[i] = br.ReadUInt16();
  240. ms.Seek(2, SeekOrigin.Current);
  241. }
  242. byte fid = 0;
  243. Entry last = null;
  244. for (uint i = 0; i < Count; i++)
  245. {
  246. ms.Seek(locations[i] + 6, SeekOrigin.Begin);
  247. var t = br.ReadByte();
  248. if (t == 0 || t == 96) // known invalid entries in sp2 files have this value. there might be more to it.
  249. {
  250. Count = i;
  251. break;
  252. }
  253. Entries[i] = new Entry();
  254. Entries[i].LoadfromStreamSP2(br, locations[i], last, ref fid);
  255. last = Entries[i];
  256. }
  257. }
  258. }
  259. protected virtual void InitTextures<T>(ArchiveBase aw = null) where T : Texture_Base, new()
  260. {
  261. var count = (int)Props.Sum(x => x.Count);
  262. if (Textures == null)
  263. Textures = new List<TextureHandler>(count);
  264. if (Textures.Count <= 0)
  265. {
  266. if (aw == null)
  267. aw = ArchiveWorker.Load(ArchiveString);
  268. T tex;
  269. Scale = new Dictionary<uint, Vector2>(count);
  270. var b = 0;
  271. for (var j = 0; j < Props.Count; j++)
  272. for (uint i = 0; i < Props[j].Count; i++)
  273. {
  274. tex = new T();
  275. var buffer = aw.GetBinaryFile(string.Format(Props[j].Filename, i + TextureStartOffset));
  276. if (buffer != null)
  277. {
  278. tex.Load(buffer);
  279. if (Props[j].Big != null && ForceOriginal == false && b < Props[j].Big.Count)
  280. {
  281. var th = TextureHandler.Create(Props[j].Big[b].Filename, tex, 2, Props[j].Big[b++].Split / 2);
  282. Textures.Add(th);
  283. Scale[i] = Vector2.One;
  284. }
  285. else
  286. {
  287. var th = TextureHandler.Create(Props[j].Filename, tex);
  288. Textures.Add(th);
  289. Scale[i] = th.GetScale(); //scale might not be used outside of texturehandler.
  290. }
  291. }
  292. }
  293. }
  294. }
  295. protected virtual void InsertCustomEntries()
  296. {
  297. }
  298. #endregion Methods
  299. }
  300. }