Ejemplo n.º 1
0
        public void LoadAtlasData(string dataPath, AtlasDataFormat format)
        {
            switch (format)
            {
            case AtlasDataFormat.TexturePacker_Sparrow:
            {
                XmlDocument xml         = Calc.LoadContentXML(dataPath);
                XmlElement  at          = xml["TextureAtlas"];
                var         subtextures = at.GetElementsByTagName("SubTexture");

                atlas = new Dictionary <string, MTexture>(subtextures.Count, StringComparer.InvariantCultureIgnoreCase);
                foreach (XmlElement sub in subtextures)
                {
                    var clipRect = sub.Rect();
                    if (sub.HasAttr("frameX"))
                    {
                        atlas.Add(sub.Attr("name"), new MTexture(this, sub.Attr("name"), clipRect, new Vector2(-sub.AttrInt("frameX"), -sub.AttrInt("frameY")), sub.AttrInt("frameWidth"), sub.AttrInt("frameHeight")));
                    }
                    else
                    {
                        atlas.Add(sub.Attr("name"), new MTexture(this, sub.Attr("name"), clipRect));
                    }
                }
            }
            break;

            default:
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 2
0
        public static Atlas FromAtlas(string path, AtlasDataFormat format)
        {
            var atlas = new Atlas();

            atlas.Sources = new List <Texture2D>();
            ReadAtlasData(atlas, path, format);
            return(atlas);
        }
Ejemplo n.º 3
0
        public static new Atlas FromAtlas(string path, AtlasDataFormat format)
        {
            patch_Atlas atlas = (patch_Atlas)orig_FromAtlas(path, format);

            atlas.DataMethod = "FromAtlas";
            atlas.DataPath   = path;
            atlas.DataFormat = format;
            Everest.Content.Process(atlas, atlas.DataPath);
            return(atlas);
        }
Ejemplo n.º 4
0
        public static new Atlas FromMultiAtlas(string rootPath, string filename, AtlasDataFormat format)
        {
            patch_Atlas atlas = (patch_Atlas)orig_FromMultiAtlas(rootPath, filename, format);

            atlas.DataMethod = "FromMultiAtlas";
            atlas.DataPath   = rootPath;
            atlas.DataPaths  = new string[] { filename };
            atlas.DataFormat = format;
            Everest.Content.Process(atlas, atlas.DataPath);
            return(atlas);
        }
Ejemplo n.º 5
0
        public static new Atlas FromMultiAtlas(string rootPath, string[] dataPath, AtlasDataFormat format)
        {
            patch_Atlas atlas = (patch_Atlas)orig_FromMultiAtlas(rootPath, dataPath, format);

            atlas.DataMethod = "FromMultiAtlas";
            atlas.DataPath   = rootPath;
            atlas.DataPaths  = dataPath;
            atlas.DataFormat = format;
            Everest.Content.Process(atlas.DataPath, atlas);
            Everest.Events.Atlas.Load(atlas);
            return(atlas);
        }
Ejemplo n.º 6
0
        public static Atlas FromMultiAtlas(string rootPath, string[] dataPath, AtlasDataFormat format)
        {
            var atlas = new Atlas();

            atlas.Sources = new List <Texture2D>();

            for (int i = 0; i < dataPath.Length; i++)
            {
                ReadAtlasData(atlas, Path.Combine(rootPath, dataPath[i]), format);
            }

            return(atlas);
        }
Ejemplo n.º 7
0
        private static void ReadAtlasData(Atlas atlas, string path, AtlasDataFormat format)
        {
            string pathFull = Path.Combine(Engine.ContentDirectory, path);

            // If the file doesn't exist, don't add any data to the atlas.
            switch (format)
            {
            case AtlasDataFormat.TexturePacker_Sparrow:
            case AtlasDataFormat.CrunchXml:
            case AtlasDataFormat.CrunchBinary:
                // These formats don't append any file extension.
                if (!File.Exists(pathFull))
                {
                    return;
                }
                break;

            case AtlasDataFormat.CrunchXmlOrBinary:
                // Check against both .bin and .xml paths, as the game reads whichever exists.
                if (!File.Exists(pathFull + ".bin") && !File.Exists(pathFull + ".xml"))
                {
                    return;
                }
                break;

            case AtlasDataFormat.CrunchBinaryNoAtlas:
                // This appends .bin to the path for whatever reason (compared to CrunchBinary).
                if (!File.Exists(pathFull + ".bin"))
                {
                    return;
                }
                break;

            case AtlasDataFormat.Packer:
            case AtlasDataFormat.PackerNoAtlas:
                // The only format used by the game.
                if (!File.Exists(pathFull + ".meta"))
                {
                    return;
                }
                break;

            default:
                // Unsupported format. Let's avoid crashing.
                return;
            }

            orig_ReadAtlasData(atlas, path, format);
        }
Ejemplo n.º 8
0
        public static Atlas FromMultiAtlas(string rootPath, string filename, AtlasDataFormat format)
        {
            var atlas = new Atlas();

            atlas.Sources = new List <Texture2D>();

            var index = 0;

            while (true)
            {
                var dataPath = Path.Combine(rootPath, filename + index.ToString() + ".xml");

                if (!File.Exists(Path.Combine(Engine.ContentDirectory, dataPath)))
                {
                    break;
                }

                ReadAtlasData(atlas, dataPath, format);
                index++;
            }

            return(atlas);
        }
Ejemplo n.º 9
0
 public static extern Atlas orig_FromMultiAtlas(string rootPath, string filename, AtlasDataFormat format);
Ejemplo n.º 10
0
 public static extern Atlas orig_FromMultiAtlas(string rootPath, string[] dataPath, AtlasDataFormat format);
Ejemplo n.º 11
0
 public static extern Atlas orig_FromAtlas(string path, AtlasDataFormat format);
Ejemplo n.º 12
0
        private static void ReadAtlasData(Atlas atlas, string path, AtlasDataFormat format)
        {
            switch (format)
            {
            case AtlasDataFormat.TexturePacker_Sparrow:
            {
                XmlDocument xml = Calc.LoadContentXML(path);
                XmlElement  at  = xml["TextureAtlas"];

                var texturePath = at.Attr("imagePath", "");
                var fileStream  = new FileStream(Path.Combine(Path.GetDirectoryName(path), texturePath), FileMode.Open, FileAccess.Read);
                var texture     = Texture2D.FromStream(Engine.Instance.GraphicsDevice, fileStream);
                fileStream.Close();

                var mTexture = new MTexture(texture);
                atlas.Sources.Add(texture);

                var subtextures = at.GetElementsByTagName("SubTexture");

                foreach (XmlElement sub in subtextures)
                {
                    var name     = sub.Attr("name");
                    var clipRect = sub.Rect();
                    if (sub.HasAttr("frameX"))
                    {
                        atlas.textures[name] = new MTexture(mTexture, name, clipRect, new Vector2(-sub.AttrInt("frameX"), -sub.AttrInt("frameY")), sub.AttrInt("frameWidth"), sub.AttrInt("frameHeight"));
                    }
                    else
                    {
                        atlas.textures[name] = new MTexture(mTexture, name, clipRect);
                    }
                }
            }
            break;

            case AtlasDataFormat.CrunchXml:
            {
                XmlDocument xml = Calc.LoadContentXML(path);
                XmlElement  at  = xml["atlas"];

                foreach (XmlElement tex in at)
                {
                    var    texturePath = tex.Attr("n", "");
                    string fsloc       = Engine.ContentDirectory + "\\" + Path.GetDirectoryName(path) + texturePath + ".png";
                    var    fileStream  = new FileStream(fsloc, FileMode.Open, FileAccess.Read);
                    var    texture     = Texture2D.FromStream(Engine.Instance.GraphicsDevice, fileStream);
                    fileStream.Close();

                    var mTexture = new MTexture(texture);
                    atlas.Sources.Add(texture);

                    foreach (XmlElement sub in tex)
                    {
                        var name     = sub.Attr("n");
                        var clipRect = new Rectangle(sub.AttrInt("x"), sub.AttrInt("y"), sub.AttrInt("w"), sub.AttrInt("h"));
                        if (sub.HasAttr("fx"))
                        {
                            atlas.textures[name] = new MTexture(mTexture, name, clipRect, new Vector2(-sub.AttrInt("fx"), -sub.AttrInt("fy")), sub.AttrInt("fw"), sub.AttrInt("fh"));
                        }
                        else
                        {
                            atlas.textures[name] = new MTexture(mTexture, name, clipRect);
                        }
                    }
                }
            }
            break;

            case AtlasDataFormat.CrunchBinary:
                using (var stream = File.OpenRead(Path.Combine(Engine.ContentDirectory, path)))
                {
                    var reader   = new BinaryReader(stream);
                    var textures = reader.ReadInt16();

                    for (int i = 0; i < textures; i++)
                    {
                        var textureName = reader.ReadNullTerminatedString();
                        var texturePath = Path.Combine(Path.GetDirectoryName(path), textureName + ".png");
                        var fileStream  = new FileStream(texturePath, FileMode.Open, FileAccess.Read);
                        var texture     = Texture2D.FromStream(Engine.Instance.GraphicsDevice, fileStream);
                        fileStream.Close();

                        atlas.Sources.Add(texture);

                        var mTexture    = new MTexture(texture);
                        var subtextures = reader.ReadInt16();
                        for (int j = 0; j < subtextures; j++)
                        {
                            var name = reader.ReadNullTerminatedString();
                            var x    = reader.ReadInt16();
                            var y    = reader.ReadInt16();
                            var w    = reader.ReadInt16();
                            var h    = reader.ReadInt16();
                            var fx   = reader.ReadInt16();
                            var fy   = reader.ReadInt16();
                            var fw   = reader.ReadInt16();
                            var fh   = reader.ReadInt16();

                            atlas.textures[name] = new MTexture(mTexture, name, new Rectangle(x, y, w, h), new Vector2(-fx, -fy), fw, fh);
                        }
                    }
                }
                break;

            case AtlasDataFormat.CrunchBinaryNoAtlas:
                using (var stream = File.OpenRead(Path.Combine(Engine.ContentDirectory, path + ".bin")))
                {
                    var reader  = new BinaryReader(stream);
                    var folders = reader.ReadInt16();

                    for (int i = 0; i < folders; i++)
                    {
                        var folderName = reader.ReadNullTerminatedString();
                        var folderPath = Path.Combine(Path.GetDirectoryName(path), folderName);

                        var subtextures = reader.ReadInt16();
                        for (int j = 0; j < subtextures; j++)
                        {
                            var name = reader.ReadNullTerminatedString();
                            var x    = reader.ReadInt16();
                            var y    = reader.ReadInt16();
                            var w    = reader.ReadInt16();
                            var h    = reader.ReadInt16();
                            var fx   = reader.ReadInt16();
                            var fy   = reader.ReadInt16();
                            var fw   = reader.ReadInt16();
                            var fh   = reader.ReadInt16();

                            var fileStream = new FileStream(Path.Combine(folderPath, name + ".png"), FileMode.Open, FileAccess.Read);
                            var texture    = Texture2D.FromStream(Engine.Instance.GraphicsDevice, fileStream);
                            fileStream.Close();

                            atlas.Sources.Add(texture);
                            atlas.textures[name] = new MTexture(texture, new Vector2(-fx, -fy), fw, fh);
                        }
                    }
                }
                break;

            case AtlasDataFormat.Packer:

                using (var stream = File.OpenRead(Path.Combine(Engine.ContentDirectory, path + ".meta")))
                {
                    var reader = new BinaryReader(stream);
                    reader.ReadInt32();     // version
                    reader.ReadString();    // args
                    reader.ReadInt32();     // hash

                    var textures = reader.ReadInt16();
                    for (int i = 0; i < textures; i++)
                    {
                        var textureName = reader.ReadString();
                        var texturePath = Path.Combine(Path.GetDirectoryName(path), textureName + ".data");
                        var fileStream  = new FileStream(texturePath, FileMode.Open, FileAccess.Read);
                        var texture     = Texture2D.FromStream(Engine.Instance.GraphicsDevice, fileStream);
                        fileStream.Close();

                        atlas.Sources.Add(texture);

                        var mTexture    = new MTexture(texture);
                        var subtextures = reader.ReadInt16();
                        for (int j = 0; j < subtextures; j++)
                        {
                            var name = reader.ReadString().Replace('\\', '/');
                            var x    = reader.ReadInt16();
                            var y    = reader.ReadInt16();
                            var w    = reader.ReadInt16();
                            var h    = reader.ReadInt16();
                            var fx   = reader.ReadInt16();
                            var fy   = reader.ReadInt16();
                            var fw   = reader.ReadInt16();
                            var fh   = reader.ReadInt16();

                            atlas.textures[name] = new MTexture(mTexture, name, new Rectangle(x, y, w, h), new Vector2(-fx, -fy), fw, fh);
                        }
                    }
                }

                break;

            case AtlasDataFormat.PackerNoAtlas:
                using (var stream = File.OpenRead(Path.Combine(Engine.ContentDirectory, path + ".meta")))
                {
                    var reader = new BinaryReader(stream);
                    reader.ReadInt32();     // version
                    reader.ReadString();    // args
                    reader.ReadInt32();     // hash

                    var folders = reader.ReadInt16();
                    for (int i = 0; i < folders; i++)
                    {
                        var folderName = reader.ReadString();
                        var folderPath = Path.Combine(Path.GetDirectoryName(path), folderName);

                        var subtextures = reader.ReadInt16();
                        for (int j = 0; j < subtextures; j++)
                        {
                            var name = reader.ReadString().Replace('\\', '/');
                            var x    = reader.ReadInt16();
                            var y    = reader.ReadInt16();
                            var w    = reader.ReadInt16();
                            var h    = reader.ReadInt16();
                            var fx   = reader.ReadInt16();
                            var fy   = reader.ReadInt16();
                            var fw   = reader.ReadInt16();
                            var fh   = reader.ReadInt16();

                            var fileStream = new FileStream(Path.Combine(folderPath, name + ".data"), FileMode.Open, FileAccess.Read);
                            var texture    = Texture2D.FromStream(Engine.Instance.GraphicsDevice, fileStream);
                            fileStream.Close();

                            atlas.Sources.Add(texture);
                            atlas.textures[name] = new MTexture(texture, new Vector2(-fx, -fy), fw, fh);
                        }
                    }
                }
                break;

            case AtlasDataFormat.CrunchXmlOrBinary:

                if (File.Exists(Path.Combine(Engine.ContentDirectory, path + ".bin")))
                {
                    ReadAtlasData(atlas, path + ".bin", AtlasDataFormat.CrunchBinary);
                }
                else
                {
                    ReadAtlasData(atlas, path + ".xml", AtlasDataFormat.CrunchXml);
                }

                break;


            default:
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 13
0
        private static void ReadAtlasData(Atlas _atlas, string path, AtlasDataFormat format)
        {
            if (VTextureToMTextureMap == null)
            {
                VTextureToMTextureMap = new Dictionary <string, MTexture>();
            }

            patch_Atlas atlas = (patch_Atlas)_atlas;

            string pathFull = Path.Combine(Engine.ContentDirectory, path);

            XmlDocument    xmlDoc;
            VirtualTexture texV;
            MTexture       texM;

            switch (format)
            {
            case AtlasDataFormat.TexturePacker_Sparrow:
                xmlDoc = Calc.LoadContentXML(path);
                XmlElement xmlTextureAtlas = xmlDoc["TextureAtlas"];
                if (xmlTextureAtlas == null)
                {
                    break;
                }

                texV = VirtualContent.CreateTexture(Path.Combine(Path.GetDirectoryName(path), xmlTextureAtlas.Attr("imagePath", "")));
                texM = new MTexture(texV);
                VTextureToMTextureMap[texV.Name] = texM;
                atlas.Sources.Add(texV);

                XmlNodeList xmlSubs = xmlTextureAtlas.GetElementsByTagName("SubTexture");
                foreach (XmlElement xmlSub in xmlSubs)
                {
                    string    name     = xmlSub.Attr("name");
                    Rectangle clipRect = xmlSub.Rect();
                    if (xmlSub.HasAttr("frameX"))
                    {
                        atlas.textures[name] = new MTexture(
                            texM, name, clipRect,
                            new Vector2(-xmlSub.AttrInt("frameX"), -xmlSub.AttrInt("frameY")),
                            xmlSub.AttrInt("frameWidth"), xmlSub.AttrInt("frameHeight")
                            );
                    }
                    else
                    {
                        atlas.textures[name] = new MTexture(texM, name, clipRect);
                    }
                }
                break;

            case AtlasDataFormat.CrunchXml:
                if (!File.Exists(pathFull))
                {
                    break;
                }

                xmlDoc = Calc.LoadContentXML(path);
                XmlElement xmlAtlas = xmlDoc["atlas"];

                foreach (XmlElement xmlAtlasSource in xmlAtlas)
                {
                    texV = VirtualContent.CreateTexture(Path.Combine(Path.GetDirectoryName(path), xmlAtlasSource.Attr("n", "") + ".png"));
                    texM = new MTexture(texV);
                    VTextureToMTextureMap[texV.Name] = texM;
                    atlas.Sources.Add(texV);
                    foreach (XmlElement xmlSub in xmlAtlasSource)
                    {
                        string    name     = xmlSub.Attr("n");
                        Rectangle clipRect = new Rectangle(xmlSub.AttrInt("x"), xmlSub.AttrInt("y"), xmlSub.AttrInt("w"), xmlSub.AttrInt("h"));
                        if (xmlSub.HasAttr("fx"))
                        {
                            atlas.textures[name] = new MTexture(
                                texM, name, clipRect,
                                new Vector2(-xmlSub.AttrInt("fx"), -xmlSub.AttrInt("fy")),
                                xmlSub.AttrInt("fw"), xmlSub.AttrInt("fh")
                                );
                        }
                        else
                        {
                            atlas.textures[name] = new MTexture(texM, name, clipRect);
                        }
                    }
                }
                break;

            case AtlasDataFormat.CrunchBinary:
                if (!File.Exists(pathFull))
                {
                    break;
                }

                using (FileStream stream = File.OpenRead(pathFull))
                    using (BinaryReader reader = new BinaryReader(stream)) {
                        short sources = reader.ReadInt16();
                        for (int i = 0; i < sources; i++)
                        {
                            texV = VirtualContent.CreateTexture(Path.Combine(Path.GetDirectoryName(path), reader.ReadNullTerminatedString() + ".png"));
                            texM = new MTexture(texV);
                            VTextureToMTextureMap[texV.Name] = texM;
                            atlas.Sources.Add(texV);
                            short subs = reader.ReadInt16();
                            for (int j = 0; j < subs; j++)
                            {
                                string name       = reader.ReadNullTerminatedString();
                                short  clipX      = reader.ReadInt16();
                                short  clipY      = reader.ReadInt16();
                                short  clipWidth  = reader.ReadInt16();
                                short  clipHeight = reader.ReadInt16();
                                short  offsX      = reader.ReadInt16();
                                short  offsY      = reader.ReadInt16();
                                short  width      = reader.ReadInt16();
                                short  height     = reader.ReadInt16();
                                atlas.textures[name] = new MTexture(
                                    texM, name, new Rectangle(clipX, clipY, clipWidth, clipHeight),
                                    new Vector2(-offsX, -offsY),
                                    width, height
                                    );
                            }
                        }
                    }
                break;

            case AtlasDataFormat.CrunchXmlOrBinary:
                if (File.Exists(pathFull + ".bin"))
                {
                    ReadAtlasData(atlas, path + ".bin", AtlasDataFormat.CrunchBinary);
                }
                else if (File.Exists(pathFull + ".xml"))
                {
                    ReadAtlasData(atlas, path + ".xml", AtlasDataFormat.CrunchXml);
                }
                return;

            case AtlasDataFormat.CrunchBinaryNoAtlas:
                if (!File.Exists(pathFull + ".bin"))
                {
                    break;
                }

                using (FileStream stream = File.OpenRead(pathFull + ".bin"))
                    using (BinaryReader reader = new BinaryReader(stream)) {
                        short sources = reader.ReadInt16();
                        for (int i = 0; i < sources; i++)
                        {
                            string sourcePath = Path.Combine(Path.GetDirectoryName(path), reader.ReadNullTerminatedString());
                            short  subs       = reader.ReadInt16();
                            for (int j = 0; j < subs; j++)
                            {
                                string name     = reader.ReadNullTerminatedString();
                                short  unknownA = reader.ReadInt16();
                                short  unknownB = reader.ReadInt16();
                                short  unknownC = reader.ReadInt16();
                                short  unknownD = reader.ReadInt16();
                                short  offsX    = reader.ReadInt16();
                                short  offsY    = reader.ReadInt16();
                                short  width    = reader.ReadInt16();
                                short  height   = reader.ReadInt16();
                                texV = VirtualContent.CreateTexture(Path.Combine(sourcePath, name + ".png"));
                                atlas.textures[name] = VTextureToMTextureMap[texV.Name] = new MTexture(texV, new Vector2(-offsX, -offsY), width, height);
                                atlas.Sources.Add(texV);
                            }
                        }
                    }
                break;

            case AtlasDataFormat.Packer:
                if (!File.Exists(pathFull + ".meta"))
                {
                    break;
                }

                using (FileStream stream = File.OpenRead(pathFull + ".meta"))
                    using (BinaryReader reader = new BinaryReader(stream)) {
                        reader.ReadInt32();  // ???
                        reader.ReadString(); // ???
                        reader.ReadInt32();  // ???
                        short sources = reader.ReadInt16();
                        for (int i = 0; i < sources; i++)
                        {
                            texV = VirtualContent.CreateTexture(Path.Combine(Path.GetDirectoryName(path), reader.ReadString() + ".data"));
                            texM = new MTexture(texV);
                            VTextureToMTextureMap[texV.Name] = texM;
                            atlas.Sources.Add(texV);
                            short subs = reader.ReadInt16();
                            for (int j = 0; j < subs; j++)
                            {
                                string name       = reader.ReadString().Replace('\\', '/');
                                short  clipX      = reader.ReadInt16();
                                short  clipY      = reader.ReadInt16();
                                short  clipWidth  = reader.ReadInt16();
                                short  clipHeight = reader.ReadInt16();
                                short  offsX      = reader.ReadInt16();
                                short  offsY      = reader.ReadInt16();
                                short  width      = reader.ReadInt16();
                                short  height     = reader.ReadInt16();
                                atlas.textures[name] = new MTexture(
                                    texM, name, new Rectangle(clipX, clipY, clipWidth, clipHeight),
                                    new Vector2(-offsX, -offsY),
                                    width, height
                                    );
                            }
                        }
                    }
                break;

            case AtlasDataFormat.PackerNoAtlas:
                if (!File.Exists(pathFull + ".meta"))
                {
                    break;
                }

                using (FileStream stream = File.OpenRead(pathFull + ".meta"))
                    using (BinaryReader reader = new BinaryReader(stream)) {
                        reader.ReadInt32();
                        reader.ReadString();
                        reader.ReadInt32();
                        short sources = reader.ReadInt16();
                        for (int i = 0; i < sources; i++)
                        {
                            string sourcePath = Path.Combine(Path.GetDirectoryName(path), reader.ReadString());
                            short  subs       = reader.ReadInt16();
                            for (int j = 0; j < subs; j++)
                            {
                                string name     = reader.ReadString().Replace('\\', '/');
                                short  unknownA = reader.ReadInt16();
                                short  unknownB = reader.ReadInt16();
                                short  unknownC = reader.ReadInt16();
                                short  unknownD = reader.ReadInt16();
                                short  offsX    = reader.ReadInt16();
                                short  offsY    = reader.ReadInt16();
                                short  width    = reader.ReadInt16();
                                short  height   = reader.ReadInt16();
                                texV = VirtualContent.CreateTexture(Path.Combine(sourcePath, name + ".data"));
                                atlas.textures[name] = VTextureToMTextureMap[texV.Name] = new MTexture(texV, new Vector2(-offsX, -offsY), width, height);
                                atlas.Sources.Add(texV);
                            }
                        }
                    }
                break;

            default:
                break;
            }
        }
Ejemplo n.º 14
0
 private static extern void orig_ReadAtlasData(Atlas atlas, string path, AtlasDataFormat format);