FF8StringReference.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.IO;
  5. using System.Linq;
  6. namespace OpenVIII
  7. {
  8. /// <summary>
  9. /// This class stores the reference to where the string is. Can be read with Read();
  10. /// </summary>
  11. public class FF8StringReference : FF8String
  12. {
  13. #region Fields
  14. private static readonly object Lock = new object();
  15. /// <summary>
  16. /// Check if had read already encase the actual length is 0.
  17. /// </summary>
  18. private bool _hadRead;
  19. #endregion Fields
  20. #region Constructors
  21. public FF8StringReference(Memory.Archive archive, string filename, long offset, ushort length = 0, Settings settings = Settings.None)
  22. {
  23. Archive = archive;
  24. Filename = filename;
  25. Offset = offset;
  26. ReadLength = length;
  27. StringSettings = settings;
  28. }
  29. #endregion Constructors
  30. #region Enums
  31. [Flags]
  32. public enum Settings : byte
  33. {
  34. None = 0x0,
  35. //May contain multi-byte characters
  36. MultiCharByte = 0x1,
  37. //May require replacement of tags from Namedic
  38. Namedic = 0x2,
  39. }
  40. #endregion Enums
  41. #region Properties
  42. /// <summary>
  43. /// multi characters bytes and double character bytes
  44. /// </summary>
  45. /// TODO replace me.
  46. [SuppressMessage("ReSharper", "StringLiteralTypo")]
  47. public static IReadOnlyDictionary<byte, FF8String> ByteToString { get; } = new Dictionary<byte, FF8String>
  48. {
  49. //{0x01, "" },
  50. {0xC6, "VI"},// pos:166, col:20, row:9 --
  51. {0xC7, "II"},// pos:167, col:21, row:9 --
  52. //pc version sysfld00 and 01
  53. {0xCC, "GA"},// pos:172, col:5, row:9 --
  54. {0xCD, "ME"},// pos:173, col:6, row:9 --
  55. {0xCE, "FO"},// pos:174, col:7, row:9 --
  56. {0xCF, "LD"},// pos:175, col:8, row:9 --
  57. {0xD0, "ER"},// pos:176, col:9, row:9 --
  58. ////original texture - sys font
  59. //{0xCC, "ME"},// pos:172, col:5, row:9 --
  60. //{0xCD, "MO"},// pos:173, col:6, row:9 --
  61. //{0xCE, "RY"},// pos:174, col:7, row:9 --
  62. //{0xCF, "CA"},// pos:175, col:8, row:9 --
  63. //{0xD0, "RD"},// pos:176, col:9, row:9 --
  64. {0xD1, "Sl"},// pos:177, col:10, row:9 --
  65. {0xD2, "ot"},// pos:178, col:11, row:9 --
  66. {0xD3, "ing"},// pos:179, col:12, row:10 --
  67. {0xD4, "St"},// pos:180, col:13, row:10 --
  68. {0xD5, "ec"},// pos:181, col:14, row:10 --
  69. {0xD6, "kp"},// pos:182, col:15, row:10 --
  70. {0xD7, "la"},// pos:183, col:16, row:10 --
  71. {0xD8, ":z"},// pos:184, col:17, row:10 --
  72. {0xD9, "Fr"},// pos:185, col:18, row:10 --
  73. {0xDA, "nt"},// pos:186, col:19, row:10 --
  74. {0xDB, "elng"},// pos:187, col:20, row:10 --
  75. {0xDC, "re"},// pos:188, col:21, row:10 --
  76. {0xDD, "S:"},// pos:189, col:1, row:10 --
  77. {0xDE, "so"},// pos:190, col:2, row:10 --
  78. {0xDF, "Ra"},// pos:191, col:3, row:10 --
  79. {0xE0, "nu"},// pos:192, col:4, row:10 --
  80. {0xE1, "ra"},// pos:193, col:5, row:10 --
  81. // all above is render-able meaning there is an image in the texture atlas for it.
  82. // all below needs expanded into single byte characters.
  83. //{0xE3, ""},// pos:195, col:0, row:0 --
  84. //{0xE4, ""},// pos:196, col:0, row:0 --
  85. //{0xE5, ""},// pos:197, col:0, row:0 --
  86. //{0xE6, ""},// pos:198, col:0, row:0 --
  87. //{0xE7, ""},// pos:199, col:0, row:0 --
  88. {0xE8, "in"},// pos:200, col:0, row:0 --
  89. {0xE9, "e "},// pos:201, col:0, row:0 --
  90. {0xEA, "ne"},// pos:202, col:0, row:0 --
  91. {0xEB, "to"},// pos:203, col:0, row:0 --
  92. {0xEC, "re"},// pos:204, col:0, row:0 --
  93. {0xED, "HP"},// pos:205, col:0, row:0 --
  94. {0xEE, "l "},// pos:206, col:0, row:0 --
  95. {0xEF, "ll"},// pos:207, col:0, row:0 --
  96. {0xF0, "GF"},// pos:208, col:0, row:0 --
  97. {0xF1, "nt"},// pos:209, col:0, row:0 --
  98. {0xF2, "il"},// pos:210, col:0, row:0 --
  99. {0xF3, "o "},// pos:211, col:0, row:0 --
  100. {0xF4, "ef"},// pos:212, col:0, row:0 --
  101. {0xF5, "on"},// pos:213, col:0, row:0 --
  102. {0xF6, " w"},// pos:214, col:0, row:0 --
  103. {0xF7, " r"},// pos:215, col:0, row:0 --
  104. {0xF8, "wi"},// pos:216, col:0, row:0 --
  105. {0xF9, "fi"},// pos:217, col:0, row:0 --
  106. //{0xFA, ""},// pos:218, col:0, row:0 --
  107. {0xFB, "s "},// pos:219, col:0, row:0 --
  108. {0xFC, "ar"},// pos:220, col:0, row:0 --
  109. //{0xFD, ""},// pos:221, col:0, row:0 --
  110. {0xFE, " S"},// pos:222, col:0, row:0 --
  111. {0xFF, "ag"},// pos:223, col:0, row:0 --
  112. };
  113. public Memory.Archive Archive { get; }
  114. public string Filename { get; }
  115. public override int Length
  116. {
  117. get
  118. {
  119. if (base.Length == 0) Read();
  120. return base.Length;
  121. }
  122. }
  123. public long Offset { get; }
  124. public ushort ReadLength { get; }
  125. public Settings StringSettings { get; }
  126. public override byte[] Value
  127. {
  128. get
  129. {
  130. if (base.Length == 0) Read();
  131. return base.Value;
  132. }
  133. set => base.Value = value;
  134. }
  135. #endregion Properties
  136. #region Methods
  137. public static FF8String operator +(FF8StringReference a, FF8String b) => (FF8String)a + b;
  138. public static FF8String operator +(FF8String a, FF8StringReference b) => a + (FF8String)b;
  139. public static FF8String operator +(FF8StringReference a, FF8StringReference b) => (FF8String)a + (FF8String)b;
  140. public static FF8String operator +(FF8StringReference a, string b) => (FF8String)a + b;
  141. public static FF8String operator +(string a, FF8StringReference b) => a + (FF8String)b;
  142. private void InsertNamedic()
  143. {
  144. if (Length <= 0) return;
  145. var i = 0;
  146. do
  147. {
  148. i = Array.FindIndex(base.Value, i, Length - i, x => x == 0x0E);
  149. if (i < 0) continue;
  150. var id = (byte)(value[i + 1] - 0x20);
  151. byte[] newData = Memory.Strings.Read(Strings.FileID.Namedic, 0, id);
  152. var end = value.Skip(2 + i).ToArray();
  153. Array.Resize(ref value, Length + newData.Length - 2);
  154. Array.Copy(newData, 0, value, i, newData.Length);
  155. Array.Copy(end, 0, value, i + newData.Length, end.Length);
  156. i += newData.Length;
  157. }
  158. while (i >= 0 && i < Length);
  159. }
  160. private void Read()
  161. {
  162. lock (Lock)
  163. if (base.Length == 0 && !_hadRead)
  164. {
  165. _hadRead = true;
  166. var aw = ArchiveWorker.Load(Archive, true);
  167. using (var br = new BinaryReader(new MemoryStream(aw.GetBinaryFile(Filename, true))))
  168. {
  169. br.BaseStream.Seek(Offset, SeekOrigin.Begin);
  170. if (ReadLength > 0 && (StringSettings & Settings.MultiCharByte) == 0) // ReadLength set, read that. unless contains multi-char bytes
  171. Value = br.ReadBytes(ReadLength);
  172. else // Length unknown read to null
  173. {
  174. using (var bw = new BinaryWriter(new MemoryStream()))
  175. {
  176. for (var i = 0; i < br.BaseStream.Length; i++)
  177. {
  178. if (ReadLength > 0 && i > ReadLength) break;
  179. var b = br.ReadByte();
  180. if (i == 0 || b != 0)
  181. {
  182. if (b > 0xE1 && (StringSettings & Settings.MultiCharByte) != 0 && ByteToString.ContainsKey(b))
  183. bw.Write(ByteToString[b].Value);
  184. else
  185. bw.Write(b);
  186. }
  187. else break;
  188. }
  189. Value = ((MemoryStream)bw.BaseStream).ToArray();
  190. }
  191. }
  192. }
  193. if ((StringSettings & Settings.Namedic) != 0)
  194. {
  195. InsertNamedic();
  196. }
  197. }
  198. }
  199. #endregion Methods
  200. /// old read method encase i missed something.
  201. //public FF8String Read(BinaryReader br, FileID fid, uint pos)
  202. //{
  203. // if (pos == 0)
  204. // return new FF8String("");
  205. // if (pos < br.BaseStream.Length)
  206. // using (MemoryStream os = new MemoryStream(50))
  207. // {p
  208. // br.BaseStream.Seek(pos, SeekOrigin.Begin);
  209. // int c = 0;
  210. // byte b = 0;
  211. // do
  212. // {
  213. // if (br.BaseStream.Position > br.BaseStream.Length) break;
  214. // //sometimes strings start with 00 or 01. But there is another 00 at the end.
  215. // //I think it's for SeeD test like 1 is right and 0 is wrong. for now i skip them.
  216. // b = br.ReadByte();
  217. // if (b != 0 && b != 1)
  218. // {
  219. // os.WriteByte(b);
  220. // }
  221. // c++;
  222. // }
  223. // while (b != 0 || c == 0);
  224. // if (os.Length > 0)
  225. // return os.ToArray();
  226. // }
  227. // return null;
  228. //}
  229. }
  230. }