ByteReader.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using UnityEngine;
  6. public class ByteReader
  7. {
  8. public ByteReader(byte[] bytes)
  9. {
  10. this.mBuffer = bytes;
  11. }
  12. public ByteReader(TextAsset asset)
  13. {
  14. this.mBuffer = asset.bytes;
  15. }
  16. public static ByteReader Open(string path)
  17. {
  18. FileStream fileStream = File.OpenRead(path);
  19. if (fileStream != null)
  20. {
  21. fileStream.Seek(0L, SeekOrigin.End);
  22. byte[] array = new byte[fileStream.Position];
  23. fileStream.Seek(0L, SeekOrigin.Begin);
  24. fileStream.Read(array, 0, array.Length);
  25. fileStream.Close();
  26. return new ByteReader(array);
  27. }
  28. return null;
  29. }
  30. public bool canRead
  31. {
  32. get
  33. {
  34. return this.mBuffer != null && this.mOffset < this.mBuffer.Length;
  35. }
  36. }
  37. private static string ReadLine(byte[] buffer, int start, int count)
  38. {
  39. return Encoding.UTF8.GetString(buffer, start, count);
  40. }
  41. public string ReadLine()
  42. {
  43. return this.ReadLine(true);
  44. }
  45. public string ReadLine(bool skipEmptyLines)
  46. {
  47. int num = this.mBuffer.Length;
  48. if (skipEmptyLines)
  49. {
  50. while (this.mOffset < num && this.mBuffer[this.mOffset] < 32)
  51. {
  52. this.mOffset++;
  53. }
  54. }
  55. int i = this.mOffset;
  56. if (i < num)
  57. {
  58. while (i < num)
  59. {
  60. int num2 = (int)this.mBuffer[i++];
  61. if (num2 == 10 || num2 == 13)
  62. {
  63. IL_87:
  64. string result = ByteReader.ReadLine(this.mBuffer, this.mOffset, i - this.mOffset - 1);
  65. this.mOffset = i;
  66. return result;
  67. }
  68. }
  69. i++;
  70. goto IL_87;
  71. }
  72. this.mOffset = num;
  73. return null;
  74. }
  75. public Dictionary<string, string> ReadDictionary()
  76. {
  77. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  78. char[] separator = new char[]
  79. {
  80. '='
  81. };
  82. while (this.canRead)
  83. {
  84. string text = this.ReadLine();
  85. if (text == null)
  86. {
  87. break;
  88. }
  89. if (!text.StartsWith("//"))
  90. {
  91. string[] array = text.Split(separator, 2, StringSplitOptions.RemoveEmptyEntries);
  92. if (array.Length == 2)
  93. {
  94. string key = array[0].Trim();
  95. string value = array[1].Trim().Replace("\\n", "\n");
  96. dictionary[key] = value;
  97. }
  98. }
  99. }
  100. return dictionary;
  101. }
  102. public BetterList<string> ReadCSV()
  103. {
  104. ByteReader.mTemp.Clear();
  105. string text = string.Empty;
  106. bool flag = false;
  107. int num = 0;
  108. while (this.canRead)
  109. {
  110. if (flag)
  111. {
  112. string text2 = this.ReadLine(false);
  113. if (text2 == null)
  114. {
  115. return null;
  116. }
  117. text2 = text2.Replace("\\n", "\n");
  118. text = text + "\n" + text2;
  119. }
  120. else
  121. {
  122. text = this.ReadLine(true);
  123. if (text == null)
  124. {
  125. return null;
  126. }
  127. text = text.Replace("\\n", "\n");
  128. num = 0;
  129. }
  130. int i = num;
  131. int length = text.Length;
  132. while (i < length)
  133. {
  134. char c = text[i];
  135. if (c == ',')
  136. {
  137. if (!flag)
  138. {
  139. ByteReader.mTemp.Add(text.Substring(num, i - num));
  140. num = i + 1;
  141. }
  142. }
  143. else if (c == '"')
  144. {
  145. if (flag)
  146. {
  147. if (i + 1 >= length)
  148. {
  149. ByteReader.mTemp.Add(text.Substring(num, i - num).Replace("\"\"", "\""));
  150. return ByteReader.mTemp;
  151. }
  152. if (text[i + 1] != '"')
  153. {
  154. ByteReader.mTemp.Add(text.Substring(num, i - num).Replace("\"\"", "\""));
  155. flag = false;
  156. if (text[i + 1] == ',')
  157. {
  158. i++;
  159. num = i + 1;
  160. }
  161. }
  162. else
  163. {
  164. i++;
  165. }
  166. }
  167. else
  168. {
  169. num = i + 1;
  170. flag = true;
  171. }
  172. }
  173. i++;
  174. }
  175. if (num < text.Length)
  176. {
  177. if (flag)
  178. {
  179. continue;
  180. }
  181. ByteReader.mTemp.Add(text.Substring(num, text.Length - num));
  182. }
  183. return ByteReader.mTemp;
  184. }
  185. return null;
  186. }
  187. private byte[] mBuffer;
  188. private int mOffset;
  189. private static BetterList<string> mTemp = new BetterList<string>();
  190. }