Level.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // $Id: foo.cpp 2979 2006-01-10 00:00:04Z sommer $
  2. //
  3. // Cobble - A simple SuperTux level editor
  4. // Copyright (C) 2006 Christoph Sommer <supertux@2006.expires.deltadevelopment.de>
  5. //
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. // 02111-1307, USA.
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Text;
  23. using System.IO;
  24. using System.Diagnostics;
  25. using System.Text.RegularExpressions;
  26. using Lisp;
  27. namespace Cobble {
  28. public class Level {
  29. public int version;
  30. public string name;
  31. public string author;
  32. public List<Sector> sectors;
  33. public Level(string filename) {
  34. this.sectors = new List<Sector>();
  35. FileStream fs = new FileStream(filename, FileMode.Open);
  36. StreamReader stream = new StreamReader(fs);
  37. Lisp.Parser parser = new Lisp.Parser(stream);
  38. parser.Parse();
  39. if (parser.Type != Parser.LispType.START_LIST)
  40. throw new Exception("Expected START_LIST");
  41. parser.Parse();
  42. if (parser.Type != Parser.LispType.SYMBOL)
  43. throw new Exception("Expected symbol");
  44. if (parser.SymbolValue != "supertux-level")
  45. throw new Exception("not a supertux level file. (" + parser.SymbolValue + ")");
  46. int d = parser.Depth;
  47. while (parser.Parse() && parser.Depth >= d) {
  48. if (parser.Depth == d && parser.Type != Parser.LispType.START_LIST) {
  49. Console.WriteLine("non-cons type in list...");
  50. continue;
  51. }
  52. if (parser.Depth == d + 1) {
  53. if (parser.Type != Parser.LispType.SYMBOL) throw new Exception("Expected symbol in list element");
  54. switch (parser.SymbolValue) {
  55. case "version":
  56. parser.Parse();
  57. this.version = parser.IntegerValue;
  58. break;
  59. case "name":
  60. parser.Parse();
  61. parser.Parse();
  62. parser.Parse();
  63. this.name = parser.StringValue;
  64. parser.Parse();
  65. break;
  66. case "author":
  67. parser.Parse();
  68. this.author = parser.StringValue;
  69. break;
  70. case "extro":
  71. SkipList(parser);
  72. break;
  73. case "sector":
  74. this.sectors.Add(new Sector(parser));
  75. break;
  76. default:
  77. throw new Exception("Unexpected entry in level list: " + parser.SymbolValue);
  78. }
  79. }
  80. }
  81. stream.Close();
  82. fs.Close();
  83. }
  84. public Level() {
  85. this.version = 2;
  86. this.name = "Unnamed Level";
  87. this.author = "Anonymous";
  88. this.sectors = new List<Sector>();
  89. this.sectors.Add(new Sector("main"));
  90. }
  91. public void Write(string filename) {
  92. FileStream fs = new FileStream(filename, FileMode.Create);
  93. TextWriter tw = new StreamWriter(fs);
  94. LispWriter writer = new LispWriter(tw);
  95. writer.WriteComment("Created with Cobble");
  96. writer.StartList("supertux-level");
  97. writer.Write("version", this.version);
  98. writer.StartList("name"); writer.Write("_", this.name); writer.EndList("name");
  99. writer.Write("author", this.author);
  100. foreach (Sector sector in this.sectors) {
  101. sector.Write(writer);
  102. }
  103. writer.EndList("supertux-level");
  104. tw.Close();
  105. fs.Close();
  106. }
  107. private static void SkipList(Lisp.Parser parser) {
  108. int d = parser.Depth;
  109. while (parser.Parse() && parser.Depth >= d)
  110. ;
  111. }
  112. }
  113. }