ec99ecf8021f907729bc0636e1a528b0870d6765
[supertux.git] / contrib / tilemanager / TileSet.cs
1 using System;
2 using System.IO;
3 using System.Collections;
4 using Lisp;
5
6 public class TileGroup {
7     public string Name;
8     public ArrayList Tiles = new ArrayList();
9
10     public void Write(LispWriter writer) {
11         writer.StartList("tilegroup");
12
13         writer.Write("name", Name);
14         writer.Write("tiles", Tiles);
15
16         writer.EndList("tilegroup");
17     }
18
19     public void Parse(Lisp.Parser parser) {
20         int d = parser.Depth;
21         while(parser.Parse() && parser.Depth >= d) {
22             if(parser.Depth == d+1) {
23                 if(parser.Type != Parser.LispType.SYMBOL)
24                     throw new Exception("expected SYMBOL");
25                 string symbol = parser.SymbolValue;
26                 parser.Parse();
27                 switch(symbol) {
28                     case "name":
29                         Name = parser.StringValue;
30                         break;
31                     case "tiles":
32                         do {
33                             Tiles.Add(parser.IntegerValue);
34                         } while(parser.Parse() 
35                                 && parser.Type == Parser.LispType.INTEGER);
36                         break;
37                     default:
38                         Console.WriteLine("Unknown section " + symbol);
39                         break;
40                 }
41             }
42         }
43     }
44 }
45
46 public class TileSet {
47     public ArrayList Tiles = new ArrayList();
48     public ArrayList TileGroups = new ArrayList();
49
50     public void Write(string filename) {
51         FileStream fs = new FileStream(filename, FileMode.Create);
52        
53         TextWriter tw = new StreamWriter(fs);
54         LispWriter writer = new LispWriter(tw);
55
56         writer.WriteComment("Generated by tiler");
57         writer.StartList("supertux-tiles");
58         foreach(TileGroup tilegroup in TileGroups) {
59             tilegroup.Write(writer);
60         }
61         foreach(Tile tile in Tiles) {
62             if(tile == null)
63                 continue;
64             if(tile.ID >= 0)
65                 tile.Write(writer);
66         }
67         writer.EndList("supertux-tiles");
68         tw.Close();
69         fs.Close();
70     }
71     
72     public void Parse(string filename) {
73         FileStream fs = new FileStream(filename, FileMode.Open);
74         StreamReader stream = new StreamReader(fs);
75
76         Lisp.Parser parser = new Lisp.Parser(stream);
77         parser.Parse();
78         if(parser.Type != Parser.LispType.START_LIST)
79             throw new Exception("Expected START_LIST");
80         parser.Parse();
81         if(parser.Type != Parser.LispType.SYMBOL)
82             throw new Exception("Expected symbol");
83         if(parser.SymbolValue != "supertux-tiles")
84             throw new Exception("not a supertux tile files but " +
85                     parser.SymbolValue);
86         ParseTiles(parser);
87
88         stream.Close();
89         fs.Close();
90     }
91
92     public void ParseTiles(Lisp.Parser parser) {
93         Console.WriteLine("ParseTiles...");
94         int d = parser.Depth;
95         while(parser.Parse() && parser.Depth >= d) {
96             if(parser.Depth == d && parser.Type != Parser.LispType.START_LIST) {
97                 Console.WriteLine("non-cons type in list...");
98                 continue;
99             }
100
101             if(parser.Depth == d+1) {
102                 if(parser.Type != Parser.LispType.SYMBOL) {
103                     throw new Exception("Expected symbol in list element");
104                 }
105                 switch(parser.SymbolValue) {
106                     case "properties":
107                         SkipList(parser);
108                         break;
109                     case "tilegroup":
110                         TileGroup tilegroup = new TileGroup();
111                         tilegroup.Parse(parser);
112                         TileGroups.Add(tilegroup);
113                         break;
114                     case "tile":
115                         Tile tile = new Tile();
116                         tile.Parse(parser);
117                         Console.WriteLine("***ID: " + tile.ID);
118
119                         while(tile.ID >= Tiles.Count)
120                             Tiles.Add(null);
121                         Tiles[tile.ID] = tile;
122                         break;
123                     default:
124                         throw new Exception("Unexpected listentry: " +
125                                 parser.SymbolValue);
126                 }
127             }
128         }
129     }
130
131     private void SkipList(Lisp.Parser parser) {
132         int d = parser.Depth;
133         while(parser.Parse() && parser.Depth >= d)
134             ;
135     }
136 }
137