public void FromJSON(Ferr_JSONValue aJSON) { Ferr_JSONValue json = new Ferr_JSONValue(); applyTo = (Ferr2DT_TerrainDirection)aJSON["applyTo", (int)Ferr2DT_TerrainDirection.Top]; zOffset = aJSON["zOffset", 0f]; yOffset = aJSON["yOffset", 0f]; capOffset = aJSON["capOffset", 0f]; leftCap = new Rect( aJSON["leftCap.x", 0f], aJSON["leftCap.y", 0f], aJSON["leftCap.xMax", 0f], aJSON["leftCap.yMax", 0f]); rightCap = new Rect( aJSON["rightCap.x", 0f], aJSON["rightCap.y", 0f], aJSON["rightCap.xMax", 0f], aJSON["rightCap.yMax", 0f]); Ferr_JSONValue bodyArr = json["body"]; body = new Rect[bodyArr.Length]; for (int i = 0; i < body.Length; i++) { body[i] = new Rect( bodyArr[i]["x", 0], bodyArr[i]["y", 0], bodyArr[i]["xMax", 50], bodyArr[i]["yMax", 50]); } }
public Ferr_JSONValue ToJSON() { Ferr_JSONValue json = new Ferr_JSONValue(); json["applyTo"] = (int)applyTo; json["zOffset"] = zOffset; json["yOffset"] = yOffset; json["capOffset"] = capOffset; json["leftCap.x"] = leftCap.x; json["leftCap.y"] = leftCap.y; json["leftCap.xMax"] = leftCap.xMax; json["leftCap.yMax"] = leftCap.yMax; json["rightCap.x"] = rightCap.x; json["rightCap.y"] = rightCap.y; json["rightCap.xMax"] = rightCap.xMax; json["rightCap.yMax"] = rightCap.yMax; json["body"] = 0; Ferr_JSONValue bodyArr = json["body"]; for (int i = 0; i < body.Length; i++) { Ferr_JSONValue rect = new Ferr_JSONValue(); rect["x"] = body[i].x; rect["y"] = body[i].y; rect["xMax"] = body[i].xMax; rect["yMax"] = body[i].yMax; bodyArr[i] = rect; } return(json); }
/// <summary> /// Creates a TerrainMaterial from a JSON object. Does NOT recreate mesh data automatically or link materials! /// </summary> /// <param name="aJSON">A parsed JSON value containing PathTerrain and Path data</param> public void FromJSON(Ferr_JSONValue aJSON) { fill = (Ferr2DT_FillMode)Enum.Parse(typeof(Ferr2DT_FillMode), aJSON["fill", "Closed"]); fillY = aJSON["fillY", 0]; fillZ = aJSON["fillZ", 0.2f]; splitCorners = aJSON["splitCorners", true]; smoothPath = aJSON["smoothPath", false]; splitDist = aJSON["splitDist", 4]; pixelsPerUnit = aJSON["pixelsPerUnit", 32]; stretchThreshold = aJSON["stretchThreshold", 0.5f]; vertexColor = Ferr_Color.FromHex(aJSON["vertexColor", "FFFFFF"]); createCollider = aJSON["createCollider", true]; create3DCollider = aJSON["create3DCollider", false]; depth = aJSON["depth", 4]; surfaceOffset[0] = aJSON["surfaceOffset.0", 0]; surfaceOffset[1] = aJSON["surfaceOffset.1", 0]; surfaceOffset[2] = aJSON["surfaceOffset.2", 0]; surfaceOffset[3] = aJSON["surfaceOffset.3", 0]; collidersBottom = aJSON["colliders.bottom", true]; collidersTop = aJSON["colliders.top", true]; collidersLeft = aJSON["colliders.left", true]; collidersRight = aJSON["colliders.right", true]; Ferr_JSONValue overrides = aJSON["directionOverrides"]; for (int i = 0; i < overrides.Length; i++) { directionOverrides.Add((Ferr2DT_TerrainDirection)Enum.Parse(typeof(Ferr2DT_TerrainDirection), overrides[i, "None"])); } path.FromJSON(aJSON["path"]); }
/// <summary> /// Creates a JSON object containing PathTerrain and Path data. /// </summary> /// <returns>A JSON Value containing PathTerrain and Path data. You can ToString this for the JSON string.</returns> public Ferr_JSONValue ToJSON() { Ferr_JSONValue result = new Ferr_JSONValue(); result["fill"] = "" + fill; result["fillY"] = fillY; result["fillZ"] = fillZ; result["splitCorners"] = splitCorners; result["smoothPath"] = smoothPath; result["splitDist"] = splitDist; result["pixelsPerUnit"] = pixelsPerUnit; result["stretchThreshold"] = stretchThreshold; result["vertexColor"] = Ferr_Color.ToHex(vertexColor); result["createCollider"] = createCollider; result["create3DCollider"] = create3DCollider; result["depth"] = depth; result["surfaceOffset.0"] = surfaceOffset[0]; result["surfaceOffset.1"] = surfaceOffset[1]; result["surfaceOffset.2"] = surfaceOffset[2]; result["surfaceOffset.3"] = surfaceOffset[3]; result["colliders.bottom"] = collidersBottom; result["colliders.top"] = collidersTop; result["colliders.left"] = collidersLeft; result["colliders.right"] = collidersRight; result["directionOverrides"] = 0; Ferr_JSONValue dir = result["directionOverrides"]; for (int i = 0; i < directionOverrides.Count; i++) { dir[i] = directionOverrides[i].ToString(); } result["path"] = path.ToJSON(); return(result); }
/// <summary> /// Creates a JSON object from the given object. Only uses public fields from that specific class, /// not including parent classes. Also, only supports really basic variable types well. /// </summary> /// <returns> /// A JSON object that can be .ToString()ed into JSON data. /// </returns> /// <param name='obj'> /// A really simple object you want to convert into JSON. /// </param> public static Ferr_JSONValue MakeShallow(object obj) { Type t = obj.GetType(); FieldInfo[] fields = t.GetFields(System.Reflection.BindingFlags.Public | BindingFlags.Instance); Ferr_JSONValue val = new Ferr_JSONValue(); for (int i = 0; i < fields.Length; i++) { object data = fields[i].GetValue(obj); if (data is float || data is int || data is double || data is long) { val[fields[i].Name] = Convert.ToSingle(data); } else if (data is string) { val[fields[i].Name] = (string)data; } else if (data is bool) { val[fields[i].Name] = (bool)data; } else { val[fields[i].Name] = data.ToString(); } } return(val); }
public Ferr_JSONValue ToJSON () { Ferr_JSONValue json = new Ferr_JSONValue(); json["applyTo" ] = (int)applyTo; json["zOffset" ] = zOffset; json["yOffset" ] = yOffset; json["capOffset" ] = capOffset; json["leftCap.x" ] = leftCap.x; json["leftCap.y" ] = leftCap.y; json["leftCap.xMax" ] = leftCap.xMax; json["leftCap.yMax" ] = leftCap.yMax; json["rightCap.x" ] = rightCap.x; json["rightCap.y" ] = rightCap.y; json["rightCap.xMax"] = rightCap.xMax; json["rightCap.yMax"] = rightCap.yMax; json["body"] = 0; Ferr_JSONValue bodyArr = json["body"]; for (int i = 0; i < body.Length; i++) { Ferr_JSONValue rect = new Ferr_JSONValue(); rect["x" ] = body[i].x; rect["y" ] = body[i].y; rect["xMax"] = body[i].xMax; rect["yMax"] = body[i].yMax; bodyArr[i] = rect; } return json; }
public void FromJSON(Ferr_JSONValue aJSON) { Ferr_JSONValue json = new Ferr_JSONValue(); applyTo = (Ferr2DT_TerrainDirection)aJSON["applyTo", (int)Ferr2DT_TerrainDirection.Top]; zOffset = aJSON["zOffset",0f]; yOffset = aJSON["yOffset",0f]; capOffset = aJSON["capOffset",0f]; leftCap = new Rect( aJSON["leftCap.x", 0f], aJSON["leftCap.y", 0f], aJSON["leftCap.xMax", 0f], aJSON["leftCap.yMax", 0f]); rightCap = new Rect( aJSON["rightCap.x", 0f], aJSON["rightCap.y", 0f], aJSON["rightCap.xMax", 0f], aJSON["rightCap.yMax", 0f]); Ferr_JSONValue bodyArr = json["body"]; body = new Rect[bodyArr.Length]; for (int i = 0; i < body.Length; i++) { body[i] = new Rect( bodyArr[i]["x", 0 ], bodyArr[i]["y", 0 ], bodyArr[i]["xMax", 50], bodyArr[i]["yMax", 50]); } }
/// <summary> /// Creates a TerrainMaterial from a JSON object, does -not- link edgeMaterial or fillMaterial, you'll have to do that yourself! /// </summary> /// <param name="aJSON">A parsed JSON value</param> public void FromJSON(Ferr_JSONValue aJSON) { Ferr_JSONValue descArr = aJSON["descriptors"]; for (int i = 0; i < descArr.Length; i++) { descriptors[i] = new Ferr2DT_SegmentDescription(); descriptors[i].FromJSON(descArr[i]); } }
/// <summary> /// Creates a Path object from a JSON value object. /// </summary> /// <param name="aJSON">A JSON object with path data!</param> public void FromJSON(Ferr_JSONValue aJSON) { closed = aJSON["closed", false ]; pathVerts = new List<Vector2>(); object[] verts = aJSON["verts", new object[]{}]; for (int i = 0; i < verts.Length; i++) { if (verts[i] is Ferr_JSONValue) { Ferr_JSONValue v = verts[i] as Ferr_JSONValue; pathVerts.Add(new Vector2(v[0,0f], v[1,0f])); } } }
/// <summary> /// Overwrites data with information from the provided JSON. This method will throw an Exception /// if the text cannot be split into exactly two pieces (left and right of an initial ':') /// </summary> public void FromString(string aText) { aText = aText.Trim(); string[] words = Ferr_JSON._Split(aText, ':', true); if (words.Length != 2) { throw new Exception("Bad JSON Object: " + aText); } name = words[0].Trim().Replace("\"", ""); val = new Ferr_JSONValue(); val.FromString(words[1]); }
/// <summary> /// Parses the provided JSON into a JSON object. If errors occur, null will be returned, and error info /// will be povided in the Ferr_JSON.error and Ferr_JSON.failed fields. /// </summary> /// <param name='aText'> /// JSON text data. Whitespace isn't important. /// </param> public static Ferr_JSONValue Parse(string aText) { Ferr_JSONValue result = null; failed = false; error = null; try { result = new Ferr_JSONValue(); result.FromString(aText); } catch (Exception e) { error = e; failed = true; } return(result); }
/// <summary> /// Creates a Path object from a JSON value object. /// </summary> /// <param name="aJSON">A JSON object with path data!</param> public void FromJSON(Ferr_JSONValue aJSON) { closed = aJSON["closed", false]; pathVerts = new List <Vector2>(); object[] verts = aJSON["verts", new object[] {}]; for (int i = 0; i < verts.Length; i++) { if (verts[i] is Ferr_JSONValue) { Ferr_JSONValue v = verts[i] as Ferr_JSONValue; pathVerts.Add(new Vector2(v[0, 0f], v[1, 0f])); } } }
/// <summary> /// Creates or sets all values along the path to Object, last object on the path is /// set to the provided value. /// </summary> public Ferr_JSONValue Set(string aPath, Ferr_JSONValue aVal) { string[] words = aPath.Split('.'); Ferr_JSONValue curr = this; Ferr_JSONObject currObj = null; for (int i = 0; i < words.Length; i++) { Ferr_JSONObject result = curr.GetImmidiateChild(words[i]); if (result == null) { Ferr_JSONObject[] list = null; if (curr.type == Ferr_JSONType.Object) { list = (Ferr_JSONObject[])curr.data; if (list == null) { list = new Ferr_JSONObject[1]; } else { Array.Resize <Ferr_JSONObject>(ref list, list.Length + 1); } curr.data = list; } else { curr.type = Ferr_JSONType.Object; curr.data = list = new Ferr_JSONObject[1]; } list[list.Length - 1] = new Ferr_JSONObject(words[i], new Ferr_JSONValue()); result = list[list.Length - 1]; } currObj = result; curr = result.val; } if (currObj != null) { currObj.val = aVal; return(currObj.val); } else { return(null); } }
/// <summary> /// Creates a JSON value object from this path. /// </summary> /// <returns>JSON Value object, can put it into a larger JSON object, or just ToString it.</returns> public Ferr_JSONValue ToJSON () { Ferr_JSONValue result = new Ferr_JSONValue(); result["closed"] = closed; object[] list = new object[pathVerts.Count]; for (int i = 0; i < pathVerts.Count; i++) { Ferr_JSONValue vert = new Ferr_JSONValue(); vert[0] = pathVerts[i].x; vert[1] = pathVerts[i].y; list[i] = vert; } result["verts"] = list; return result; }
/// <summary> /// Creates a JSON string from this TerrainMaterial, edgeMaterial and fillMaterial are stored by name only. /// </summary> /// <returns>JSON Value object, can put it into a larger JSON object, or just ToString it.</returns> public Ferr_JSONValue ToJSON() { Ferr_JSONValue json = new Ferr_JSONValue(); json["fillMaterialName"] = fillMaterial.name; json["edgeMaterialName"] = edgeMaterial.name; json["descriptors"] = 0; Ferr_JSONValue descArr = json["descriptors"]; for (int i = 0; i < descriptors.Length; i++) { descArr[i] = descriptors[i].ToJSON(); } return(json); }
/// <summary> /// Creates a JSON object containing PathTerrain and Path data. /// </summary> /// <returns>A JSON Value containing PathTerrain and Path data. You can ToString this for the JSON string.</returns> public Ferr_JSONValue ToJSON() { Ferr_JSONValue result = new Ferr_JSONValue(); result["fill"] = "" + fill; result["fillY"] = fillY; result["fillZ"] = fillZ; result["splitCorners"] = splitCorners; result["smoothPath"] = smoothPath; result["splitDist"] = splitDist; result["splitCount"] = splitCount; result["splitMiddle"] = splitMiddle; result["pixelsPerUnit"] = pixelsPerUnit; result["stretchThreshold"] = stretchThreshold; result["vertexColor"] = Ferr_Color.ToHex(vertexColor); result["createTangents"] = createTangents; result["randomBWC"] = randomByWorldCoordinates; result["slantAmount"] = slantAmount; result["createCollider"] = createCollider; result["create3DCollider"] = create3DCollider; result["isTrigger"] = isTrigger; result["ssCollisions"] = smoothSphereCollisions; result["sharpCorners"] = sharpCorners; result["sharpCornerDist"] = sharpCornerDistance; result["depth"] = depth; result["uvOffset.x"] = uvOffset.x; result["uvOffset.y"] = uvOffset.y; result["surfaceOffset.0"] = surfaceOffset[0]; result["surfaceOffset.1"] = surfaceOffset[1]; result["surfaceOffset.2"] = surfaceOffset[2]; result["surfaceOffset.3"] = surfaceOffset[3]; result["colliders.bottom"] = collidersBottom; result["colliders.top"] = collidersTop; result["colliders.left"] = collidersLeft; result["colliders.right"] = collidersRight; result["directionOverrides"] = 0; Ferr_JSONValue dir = result["directionOverrides"]; for (int i = 0; i < directionOverrides.Count; i++) { dir[i] = directionOverrides[i].ToString(); } result["path"] = Path.ToJSON(); return(result); }
/// <summary> /// Creates a TerrainMaterial from a JSON object. Does NOT recreate mesh data automatically or link materials! /// </summary> /// <param name="aJSON">A parsed JSON value containing PathTerrain and Path data</param> public void FromJSON(Ferr_JSONValue aJSON) { fill = (Ferr2DT_FillMode)Enum.Parse(typeof(Ferr2DT_FillMode), aJSON["fill", "Closed"]); fillY = aJSON["fillY", 0]; fillZ = aJSON["fillZ", 0.2f]; splitCorners = aJSON["splitCorners", true]; smoothPath = aJSON["smoothPath", false]; splitCount = (int)aJSON["splitCount", 4]; splitDist = aJSON["splitDist", 1]; splitMiddle = aJSON["splitMiddle", true]; pixelsPerUnit = aJSON["pixelsPerUnit", 32]; stretchThreshold = aJSON["stretchThreshold", 0.5f]; vertexColor = Ferr_Color.FromHex(aJSON["vertexColor", "FFFFFF"]); createTangents = aJSON["createTangents", false]; createCollider = aJSON["createCollider", true]; create3DCollider = aJSON["create3DCollider", false]; depth = aJSON["depth", 4]; isTrigger = aJSON["isTrigger", false]; sharpCorners = aJSON["sharpCorners", false]; sharpCornerDistance = aJSON["sharpCornerDist", 2]; smoothSphereCollisions = aJSON["ssCollisions", false]; randomByWorldCoordinates = aJSON["randomBWC", false]; slantAmount = aJSON["slantAmount", 0]; uvOffset.x = aJSON["uvOffset.x", 0]; uvOffset.y = aJSON["uvOffset.y", 0]; surfaceOffset[0] = aJSON["surfaceOffset.0", 0]; surfaceOffset[1] = aJSON["surfaceOffset.1", 0]; surfaceOffset[2] = aJSON["surfaceOffset.2", 0]; surfaceOffset[3] = aJSON["surfaceOffset.3", 0]; collidersBottom = aJSON["colliders.bottom", true]; collidersTop = aJSON["colliders.top", true]; collidersLeft = aJSON["colliders.left", true]; collidersRight = aJSON["colliders.right", true]; Ferr_JSONValue overrides = aJSON["directionOverrides"]; for (int i = 0; i < overrides.Length; i++) { directionOverrides.Add((Ferr2DT_TerrainDirection)Enum.Parse(typeof(Ferr2DT_TerrainDirection), overrides[i, "None"])); vertScales.Add(1); } Path.FromJSON(aJSON["path"]); }
/// <summary> /// Creates a JSON value object from this path. /// </summary> /// <returns>JSON Value object, can put it into a larger JSON object, or just ToString it.</returns> public Ferr_JSONValue ToJSON() { Ferr_JSONValue result = new Ferr_JSONValue(); result["closed"] = closed; object[] list = new object[pathVerts.Count]; for (int i = 0; i < pathVerts.Count; i++) { Ferr_JSONValue vert = new Ferr_JSONValue(); vert[0] = pathVerts[i].x; vert[1] = pathVerts[i].y; list[i] = vert; } result["verts"] = list; return(result); }
/// <summary> /// Gets the value at the specific path. Paths are separated using '.'s Ex: /// "scene.player.name", returns null if anything along the path cannot be found, /// case sensitive. /// </summary> public Ferr_JSONValue Get(string aPath) { string[] words = aPath.Split('.'); Ferr_JSONValue curr = this; for (int i = 0; i < words.Length; i++) { Ferr_JSONObject child = curr.GetImmidiateChild(words[i]); if (child != null) { curr = child.val; } else { return(null); } } return(curr); }
public Ferr_JSONValue Set(int aIndex, Ferr_JSONValue aVal) { if (type == Ferr_JSONType.Array) { object[] list = (object[])data; if (list.Length <= aIndex) { Array.Resize <object>(ref list, aIndex + 1); data = list; } list[aIndex] = aVal; return(aVal); } else { type = Ferr_JSONType.Array; object[] list = new object[aIndex + 1]; list[aIndex] = aVal; data = list; return(aVal); } }
public bool this[int aIndex, bool aDefaultValue] { get { Ferr_JSONValue val = Get(aIndex); return(val == null || val.type != Ferr_JSONType.Bool ? aDefaultValue: (bool )val.data); } }
public float this[int aIndex, float aDefaultValue] { get { Ferr_JSONValue val = Get(aIndex); return(val == null || val.type != Ferr_JSONType.Number ? aDefaultValue: (float )val.data); } }
public object[] this[int aIndex, object[] aDefaultValue] { get { Ferr_JSONValue val = Get(aIndex); return(val == null || val.type != Ferr_JSONType.Array ? aDefaultValue: (object[] )val.data); } }
public bool this[string aPath, bool aDefaultValue] { get { Ferr_JSONValue val = Get(aPath); return(val == null || val.type != Ferr_JSONType.Bool ? aDefaultValue: (bool )val.data); } }
public string this[string aPath, string aDefaultValue] { get { Ferr_JSONValue val = Get(aPath); return(val == null || val.type != Ferr_JSONType.String ? aDefaultValue: (string )val.data); } }
public Ferr_JSONValue Set (int aIndex, Ferr_JSONValue aVal) { if (type == Ferr_JSONType.Array) { object[] list = (object[])data; if (list.Length <= aIndex) { Array.Resize<object>(ref list, aIndex+1); data = list; } list[aIndex] = aVal; return aVal; } else { type = Ferr_JSONType.Array; object[] list = new object[aIndex+1]; list[aIndex] = aVal; data = list; return aVal; } }
/// <summary> /// Creates a JSON string from this TerrainMaterial, edgeMaterial and fillMaterial are stored by name only. /// </summary> /// <returns>JSON Value object, can put it into a larger JSON object, or just ToString it.</returns> public Ferr_JSONValue ToJSON() { Ferr_JSONValue json = new Ferr_JSONValue(); json["fillMaterialName"] = fillMaterial.name; json["edgeMaterialName"] = edgeMaterial.name; json["descriptors" ] = 0; Ferr_JSONValue descArr = json["descriptors"]; for (int i = 0; i < descriptors.Length; i++) { descArr[i] = descriptors[i].ToJSON(); } return json; }
public float this[string aPath, float aDefaultValue] { get { Ferr_JSONValue val = Get(aPath); return(val == null || val.type != Ferr_JSONType.Number ? aDefaultValue: (float )val.data); } }
public object[] this[string aPath, object[] aDefaultValue] { get { Ferr_JSONValue val = Get(aPath); return(val == null || val.type != Ferr_JSONType.Array ? aDefaultValue: (object[])val.data); } }
/// <summary> /// Manually creates a JSON object /// </summary> public Ferr_JSONObject(string aName, Ferr_JSONValue aVal) { name = aName; val = aVal; }
/// <summary> /// Recursively converts a JSON string into a JSON data structure. Throws an exception /// if given an empty string. Unrecognized data is converted into a string. /// </summary> public void FromString(string aValue) { string text = aValue.Trim(); float fVal = 0; if (text.Length <= 0) throw new Exception("Bad JSON value: " + aValue); if (text[0] == '{') { type = Ferr_JSONType.Object; text = text.Remove(0,1); text = text.Remove(text.Length-1,1); string [] items = Ferr_JSON._Split(text, ',', true); Ferr_JSONObject[] result = new Ferr_JSONObject[items.Length]; for (int i = 0; i < items.Length; i++) { result[i] = new Ferr_JSONObject(items[i]); } data = result; } else if (text[0] == '[') { type = Ferr_JSONType.Array; text = text.Remove(0,1); text = text.Remove(text.Length-1,1); string [] items = Ferr_JSON._Split(text, ',', true); Ferr_JSONValue[] result = new Ferr_JSONValue[items.Length]; for (int i = 0; i < items.Length; i++) { result[i] = new Ferr_JSONValue(); result[i].FromString (items[i]); } data = result; } else if (text[0] == '\"') { type = Ferr_JSONType.String; text = text.Remove(0,1); text = text.Remove(text.Length-1,1); data = text; } else if (float.TryParse(text, out fVal) ) { type = Ferr_JSONType.Number; data = fVal; } else { string lower = text.ToLower(); if (lower == "false" || lower == "true") { type = Ferr_JSONType.Bool; data = bool.Parse(text); } else if (lower == "null") { type = Ferr_JSONType.Object; data = null; } else { type = Ferr_JSONType.String; data = text; } } }
/// <summary> /// Creates or sets all values along the path to Object, last object on the path is /// set to the provided value. /// </summary> public Ferr_JSONValue Set (string aPath, Ferr_JSONValue aVal) { string[] words = aPath.Split('.'); Ferr_JSONValue curr = this; Ferr_JSONObject currObj = null; for (int i = 0; i < words.Length; i++) { Ferr_JSONObject result = curr.GetImmidiateChild(words[i]); if (result == null) { Ferr_JSONObject[] list = null; if (curr.type == Ferr_JSONType.Object) { list = (Ferr_JSONObject[])curr.data; if (list == null) list = new Ferr_JSONObject[1]; else Array.Resize<Ferr_JSONObject>(ref list, list.Length + 1); curr.data = list; } else { curr.type = Ferr_JSONType.Object; curr.data = list = new Ferr_JSONObject[1]; } list[list.Length-1] = new Ferr_JSONObject(words[i], new Ferr_JSONValue()); result = list[list.Length-1]; } currObj = result; curr = result.val; } if (currObj != null){ currObj.val = aVal; return currObj.val; } else { return null; } }
/// <summary> /// Creates a JSON object from the given object. Only uses public fields from that specific class, /// not including parent classes. Also, only supports really basic variable types well. /// </summary> /// <returns> /// A JSON object that can be .ToString()ed into JSON data. /// </returns> /// <param name='obj'> /// A really simple object you want to convert into JSON. /// </param> public static Ferr_JSONValue MakeShallow(object obj) { Type t = obj.GetType(); FieldInfo[] fields = t .GetFields(System.Reflection.BindingFlags.Public | BindingFlags.Instance); Ferr_JSONValue val = new Ferr_JSONValue(); for (int i = 0; i < fields.Length; i++) { object data = fields[i].GetValue(obj); if (data is float || data is int || data is double || data is long) val[fields[i].Name] = Convert.ToSingle(data); else if (data is string) val[fields[i].Name] = (string)data; else if (data is bool) val[fields[i].Name] = (bool)data; else val[fields[i].Name] = data.ToString (); } return val; }
/// <summary> /// Creates a TerrainMaterial from a JSON object. Does NOT recreate mesh data automatically or link materials! /// </summary> /// <param name="aJSON">A parsed JSON value containing PathTerrain and Path data</param> public void FromJSON(Ferr_JSONValue aJSON) { fill = (Ferr2DT_FillMode)Enum.Parse(typeof(Ferr2DT_FillMode), aJSON["fill", "Closed"]); fillY = aJSON["fillY", 0]; fillZ = aJSON["fillZ", 0.2f]; splitCorners = aJSON["splitCorners", true]; smoothPath = aJSON["smoothPath", false]; splitCount = (int)aJSON["splitCount", 4]; splitDist = aJSON["splitDist", 1]; splitMiddle = aJSON["splitMiddle", true]; pixelsPerUnit = aJSON["pixelsPerUnit", 32]; stretchThreshold = aJSON["stretchThreshold", 0.5f]; vertexColor = Ferr_Color.FromHex(aJSON["vertexColor", "FFFFFF"]); createTangents = aJSON["createTangents", false]; createCollider = aJSON["createCollider", true]; create3DCollider = aJSON["create3DCollider", false]; depth = aJSON["depth", 4]; isTrigger = aJSON["isTrigger", false]; sharpCorners = aJSON["sharpCorners", false]; sharpCornerDistance = aJSON["sharpCornerDist", 2]; smoothSphereCollisions = aJSON["ssCollisions", false]; randomByWorldCoordinates = aJSON["randomBWC", false]; slantAmount = aJSON["slantAmount", 0]; uvOffset.x = aJSON["uvOffset.x", 0]; uvOffset.y = aJSON["uvOffset.y", 0]; surfaceOffset[0] = aJSON["surfaceOffset.0", 0]; surfaceOffset[1] = aJSON["surfaceOffset.1", 0]; surfaceOffset[2] = aJSON["surfaceOffset.2", 0]; surfaceOffset[3] = aJSON["surfaceOffset.3", 0]; collidersBottom = aJSON["colliders.bottom", true]; collidersTop = aJSON["colliders.top", true]; collidersLeft = aJSON["colliders.left", true]; collidersRight = aJSON["colliders.right", true]; Ferr_JSONValue overrides = aJSON["directionOverrides"]; for (int i = 0; i < overrides.Length; i++) { directionOverrides.Add ( (Ferr2DT_TerrainDirection)Enum.Parse(typeof(Ferr2DT_TerrainDirection), overrides[i,"None"]) ); vertScales.Add(1); } Path.FromJSON (aJSON["path"]); }
/// <summary> /// Creates a JSON object containing PathTerrain and Path data. /// </summary> /// <returns>A JSON Value containing PathTerrain and Path data. You can ToString this for the JSON string.</returns> public Ferr_JSONValue ToJSON() { Ferr_JSONValue result = new Ferr_JSONValue(); result["fill" ] = ""+fill; result["fillY" ] = fillY; result["fillZ" ] = fillZ; result["splitCorners" ] = splitCorners; result["smoothPath" ] = smoothPath; result["splitDist" ] = splitDist; result["splitCount" ] = splitCount; result["splitMiddle" ] = splitMiddle; result["pixelsPerUnit" ] = pixelsPerUnit; result["stretchThreshold"] = stretchThreshold; result["vertexColor" ] = Ferr_Color.ToHex(vertexColor); result["createTangents" ] = createTangents; result["randomBWC" ] = randomByWorldCoordinates; result["slantAmount" ] = slantAmount; result["createCollider" ] = createCollider; result["create3DCollider"] = create3DCollider; result["isTrigger" ] = isTrigger; result["ssCollisions" ] = smoothSphereCollisions; result["sharpCorners" ] = sharpCorners; result["sharpCornerDist" ] = sharpCornerDistance; result["depth" ] = depth; result["uvOffset.x" ] = uvOffset.x; result["uvOffset.y" ] = uvOffset.y; result["surfaceOffset.0" ] = surfaceOffset[0]; result["surfaceOffset.1" ] = surfaceOffset[1]; result["surfaceOffset.2" ] = surfaceOffset[2]; result["surfaceOffset.3" ] = surfaceOffset[3]; result["colliders.bottom"] = collidersBottom; result["colliders.top" ] = collidersTop; result["colliders.left" ] = collidersLeft; result["colliders.right" ] = collidersRight; result["directionOverrides"] = 0; Ferr_JSONValue dir = result["directionOverrides"]; for (int i = 0; i < directionOverrides.Count; i++) { dir[i] = directionOverrides[i].ToString (); } result["path"] = Path.ToJSON(); return result; }
/// <summary> /// Overwrites data with information from the provided JSON. This method will throw an Exception /// if the text cannot be split into exactly two pieces (left and right of an initial ':') /// </summary> public void FromString(string aText) { aText = aText.Trim(); string[] words = Ferr_JSON._Split(aText, ':', true); if (words.Length != 2) throw new Exception("Bad JSON Object: " + aText); name = words[0].Trim().Replace("\"", ""); val = new Ferr_JSONValue(); val.FromString(words[1]); }
/// <summary> /// Parses the provided JSON into a JSON object. If errors occur, null will be returned, and error info /// will be povided in the Ferr_JSON.error and Ferr_JSON.failed fields. /// </summary> /// <param name='aText'> /// JSON text data. Whitespace isn't important. /// </param> public static Ferr_JSONValue Parse (string aText) { Ferr_JSONValue result = null; failed = false; error = null; try { result = new Ferr_JSONValue(); result.FromString(aText); } catch (Exception e) { error = e; failed = true; } return result; }
/// <summary> /// Recursively converts a JSON string into a JSON data structure. Throws an exception /// if given an empty string. Unrecognized data is converted into a string. /// </summary> public void FromString(string aValue) { string text = aValue.Trim(); float fVal = 0; if (text.Length <= 0) { throw new Exception("Bad JSON value: " + aValue); } if (text[0] == '{') { type = Ferr_JSONType.Object; text = text.Remove(0, 1); text = text.Remove(text.Length - 1, 1); string [] items = Ferr_JSON._Split(text, ',', true); Ferr_JSONObject[] result = new Ferr_JSONObject[items.Length]; for (int i = 0; i < items.Length; i++) { result[i] = new Ferr_JSONObject(items[i]); } data = result; } else if (text[0] == '[') { type = Ferr_JSONType.Array; text = text.Remove(0, 1); text = text.Remove(text.Length - 1, 1); string [] items = Ferr_JSON._Split(text, ',', true); Ferr_JSONValue[] result = new Ferr_JSONValue[items.Length]; for (int i = 0; i < items.Length; i++) { result[i] = new Ferr_JSONValue(); result[i].FromString(items[i]); } data = result; } else if (text[0] == '\"') { type = Ferr_JSONType.String; text = text.Remove(0, 1); text = text.Remove(text.Length - 1, 1); data = text; } else if (float.TryParse(text, out fVal)) { type = Ferr_JSONType.Number; data = fVal; } else { string lower = text.ToLower(); if (lower == "false" || lower == "true") { type = Ferr_JSONType.Bool; data = bool.Parse(text); } else if (lower == "null") { type = Ferr_JSONType.Object; data = null; } else { type = Ferr_JSONType.String; data = text; } } }