Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. public static ActorLook Parse(string str)
  2.         {
  3.             if (string.IsNullOrEmpty(str) || str[0] != '{')
  4.                 throw new Exception("Incorrect EntityLook format : " + str);
  5.  
  6.             var cursorPos = 1;
  7.             var separatorPos = str.IndexOf('|');
  8.  
  9.             if (separatorPos == -1)
  10.             {
  11.                 separatorPos = str.IndexOf("}");
  12.  
  13.                 if (separatorPos == -1)
  14.                     throw new Exception("Incorrect EntityLook format : " + str);
  15.             }
  16.  
  17.             var bonesId = short.Parse(str.Substring(cursorPos, separatorPos - cursorPos));
  18.             cursorPos = separatorPos + 1;
  19.  
  20.             var skins = new short[0];
  21.             if (( separatorPos = str.IndexOf('|', cursorPos) ) != -1 ||
  22.                  ( separatorPos = str.IndexOf('}', cursorPos) ) != -1)
  23.             {
  24.                 skins = ParseCollection(str.Substring(cursorPos, separatorPos - cursorPos), short.Parse);
  25.                 cursorPos = separatorPos + 1;
  26.             }
  27.  
  28.             var colors = new Tuple<int, int>[0];
  29.             if (( separatorPos = str.IndexOf('|', cursorPos) ) != -1 ||
  30.                  ( separatorPos = str.IndexOf('}', cursorPos) ) != -1) // if false there are no informations between the two separators
  31.             {
  32.                 colors = ParseCollection(str.Substring(cursorPos, separatorPos - cursorPos), ParseIndexedColor);
  33.                 cursorPos = separatorPos + 1;
  34.             }
  35.  
  36.             var scales = new short[0];
  37.             if (( separatorPos = str.IndexOf('|', cursorPos) ) != -1 ||
  38.                  ( separatorPos = str.IndexOf('}', cursorPos) ) != -1) // if false there are no informations between the two separators
  39.             {
  40.                 scales = ParseCollection(str.Substring(cursorPos, separatorPos - cursorPos), short.Parse);
  41.                 cursorPos = separatorPos + 1;
  42.             }
  43.  
  44.             var subEntities = new List<SubActorLook>();
  45.             while (cursorPos < str.Length && (str.Length - cursorPos) >= 3)
  46.             {
  47.                 var atSeparatorIndex = str.IndexOf('@', cursorPos, 3); // max size of a byte = 255, so 3 characters
  48.                 var equalsSeparatorIndex = str.IndexOf('=', atSeparatorIndex + 1, 3); // max size of a byte = 255, so 3 characters
  49.                 var category = byte.Parse(str.Substring(cursorPos, atSeparatorIndex - cursorPos));
  50.                 var index = byte.Parse(str.Substring(atSeparatorIndex + 1, equalsSeparatorIndex - ( atSeparatorIndex + 1 )));
  51.  
  52.                 var hookDepth = 0;
  53.                 var i = equalsSeparatorIndex + 1;
  54.                 var subEntity = new StringBuilder();
  55.                 do
  56.                 {
  57.                     subEntity.Append(str[i]);
  58.  
  59.                     switch (str[i])
  60.                     {
  61.                         case '{':
  62.                             hookDepth++;
  63.                             break;
  64.                         case '}':
  65.                             hookDepth--;
  66.                             break;
  67.                     }
  68.  
  69.                     i++;
  70.                 } while (hookDepth > 0);
  71.  
  72.                 subEntities.Add(new SubActorLook((sbyte)index, (SubEntityBindingPointCategoryEnum) category, Parse(subEntity.ToString())));
  73.  
  74.                 cursorPos = i + 1; // ignore the comma and the last '}' char
  75.             }
  76.  
  77.             return new ActorLook(bonesId, skins, colors.ToDictionary(x => x.Item1, x => Color.FromArgb(x.Item2)), scales, subEntities.ToArray());
  78.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement