lrussell

WorldFormat

Dec 29th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.87 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 System.Drawing;
  7. using PlayerIOClient;
  8. using ProtoBuf;
  9.  
  10. namespace WorldIndexer
  11. {
  12.     class WorldFormat
  13.     {
  14.         private static string UIntToHex(uint color)
  15.         {
  16.             byte a = (byte)(color >> 24);
  17.             byte r = (byte)(color >> 16);
  18.             byte g = (byte)(color >> 8);
  19.             byte b = (byte)(color >> 0);
  20.             return ColorTranslator.ToHtml(Color.FromArgb(a, r, g, b));
  21.         }
  22.  
  23.         [ProtoContract]
  24.         public class World
  25.         {
  26.             [ProtoMember(1)]
  27.             public string Id;
  28.             [ProtoMember(2)]
  29.             public string Name = "Untitled World";
  30.             [ProtoMember(3)]
  31.             public string Owner = "(unknown)";
  32.             [ProtoMember(4)]
  33.             public string OwnerId = "(unknown)";
  34.             [ProtoMember(5)]
  35.             public int Plays;
  36.             [ProtoMember(6)]
  37.             public int Width = 200;
  38.             [ProtoMember(7)]
  39.             public int Height = 200;
  40.             [ProtoMember(8)]
  41.             public int Woots;
  42.             [ProtoMember(9)]
  43.             public int TotalWoots;
  44.             [ProtoMember(10)]
  45.             public bool AllowPotions;
  46.             [ProtoMember(11)]
  47.             public bool Visible = true;
  48.             [ProtoMember(12)]
  49.             public string BackgroundColor;
  50.             [ProtoMember(13)]
  51.             public List<WorldData> WorldData = new List<WorldData>();
  52.         }
  53.  
  54.         [ProtoContract]
  55.         public class WorldData
  56.         {
  57.             [ProtoMember(1)]
  58.             public int type;
  59.             [ProtoMember(2)]
  60.             public int layer;
  61.             [ProtoMember(3)]
  62.             public int rotation;
  63.             [ProtoMember(4)]
  64.             public int id;
  65.             [ProtoMember(5)]
  66.             public int goal;
  67.             [ProtoMember(6)]
  68.             public string target;
  69.             [ProtoMember(7)]
  70.             public string text;
  71.             [ProtoMember(8)]
  72.             public List<Tuple<int, int>> locs = new List<Tuple<int,int>>();
  73.         }
  74.  
  75.         public class Converter
  76.         {
  77.             private World w = new World();
  78.             public World Convert(DatabaseObject dbo)
  79.             {
  80.                 w.Id = dbo.Key;
  81.  
  82.                 foreach (KeyValuePair<string, object> wi in dbo)
  83.                 {
  84.                     switch (wi.Key)
  85.                     {
  86.                         case "width":
  87.                             w.Width = System.Convert.ToInt32(wi.Value);
  88.                             break;
  89.                         case "height":
  90.                             w.Height = System.Convert.ToInt32(wi.Value);
  91.                             break;
  92.                         case "name":
  93.                             w.Name = System.Convert.ToString(wi.Value);
  94.                             break;
  95.                         case "plays":
  96.                             w.Plays = System.Convert.ToInt32(wi.Value);
  97.                             break;
  98.                         case "owner":
  99.                             //w.Owner = EE.IdToUser(System.Convert.ToString(wi.Value)); Add your own IdToUser function or remove.
  100.                             w.OwnerId = System.Convert.ToString(wi.Value);
  101.                             break;
  102.                         case "woots":
  103.                             w.Woots = System.Convert.ToInt32(wi.Value);
  104.                             break;
  105.                         case "totalwoots":
  106.                             w.TotalWoots = System.Convert.ToInt32(wi.Value);
  107.                             break;
  108.                         case "allowpotions":
  109.                             w.AllowPotions = System.Convert.ToBoolean(wi.Value);
  110.                             break;
  111.                         case "visible":
  112.                             w.Visible = System.Convert.ToBoolean(wi.Value);
  113.                             break;
  114.                         case "backgroundColor":
  115.                             w.BackgroundColor = (System.Convert.ToUInt32(wi.Value) != 0) ? UIntToHex(System.Convert.ToUInt32(wi.Value)) : null;
  116.                             break;
  117.                         case "worlddata":
  118.                             {
  119.                                 foreach (DatabaseObject obj in (DatabaseArray)wi.Value)
  120.                                 {
  121.                                     if (obj != null)
  122.                                     {
  123.                                         WorldData wd = new WorldData();
  124.  
  125.                                         wd.type = obj.GetInt("type", 0);
  126.                                         wd.layer = obj.GetInt("layer", 0);
  127.                                         wd.rotation = obj.GetInt("rotation", 0);
  128.                                         wd.id = obj.GetInt("id", 0);
  129.                                         wd.target = obj.GetString("target", null);
  130.                                         wd.goal = obj.GetInt("goal", 0);
  131.                                         wd.text = obj.GetString("text", null);
  132.                                         if (obj.Contains("x") && obj.Contains("y"))
  133.                                         {
  134.                                             byte[] x = obj.GetBytes("x");
  135.                                             byte[] y = obj.GetBytes("y");
  136.                                             for (int p = 0; p < x.Length; p += 2)
  137.                                                 wd.locs.Add(new Tuple<int, int>(x[p] << 8 | x[p + 1], y[p] << 8 | y[p + 1]));
  138.                                         }
  139.  
  140.                                         w.WorldData.Add(wd);
  141.                                     }
  142.                                 }
  143.                             }
  144.                             break;
  145.                     }
  146.                 }
  147.                 return w;
  148.             }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment