コード例 #1
0
    public static void WriteConfigAsJson(Type type, string folder)
    {
        var exportFields = ClassFieldFilter.GetConfigFieldInfo(type);

        if (exportFields.Count == 0)
        {
            return;
        }

        foreach (var field in exportFields)
        {
            if (field.GetValue(null) == null)
            {
                UnityEngine.Debug.LogErrorFormat("Export {0} is null", field.Name);
                return;
            }
            var    file = Path.Combine(folder, field.Name + ".json");
            fsData data;
            _serializer.TrySerialize(field.FieldType, field.GetValue(null), out data).AssertSuccess();
            if (File.Exists(file))
            {
                File.Delete(file);
            }
            using (FileStream fs = File.Create(file))
            {
                byte[] content = new UTF8Encoding(true).GetBytes(fsJsonPrinter.PrettyJson(data));
                fs.Write(content, 0, content.Length);
            }
        }
    }
コード例 #2
0
    public override string GenerateSerializerCode(Type type)
    {
        var fields = ClassFieldFilter.GetConfigFieldInfo(type);

        return(GenSerializerStaticClassCodeByTemplate(
                   type,
                   FieldReferencedTypes(fields),
                   GenFieldReadCode(type, fields),
                   GenFieldWriteCode(type, fields)));
    }
コード例 #3
0
    public static void ReadConfigAsJson(Type type, string folder)
    {
        if (readTypes.Contains(type))
        {
            return;
        }
        else
        {
            readTypes.Add(type);
        }
        var exportFields = ClassFieldFilter.GetConfigFieldInfo(type);

        if (exportFields.Count == 0)
        {
            return;
        }
        foreach (var field in exportFields)
        {
            var file = Path.Combine(folder, field.Name + ".json");
            if (!File.Exists(file))
            {
                Debug.LogWarningFormat("Json file {0} not found", field.Name);
                continue;
            }
            FileStream    fs   = File.Open(file, FileMode.Open);
            StringBuilder sb   = new StringBuilder();
            byte[]        b    = new byte[1024];
            UTF8Encoding  temp = new UTF8Encoding(true);

            while (fs.Read(b, 0, b.Length) > 0)
            {
                sb.Append(temp.GetString(b));
            }
            fs.Close();

            fsData   data;
            fsResult res = fsJsonParser.Parse(sb.ToString(), out data);
            if (res.Failed)
            {
                Debug.LogWarningFormat("Json file {0} parsed error {1}", field.Name, res.FormattedMessages);
                continue;
            }

            var value = field.GetValue(null);
            _serializer.TryDeserialize(data, field.FieldType, ref value).AssertSuccess();
            field.SetValue(null, value);
        }
    }
コード例 #4
0
    public override Type[] DirectlyUsedTypesExcludeSelf(Type type)
    {
        var fields = ClassFieldFilter.GetConfigFieldInfo(type);

        return(fields.Select(f => f.FieldType).ToArray());
    }