public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer szr)
        {
            JObject jObject    = JObject.Load(reader);
            var     name       = (string)jObject["name"];
            var     typeName   = (string)jObject["$type"];
            var     realType   = Type.GetType(typeName);
            var     settings   = JsonBlueprints.CreateSettings(realType);
            var     serializer = JsonSerializer.Create(settings);
            BlueprintScriptableObject result = null;

            if (jObject["$append"] != null)
            {
                serializer.ObjectCreationHandling = ObjectCreationHandling.Reuse;
                var copy = (string)jObject["$append"];
                jObject.Remove("$append");
                var parts = copy.Split(':');
                result = ResourcesLibrary.TryGetBlueprint(parts[1]);
                name   = result.name;
                Main.DebugLog($"Appending to {result.name}");
            }
            if (jObject["$replace"] != null)
            {
                var copy = (string)jObject["$replace"];
                jObject.Remove("$replace");
                var parts = copy.Split(':');
                result = ResourcesLibrary.TryGetBlueprint(parts[1]);
                name   = result.name;
                Main.DebugLog($"replacing to {result.name}");
            }
            if (jObject["$copy"] != null)
            {
                var copy = (string)jObject["$copy"];
                jObject.Remove("$copy");
                var parts    = copy.Split(':');
                var resource = ResourcesLibrary.TryGetBlueprint(parts[1]);
                result = (BlueprintScriptableObject)BlueprintUtil.ShallowClone(resource);
                Main.DebugLog($"Copying {resource.name}");
            }
            if (name == null)
            {
                throw new System.Exception("Missing name");
            }
            if (JsonBlueprints.Blueprints.ContainsKey(name))
            {
                //throw new System.Exception("Cannot create blueprint twice");
            }
            if (result == null)
            {
                result = ScriptableObject.CreateInstance(realType) as BlueprintScriptableObject;
            }
            JsonBlueprints.Blueprints[name] = result;
            BlueprintUtil.AddBlueprint(result, name);
            serializer.Populate(jObject.CreateReader(), result);
            return(result);
        }
Exemple #2
0
        public static void DumpBlueprint(BlueprintScriptableObject blueprint, string directory = "Blueprints", bool verbose = false)
        {
            JsonSerializerSettings settings = null;

            if (verbose)
            {
                settings = JsonBlueprints.CreateSettings();
                settings.DefaultValueHandling = DefaultValueHandling.Include;
            }
            JsonBlueprints.Dump(blueprint, $"{directory}/{blueprint.GetType()}/{blueprint.name}.{blueprint.AssetGuid}.json", settings);
        }
        public override void WriteJson(JsonWriter w, object o, JsonSerializer szr)
        {
            var newSerializer = JsonSerializer.Create(JsonBlueprints.CreateSettings(null));
            var j             = new JObject();

            j.AddFirst(new JProperty("$type", o.GetType().Name));
            foreach (var field in GetSerializableMembers(o.GetType()))
            {
                var value = Traverse.Create(o).Field(field.Name).GetValue();
                j.Add(field.Name, value != null ? JToken.FromObject(value, newSerializer) : null);
            }
            j.WriteTo(w);
        }
        public object ReadResource(JsonReader reader, Type objectType, object existingValue, JsonSerializer szr)
        {
            JObject jObject = JObject.Load(reader);
            var     name    = (string)jObject["name"];

            Main.DebugLog($"Deserializing {name} of {objectType.Name} with {GetType().Name}");
            var typeName            = (string)jObject["$type"];
            var realType            = Type.GetType(typeName);
            ScriptableObject result = null;

            if (jObject["$append"] != null)
            {
                var settings = JsonBlueprints.CreateSettings(null);
                szr = JsonSerializer.Create(settings);
                szr.ObjectCreationHandling = ObjectCreationHandling.Reuse;
                var copy = (string)jObject["$append"];
                jObject.Remove("$append");
                var parts = copy.Split(':');
                result = ResourcesLibrary.TryGetResource <ScriptableObject>(parts[1]);
                name   = result.name;
                Main.DebugLog($"Appending to {result.name}");
            }
            if (jObject["$replace"] != null)
            {
                var copy = (string)jObject["$replace"];
                jObject.Remove("$replace");
                var parts = copy.Split(':');
                result = ResourcesLibrary.TryGetBlueprint(parts[1]);
                name   = result.name;
            }
            if (jObject["$copy"] != null)
            {
                var copy = (string)jObject["$copy"];
                jObject.Remove("$copy");
                var parts    = copy.Split(':');
                var resource = ResourcesLibrary.TryGetResource <ScriptableObject>(parts[1]);
                result = (ScriptableObject)BlueprintUtil.ShallowClone(resource);
                Main.DebugLog($"Copying {resource.name}");
            }
            if (result == null)
            {
                result = ScriptableObject.CreateInstance(realType);
            }
            szr.Populate(jObject.CreateReader(), result);
            return(result);
        }
        public override void WriteJson(JsonWriter w, object o, JsonSerializer szr)
        {
            var settings      = JsonBlueprints.CreateSettings(null);
            var newSerializer = JsonSerializer.Create(settings);
            var j             = new JObject();

            j.AddFirst(new JProperty("$type", JsonBlueprints.GetTypeName(o.GetType())));
            foreach (var memberInfo in JsonBlueprints.GetUnitySerializableMembers(o.GetType()))
            {
                object value = null;
                if (memberInfo.MemberType == MemberTypes.Field)
                {
                    value = ((FieldInfo)memberInfo).GetValue(o);
                }
                else if (memberInfo.MemberType == MemberTypes.Property)
                {
                    value = ((PropertyInfo)memberInfo).GetValue(o);
                }
                j.Add(memberInfo.Name, value != null ? JToken.FromObject(value, newSerializer) : null);
            }
            j.WriteTo(w);
        }