SP2.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. namespace FF8
  7. {
  8. public abstract class SP2
  9. {
  10. #region Constructors
  11. protected SP2()
  12. {
  13. Count = 0;
  14. PalletCount = 1;
  15. EntriesPerTexture = 1;
  16. Scale = null;
  17. TextureStartOffset = 0;
  18. IndexFilename = "";
  19. Textures = null;
  20. Entries = null;
  21. ArchiveString = Memory.Archives.A_MENU;
  22. }
  23. #endregion Constructors
  24. #region Enums
  25. /// <summary>
  26. /// enum to be added to class when implemented
  27. /// </summary>
  28. public enum ID { NotImplemented }
  29. #endregion Enums
  30. #region Properties
  31. /// <summary>
  32. /// Number of Entries
  33. /// </summary>
  34. public uint Count { get; protected set; }
  35. /// <summary>
  36. /// Number of Pallets
  37. /// </summary>
  38. public uint PalletCount { get; protected set; }
  39. protected string ArchiveString { get; set; }
  40. /// <summary>
  41. /// Dictionary of Entries
  42. /// </summary>
  43. protected virtual Dictionary<uint, Entry> Entries { get; set; }
  44. /// <summary>
  45. /// Entries per texture,ID MOD EntriesPerTexture to get current entry to use on this texture
  46. /// </summary>
  47. protected virtual int EntriesPerTexture { get; set; }
  48. /// <summary>
  49. /// If true disable mods and high res textures.
  50. /// </summary>
  51. protected bool FORCE_ORIGINAL { get; set; } = false;
  52. /// <summary>
  53. /// *.sp1 or *.sp2 that contains the entries or entrygroups. With Rectangle and offset information.
  54. /// </summary>
  55. protected string IndexFilename { get; set; }
  56. protected List<TexProps> Props { get; set; }
  57. /// <summary>
  58. /// Should be Vector2.One unless reading a high res version of textures.
  59. /// </summary>
  60. protected Dictionary<uint, Vector2> Scale { get; set; }
  61. /// <summary>
  62. /// List of textures
  63. /// </summary>
  64. protected virtual List<TextureHandler> Textures { get; set; }
  65. /// <summary>
  66. /// Some textures start with 1 and some start with 0. This is added to the current number in
  67. /// when reading the files in.
  68. /// </summary>
  69. protected int TextureStartOffset { get; set; }
  70. #endregion Properties
  71. #region Indexers
  72. public Entry this[Enum id] => GetEntry(id);
  73. #endregion Indexers
  74. #region Methods
  75. /// <summary>
  76. /// Draw Item
  77. /// </summary>
  78. /// <param name="id"></param>
  79. /// <param name="dst"></param>
  80. /// <param name="fade"></param>
  81. public virtual void Draw(Enum id, Rectangle dst, float fade = 1)
  82. {
  83. Rectangle src = GetEntry(id).GetRectangle;
  84. TextureHandler tex = GetTexture(id);
  85. tex.Draw(dst, src, Color.White * fade);
  86. }
  87. public virtual void Draw(Enum id, Rectangle dst, Vector2 fill, float fade = 1)
  88. {
  89. Rectangle src = GetEntry(id).GetRectangle;
  90. if (fill == Vector2.UnitX)
  91. {
  92. float r = (float)dst.Height / dst.Width;
  93. src.Height = (int)Math.Round(src.Height * r);
  94. }
  95. else if (fill == Vector2.UnitY)
  96. {
  97. float r = (float)dst.Width / dst.Height;
  98. src.Width = (int)Math.Round(src.Width * r);
  99. }
  100. TextureHandler tex = GetTexture(id);
  101. tex.Draw(dst, src, Color.White * fade);
  102. }
  103. public virtual Entry GetEntry(Enum id)
  104. {
  105. if (Entries.ContainsKey(Convert.ToUInt32(id)))
  106. return Entries[Convert.ToUInt32(id)];
  107. if (Entries.ContainsKey((uint)(Convert.ToUInt32(id) % EntriesPerTexture)))
  108. return Entries[(uint)(Convert.ToUInt32(id) % EntriesPerTexture)];
  109. return null;
  110. }
  111. public virtual TextureHandler GetTexture(Enum id, out Vector2 scale)
  112. {
  113. uint pos = Convert.ToUInt32(id);
  114. uint File = GetEntry(id).File;
  115. //check if we set a custom file and we have a pos more then set entriespertexture
  116. if (File == 0 && EntriesPerTexture > 0 && pos > EntriesPerTexture)
  117. File = (uint)(pos / EntriesPerTexture);
  118. if (File > 0)
  119. {
  120. uint j = (uint)Props.Sum(x => x.Count);
  121. if (File >= j)
  122. {
  123. File %= j;
  124. }
  125. }
  126. scale = Scale[File];
  127. return Textures[(int)File];
  128. }
  129. protected virtual void Init()
  130. {
  131. ArchiveWorker aw = new ArchiveWorker(ArchiveString);
  132. InitEntries(aw);
  133. InitTextures(aw);
  134. InsertCustomEntries();
  135. }
  136. protected virtual void InitEntries(ArchiveWorker aw = null)
  137. {
  138. if (Entries == null)
  139. {
  140. if (aw == null)
  141. aw = new ArchiveWorker(ArchiveString);
  142. using (MemoryStream ms = new MemoryStream(ArchiveWorker.GetBinaryFile(ArchiveString,
  143. aw.GetListOfFiles().First(x => x.IndexOf(IndexFilename, StringComparison.OrdinalIgnoreCase) >= 0))))
  144. {
  145. ushort[] locs;
  146. using (BinaryReader br = new BinaryReader(ms))
  147. {
  148. Count = br.ReadUInt32();
  149. locs = new ushort[Count];//br.ReadUInt32(); 32 valid values in face.sp2 rest is invalid
  150. Entries = new Dictionary<uint, Entry>((int)Count);
  151. for (uint i = 0; i < Count; i++)
  152. {
  153. locs[i] = br.ReadUInt16();
  154. ms.Seek(2, SeekOrigin.Current);
  155. }
  156. byte fid = 0;
  157. Entry Last = null;
  158. for (uint i = 0; i < Count; i++)
  159. {
  160. ms.Seek(locs[i] + 6, SeekOrigin.Begin);
  161. byte t = br.ReadByte();
  162. if (t == 0 || t == 96) // known invalid entries in sp2 files have this value. there might be more to it.
  163. {
  164. Count = i;
  165. break;
  166. }
  167. Entries[i] = new Entry();
  168. Entries[i].LoadfromStreamSP2(br, locs[i], Last, ref fid);
  169. Last = Entries[i];
  170. }
  171. }
  172. }
  173. }
  174. }
  175. protected virtual void InitTextures(ArchiveWorker aw = null)
  176. {
  177. int count = (int)Props.Sum(x => x.Count);
  178. if (Textures == null)
  179. Textures = new List<TextureHandler>(count);
  180. if (Textures.Count <= 0)
  181. {
  182. if (aw == null)
  183. aw = new ArchiveWorker(ArchiveString);
  184. TEX tex;
  185. Scale = new Dictionary<uint, Vector2>(count);
  186. int b = 0;
  187. for (int j = 0; j < Props.Count; j++)
  188. for (uint i = 0; i < Props[j].Count; i++)
  189. {
  190. string path = aw.GetListOfFiles().First(x => x.ToLower().Contains(string.Format(Props[j].Filename, i + TextureStartOffset)));
  191. tex = new TEX(aw.GetBinaryFile(path));
  192. if (Props[j].Big != null && FORCE_ORIGINAL == false && b < Props[j].Big.Count)
  193. {
  194. TextureHandler th = new TextureHandler(Props[j].Big[b].Filename, tex, 2, Props[j].Big[b++].Split / 2);
  195. Textures.Add(th);
  196. Scale[i] = Vector2.One;
  197. }
  198. else
  199. {
  200. TextureHandler th = new TextureHandler(path, tex);
  201. Textures.Add(th);
  202. Scale[i] = th.GetScale(); //scale might not be used outside of texturehandler.
  203. }
  204. }
  205. }
  206. }
  207. protected virtual void InsertCustomEntries()
  208. {
  209. }
  210. private TextureHandler GetTexture(Enum id) => GetTexture(id, out Vector2 scale);
  211. #endregion Methods
  212. #region Classes
  213. /// <summary>
  214. /// For big textures.
  215. /// </summary>
  216. public class BigTexProps
  217. {
  218. #region Fields
  219. /// <summary>
  220. /// Filename; To match more than one number use {0:00} or {00:00} for ones with leading zeros.
  221. /// </summary>
  222. public string Filename;
  223. /// <summary>
  224. /// Big versions of textures take the file and split it into multiple. How many splits
  225. /// per BigFilename. Value to be interval of 2. As these files are all 2 cols wide. And
  226. /// must be &gt;= 2
  227. /// </summary>
  228. public uint Split;
  229. /// <summary>
  230. /// leave null unless big version has a different custom pallet than normal.
  231. /// </summary>
  232. public Color[] Colors;
  233. #endregion Fields
  234. #region Constructors
  235. public BigTexProps(string filename, uint split,Color[] colors=null)
  236. {
  237. Filename = filename;
  238. Split = split;
  239. Colors = colors;
  240. }
  241. #endregion Constructors
  242. }
  243. public class TexProps
  244. { /// <summary>
  245. #region Fields
  246. /// <summary>
  247. /// For big textures.
  248. /// </summary>
  249. public List<BigTexProps> Big;
  250. /// <summary>
  251. /// Override pallet of texture to this and don't load other pallets. If null ignore.
  252. /// </summary>
  253. public Color[] Colors;
  254. /// <summary>
  255. /// Number of Textures
  256. /// </summary>
  257. public uint Count;
  258. /// Filename. To match more than one number use {0:00} or {00:00} for ones with leading
  259. /// zeros. </summary>
  260. public string Filename;
  261. #endregion Fields
  262. #region Constructors
  263. public TexProps(string filename, uint count, params BigTexProps[] big)
  264. {
  265. Filename = filename;
  266. Count = count;
  267. if (big != null && Count != big.Length && big.Length > 0)
  268. throw new Exception($"Count of big textures should match small ones {Count} != {big.Length}");
  269. Big = big.ToList();
  270. Colors = null;
  271. }
  272. public TexProps(string filename, uint count, Color[] colors, params BigTexProps[] big)
  273. {
  274. Filename = filename;
  275. Count = count;
  276. if (big != null && Count != big.Length && big.Length > 0)
  277. throw new Exception($"Count of big textures should match small ones {Count} != {big.Length}");
  278. Big = big.ToList();
  279. Colors = colors;
  280. }
  281. #endregion Constructors
  282. }
  283. #endregion Classes
  284. }
  285. }