Esempio n. 1
0
        public static CTPKEntry FromFile(string _xmlFilename, string foldername, string _pngFilename)
        {
            if (!File.Exists(_xmlFilename))
            {
                return(new CTPKEntry());
            }

            using (XmlTextReader reader = new XmlTextReader(_xmlFilename))
            {
                reader.WhitespaceHandling = WhitespaceHandling.All;

                XmlSerializer serializer = new XmlSerializer(typeof(CTPKEntry));

                CTPKEntry entry = (CTPKEntry)serializer.Deserialize(reader);

                Console.WriteLine("Reading {0}...", entry.FilePath);

                string path;
                if (_pngFilename == null)
                {
                    path = entry.FilePath;
                    if (!String.IsNullOrWhiteSpace(foldername))
                    {
                        path = Path.Combine(foldername, path);
                    }
                }
                else
                {
                    path = _pngFilename;
                }

                /*
                 * var pixelSize = 3;
                 * var bmpPixelFormat = PixelFormat.Format24bppRgb;
                 * entry.Format = (int)TextureFormat.Rgb8;
                 *
                 * entry.HasAlpha = true;
                 * if (entry.HasAlpha)
                 * {
                 *  bmpPixelFormat = PixelFormat.Format32bppArgb;
                 *  entry.Format = (int)TextureFormat.Rgba8;
                 *  pixelSize = 4;
                 * }
                 *
                 * var bmp = new Bitmap(origbmp.Width, origbmp.Height, bmpPixelFormat);
                 * using (Graphics gr = Graphics.FromImage(bmp))
                 * {
                 *  gr.DrawImage(origbmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
                 * }
                 *
                 * entry.Width = (ushort)bmp.Width;
                 * entry.Height = (ushort)bmp.Height;
                 *
                 * var scramble = new Texture().GetScrambledTextureData(bmp);
                 * var dataSize = bmp.Width * bmp.Height * pixelSize;
                 *
                 * entry.TextureRawData = new byte[dataSize];
                 * entry.TextureSize = (uint)dataSize;
                 * Array.Copy(scramble, entry.TextureRawData, dataSize);
                 */


                if (entry.Raw)
                {
                    using (BinaryReader bmp = new BinaryReader(File.Open(path, FileMode.Open)))
                    {
                        entry.TextureRawData = bmp.ReadBytes((int)bmp.BaseStream.Length);
                    }
                }
                else
                {
                    // Import image file
                    entry._textureData = new Texture();
                    var origbmp = Bitmap.FromFile(path);

                    var bmpPixelFormat = PixelFormat.Format24bppRgb;
                    if (entry.Format != (uint)TextureFormat.Rgba8 &&
                        entry.Format != (uint)TextureFormat.Rgb8 &&
                        entry.Format != (uint)TextureFormat.Rgb565 &&
                        entry.Format != (uint)TextureFormat.Rgba4 &&
                        entry.Format != (uint)TextureFormat.La8 &&
                        entry.Format != (uint)TextureFormat.Hilo8 &&
                        entry.Format != (uint)TextureFormat.L8 &&
                        entry.Format != (uint)TextureFormat.A8 &&
                        entry.Format != (uint)TextureFormat.Etc1 &&
                        entry.Format != (uint)TextureFormat.Etc1A4)
                    {
                        // Set everything that isn't one of the normal formats to Rgba8
                        entry.Format   = (uint)TextureFormat.Rgba8;
                        bmpPixelFormat = PixelFormat.Format32bppArgb;
                        entry.HasAlpha = true;
                    }
                    else if (entry.Format == (uint)TextureFormat.Rgba8 || entry.Format == (uint)TextureFormat.Rgba4 || entry.Format == (uint)TextureFormat.La8 || entry.Format == (uint)TextureFormat.A8)
                    {
                        bmpPixelFormat = PixelFormat.Format32bppArgb;
                        entry.HasAlpha = true;
                    }
                    else if (entry.Format == (uint)TextureFormat.Etc1A4)
                    {
                        bmpPixelFormat = PixelFormat.Format32bppArgb;
                        entry.HasAlpha = true;
                    }
                    else
                    {
                        bmpPixelFormat = PixelFormat.Format24bppRgb;
                        entry.HasAlpha = false;
                    }

                    var bmp = new Bitmap(origbmp.Width, origbmp.Height, bmpPixelFormat);
                    using (Graphics gr = Graphics.FromImage(bmp))
                    {
                        gr.DrawImage(origbmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
                    }

                    entry.Width  = (ushort)bmp.Width;
                    entry.Height = (ushort)bmp.Height;

                    entry.TextureRawData = Texture.FromBitmap(bmp, (TextureFormat)entry.Format, true);
                }

                entry.TextureSize = (uint)entry.TextureRawData.Length;
                entry.FileTime    = (uint)File.GetLastWriteTime(path).Ticks; // This is right exactly? Not sure, don't think it matters either

                var filenameData = Encoding.GetEncoding(932).GetBytes(entry.InternalFilePath);
                entry.FilenameHash = Crc32.Calculate(filenameData, filenameData.Length);

                return(entry);
            }
        }