Ejemplo n.º 1
0
        public static void ReplaceWithTextures(tk2dSpriteCollectionData data)
        {
            List <Texture2D> replacements = ResourceExtractor.GetTexturesFromFolder(data.name);

            foreach (Texture2D texture in replacements)
            {
                tk2dSpriteDefinition def = GetDefinition(data, texture.name);
                if (def != null)
                {
                    def.ReplaceTexture(texture);
                }
                else
                {
                    ETGModConsole.Log("<color=#FF0000FF>" + texture.name + " not found. </color>");
                }
            }
        }
Ejemplo n.º 2
0
        public static void HandleSprites(tk2dSpriteCollectionData sprites)
        {
            if (sprites == null)
            {
                return;
            }
            string path = "sprites/" + sprites.spriteCollectionName;

            Texture2D     replacement;
            AssetMetadata metadata;

            Texture mainTexture = sprites.materials?.Length != 0 ? sprites.materials[0]?.mainTexture : null;
            string  atlasName   = mainTexture?.name;

            if (mainTexture != null && (atlasName == null || atlasName.Length == 0 || atlasName[0] != '~'))
            {
                if (TextureMap.TryGetValue(path, out replacement))
                {
                }
                else if (TryGetMapped(path, out metadata))
                {
                    TextureMap[path] = replacement = Resources.Load <Texture2D>(path);
                }
                else
                {
                    foreach (KeyValuePair <string, AssetMetadata> mapping in Map)
                    {
                        if (!mapping.Value.HasData)
                        {
                            continue;
                        }
                        string resourcePath = mapping.Key;
                        if (!resourcePath.StartsWithInvariant("sprites/@"))
                        {
                            continue;
                        }
                        string spriteName = resourcePath.Substring(9);
                        if (sprites.spriteCollectionName.Contains(spriteName))
                        {
                            string copyPath = Path.Combine(ResourcesDirectory, ("DUMP" + path).Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar) + ".png");
                            if (mapping.Value.Container == AssetMetadata.ContainerType.Filesystem && !File.Exists(copyPath))
                            {
                                Directory.GetParent(copyPath).Create();
                                File.Copy(mapping.Value.File, copyPath);
                            }
                            TextureMap[path] = replacement = Resources.Load <Texture2D>(resourcePath);
                            break;
                        }
                    }
                }

                if (replacement != null)
                {
                    // Full atlas texture replacement.
                    replacement.name = '~' + atlasName;
                    for (int i = 0; i < sprites.materials.Length; i++)
                    {
                        if (sprites.materials[i]?.mainTexture == null)
                        {
                            continue;
                        }
                        sprites.materials[i].mainTexture = replacement;
                    }
                }
            }

            if (DumpSprites)
            {
                Dump.DumpSpriteCollection(sprites);
            }
            if (DumpSpritesMetadata)
            {
                Dump.DumpSpriteCollectionMetadata(sprites);
            }

            List <tk2dSpriteDefinition> list = null;

            foreach (KeyValuePair <string, AssetMetadata> mapping in Map)
            {
                string assetPath = mapping.Key;
                if (assetPath.Length <= path.Length + 1)
                {
                    continue;
                }
                if (!assetPath.StartsWithInvariant(path) || mapping.Value.AssetType != t_Texture2D)
                {
                    continue;
                }

                string name = assetPath.Substring(path.Length + 1);
                tk2dSpriteDefinition frame = sprites.GetSpriteDefinition(name);

                if (frame != null && frame.materialInst != null)
                {
                    Texture2D origTex = (Texture2D)frame.materialInst.mainTexture;
                    if (Packer.IsPageTexture(origTex))
                    {
                        continue;
                    }
                }

                if (!TextureMap.TryGetValue(assetPath, out replacement))
                {
                    replacement = TextureMap[assetPath] = Resources.Load <Texture2D>(assetPath);
                }
                if (replacement == null)
                {
                    continue;
                }

                if (frame == null && name[0] == '@')
                {
                    name = name.Substring(1);
                    for (int i = 0; i < sprites.spriteDefinitions.Length; i++)
                    {
                        tk2dSpriteDefinition frame_ = sprites.spriteDefinitions[i];
                        if (frame_.Valid && frame_.name.Contains(name))
                        {
                            frame = frame_;
                            name  = frame_.name;
                            break;
                        }
                    }
                    if (frame == null)
                    {
                        continue;
                    }
                }

                if (frame != null)
                {
                    // Replace old sprite.
                    frame.ReplaceTexture(replacement);
                }
                else
                {
                    // Add new sprite.
                    if (list == null)
                    {
                        list = new List <tk2dSpriteDefinition>(sprites.spriteDefinitions?.Length ?? 32);
                        if (sprites.spriteDefinitions != null)
                        {
                            list.AddRange(sprites.spriteDefinitions);
                        }
                    }
                    frame          = new tk2dSpriteDefinition();
                    frame.name     = name;
                    frame.material = sprites.materials[0];
                    frame.ReplaceTexture(replacement);

                    AssetSpriteData frameData    = new AssetSpriteData();
                    AssetMetadata   jsonMetadata = GetMapped(assetPath + ".json");
                    if (jsonMetadata != null)
                    {
                        frameData = JSONHelper.ReadJSON <AssetSpriteData>(jsonMetadata.Stream);
                    }

                    frame.normals  = new Vector3[0];
                    frame.tangents = new Vector4[0];
                    frame.indices  = new int[] { 0, 3, 1, 2, 3, 0 };

                    // TODO figure out this black magic
                    const float pixelScale = 0.0625f;
                    float       w          = replacement.width * pixelScale;
                    float       h          = replacement.height * pixelScale;
                    frame.position0         = new Vector3(0f, 0f, 0f);
                    frame.position1         = new Vector3(w, 0f, 0f);
                    frame.position2         = new Vector3(0f, h, 0f);
                    frame.position3         = new Vector3(w, h, 0f);
                    frame.boundsDataCenter  = frame.untrimmedBoundsDataCenter = new Vector3(w / 2f, h / 2f, 0f);
                    frame.boundsDataExtents = frame.untrimmedBoundsDataExtents = new Vector3(w, h, 0f);

                    sprites.SetAttachPoints(list.Count, frameData.attachPoints);

                    list.Add(frame);
                }
            }
            if (list != null)
            {
                sprites.spriteDefinitions = list.ToArray();
                ReflectionHelper.SetValue(f_tk2dSpriteCollectionData_spriteNameLookupDict, sprites, null);
            }

            if (sprites.hasPlatformData)
            {
                sprites.inst.Handle();
            }
        }