SP2.cs 12 KB

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