Пример #1
0
    public void FromJson(BonDocument json)
    {
        foreach (BonElement p in json)
        {
            switch (p.name)
            {
            case "dlcType": {
                dlcType = (DLCType)p.value.AsInt;
                break;
            }

            case "files": {
                foreach (BonElement pp in p.value.AsBonDocument)
                {
                    FileDesc fd = new FileDesc();
                    fd.name        = pp.name;
                    fd.pak         = this;
                    files[fd.name] = fd;
                    fd.FromJson(pp.value.AsBonDocument);
                }
                break;
            }

            case "dependences": {
                BonArray arr = p.value.AsBonArray;
                dependences = new string[arr.Count];
                for (int i = dependences.Length; --i >= 0;)
                {
                    dependences[i] = arr[i].AsString;
                }
                break;
            }
            }
        }
    }
Пример #2
0
    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);
    }
Пример #3
0
    public BonArray GetBonArray(string name, BonArray defaultValue = null)
    {
        BonElement el;

        if (elements.TryGetValue(name, out el))
        {
            return(el.value.AsBonArray);
        }
        return(defaultValue);
    }
Пример #4
0
    private BonArray ReadArray()
    {
        BonArray arr   = new BonArray();
        int      count = br.Read7BitEncodedInt();

        for (int i = 0; i < count; i++)
        {
            arr.Add(Read());
        }
        return(arr);
    }
Пример #5
0
    public BonDocument ToJson()
    {
        BonDocument json = new BonDocument();

        json["dlcType"] = (int)dlcType;
        BonDocument files = new BonDocument();

        json["files"] = files;
        foreach (KeyValuePair <string, FileDesc> p in this.files)
        {
            files[p.Key] = p.Value.ToJson();
        }
        if (dependences != null)
        {
            BonArray dps = new BonArray();
            foreach (string d in dependences)
            {
                dps.Add(d);
            }
            json["dependences"] = dps;
        }
        return(json);
    }
Пример #6
0
    public static BonValue ToBon(object obj, HashSet <string> fields = null, Type declareType = null)
    {
        if (obj == null)
        {
            return(BonNull.value);
        }
        Type t = obj.GetType();

        switch (t.Name)
        {
        case "Byte": return((int)(byte)obj);

        case "SByte": return((int)(sbyte)obj);

        case "Int16": return((int)(short)obj);

        case "UInt16": return((int)(ushort)obj);

        case "Int32": return((int)obj);

        case "UInt32": return((int)(uint)obj);

        case "Int64": return((long)obj);

        case "UInt64": return((long)(ulong)obj);

        case "Single": return((float)obj);

        case "Double": return((double)obj);

        case "Boolean": return((bool)obj);

        case "String": return((string)obj);

        case "Byte[]": return((byte[])obj);

        default: {
            if (t.IsEnum)
            {
                return((int)obj);
            }
            break;
        }
        }

        switch (t.Name)
        {
        case "List`1": {
            Type     et  = t.GetGenericArguments()[0];
            BonArray arr = null;
            arr = new BonArray();
            IList list = (IList)obj;
            int   num  = list.Count;
            for (int i = 0; i < num; i++)
            {
                arr.Add(ToBon(list[i], fields, et));
            }
            return(arr);
        }

        case "Dictionary`2": {
            Type        et  = t.GetGenericArguments()[1];
            BonDocument doc = null;
            doc = new BonDocument();
            foreach (DictionaryEntry kv in (IDictionary)obj)
            {
                if (kv.Key.GetType().IsEnum)
                {
                    doc[((int)kv.Key).ToString()] = ToBon(kv.Value, fields, et);
                }
                else
                {
                    doc[kv.Key.ToString()] = ToBon(kv.Value, fields, et);
                }
            }
            return(doc);
        }

        default: {
            if (t.IsArray)
            {
                Type     et  = t.GetElementType();
                BonArray arr = null;
                arr = new BonArray();
                Array list = (Array)obj;
                int   num  = list.Length;
                for (int i = 0; i < num; i++)
                {
                    arr.Add(ToBon(list.GetValue(i), fields, et));
                }
                return(arr);
            }
            {
                if (obj is IBon)
                {
                    return(((IBon)obj).ToBon());
                }
                ClassInfo   ci  = ClassInfo.Get(t);
                BonDocument doc = new BonDocument();
                if (declareType != null && declareType != t)
                {
                    doc["_t_"] = t.FullName;
                }
                FldInfo[] fis = ci.fields.Values;
                for (int i = fis.Length; --i >= 0;)
                {
                    FldInfo fi = fis[i];
                    if (fields != null && !fields.Contains(fi.name))
                    {
                        continue;
                    }
                    doc[fi.name] = ToBon(fi.GetValue(obj), fi.subFields);
                }
                return(doc);
            }
        }
        }
    }
Пример #7
0
    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);
            }
        }
        }
    }