EraStreamReader.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using NTERA.EmuEra.Game.EraEmu.GameData;
  6. namespace NTERA.EmuEra.Game.EraEmu.Sub
  7. {
  8. internal sealed class EraStreamReader : IDisposable
  9. {
  10. public EraStreamReader(bool useRename)
  11. {
  12. this.useRename = useRename;
  13. }
  14. string filepath;
  15. string filename;
  16. bool useRename;
  17. int curNo;
  18. int nextNo;
  19. StreamReader reader;
  20. FileStream stream;
  21. public bool Open(string path)
  22. {
  23. return Open(path, Path.GetFileName(path));
  24. }
  25. public bool Open(string path, string name)
  26. {
  27. //そんなお行儀の悪いことはしていない
  28. //if (disposed)
  29. // throw new ExeEE("破棄したオブジェクトを再利用しようとした");
  30. //if ((reader != null) || (stream != null) || (filepath != null))
  31. // throw new ExeEE("使用中のオブジェクトを別用途に再利用しようとした");
  32. filepath = path;
  33. filename = name;
  34. nextNo = 0;
  35. curNo = 0;
  36. try
  37. {
  38. stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  39. reader = new StreamReader(stream, Config.Config.Encode);
  40. }
  41. catch
  42. {
  43. Dispose();
  44. return false;
  45. }
  46. return true;
  47. }
  48. public string ReadLine()
  49. {
  50. nextNo++;
  51. curNo = nextNo;
  52. return reader.ReadLine();
  53. }
  54. /// <summary>
  55. /// 次の有効な行を読む。LexicalAnalyzer経由でConfigを参照するのでConfig完成までつかわないこと。
  56. /// </summary>
  57. public StringStream ReadEnabledLine()
  58. {
  59. string line = null;
  60. StringStream st = null;
  61. curNo = nextNo;
  62. while (true)
  63. {
  64. line = reader.ReadLine();
  65. curNo++;
  66. nextNo++;
  67. if (line == null)
  68. return null;
  69. if (line.Length == 0)
  70. continue;
  71. if (useRename && (line.IndexOf("[[") >= 0) && (line.IndexOf("]]") >= 0))
  72. {
  73. foreach (KeyValuePair<string, string> pair in ParserMediator.RenameDic)
  74. line = line.Replace(pair.Key, pair.Value);
  75. }
  76. st = new StringStream(line);
  77. LexicalAnalyzer.SkipWhiteSpace(st);
  78. if (st.EOS)
  79. continue;
  80. if (st.Current == '}')
  81. throw new CodeEE("予期しない行連結終端記号'}'が見つかりました");
  82. if (st.Current == '{')
  83. {
  84. if (line.Trim() != "{")
  85. throw new CodeEE("行連結始端記号'{'の行に'{'以外の文字を含めることはできません");
  86. break;
  87. }
  88. return st;
  89. }
  90. //curNoはこの後加算しない(始端記号の行を行番号とする)
  91. StringBuilder b = new StringBuilder();
  92. while (true)
  93. {
  94. line = reader.ReadLine();
  95. nextNo++;
  96. if (line == null)
  97. {
  98. throw new CodeEE("行連結始端記号'{'が使われましたが終端記号'}'が見つかりません");
  99. }
  100. if (useRename && (line.IndexOf("[[") >= 0) && (line.IndexOf("]]") >= 0))
  101. {
  102. foreach (KeyValuePair<string, string> pair in ParserMediator.RenameDic)
  103. line = line.Replace(pair.Key, pair.Value);
  104. }
  105. string test = line.TrimStart();
  106. if (test.Length > 0)
  107. {
  108. if (test[0] == '}')
  109. {
  110. if (test.Trim() != "}")
  111. throw new CodeEE("行連結終端記号'}'の行に'}'以外の文字を含めることはできません");
  112. break;
  113. }
  114. if (test[0] == '{')
  115. throw new CodeEE("予期しない行連結始端記号'{'が見つかりました");
  116. }
  117. b.Append(line);
  118. b.Append(" ");
  119. }
  120. st = new StringStream(b.ToString());
  121. LexicalAnalyzer.SkipWhiteSpace(st);
  122. return st;
  123. }
  124. /// <summary>
  125. /// 直前に読んだ行の行番号
  126. /// </summary>
  127. public int LineNo => curNo;
  128. public string Filename => filename;
  129. //public string Filepath
  130. //{
  131. // get
  132. // {
  133. // return filepath;
  134. // }
  135. //}
  136. public void Close() { Dispose(); }
  137. bool disposed;
  138. #region IDisposable メンバ
  139. public void Dispose()
  140. {
  141. if (disposed)
  142. return;
  143. if (reader != null)
  144. reader.Close();
  145. else if (stream != null)
  146. stream.Close();
  147. filepath = null;
  148. filename = null;
  149. reader = null;
  150. stream = null;
  151. disposed = true;
  152. }
  153. #endregion
  154. }
  155. }