private BonArray NextArray() { BonArray b = new BonArray(); while (true) { char c = NextToken(); if (c == ']') { break; } myIndex--; BonValue v = NextValue(); b.Add(v); c = NextToken(); if (c == ']') { break; } if (c != ',') { throw SyntaxError("Array error"); } } return(b); }
public static void SaveSeed(BonValue seed) { byte[] bytes = seed.ToBonBytes(); bytes = EncryptDecrypt.Encrypt(bytes); File.WriteAllBytes(seedpath, bytes); Debug.Log("SD_AllSeeds saved." + seedpath); }
private BonDocument NextDocument() { BonDocument d = new BonDocument(); while (true) { char c = NextToken(); if (c == '}') { break; } if (c != '\"')// && c != '\'') { throw SyntaxError("Document error"); } string k = GetNextString(); if (NextToken() != ':') { throw SyntaxError("Document error"); } BonValue v = NextValue(); d[k] = v; c = NextToken(); if (c == '}') { break; } if (c != ',') { string txt = ""; int ends = myIndex + 20; ends = ends < (mySource.Length - 1) ? ends : mySource.Length - 1; for (int i = myIndex - 1; i < myIndex + 10; i++) { txt += mySource[i]; } throw SyntaxError("Document error token [" + txt + "]"); } } return(d); }
public static object ToObj(BonValue v, Type t, object old, HashSet <string> fields = null) { if (v == null) { return(null); } switch (t.Name) { case "Byte": return((byte)v.AsInt); case "SByte": return((sbyte)v.AsInt); case "Int16": return((short)v.AsInt); case "UInt16": return((ushort)v.AsInt); case "Int32": return(v.AsInt); case "UInt32": return((uint)v.AsInt); case "Int64": return(v.AsLong); case "UInt64": return((ulong)v.AsLong); case "Single": return(v.AsFloat); case "Double": return(v.AsDouble); case "Boolean": return(v.AsBoolean); case "String": return(v.AsString); case "Byte[]": return(v.AsBinary); case "List`1": { BonArray arr = v.AsBonArray; if (arr == null) { return(null); } int num = arr.Count; IList l = null; if (old != null) { l = (IList)old; } else { l = (IList)Activator.CreateInstance(t, num); } Type t2 = t.GetGenericArguments()[0]; l.Clear(); for (int i = 0; i < num; i++) { l.Add(ToObj(arr[i], t2, null, fields)); } return(l); } case "Dictionary`2": { BonDocument doc = v.AsBonDocument; if (doc == null) { return(null); } int num = doc.Count; IDictionary d = null; if (old != null) { d = (IDictionary)old; } else { d = (IDictionary)Activator.CreateInstance(t, num); } Type[] t2s = t.GetGenericArguments(); Type tk = t2s[0]; Type t2 = t2s[1]; for (int i = 0; i < num; i++) { BonElement el = doc[i]; object key = null; switch (tk.Name) { case "Int32": key = Convert.ToInt32(el.name); break; case "Int64": key = Convert.ToInt64(el.name); break; case "String": key = el.name; break; default: { if (tk.IsEnum) { key = Enum.ToObject(tk, Convert.ToInt32(el.name)); } break; } } if (key != null) { BonValue v2 = el.value; object obj = null; if (d.Contains(key)) { obj = ToObj(v2, t2, d[key], fields); } else { obj = ToObj(v2, t2, null, fields); } if (obj == null) { d.Remove(key); } else { d[key] = obj; } } } return(d); } default: { if (t.IsEnum) { return(Enum.ToObject(t, v.AsInt)); } if (t.IsArray) { BonArray arr = v.AsBonArray; if (arr == null) { return(null); } int num = arr.Count; Type t2 = t.GetElementType(); var obj = Array.CreateInstance(t2, num); for (int i = 0; i < num; i++) { obj.SetValue(ToObj(arr[i], t2, null, fields), i); } return(obj); } if (!v.IsBonDocument) { return(null); } { BonDocument doc = v.AsBonDocument; string _t_ = doc.GetString("_t_"); if (_t_ != null) { try { t = Type.GetType(_t_); } catch (Exception) { Debug.LogWarning("δÕÒµ½ÀàÐÍ: " + doc["_t_"].AsString); return(null); } } ClassInfo ci = ClassInfo.Get(t); object obj = old; Dictionary <string, List <DataMonitor> > dataMonitors = null; bool monitorObj = false; if (old != null) { monitorObj = dataMonitor.TryGetValue(old, out dataMonitors); } if (obj == null) { obj = Activator.CreateInstance(t); } if (obj is IBon) { ((IBon)obj).FromBon(doc); return(obj); } CachedDictionary <string, FldInfo> fis = ci.fields; bool isValueType = t.IsValueType; int num = doc.Count; for (int i = 0; i < num; i++) { BonElement el = doc[i]; if (fields != null && !fields.Contains(el.name)) { continue; } FldInfo fi; if (fis.TryGetValue(el.name, out fi)) { List <DataMonitor> fieldMonitors = null; bool monitorField = monitorObj && dataMonitors.TryGetValue(fi.name, out fieldMonitors) && fieldMonitors.Count > 0; if ((fi.type.IsValueType || fi.type == typeof(string) || fi.type.IsEnum) && !monitorField) { fi.SetValue(obj, ToObj(el.value, fi.type, null, fi.subFields)); } else { object oldv = fi.GetValue(obj); object newv = ToObj(el.value, fi.type, oldv, fi.subFields); fi.SetValue(obj, newv); if (monitorField && (fi.type.IsClass || oldv != newv)) { dataChanged.AddRange(fieldMonitors); } } } } return(obj); } } } }
public static T ToObj <T>(BonValue v, T old, HashSet <string> fields = null) { return((T)ToObj(v, typeof(T), old, fields)); }
public BonElement(string name, BonValue value) { this.name = name; this.value = value == null ? BonNull.value : value; }
public void FromBon(BonValue jv, HashSet <string> fields) { BonUtil.ToObj(jv, this.GetType(), this, fields);//, this as IBonMonitor); }
public void FromBon(BonValue jv) { FromBon(jv, null); }