2
0

FF8String.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace FF8
  6. {
  7. /// <summary>
  8. /// This holds the encoded bytes and provides implict casts to string and byte[]
  9. /// </summary>
  10. public class FF8String : IEnumerator, IEnumerable, ICloneable
  11. {
  12. #region Fields
  13. private byte[] value;
  14. private int position = 0;
  15. #endregion Fields
  16. #region Constructors
  17. public FF8String()
  18. {
  19. }
  20. public static void Init()
  21. {
  22. if (BytetoStr == null)
  23. {
  24. BytetoStr = new Dictionary<byte, FF8String>
  25. {
  26. //{0x01, "" },
  27. {0xC6, "VI"},// pos:166, col:20, row:9 --
  28. {0xC7, "II"},// pos:167, col:21, row:9 --
  29. //pc version sysfld00 and 01
  30. {0xCC, "GA"},// pos:172, col:5, row:9 --
  31. {0xCD, "ME"},// pos:173, col:6, row:9 --
  32. {0xCE, "FO"},// pos:174, col:7, row:9 --
  33. {0xCF, "LD"},// pos:175, col:8, row:9 --
  34. {0xD0, "ER"},// pos:176, col:9, row:9 --
  35. ////original texture - sysfont
  36. //{0xCC, "ME"},// pos:172, col:5, row:9 --
  37. //{0xCD, "MO"},// pos:173, col:6, row:9 --
  38. //{0xCE, "RY"},// pos:174, col:7, row:9 --
  39. //{0xCF, "CA"},// pos:175, col:8, row:9 --
  40. //{0xD0, "RD"},// pos:176, col:9, row:9 --
  41. {0xD1, "Sl"},// pos:177, col:10, row:9 --
  42. {0xD2, "ot"},// pos:178, col:11, row:9 --
  43. {0xD3, "ing"},// pos:179, col:12, row:10 --
  44. {0xD4, "St"},// pos:180, col:13, row:10 --
  45. {0xD5, "ec"},// pos:181, col:14, row:10 --
  46. {0xD6, "kp"},// pos:182, col:15, row:10 --
  47. {0xD7, "la"},// pos:183, col:16, row:10 --
  48. {0xD8, ":z"},// pos:184, col:17, row:10 --
  49. {0xD9, "Fr"},// pos:185, col:18, row:10 --
  50. {0xDA, "nt"},// pos:186, col:19, row:10 --
  51. {0xDB, "elng"},// pos:187, col:20, row:10 --
  52. {0xDC, "re"},// pos:188, col:21, row:10 --
  53. {0xDD, "S:"},// pos:189, col:1, row:10 --
  54. {0xDE, "so"},// pos:190, col:2, row:10 --
  55. {0xDF, "Ra"},// pos:191, col:3, row:10 --
  56. {0xE0, "nu"},// pos:192, col:4, row:10 --
  57. {0xE1, "ra"},// pos:193, col:5, row:10 --
  58. //{0xE3, ""},// pos:195, col:0, row:0 --
  59. //{0xE4, ""},// pos:196, col:0, row:0 --
  60. //{0xE5, ""},// pos:197, col:0, row:0 --
  61. //{0xE6, ""},// pos:198, col:0, row:0 --
  62. //{0xE7, ""},// pos:199, col:0, row:0 --
  63. {0xE8, "in"},// pos:200, col:0, row:0 --
  64. {0xE9, "e "},// pos:201, col:0, row:0 --
  65. {0xEA, "ne"},// pos:202, col:0, row:0 --
  66. {0xEB, "to"},// pos:203, col:0, row:0 --
  67. {0xEC, "re"},// pos:204, col:0, row:0 --
  68. {0xED, "HP"},// pos:205, col:0, row:0 --
  69. {0xEE, "l "},// pos:206, col:0, row:0 --
  70. {0xEF, "ll"},// pos:207, col:0, row:0 --
  71. {0xF0, "GF"},// pos:208, col:0, row:0 --
  72. {0xF1, "nt"},// pos:209, col:0, row:0 --
  73. {0xF2, "il"},// pos:210, col:0, row:0 --
  74. {0xF3, "o "},// pos:211, col:0, row:0 --
  75. {0xF4, "ef"},// pos:212, col:0, row:0 --
  76. {0xF5, "on"},// pos:213, col:0, row:0 --
  77. {0xF6, " w"},// pos:214, col:0, row:0 --
  78. {0xF7, " r"},// pos:215, col:0, row:0 --
  79. {0xF8, "wi"},// pos:216, col:0, row:0 --
  80. {0xF9, "fi"},// pos:217, col:0, row:0 --
  81. //{0xFA, ""},// pos:218, col:0, row:0 --
  82. {0xFB, "s "},// pos:219, col:0, row:0 --
  83. {0xFC, "ar"},// pos:220, col:0, row:0 --
  84. //{0xFD, ""},// pos:221, col:0, row:0 --
  85. {0xFE, " S"},// pos:222, col:0, row:0 --
  86. {0xFF, "ag"},// pos:223, col:0, row:0 --
  87. };
  88. }
  89. }
  90. /// <summary>
  91. /// multi characters bytes and double character bytes
  92. /// </summary>
  93. /// TODO replace me.
  94. public static Dictionary<byte, FF8String> BytetoStr { get; private set; }
  95. public FF8String(byte[] @value) => Value = @value;
  96. private static Encoding.FF8TextEncoding encoding = new Encoding.FF8TextEncoding(Encoding.FF8TextEncodingCodepage.Create());
  97. public FF8String(string @value) => Value = @value != null? encoding.GetBytes(@value):null;
  98. #endregion Constructors
  99. #region Properties
  100. public byte[] Value { get => value; set => this.value = value; }
  101. public string Value_str => encoding.GetString(Value).TrimEnd('\0');
  102. public int Length => value==null? 0:value.Length;
  103. public object Current { get => Value[position-1] ; }
  104. #endregion Properties
  105. #region Indexers
  106. public byte this[int index] => value[index];
  107. #endregion Indexers
  108. #region Methods
  109. public static implicit operator byte[] (FF8String input) => input?.Value;
  110. public static implicit operator FF8String(string input) => new FF8String(input);
  111. public static implicit operator FF8String(byte[] input) => new FF8String(input);
  112. public static implicit operator string(FF8String input) => input?.Value_str;
  113. public override string ToString() => Value_str;
  114. public bool MoveNext()
  115. {
  116. if(++position <= Length)
  117. return true;
  118. else
  119. {
  120. Reset();
  121. return false;
  122. }
  123. }
  124. public void Reset() => position = 0;
  125. public IEnumerator GetEnumerator() => this;
  126. public FF8String Append(FF8String end)
  127. {
  128. if (end != null && end.Length > 0)
  129. {
  130. Array.Resize(ref value, Length + end.Length);
  131. Array.Copy(end, 0, value, Length, end.Length);
  132. }
  133. return this;
  134. }
  135. public static FF8String Combine(FF8String start,FF8String end)
  136. {
  137. if (end != null && end.Length > 0)
  138. {
  139. byte[] combine = new byte[start.Length + end.Length];
  140. Array.Copy(start, 0, combine, 0, start.Length);
  141. Array.Copy(end, 0, combine, start.Length, end.Length);
  142. return combine;
  143. }
  144. else
  145. return start;
  146. }
  147. public FF8String ReplaceRegion()
  148. {
  149. int i = 0;
  150. do
  151. {
  152. i = Array.FindIndex(value, i, Length - i, x => x == 0x0E);
  153. if (i >= 0)
  154. {
  155. byte id = (byte)(value[i + 1] - 0x20);
  156. //byte[] start = value.Take(i).ToArray();
  157. byte[] newdata = Memory.Strings.Read(Strings.FileID.NAMEDIC, 0, id);
  158. byte[] end = value.Skip(2 + i).ToArray();
  159. Array.Resize(ref value, Length + newdata.Length - 2);
  160. Array.Copy(newdata,0,value,i,newdata.Length);
  161. Array.Copy(end, 0, value, i+newdata.Length, end.Length);
  162. i+=newdata.Length;
  163. }
  164. }
  165. while (i >= 0&& i < Length);
  166. return this;
  167. }
  168. public object Clone() => new FF8String
  169. {
  170. value = value
  171. };
  172. #endregion Methods
  173. }
  174. }