// private void EmitToParents(JToken oldValue) { if (!ReferenceEquals(this, root)) { Tome.EmitParentChange(Parent); } }
// public void Splice(int index, int deleteCount, JArray insertItems) { lock ((object)this) { // Delete given item count starting at given index for (int delI = index + deleteCount - 1; delI >= index; delI -= 1) { if (delI > Count - 1) { continue; } Del(delI); this[delI].Remove(); } // Insert given items starting at given index for (var addI = 0; addI < insertItems.Count; addI += 1) { int insertI = index + addI; Insert(insertI, Tome.Conjure(insertItems[addI])); if (OnAdd != null) { OnAdd.Invoke(insertI); } } } }
// public void UnShift(JToken item) { lock ((object)this) { AddFirst(Tome.Conjure(item, root)); if (OnAdd != null) { OnAdd.Invoke(0); } } }
// public void Set(int index, JToken value) { lock ((object)this) { // Make sure the property exists, filling in missing indexes if (Count <= index) { while (Count < index) { Add(Tome.Conjure(JValue.CreateNull(), root)); } Add(Tome.Conjure(value, root)); if (OnAdd != null) { OnAdd.Invoke(index); } return; } // Assign the property JToken property = this[index]; switch (property.Type) { case JTokenType.Array: ((TomeArray)property).Assign(value); break; case JTokenType.Object: ((TomeObject)property).Assign(value); break; default: var tomeValue = property as TomeValue; if (tomeValue == null) { Mage.Instance.Logger("Tomes").Data(property).Error("property is not a tome value: " + index.ToString()); UnityEngine.Debug.Log(this); } else { tomeValue.Assign(value); } break; } } }
// public void ApplyOperation(string op, JToken val, JToken root) { lock ((object)this) { switch (op) { case "assign": Assign(val); break; case "set": Set((string)val["key"], val["val"]); break; case "del": Del((string)val); break; case "move": var fromKey = (string)val["key"]; JToken toParent = Tome.PathValue(root, val["newParent"] as JArray); JToken toKey = (val["newKey"] != null) ? val["newKey"] : new JValue(fromKey); Move(fromKey, toParent, toKey); break; case "rename": foreach (KeyValuePair <string, JToken> property in (JObject)val) { string wasKey = property.Key; var isKey = (string)property.Value; Rename(wasKey, isKey); } break; case "swap": var firstKey = (string)val["key"]; JToken secondParent = Tome.PathValue(root, val["newParent"] as JArray); JToken secondKey = (val["newKey"] != null) ? val["newKey"] : new JValue(firstKey); Swap(firstKey, secondParent, secondKey); break; default: throw new Exception("TomeObject - Unsupported operation: " + op); } } }
// public void Set(string propertyName, JToken value) { lock ((object)this) { // Make sure the property exists if (this[propertyName] == null) { Add(propertyName, Tome.Conjure(value, root)); if (OnAdd != null) { OnAdd.Invoke(propertyName); } return; } // Assign the property JToken property = this[propertyName]; switch (property.Type) { case JTokenType.Array: ((TomeArray)property).Assign(value); break; case JTokenType.Object: ((TomeObject)property).Assign(value); break; default: var tomeValue = property as TomeValue; if (tomeValue == null) { Mage.Instance.Logger("Tomes").Data(property).Error("property is not a tome value: " + propertyName); UnityEngine.Debug.Log(this); } else { tomeValue.Assign(value); } break; } } }
// public TomeArray(JArray data, JToken root) { // this.root = root; if (this.root == null) { this.root = this; } // for (var i = 0; i < data.Count; i += 1) { Add(Tome.Conjure(data[i], this.root)); } // OnChanged += EmitToParents; OnAdd += EmitChanged; OnDel += EmitChanged; }
// public void Destroy() { lock ((object)this) { foreach (JToken value in this) { Tome.Destroy(value); } if (OnDestroy != null) { OnDestroy.Invoke(); } OnChanged = null; OnDestroy = null; OnAdd = null; OnDel = null; } }
// public TomeObject(JObject data, JToken root) { // this.root = root; if (this.root == null) { this.root = this; } // foreach (JProperty property in data.Properties()) { Add(property.Name, Tome.Conjure(property.Value, this.root)); } // OnChanged += EmitToParents; OnAdd += EmitChanged; OnDel += EmitChanged; }
// public void Destroy() { lock ((object)this) { foreach (KeyValuePair <string, JToken> property in this) { Tome.Destroy(property.Value); } if (OnDestroy != null) { OnDestroy.Invoke(); } OnChanged = null; OnDestroy = null; OnAdd = null; OnDel = null; } }
// public void ApplyOperation(string op, JToken val, JToken root) { lock ((object)this) { switch (op) { case "assign": Assign(val); break; case "set": Set((int)val["key"], val["val"]); break; case "del": Del((int)val); break; case "move": var fromKey = (int)val["key"]; JToken newParent = Tome.PathValue(root, val["newParent"] as JArray); JToken toKey = (val["newKey"] != null) ? val["newKey"] : new JValue(fromKey); Move(fromKey, newParent, toKey); break; case "rename": foreach (KeyValuePair <string, JToken> property in (JObject)val) { int wasKey = int.Parse(property.Key); var isKey = (int)property.Value; Rename(wasKey, isKey); } break; case "swap": var firstKey = (int)val["key"]; JToken secondParent = Tome.PathValue(root, val["newParent"] as JArray); JToken secondKey = (val["newKey"] != null) ? val["newKey"] : new JValue(firstKey); Swap(firstKey, secondParent, secondKey); break; case "push": foreach (JToken item in (JArray)val) { Push(item); } break; case "pop": Pop(); break; case "shift": Shift(); break; case "unshift": var unshiftItems = val as JArray; for (int i = unshiftItems.Count; i > 0; i -= 1) { UnShift(unshiftItems[i - 1]); } break; case "reverse": Reverse(); break; case "splice": var index = (int)val[0]; var deleteCount = (int)val[1]; var items = new JArray(val as JArray); items.First.Remove(); items.First.Remove(); Splice(index, deleteCount, items); break; default: throw new Exception("TomeArray - Unsupported operation: " + op); } } }