Advertisement
Fastmapler

Processor's InitParse

Feb 19th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. public static class InitParse
  2.     {
  3.         public static DataChunk[] Parse(Message m)
  4.         {
  5.             if (m == null) throw new ArgumentNullException("m");
  6.             if (m.Type != "init" && m.Type != "reset") throw new ArgumentException("Invalid message type.", "m");
  7.  
  8.             // Get world data
  9.             var p = 0u;
  10.             var data = new Stack<object>();
  11.             while (m[p++] as string != "ws") { }
  12.             while (m[p] as string != "we") { data.Push(m[p++]); }
  13.  
  14.             // Parse world data
  15.             var chunks = new List<DataChunk>();
  16.             while (data.Count > 0)
  17.             {
  18.                 var args = new Stack<object>();
  19.                 while (!(data.Peek() is byte[]))
  20.                     args.Push(data.Pop());
  21.  
  22.                 var ys = (byte[])data.Pop();
  23.                 var xs = (byte[])data.Pop();
  24.                 var layer = (int)data.Pop();
  25.                 var type = (uint)data.Pop();
  26.  
  27.                 chunks.Add(new DataChunk(layer, type, xs, ys, args.ToArray()));
  28.             }
  29.  
  30.             return chunks.ToArray();
  31.         }
  32.     }
  33.  
  34.     public class DataChunk
  35.     {
  36.         public int Layer { get; set; }
  37.         public uint Type { get; set; }
  38.         public Point[] Locations { get; set; }
  39.         public object[] Args { get; set; }
  40.  
  41.         public DataChunk(int layer, uint type, byte[] xs, byte[] ys, object[] args)
  42.         {
  43.             this.Layer = layer;
  44.             this.Type = type;
  45.             this.Args = args;
  46.             this.Locations = GetLocations(xs, ys);
  47.         }
  48.  
  49.         private static Point[] GetLocations(byte[] xs, byte[] ys)
  50.         {
  51.             var points = new List<Point>();
  52.             for (var i = 0; i < xs.Length; i += 2)
  53.                 points.Add(new Point(
  54.                     (xs[i] << 8) | xs[i + 1],
  55.                     (ys[i] << 8) | ys[i + 1]));
  56.             return points.ToArray();
  57.         }
  58.     }
  59.  
  60.     public struct Point
  61.     {
  62.         public int X { get; set; }
  63.         public int Y { get; set; }
  64.  
  65.         public Point(int x, int y)
  66.             : this()
  67.         {
  68.             this.X = x;
  69.             this.Y = y;
  70.         }
  71.     }
  72.  
  73.  
  74. var chunks = InitParse.Parse(m);
  75.                 foreach (var chunk in chunks)
  76.                     foreach (var pos in chunk.Locations)
  77.                         roomData[chunk.Layer, pos.X, pos.Y] = chunk.Type;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement