Ejemplo 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);
            }
        }
    }
Ejemplo n.º 2
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);
        }