Esempio n. 1
0
    public void FillComponent(JsonHelperReader json, GameObject go)
    {
        json.Read(); // Drop StartObject
        string metaProp = json.ReadPropertyName();

        if (metaProp == JSONHelper.META.PROP)
        {
            Type componentType = json.ReadMetaObjectType();
            json.FillObject(go.GetComponent(componentType), componentType, null, true);
            return;
        }

        if (metaProp == JSONHelper.META.MARKER)
        {
            // Only meta currently allowed here is an external reference.
            // References back aren't allowed as one would need to read the same .json
            // and read / skip until the ref ID required is reached, then fill the component.
            // But doing even that only creates a shallow clone...
            // TODO do dat if it should be a shallow clone anyway.
            json.Read();    // Drop value
            using (JsonHelperReader ext = json.OpenMetaExternal(true)) {
                ext.Read(); // Go to Start
                FillComponent(ext, go);
            }
        }
    }
Esempio n. 2
0
    public void DeserializeComponentData(JsonHelperReader json, GameObject go, bool property = true)
    {
        Transform transform = go.transform;
        int       children  = transform.childCount;

        if (property)
        {
            json.ReadPropertyName("componentData");
        }
        json.Read(); // Drop StartArray

        int components = (int)(long)json.Value; json.Read();

        for (int i = 0; i < components; i++)
        {
            FillComponent(json, go);
        }

        json.Read(); // Drop StartArray
        int ii = -1;

        while (json.TokenType != JsonToken.EndArray)
        {
            GameObject child = transform.GetChild(++ii).gameObject;
            DeserializeComponentData(json, child, false);
        }
        json.Read(); // Drop EndArray

        json.Read(); // Drop EndArray
    }
 public override object Deserialize(JsonHelperReader json, object obj)
 {
     return(new DictionaryEntry(
                json.ReadRawProperty("key"),
                json.ReadRawProperty("value")
                ));
 }
Esempio n. 4
0
 public virtual object New(JsonHelperReader json, Type type)
 {
     try {
         return(ReflectionHelper.Instantiate(type));
     } catch (Exception e) {
         throw new JsonReaderException("Could not instantiate type " + type.FullName + "!", e);
     }
 }
Esempio n. 5
0
    public override object Deserialize(JsonHelperReader json, object obj)
    {
        Texture t = null;

        string name = (string)json.ReadRawProperty("name");
        string type = (string)json.ReadRawProperty("type");
        bool   mipmaps, readwrite;

        if (type == "2D")
        {
            TextureFormat format = json.ReadProperty <TextureFormat>("format");
            mipmaps   = (bool)json.ReadRawProperty("mipmaps");
            readwrite = (bool)json.ReadRawProperty("readwrite");

            obj = t = new Texture2D(2, 2, format, mipmaps);
        }
        else
        {
            throw new JsonReaderException("Texture types other than Texture2D not supported!");
        }

        t.name = name;

        if (t is Texture2D)
        {
            Texture2D t2D = (Texture2D)t;

            string dumppath = null;
            string @in      = (string)json.ReadRawProperty(JSONHelper.META.EXTERNAL_IN);
            string path     = (string)json.ReadRawProperty(JSONHelper.META.EXTERNAL_PATH);
            if (@in == JSONHelper.META.EXTERNAL_IN_SHARED && JSONHelper.SharedDir != null)
            {
                dumppath = Path.Combine(JSONHelper.SharedDir, "Texture2Ds");
            }
            else if (@in == JSONHelper.META.EXTERNAL_IN_RELATIVE && json.RelativeDir != null)
            {
                dumppath = json.RelativeDir;
            }
            if (dumppath == null)
            {
                t2D.Apply(mipmaps, !readwrite);
                return(t2D);
            }

            dumppath = Path.Combine(dumppath, path);
            if (!File.Exists(dumppath))
            {
                return(t2D);
            }
            t2D.LoadImage(File.ReadAllBytes(dumppath), false);
            t2D.Apply(mipmaps, !readwrite);
            return(t2D);
        }

        return(t);
    }
    public override object Deserialize(JsonHelperReader json, object obj)
    {
        Vector2 v = (Vector2)obj;

        v.Set(
            (float)(double)json.ReadRawProperty("x"),
            (float)(double)json.ReadRawProperty("y")
            );
        return(v);
    }
Esempio n. 7
0
    public bool DeserializeMain(JsonHelperReader json, GameObject go)
    {
        go.name = (string)json.ReadRawProperty("name");

        json.Global[go.name] = go;
        json.Global[go.transform.GetPath()] = go;

        go.tag   = (string)json.ReadRawProperty("tag");
        go.layer = (int)(long)json.ReadRawProperty("layer");
        return((bool)json.ReadRawProperty("activeSelf"));
    }
    public override object Deserialize(JsonHelperReader json, object obj)
    {
        Quaternion q = (Quaternion)obj;

        q.Set(
            (float)(double)json.ReadRawProperty("x"),
            (float)(double)json.ReadRawProperty("y"),
            (float)(double)json.ReadRawProperty("z"),
            (float)(double)json.ReadRawProperty("w")
            );
        return(q);
    }
Esempio n. 9
0
 public void DeserializeComponentTypes(JsonHelperReader json, GameObject go)
 {
     json.ReadPropertyName("componentTypes");
     json.Read(); // Drop StartArray
     while (json.TokenType != JsonToken.EndArray)
     {
         Type componentType = json.ReadObject <Type>();
         if (go.GetComponent(componentType) == null)
         {
             go.AddComponent(componentType);
         }
     }
     json.Read(); // Drop EndArray
 }
Esempio n. 10
0
    public virtual object Deserialize(JsonHelperReader json, object obj)
    {
        if (obj is UnityEngine.Object && !(obj is Component))
        {
            ((UnityEngine.Object)obj).name = (string)json.ReadRawProperty("name");
        }

        while (json.TokenType != JsonToken.EndObject)
        {
            Deserialize(json, obj, json.ReadPropertyName());
        }

        return(obj);
    }
Esempio n. 11
0
 public override string ReadMetaHeader(JsonHelperReader json, ref Type type)
 {
     /*
      * string metaProp = json.ReadPropertyName();
      * if (metaProp != JSONHelper.META.PROP) {
      *  return metaProp;
      * }
      * string valuetype = (string) json.Value;
      * json.Read(); // Drop String
      * if (JSONHelper.CheckOnRead && valuetype != JSONHelper.META.VALUETYPE) {
      *  throw new JsonReaderException("Type mismatch! Expected " + JSONHelper.META.VALUETYPE + ", got " + valuetype);
      * }
      */
     return(null);
 }
Esempio n. 12
0
    public override object Deserialize(JsonHelperReader json, object obj)
    {
        GameObject go = (GameObject)obj;

        bool active = DeserializeMain(json, go);

        go.SetActive(false);
        DeserializeComponentTypes(json, go);
        DeserializeHierarchy(json, go);

        DeserializeComponentData(json, go);

        go.SetActive(active);
        return(go);
    }
Esempio n. 13
0
    public void DeserializeHierarchy(JsonHelperReader json, GameObject go)
    {
        json.ReadPropertyName("hierarchy");
        Transform transform = go.transform;

        json.Read(); // Drop StartArray
        while (json.TokenType != JsonToken.EndArray)
        {
            GameObject child = new GameObject();
            child.transform.SetParent(transform);
            json.Read(); // Drop StartObject
            DeserializeMain(json, child);
            DeserializeComponentTypes(json, child);
            DeserializeHierarchy(json, child);
            json.Read(); // Drop EndObject
        }
        json.Read();     // Drop EndArray
    }
Esempio n. 14
0
    public virtual void Deserialize(JsonHelperReader json, object obj, string prop)
    {
        MemberInfo info;

        if (!_MemberMap.TryGetValue(prop, out info))
        {
            // Forcibly throw here - can't parse the following data anymore
            throw new JsonReaderException("Invalid property " + prop + "!");
        }

        object value = json.ReadObject(info.GetValueType());

        if (obj == null)
        {
            // Just drop the value.
            return;
        }
        ReflectionHelper.SetValue(info, obj, value);
    }
Esempio n. 15
0
    public virtual string ReadMetaHeader(JsonHelperReader json, ref Type type)
    {
        string metaProp = json.ReadPropertyName();

        if (metaProp != JSONHelper.META.PROP)
        {
            return(metaProp);
        }

        Type typeR = json.ReadMetaObjectType();

        if (JSONHelper.CheckOnRead && !type.IsAssignableFrom(typeR))
        {
            throw new JsonReaderException("Type mismatch! Expected " + type.FullName + ", got " + typeR.FullName);
        }
        type = typeR;

        return(null);
    }
Esempio n. 16
0
    protected override object Deserialize_(JsonHelperReader json, object obj)
    {
        if (obj == null)
        {
            // TODO all the (2) various types of Transforms
            /*Drop */ json.ReadProperty <Vector3>("position");
            /*Drop */ json.ReadProperty <Quaternion>("rotation");
            /*Drop */ json.ReadProperty <Vector3>("localScale");
            return(null);
        }

        Transform t = (Transform)obj;

        // TODO all the (2) various types of Transforms
        t.position   = json.ReadProperty <Vector3>("position");
        t.rotation   = json.ReadProperty <Quaternion>("rotation");
        t.localScale = json.ReadProperty <Vector3>("localScale");

        return(t);
    }
Esempio n. 17
0
    public override object Deserialize(JsonHelperReader json, object obj)
    {
        Component c = (Component)obj;

        string componentType = (string)json.ReadRawProperty(JSONHelper.META.TYPE);

        if (componentType == JSONHelper.META.COMPONENTTYPE_DEFINITION)
        {
            return(Deserialize_(json, c));
        }

        if (componentType == JSONHelper.META.COMPONENTTYPE_REFERENCE)
        {
            string     name    = (string)json.ReadRawProperty("name");
            string     path    = (string)json.ReadRawProperty("path");
            GameObject holding = null;
            object     holdingObj;
            if (json.Global.TryGetValue(path, out holdingObj))
            {
                holding = holdingObj as GameObject;
            }
            if (json.Global.TryGetValue(name, out holdingObj))
            {
                holding = holdingObj as GameObject;
            }
            else
            {
                holding = GameObject.Find(path);
            }
            if (holding == null)
            {
                Console.WriteLine("WARNING: Could not find GameObject " + path + " holding " + _T.Name + "!");
                return(null);
            }
            return(holding.GetComponent(_T));
        }

        throw new JsonReaderException("Unknown component type: " + componentType);
    }
Esempio n. 18
0
 public override string ReadMetaHeader(JsonHelperReader json, ref Type type)
 {
     return(null);
 }
Esempio n. 19
0
 public override object New(JsonHelperReader json, Type type)
 {
     return(null);
 }
Esempio n. 20
0
        public static UnityEngine.Object Load(string path, Type type)
        {
            if (path == "PlayerCoopCultist" && Player.CoopReplacement != null)
            {
                path = Player.CoopReplacement;
            }
            else if (path.StartsWithInvariant("Player") && Player.PlayerReplacement != null)
            {
                path = Player.PlayerReplacement;
            }

            UnityEngine.Object customobj = null;
            for (int i = 0; i < _Protocols.Length; i++)
            {
                var protocol = _Protocols[i];
                customobj = protocol.Get(path);
                if (customobj != null)
                {
                    return(customobj);
                }
            }

#if DEBUG
            if (DumpResources)
            {
                Dump.DumpResource(path);
            }
#endif

            AssetMetadata metadata;
            bool          isJson  = false;
            bool          isPatch = false;
            if (TryGetMapped(path, out metadata, true))
            {
            }
            else if (TryGetMapped(path + ".json", out metadata))
            {
                isJson = true;
            }
            else if (TryGetMapped(path + ".patch.json", out metadata))
            {
                isPatch = true; isJson = true;
            }

            if (metadata != null)
            {
                if (isJson)
                {
                    if (isPatch)
                    {
                        UnityEngine.Object obj = Resources.Load(path + ETGModUnityEngineHooks.SkipSuffix);
                        using (JsonHelperReader json = JSONHelper.OpenReadJSON(metadata.Stream)) {
                            json.Read(); // Go to start;
                            return((UnityEngine.Object)json.FillObject(obj));
                        }
                    }
                    return((UnityEngine.Object)JSONHelper.ReadJSON(metadata.Stream));
                }

                if (t_tk2dSpriteCollectionData == type)
                {
                    AssetMetadata json = GetMapped(path + ".json");
                    if (metadata.AssetType == t_Texture2D && json != null)
                    {
                        // Atlas
                        string[]        names;
                        Rect[]          regions;
                        Vector2[]       anchors;
                        AttachPoint[][] attachPoints;
                        AssetSpriteData.ToTK2D(JSONHelper.ReadJSON <List <AssetSpriteData> >(json.Stream), out names, out regions, out anchors, out attachPoints);
                        tk2dSpriteCollectionData sprites = tk2dSpriteCollectionData.CreateFromTexture(
                            Resources.Load <Texture2D>(path), tk2dSpriteCollectionSize.Default(), names, regions, anchors
                            );
                        for (int i = 0; i < attachPoints.Length; i++)
                        {
                            sprites.SetAttachPoints(i, attachPoints[i]);
                        }
                        return(sprites);
                    }

                    if (metadata.AssetType == t_AssetDirectory)
                    {
                        // Separate textures
                        // TODO create collection from "children" assets
                        tk2dSpriteCollectionData data = new GameObject(path.StartsWithInvariant("sprites/") ? path.Substring(8) : path).AddComponent <tk2dSpriteCollectionData>();
                        tk2dSpriteCollectionSize size = tk2dSpriteCollectionSize.Default();

                        data.spriteCollectionName = data.name;
                        data.Transient            = true;
                        data.version            = 3;
                        data.invOrthoSize       = 1f / size.OrthoSize;
                        data.halfTargetHeight   = size.TargetHeight * 0.5f;
                        data.premultipliedAlpha = false;
                        data.material           = new Material(DefaultSpriteShader);
                        data.materials          = new Material[] { data.material };
                        data.buildKey           = UnityEngine.Random.Range(0, int.MaxValue);

                        data.Handle();

                        data.textures = new Texture2D[data.spriteDefinitions.Length];
                        for (int i = 0; i < data.spriteDefinitions.Length; i++)
                        {
                            data.textures[i] = data.spriteDefinitions[i].materialInst.mainTexture;
                        }

                        return(data);
                    }
                }

                if (t_Texture.IsAssignableFrom(type) ||
                    type == t_Texture2D ||
                    (type == t_Object && metadata.AssetType == t_Texture2D))
                {
                    Texture2D tex = new Texture2D(2, 2);
                    tex.name = path;
                    tex.LoadImage(metadata.Data);
                    tex.filterMode = FilterMode.Point;
                    return(tex);
                }
            }

            UnityEngine.Object orig = Resources.Load(path + ETGModUnityEngineHooks.SkipSuffix, type);
            if (orig is GameObject)
            {
                Objects.HandleGameObject((GameObject)orig);
            }
            return(orig);
        }
Esempio n. 21
0
 public override object New(JsonHelperReader json, Type type)
 {
     return(new AttachPointData(null));
 }
Esempio n. 22
0
 public override object New(JsonHelperReader json, Type type)
 {
     return(ScriptableObject.CreateInstance(type));
 }
Esempio n. 23
0
 protected virtual object Deserialize_(JsonHelperReader json, object obj)
 {
     return(base.Deserialize(json, obj));
 }
 public override object New(JsonHelperReader json, Type type)
 {
     return(new Vector4());
 }
Esempio n. 25
0
 public override object Deserialize(JsonHelperReader json, object obj)
 {
     ((patch_TextAsset)obj).textOverride = (string)json.ReadRawProperty("text");
     return(obj);
 }
Esempio n. 26
0
 public override object New(JsonHelperReader json, Type type)
 {
     return(new OverridableBool(false));
 }
Esempio n. 27
0
 public override object Deserialize(JsonHelperReader json, object obj)
 {
     json.Read(); // Drop PropertyName
     json.Read(); // Drop String
     return(obj);
 }
Esempio n. 28
0
 public override object New(JsonHelperReader json, Type type)
 {
     return(new BagelCollider(0, 0, null));
 }
 public override object New(JsonHelperReader json, Type type)
 {
     return(new Quaternion());
 }