Advertisement
KubaPL20935

WorldData.dll

Apr 3rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using PlayerIOClient;
  7.  
  8. namespace WorldData
  9. {
  10.     public class World
  11.     {
  12.         public struct Block
  13.         {
  14.             public Block(uint id, uint placer = 0, object[] data = null)
  15.             {
  16.                 if (data == null) { data = new Object[] { }; }
  17.                 this.Id = id;
  18.                 this.Extra = data;
  19.                 this.Placer = placer;
  20.             }
  21.             /// <summary>
  22.             /// ID of the block.
  23.             /// </summary>
  24.             public uint Id { get; set; }
  25.             /// <summary>
  26.             /// Extra data for block (sign text, rotation, portal ids etc)
  27.             /// </summary>
  28.             public object[] Extra { get; set; }
  29.             /// <summary>
  30.             /// ID of the player that placed this block.
  31.             /// </summary>
  32.             public uint Placer { get; set; }
  33.         }
  34.         /// <summary>
  35.         /// World/Block Data in format; x, y, z(layer) (0 foreground, 1 background)
  36.         /// </summary>
  37.         public Block[,,] Data { get; private set; }
  38.  
  39.         /// <summary>
  40.         /// Size of the world.
  41.         /// </summary>
  42.         public Point Size { get; private set; }
  43.  
  44.         /// <summary>
  45.         /// Username of world owner.
  46.         /// </summary>
  47.         public string Ownername { get; private set; }
  48.  
  49.         /// <summary>
  50.         /// Name of the world.
  51.         /// </summary>
  52.         public string Name { get; private set; }
  53.  
  54.         /// <summary>
  55.         /// World plays.
  56.         /// </summary>
  57.         public int Plays { get; private set; }
  58.  
  59.         /// <summary>
  60.         /// World likes.
  61.         /// </summary>
  62.         public int Likes { get; private set; }
  63.  
  64.         /// <summary>
  65.         /// World favorites.
  66.         /// </summary>
  67.         public int Favorites { get; private set; }
  68.  
  69.         /// <summary>
  70.         /// Parses PlayerIO Message. Put "world.Parse(Message);" at the beginning of OnMessage event.
  71.         /// </summary>
  72.         public void Parse(Message m)
  73.         {
  74.             switch (m.Type)
  75.             {
  76.                 case "init":
  77.                     UpdateMeta(m.GetString(0), m.GetString(1), m.GetInt(2), m.GetInt(3), m.GetInt(4));
  78.                     this.Size = new Point(m.GetInt(18), m.GetInt(19));
  79.                     Clear(m);
  80.                     Deserialize(m);
  81.                     break;
  82.                 case "reset":
  83.                     Clear(m);
  84.                     Deserialize(m);
  85.                     break;
  86.                 case "updatemeta":
  87.                     UpdateMeta(m.GetString(0), m.GetString(1), m.GetInt(2), m.GetInt(3), m.GetInt(4));
  88.                     break;
  89.                 #region parse placed blocks
  90.                 case "b": // normal block
  91.                     this.Data[m.GetInt(1), m.GetInt(2), m.GetInt(0)] = new Block(m.GetUInt(3), m.GetUInt(4));
  92.                     break;
  93.                 case "bc": // block with number value
  94.                     this.Data[m.GetInt(0), m.GetInt(1), 0] = new Block(m.GetUInt(2), m.GetUInt(4), new Object[] { m.GetUInt(3) });
  95.                     break;
  96.                 case "br": // block with rotation
  97.                     this.Data[m.GetInt(0), m.GetInt(1), m.GetInt(4)] = new Block(m.GetUInt(2), m.GetUInt(5), new Object[] { m.GetUInt(3) });
  98.                     break;
  99.                 case "bs": // music block
  100.                     this.Data[m.GetInt(0), m.GetInt(1), 0] = new Block(m.GetUInt(2), m.GetUInt(4), new Object[] { m.GetUInt(3) });
  101.                     break;
  102.                 case "pt": // portal block
  103.                     this.Data[m.GetInt(0), m.GetInt(1), 0] = new Block(m.GetUInt(2), (uint)m.GetInt(6), new Object[] { m.GetUInt(3), m.GetUInt(4), m.GetUInt(5) });
  104.                     break;
  105.                 case "ts": //.. sign block
  106.                     this.Data[m.GetInt(0), m.GetInt(1), 0] = new Block(m.GetUInt(2), m.GetUInt(5), new Object[] { m.GetString(3), m.GetUInt(4) });
  107.                     break;
  108.                 case "wp": //... world portal
  109.                     this.Data[m.GetInt(0), m.GetInt(1), 0] = new Block(m.GetUInt(2), m.GetUInt(4), new Object[] { m.GetString(3) });
  110.                     break;
  111.                 case "lb": // label block
  112.                     this.Data[m.GetInt(0), m.GetInt(1), 0] = new Block(m.GetUInt(2), m.GetUInt(5), new Object[] { m.GetString(3), m.GetString(4) });
  113.                     break;
  114.                 #endregion
  115.                 case "clear":
  116.                     Clear(m);
  117.                     break;
  118.             }
  119.         }
  120.  
  121.         #region deserializer
  122.         private void Clear(Message m)
  123.         {
  124.             uint b = m.Type == "clear" ? m.GetUInt(2) : /*9*/0, f = m.Type == "clear" ? m.GetUInt(3) : 0; // aahhhhhhhhhhh
  125.             this.Data = new Block[this.Size.X, this.Size.Y, 2];
  126.             int x = 0, y = 0;
  127.             for (x = 0; x < this.Size.X; x++)
  128.             {
  129.                 for (y = 0; y < this.Size.Y; y++)
  130.                 {
  131.                     this.Data[x, y, 1] = new Block(0);
  132.                     if (x == 0 || x == this.Size.X - 1 || y == 0 || y == this.Size.Y - 1) { this.Data[x, y, 0] = new Block(b); } // check for borders
  133.                     else { this.Data[x, y, 0] = new Block(f); }
  134.                 }
  135.             }
  136.         }
  137.  
  138.         private void Deserialize(Message m)
  139.         {
  140.             var chunks = InitParse.Parse(m);
  141.             foreach (var chunk in chunks)
  142.             {
  143.                 foreach (var pos in chunk.Locations)
  144.                 {
  145.                     this.Data[pos.X, pos.Y, chunk.Layer] = new Block(chunk.Type, 0, chunk.Args);
  146.                 }
  147.             }
  148.         }
  149.  
  150.         private static class InitParse
  151.         { // made by processor // https://gist.github.com/Yonom/3c9ebfe69b1432452f9b // thx <3
  152.             public static DataChunk[] Parse(Message m)
  153.             {
  154.                 if (m == null) throw new ArgumentNullException("m");
  155.                 if (m.Type != "init" && m.Type != "reset") throw new ArgumentException("Invalid message type.", "m");
  156.  
  157.                 // Get world data
  158.                 var p = 0u;
  159.                 var data = new Stack<object>();
  160.                 while (m[p++] as string != "ws") { }
  161.                 while (m[p] as string != "we") { data.Push(m[p++]); }
  162.  
  163.                 // Parse world data
  164.                 var chunks = new List<DataChunk>();
  165.                 while (data.Count > 0)
  166.                 {
  167.                     var args = new Stack<object>();
  168.                     while (!(data.Peek() is byte[]))
  169.                         args.Push(data.Pop());
  170.  
  171.                     var ys = (byte[])data.Pop();
  172.                     var xs = (byte[])data.Pop();
  173.                     var layer = (int)data.Pop();
  174.                     var type = (uint)data.Pop();
  175.  
  176.                     chunks.Add(new DataChunk(layer, type, xs, ys, args.ToArray()));
  177.                 }
  178.  
  179.                 return chunks.ToArray();
  180.             }
  181.  
  182.             public class DataChunk
  183.             {
  184.                 public int Layer { get; set; }
  185.                 public uint Type { get; set; }
  186.                 public Point[] Locations { get; set; }
  187.                 public object[] Args { get; set; }
  188.  
  189.                 public DataChunk(int layer, uint type, byte[] xs, byte[] ys, object[] args)
  190.                 {
  191.                     this.Layer = layer;
  192.                     this.Type = type;
  193.                     this.Args = args;
  194.                     this.Locations = GetLocations(xs, ys);
  195.                 }
  196.  
  197.                 private static Point[] GetLocations(byte[] xs, byte[] ys)
  198.                 {
  199.                     var points = new List<Point>();
  200.                     for (var i = 0; i < xs.Length; i += 2)
  201.                         points.Add(new Point(
  202.                             (xs[i] << 8) | xs[i + 1],
  203.                             (ys[i] << 8) | ys[i + 1]));
  204.                     return points.ToArray();
  205.                 }
  206.             }
  207.         }
  208.         #endregion
  209.  
  210.         private void UpdateMeta(string owner, string name, int plays, int favs, int likes)
  211.         {
  212.             this.Ownername = owner;
  213.             this.Name = name;
  214.             this.Plays = plays;
  215.             this.Favorites = favs;
  216.             this.Likes = likes;
  217.         }
  218.     }
  219.  
  220.     public struct Point
  221.     {
  222.         public int X { get; set; }
  223.         public int Y { get; set; }
  224.  
  225.         public Point(int x, int y) : this()
  226.         {
  227.             this.X = x;
  228.             this.Y = y;
  229.         }
  230.     }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement