Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class InitParse
- {
- public static DataChunk[] Parse(Message m)
- {
- if (m == null) throw new ArgumentNullException("m");
- if (m.Type != "init" && m.Type != "reset") throw new ArgumentException("Invalid message type.", "m");
- // Get world data
- var p = 0u;
- var data = new Stack<object>();
- while (m[p++] as string != "ws") { }
- while (m[p] as string != "we") { data.Push(m[p++]); }
- // Parse world data
- var chunks = new List<DataChunk>();
- while (data.Count > 0)
- {
- var args = new Stack<object>();
- while (!(data.Peek() is byte[]))
- args.Push(data.Pop());
- var ys = (byte[])data.Pop();
- var xs = (byte[])data.Pop();
- var layer = (int)data.Pop();
- var type = (uint)data.Pop();
- chunks.Add(new DataChunk(layer, type, xs, ys, args.ToArray()));
- }
- return chunks.ToArray();
- }
- }
- public class DataChunk
- {
- public int Layer { get; set; }
- public uint Type { get; set; }
- public Point[] Locations { get; set; }
- public object[] Args { get; set; }
- public DataChunk(int layer, uint type, byte[] xs, byte[] ys, object[] args)
- {
- this.Layer = layer;
- this.Type = type;
- this.Args = args;
- this.Locations = GetLocations(xs, ys);
- }
- private static Point[] GetLocations(byte[] xs, byte[] ys)
- {
- var points = new List<Point>();
- for (var i = 0; i < xs.Length; i += 2)
- points.Add(new Point(
- (xs[i] << 8) | xs[i + 1],
- (ys[i] << 8) | ys[i + 1]));
- return points.ToArray();
- }
- }
- public struct Point
- {
- public int X { get; set; }
- public int Y { get; set; }
- public Point(int x, int y)
- : this()
- {
- this.X = x;
- this.Y = y;
- }
- }
- var chunks = InitParse.Parse(m);
- foreach (var chunk in chunks)
- foreach (var pos in chunk.Locations)
- roomData[chunk.Layer, pos.X, pos.Y] = chunk.Type;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement