//Serialize a set of rectangles(RectList) to SerializedRects public static SerializedRects RectsToSerializedRects(RectList rectSet) { GridConverter.BuildCircuit(rectSet, false); var sb = new StringBuilder(); bool mode2D = false;// RectsAre2D(rectSet); //Sort the rectlist into sets of rectlists where rects have same properties IEnumerable <List <Rect> > superRects = SuperSortByProperties(rectSet); //Iterate through entire set of rectlists foreach (List <Rect> rectList in superRects) { string rectPropertiesSerialized = SerializeRectProperties(rectList[0].Properties, mode2D); sb.Append(rectPropertiesSerialized); //Iterate through each rect in the set (will all have same properties) //Each will inherit properties values when read later from last properties write foreach (Rect rect in rectList) { string rectSerialized = Transcode64.SerializeRectPointsToString(rect, 1, mode2D); rectSerialized += SerializeCircuitProperties(rectList[0].Properties); sb.Append(rectSerialized); } } //Then return new serialized rect with that string return(new SerializedRects(sb.ToString())); }
//Limits it to 255 strings between carriage returns public static string SerializeLimit255(RectList rectSet) { IEnumerable <List <Rect> > superRects = SuperSortByProperties(rectSet); string str = ""; string runStr = ""; bool mode2D = false;// RectsAre2D(rectSet); foreach (List <Rect> rectList in superRects) { runStr += SerializeRectProperties(rectList[0].Properties, mode2D); foreach (Rect t in rectList) { runStr += Transcode64.SerializeRectPointsToString(t, 1, mode2D); runStr += SerializeCircuitProperties(rectList[0].Properties); if (runStr.Length > 200)//236) { str += runStr + CharLimit255; runStr = SerializeRectProperties(rectList[0].Properties, mode2D); } } } str += runStr + CharLimit255; str = str.TrimEnd('\n') + "\n";//ensure only one \n at end return(str); }
public static string SerializeCircuitProperties(CellProperties properties) { string str = ""; if (properties.CircuitIds != null) { foreach (int circuitId in properties.CircuitIds) { if (circuitId > 0) { str += "" + CharCircuit; str += Transcode64.To64(properties.PhysicsId); str += Transcode64.To64(circuitId); } } } return(str); }
//Serialize cellproperties to a string private static string SerializeRectProperties(CellProperties properties, bool Mode2D) { string str = ""; if (Mode2D) { str += CharRects2D; } else { str += CharRgba; } byte r, g, b, a; Transcode64.RecodeUlongtoRgba(properties.Rgba, out r, out g, out b, out a); str += Transcode64.To64(r); str += Transcode64.To64(g); str += Transcode64.To64(b); str += Transcode64.To64(a); str += Transcode64.To64(properties.ShapeId); str += Transcode64.To64(properties.TextureId); if (properties.GroupId > 0) { str += "" + CharGroup; str += Transcode64.To64(properties.GroupId); } if (properties.WorldId > 0) { str += "" + CharWorld; str += Transcode64.To64((byte)((properties.WorldId >> 24) & 0xFF)); str += Transcode64.To64((byte)((properties.WorldId >> 16) & 0xFF)); str += Transcode64.To64((byte)((properties.WorldId >> 8) & 0xFF)); str += Transcode64.To64((byte)((properties.WorldId >> 0) & 0xFF)); } return(str); }
public static Bytes HexDataToBytes(string data) { return(new Bytes(Transcode64.HexDataToByteArray(data))); }
//Translates hex data in string format to byte array public static byte[] HexDataToByteArray(string data) { return(Transcode64.HexDataToByteArray(data)); }
//Deserialized SerializedRects back to set of rectangles(RectList) public static RectList SerializedRectsToRects(SerializedRects serializedRects) { RectList rects = new RectList(); char state = ' '; byte shapeId = 0; byte textureId = 0; ulong rgba = 0; byte groupId = 0; ulong worldid = 0; string serial = serializedRects.SerializedData; char mode = CharRects3D; for (int i = 0; i < serial.Length; i++) { if (serial[i] == CharRects2D) { mode = CharRects2D; state = CharRgba; } if (serial[i] == CharRgba) { mode = CharRects3D; state = CharRgba; } if (serial[i] == CharGroup) { state = CharGroup; } if (serial[i] == CharLimit255) { state = CharLimit255; } if (serial[i] == CharCircuit) { state = CharCircuit; } if (serial[i] == CharWorld) { state = CharWorld; } switch (state) { case CharGroup: { groupId = Transcode64.From64(serial[++i]); state = mode; break; } case CharRgba: //Reading RGBA palette { i++; int r = Transcode64.From64(serial[i++]); int g = Transcode64.From64(serial[i++]); int b = Transcode64.From64(serial[i++]); int a = Transcode64.From64(serial[i++]); rgba = Transcode64.RecodeRgbatoUlong((byte)r, (byte)g, (byte)b, (byte)a); shapeId = Transcode64.From64(serial[i++]); textureId = Transcode64.From64(serial[i]); state = mode; break; } case CharRects2D: //Reading emergents { Rect rect = new Rect(); rect.Pt1[0] = Transcode64.From64(serial[i++]); rect.Pt1[2] = Transcode64.From64(serial[i++]); rect.Pt2[0] = Transcode64.From64(serial[i++]); rect.Pt2[2] = Transcode64.From64(serial[i]); //Convert from inclusve rect.Pt2[0]++; rect.Pt2[2]++; rect.Properties.Rgba = rgba; rect.Properties.ShapeId = shapeId; rect.Properties.TextureId = textureId; rect.Properties.GroupId = groupId; rect.Properties.WorldId = worldid; rects.AddRect(rect); break; } case CharRects3D: //Reading emergents { Rect rect = new Rect(); rect.Pt1[0] = Transcode64.From64(serial[i++]); rect.Pt1[1] = Transcode64.From64(serial[i++]); rect.Pt1[2] = Transcode64.From64(serial[i++]); rect.Pt2[0] = Transcode64.From64(serial[i++]); rect.Pt2[1] = Transcode64.From64(serial[i++]); rect.Pt2[2] = Transcode64.From64(serial[i]); //Convert from inclusve rect.Pt2[0]++; rect.Pt2[1]++; rect.Pt2[2]++; rect.Properties.Rgba = rgba; rect.Properties.ShapeId = shapeId; rect.Properties.TextureId = textureId; rect.Properties.GroupId = groupId; rect.Properties.WorldId = worldid; rects.AddRect(rect); break; } case CharCircuit: { i++; byte pid = Transcode64.From64(serial[i++]); //physics id byte cid = Transcode64.From64(serial[i]); //circuit id if (rects.GetRect(rects.Count - 1).Properties.CircuitIds == null) { rects.GetRect(rects.Count - 1).Properties.CircuitIds = new List <int>(); } rects.GetRect(rects.Count - 1).Properties.PhysicsId = pid; rects.GetRect(rects.Count - 1).Properties.CircuitIds.Add(cid); state = mode; break; } case CharWorld: //Reading World ID { i++; int v4 = Transcode64.From64(serial[i++]); int v3 = Transcode64.From64(serial[i++]); int v2 = Transcode64.From64(serial[i++]); int v1 = Transcode64.From64(serial[i++]); worldid = (ulong)((v4 << 24) | (v3 << 16) | (v2 << 8) | v1); state = mode; break; } } } GridConverter.BuildCircuit(rects, false); return(rects); }