// 必须和ApplyDiff使用 // 以new为基准,获取new中有,但old中没有的 // local diff = table.GetNotExist(old, new) // table.ApplyDiff(old, diff) // 这样old就有new中的字段 public static LinkedHashtable GetNotExist(IList oldList, IList newList) { var diff = new LinkedHashtable(); for (int i = newList.Count - 1; i >= 0; i--) { var newK = i; var newV = newList[i]; if (!oldList.ContainsIndex(i)) { diff[newK] = newK; } else { switch (oldList[newK]) { case IList _ when newV is IList list: diff[newK] = GetDiff(oldList[newK] as IList, list); break; case IDictionary _ when newV is IDictionary dictionary: diff[newK] = IDictionaryUtil.GetNotExist(oldList[newK] as IDictionary, dictionary); break; } //其他情况不用处理 } } diff.Sort((a, b) => a.To <int>() >= b.To <int>()); return(diff); }
protected static bool SerializeObject(LinkedHashtable anObject, StringBuilder builder) { builder.Append("{"); IDictionaryEnumerator e = anObject.GetEnumerator(); bool first = true; while (e.MoveNext()) { string key = e.Key.ToString(); object value = e.Value; if (!first) { builder.Append(", "); } SerializeString(key, builder); builder.Append(":"); if (!SerializeValue(value, builder)) { return(false); } first = false; } builder.Append("}"); return(true); }
protected static LinkedHashtable ParseObject(char[] json, ref int index, ref bool success) { LinkedHashtable table = new LinkedHashtable(); int token; // { NextToken(json, ref index); bool done = false; while (!done) { token = LookAhead(json, index); if (token == MiniJsonLinked.TOKEN_NONE) { success = false; return(null); } else if (token == MiniJsonLinked.TOKEN_COMMA) { NextToken(json, ref index); } else if (token == MiniJsonLinked.TOKEN_CURLY_CLOSE) { NextToken(json, ref index); return(table); } else { // name string name = ParseString(json, ref index, ref success); if (!success) { success = false; return(null); } // : token = NextToken(json, ref index); if (token != MiniJsonLinked.TOKEN_COLON) { success = false; return(null); } // value object value = ParseValue(json, ref index, ref success); if (!success) { success = false; return(null); } table[name] = value; } } return(table); }
public void OnPropertyChanged(Dictionary <string, float> oldCalcPropDict, Dictionary <string, float> newCalcPropDict, LinkedHashtable calcPropDictDiff) { foreach (var key in calcPropDictDiff.Keys) { if (key.Equals("技能冷却减少百分比") || key.Equals("攻击速度")) { this.OnSpellCooldownRateChange(); } else if (key.Equals("移动速度")) { this.OnSpeedChange(oldCalcPropDict.Get <float>(key), newCalcPropDict.Get <float>(key)); } else if (key.Equals("生命上限")) { this.OnMaxHpChange(oldCalcPropDict.Get <int>(key), newCalcPropDict.Get <int>(key)); } } }
/// <summary> /// 转化为ToLinkedHashtable /// </summary> /// <param name="self"></param> /// <returns></returns> public static object ToLinkedHashtable2 <T>(this ICollection <T> self) { if (self is IDictionary dictionary) { LinkedHashtable linkedHashtable = new LinkedHashtable(); foreach (var key in dictionary.Keys) { var value = dictionary[key]; linkedHashtable.Put(key, value.ToLinkedHashtable2()); } return(linkedHashtable); } ArrayList list = new ArrayList(); foreach (object o in self) { list.Add(o.ToLinkedHashtable2()); } return(list); }
////////////////////////////////////////////////////////////////////// // Diff相关 ////////////////////////////////////////////////////////////////////// // 必须和ApplyDiff使用 // 以new为基准,获取new相对于old不一样的部分 // local diff = table.GetDiff(old, new) // table.ApplyDiff(old, diff) // 这样old的就变成和new一模一样的数据 public static LinkedHashtable GetDiff(IDictionary oldDict, IDictionary newDict) { var diff = new LinkedHashtable(); foreach (DictionaryEntry dictionaryEntry in newDict) { var newK = dictionaryEntry.Key; var newV = dictionaryEntry.Value; if (newV is IDictionary newVDict) { switch (newVDict.Count) { case 0 when(!oldDict.Contains(newK) || oldDict[newK].GetType() != newVDict.GetType() || (oldDict[newK] is IDictionary && ((IDictionary)oldDict[newK]).Count != 0)): diff[newK] = StringConst.String_New_In_Table + newVDict.GetType(); break; default: { if (oldDict.Contains(newK) && oldDict[newK] is IDictionary) { diff[newK] = GetDiff((IDictionary)oldDict[newK], newVDict); } else if (!oldDict.Contains(newK) || !newVDict.Equals(oldDict[newK])) { diff[newK] = CloneUtil.CloneDeep(newV); } break; } } } else if (newV is IList list && oldDict.Contains(newK) && oldDict[newK] is IList) { diff[newK] = ListUtil.GetDiff((IList)oldDict[newK], list); }
// table.ApplyDiff(old, diff) // 将diff中的东西应用到old中 // 重要:当为Array的时候,需要重新赋值;List的时候,可以不需要重新赋值 public static IList ApplyDiff(IList oldList, LinkedHashtable diffDict) { if (diffDict == null) { return(oldList); } int oldListCount = oldList.Count; foreach (var tmpK in diffDict.Keys) { var k = tmpK.To <int>(); var v = diffDict[tmpK]; if (v.Equals(StringConst.String_Nil_In_Table)) { if (oldList is Array) { oldList = (oldList as Array).RemoveAt_Array(k); } else { oldList.RemoveAt(k); } } else if (v.ToString().StartsWith(StringConst.String_New_In_Table)) { string typeString = v.ToString().Substring(StringConst.String_New_In_Table.Length); Type type = TypeUtil.GetType(typeString); var value = type.CreateInstance <object>(); if (k < oldListCount) { oldList[k] = value; } else { if (oldList is Array) { oldList = (oldList as Array).Insert_Array(oldListCount, v); } else { oldList.Insert(oldListCount, v); } } } else if (oldList.ContainsIndex(k) && oldList[k] is IList && v is LinkedHashtable hashtable) { ApplyDiff(oldList[k] as IList, hashtable); } else if (oldList.ContainsIndex(k) && oldList[k] is IDictionary && v is LinkedHashtable linkedHashtable) { IDictionaryUtil.ApplyDiff(oldList[k] as IDictionary, linkedHashtable); } else { if (k < oldListCount) { oldList[k] = v; } else { if (oldList is Array) { oldList = (oldList as Array).Insert_Array(oldListCount, v); } else { oldList.Insert(oldListCount, v); } } } } return(oldList); }
////////////////////////////////////////////////////////////////////// // Diff相关 ////////////////////////////////////////////////////////////////////// // 必须和ApplyDiff使用 // 以new为基准,获取new相对于old不一样的部分 // local diff = table.GetDiff(old, new) // table.ApplyDiff(old, diff) // 这样old的就变成和new一模一样的数据 public static LinkedHashtable GetDiff(IList oldList, IList newList) { var diff = new LinkedHashtable(); for (int i = newList.Count - 1; i >= 0; i--) { var newK = i; var newV = newList[i]; switch (newV) { case IList _: { var newVList = newV as IList; if (newVList.Count == 0 && (!oldList.ContainsIndex(newK) || oldList[newK].GetType() != newV.GetType() || (oldList[newK] is IList && (oldList[newK] as IList).Count != 0))) { diff[newK] = StringConst.String_New_In_Table + newV.GetType(); } else if (oldList.ContainsIndex(newK) && oldList[newK] is IList) { diff[newK] = GetDiff(oldList[newK] as IList, newVList); } else if (!oldList.ContainsIndex(newK) || !newV.Equals(oldList[newK])) { diff[newK] = CloneUtil.CloneDeep(newV); } break; } case IDictionary _ when oldList.ContainsIndex(newK) && oldList[newK] is IDictionary: diff[newK] = IDictionaryUtil.GetDiff(oldList[newK] as IDictionary, newV as IDictionary); break; default: { if (!oldList.ContainsIndex(newK) || !newV.Equals(oldList[newK])) { diff[newK] = newV; } break; } } } for (int i = 0; i < oldList.Count; i++) { if (!newList.ContainsIndex(i)) { diff[i] = StringConst.String_Nil_In_Table; } } diff.Sort((a, b) => a.To <int>() >= b.To <int>()); if (diff.Count == 0) { diff = null; } return(diff); }
// table.ApplyDiff(old, diff) // 将diff中的东西应用到old中 // 重要:当为Array的时候,需要重新赋值;List的时候,可以不需要重新赋值 public static IList ApplyDiff(this IList oldList, LinkedHashtable diffDict) { return(ListUtil.ApplyDiff(oldList, diffDict)); }