Sym.Reader.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace OpenVIII.Fields
  5. {
  6. public static partial class Sym
  7. {
  8. public static class Reader
  9. {
  10. public static GameObjects FromBytes(byte[] buffer)
  11. {
  12. using (var ms = new MemoryStream(buffer))
  13. return FromStream(ms);
  14. }
  15. public static GameObjects FromStream(Stream input)
  16. {
  17. using (var reader = new StreamReader(input, System.Text.Encoding.ASCII, false, bufferSize: 4096, leaveOpen: true))
  18. return FromReader(reader);
  19. }
  20. public static GameObjects FromReader(StreamReader reader)
  21. {
  22. var result = new GameObjects();
  23. while (!reader.EndOfStream)
  24. {
  25. var str = reader.ReadLine();
  26. if (string.IsNullOrWhiteSpace(str))
  27. continue;
  28. Parse(str, result);
  29. }
  30. return result;
  31. }
  32. private static void Parse(string str, GameObjects result)
  33. {
  34. var pair = str.Split(new[] {"::"}, StringSplitOptions.None);
  35. if (pair.Length == 1)
  36. result.AddObject(pair[0].Trim());
  37. else if (pair.Length == 2)
  38. result.AddScript(pair[0].Trim(), pair[1].Trim());
  39. else
  40. throw new NotSupportedException($"Cannot parse the invalid symbol: {str}");
  41. }
  42. }
  43. }
  44. }