Пример #1
0
        public static BMAP Load(string path, bool bDump = false)
        {
            FileInfo fi = new FileInfo(path);
            BMAP bmap = new BMAP();

            byte[] buff = new byte[134217728];
            int size = 0;

            using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(path)))
            using (BinaryReader br = new BinaryReader(ms))
            {
                if (!IsBMAP(br)) { return null; }

                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    int length = (int)br.ReadUInt32();

                    using (var lzs = new MemoryStream(br.ReadBytes(length)))
                    using (var lz4 = new LZ4Decompress(lzs))
                    {
                        size += lz4.Read(buff, size, buff.Length);
                    }
                }
            }

            if (bDump)
            {
                using (BinaryWriter bw = new BinaryWriter(new FileStream(path.Replace(".bmap", ".raw"), FileMode.Create)))
                {
                    bw.Write(buff, 0, size);
                }
            }

            using (MemoryStream ms = new MemoryStream(buff, 0, size))
            using (BinaryReader br = new BinaryReader(ms))
            {
                bmap.mode = (int)br.ReadUInt32();
                bmap.path = br.ReadString((int)br.ReadUInt32());
                int dataSize = (int)br.ReadUInt32();

                switch (bmap.mode)
                {
                    case 0:
                        bmap.dds = DDS.Load(br.ReadBytes(dataSize));
                        break;

                    case 1:
                        br.ReadUInt16();
                        br.ReadUInt16();
                        br.ReadUInt32();
                        br.ReadUInt32();
                        bmap.raw = new Bitmap(br.ReadUInt16(), br.ReadUInt16(), PixelFormat.Format32bppArgb);
                        br.ReadNullTerminatedString();

                        BitmapData bmpdata = bmap.raw.LockBits(new Rectangle(0, 0, bmap.raw.Width, bmap.raw.Height), ImageLockMode.ReadWrite, bmap.raw.PixelFormat);
                        dataSize = (int)(br.BaseStream.Length - br.BaseStream.Position);
                        Marshal.Copy(br.ReadBytes(dataSize), 0, bmpdata.Scan0, dataSize);
                        break;
                }
            }

            return bmap;
        }
Пример #2
0
        public static BMAP Load(string path, bool dump = false)
        {
            BMAP bmap = new BMAP();

            byte[] buff = new byte[536870912];
            int    size = 0;

            using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(path)))
                using (BinaryReader br = new BinaryReader(ms))
                {
                    if (!IsBMAP(br))
                    {
                        return(null);
                    }

                    while (br.BaseStream.Position < br.BaseStream.Length)
                    {
                        int length = (int)br.ReadUInt32();

                        using (MemoryStream lzs = new MemoryStream(br.ReadBytes(length)))
                            using (LZ4Decompress lz4 = new LZ4Decompress(lzs))
                            {
                                size += lz4.Read(buff, size, buff.Length);
                            }
                    }
                }

            if (dump)
            {
                using (BinaryWriter bw = new BinaryWriter(new FileStream(path.Replace(".bmap", ".raw"), FileMode.Create)))
                {
                    bw.Write(buff, 0, size);
                }
            }

            using (MemoryStream ms = new MemoryStream(buff, 0, size))
                using (BinaryReader br = new BinaryReader(ms))
                {
                    bmap.Mode = (int)br.ReadUInt32();
                    bmap.Path = br.ReadString((int)br.ReadUInt32());
                    int dataSize = (int)br.ReadUInt32();

                    switch (bmap.Mode)
                    {
                    case 0:
                        bmap.DDS = DDS.Load(br.ReadBytes(dataSize));
                        break;

                    case 1:
                        br.ReadUInt16();
                        br.ReadUInt16();
                        br.ReadUInt32();
                        br.ReadUInt32();
                        bmap.Raw = new Bitmap(br.ReadUInt16(), br.ReadUInt16(), PixelFormat.Format32bppArgb);
                        br.ReadNullTerminatedString();

                        BitmapData bmpdata = bmap.Raw.LockBits(new Rectangle(0, 0, bmap.Raw.Width, bmap.Raw.Height), ImageLockMode.ReadWrite, bmap.Raw.PixelFormat);
                        dataSize = (int)(br.BaseStream.Length - br.BaseStream.Position);
                        Marshal.Copy(br.ReadBytes(dataSize), 0, bmpdata.Scan0, dataSize);
                        break;
                    }
                }

            return(bmap);
        }
Пример #3
0
        private static string processFile(string path)
        {
            BMAP   bmap      = new BMAP();
            string extension = Path.GetExtension(path).Substring(1);
            string outputName;

            if (settings.Raw)
            {
                if (Raw.IsValid(path))
                {
                    Console.WriteLine($"Loading  : {Path.GetFileName(path)}");
                    Raw.Load(path, true);

                    return(path);
                }
            }
            else if (settings.Compress)
            {
                if (Enum.TryParse(extension, out WreckfestExtensions we))
                {
                    using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(path)))
                        using (BinaryReader br = new BinaryReader(ms))
                        {
                            if (
                                br.ReadInt32() != 4 ||
                                br.ReadByte() != extension[3] ||
                                br.ReadByte() != extension[2] ||
                                br.ReadByte() != extension[1] ||
                                br.ReadByte() != extension[0])
                            {
                                br.BaseStream.Position = 0;
                                byte[] input = br.ReadBytes((int)ms.Length);

                                File.Move(path, $"{path}.bak");

                                using (BinaryWriter bw = new BinaryWriter(new FileStream(path, FileMode.Create)))
                                {
                                    bw.Write(4);
                                    bw.Write(extension[3]);
                                    bw.Write(extension[2]);
                                    bw.Write(extension[1]);
                                    bw.Write(extension[0]);
                                    bw.Write((int)we);

                                    int[]  hashTable = new int[1 << (14 - 2)];
                                    byte[] output    = new byte[LZ4Compress.CalculateChunkSize(input.Length)];
                                    int    i         = 0;

                                    while (i < input.Length)
                                    {
                                        byte[] chunk = new byte[Math.Min(input.Length - i, output.Length)];

                                        Array.Copy(input, i, chunk, 0, chunk.Length);
                                        Array.Clear(hashTable, 0, hashTable.Length);

                                        int size = LZ4Compress.Compress(hashTable, chunk, output, chunk.Length, chunk.Length + 4);

                                        bw.Write(size);
                                        bw.Write(output, 0, size);

                                        i += chunk.Length;
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine($"Skipping : {Path.GetFileName(path)} is already compressed");
                            }
                        }
                }
                else
                {
                    Console.WriteLine($"Error    : unsupported extension '{extension}'.");
                }
            }
            else if (extension == "bmap")
            {
                if (BMAP.IsBMAP(path))
                {
                    Console.WriteLine($"Loading  : {Path.GetFileName(path)}");
                    bmap = BMAP.Load(path, false);

                    outputName = $"{Path.GetFileNameWithoutExtension(path)}.{(bmap.Mode == 1 ? "clutter" : (bmap.DDS.Format == D3DFormat.A8R8G8B8 ? "raw" : bmap.DDS.Format.ToString().ToLower()))}.{settings.SaveAs}";

                    Console.WriteLine($"Saving   : {outputName}");
                    if (!overwrite($@"{Path.GetDirectoryName(path)}\{outputName}"))
                    {
                        return(null);
                    }
                    bmap.SaveAs(outputName, settings.SaveAs);

                    return(path);
                }
            }
            else if (extension == "dds")
            {
                string newExtension = $"{(settings.NoRename ? "" : ".x")}.bmap";

                Console.WriteLine($"Loading  : {Path.GetFileName(path)}");
                Console.WriteLine($"Saving   : {Path.GetFileNameWithoutExtension(path)}{newExtension}");

                bmap.Path = Path.GetFileName(path);
                bmap.DDS  = DDS.Load(path);
                if (!overwrite(path.Replace(".dds", newExtension)))
                {
                    return(null);
                }
                bmap.Save(path.Replace(".dds", newExtension));

                return(path);
            }
            else if (Array.IndexOf(new string[] { "png", "tga", "tif" }, extension) > -1)
            {
                Texture texture = null;
                Console.WriteLine($"Loading   : {Path.GetFileName(path)}");

                switch (extension)
                {
                case "tga":
                    texture = TGA.Load(path);
                    break;

                case "tif":
                case "png":
                    texture = Texture.Load(path);
                    break;
                }

                BreckfestSettings original = settings.Clone();

                if (Path.GetFileNameWithoutExtension(path).EndsWith(".clutter", StringComparison.OrdinalIgnoreCase))
                {
                    settings.Clutter = true;
                    path             = path.Replace(".clutter", "");
                }
                else if (Path.GetFileNameWithoutExtension(path).EndsWith(".raw", StringComparison.OrdinalIgnoreCase))
                {
                    settings.Format = D3DFormat.A8R8G8B8;
                    path            = path.Replace(".raw", "");
                }
                else if (Path.GetFileNameWithoutExtension(path).EndsWith(".dxt1", StringComparison.OrdinalIgnoreCase))
                {
                    settings.Format = D3DFormat.DXT1;
                    path            = path.Replace(".dxt1", "");
                }
                else if (Path.GetFileNameWithoutExtension(path).EndsWith(".dxt5", StringComparison.OrdinalIgnoreCase))
                {
                    settings.Format = D3DFormat.DXT5;
                    path            = path.Replace(".dxt5", "");
                }

                bmap.Path = Path.GetFileName(path);

                if (settings.Clutter)
                {
                    Console.WriteLine($"Cluttering: {texture.Bitmap.Width}x{texture.Bitmap.Height}");

                    bmap.Mode = 1;
                    bmap.Raw  = texture.Bitmap;
                }
                else
                {
                    if (settings.Format == D3DFormat.A8R8G8B8)
                    {
                        Console.WriteLine($"Formatting: {texture.Bitmap.Width}x{texture.Bitmap.Height} (this might take awhile)");
                    }
                    else
                    {
                        Console.WriteLine($"Squishing : {texture.Bitmap.Width}x{texture.Bitmap.Height} (this might take awhile)");
                    }

                    bmap.DDS = new DDS(settings.Format, texture.Bitmap);
                }

                string newExtension = $"{(settings.NoRename ? "" : ".x")}.bmap";

                Console.WriteLine($"Saving    : {Path.GetFileNameWithoutExtension(path)}{newExtension}");
                if (!overwrite(path.Replace($".{extension}", $"{newExtension}")))
                {
                    return(null);
                }
                bmap.Save(path.Replace($".{extension}", $"{newExtension}"), true);

                settings = original.Clone();

                return(path);
            }

            return(null);
        }
Пример #4
0
        private static string processFile(string path)
        {
            BMAP bmap = new BMAP();
            string extension = Path.GetExtension(path).Substring(1);
            string outputName = "";

            if (settings.Raw)
            {
                if (Raw.IsValid(path))
                {
                    Console.WriteLine("Loading  : {0}", Path.GetFileName(path));
                    Raw.Load(path, true);

                    return path;
                }
            }
            else if (settings.Compress)
            {
                WreckfestExtensions we;

                if (Enum.TryParse<WreckfestExtensions>(extension, out we))
                {
                    using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(path)))
                    using (BinaryReader br = new BinaryReader(ms))
                    {
                        if (
                            br.ReadInt32() != 4 ||
                            br.ReadByte() != extension[3] ||
                            br.ReadByte() != extension[2] ||
                            br.ReadByte() != extension[1] ||
                            br.ReadByte() != extension[0])
                        {
                            br.BaseStream.Position = 0;
                            var input = br.ReadBytes((int)ms.Length);

                            File.Move(path, path + ".bak");

                            using (BinaryWriter bw = new BinaryWriter(new FileStream(path, FileMode.Create)))
                            {
                                bw.Write(4);
                                bw.Write(extension[3]);
                                bw.Write(extension[2]);
                                bw.Write(extension[1]);
                                bw.Write(extension[0]);
                                bw.Write((int)we);

                                var hashTable = new int[1 << (14 - 2)];
                                var output = new byte[LZ4Compress.CalculateChunkSize(input.Length)];
                                int i = 0;

                                while (i < input.Length)
                                {
                                    byte[] chunk = new byte[Math.Min(input.Length - i, output.Length)];

                                    Array.Copy(input, i, chunk, 0, chunk.Length);
                                    Array.Clear(hashTable, 0, hashTable.Length);

                                    int size = LZ4Compress.Compress(hashTable, chunk, output, chunk.Length, chunk.Length + 4);

                                    bw.Write(size);
                                    bw.Write(output, 0, size);

                                    i += chunk.Length;
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("Skipping : {0} is already compressed", Path.GetFileName(path));
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Error    : unsupported extension '{0}'.", extension);
                }
            }
            else if (extension == "bmap")
            {
                if (BMAP.IsBMAP(path))
                {
                    Console.WriteLine("Loading  : {0}", Path.GetFileName(path));
                    bmap = BMAP.Load(path, false);

                    outputName = string.Format("{0}.{1}.png", Path.GetFileNameWithoutExtension(path), (bmap.Mode == 1 ? "clutter" : (bmap.DDS.Format == D3DFormat.A8R8G8B8 ? "raw" : bmap.DDS.Format.ToString().ToLower())));

                    Console.WriteLine("Saving   : {0}", outputName);
                    if (!Overwrite(Path.GetFullPath(path.Replace(Path.GetFileName(path), outputName)))) { return null; }
                    bmap.SaveAsPNG(outputName);

                    return path;
                }
            }
            else if (extension == "dds")
            {
                Console.WriteLine("Loading  : {0}", Path.GetFileName(path));
                bmap.Path = Path.GetFileName(path);
                bmap.DDS = DDS.Load(path);

                if (!settings.NoRename)
                {

                    if (!Overwrite(Path.GetFullPath(path.Replace(".dds", ".x.bmap")))) { return null; }
                    Console.WriteLine("Saving   : {0}", Path.GetFileName(path.Replace(".dds", ".x.bmap")));
                    bmap.Save(path.Replace(".dds", ".x.bmap"));
                }
                else
                {
                    if (!Overwrite(Path.GetFullPath(path.Replace(".dds", ".bmap")))) { return null; }
                    Console.WriteLine("Saving   : {0}", Path.GetFileName(path.Replace(".dds", ".bmap")));
                    bmap.Save(path.Replace(".dds", ".bmap"));
                }

                return path;
            }
            else if (Array.IndexOf(new string[] { "png", "tga", "tif" }, extension) > -1)
            {
                Texture texture = null;
                Console.WriteLine("Loading   : {0}", Path.GetFileName(path));

                switch (extension)
                {
                    case "png":
                        texture = PNG.Load(path);
                        break;

                    case "tga":
                        texture = TGA.Load(path);
                        break;

                    case "tif":
                        texture = TIF.Load(path);
                        break;
                }

                BreckfestSettings original = settings.Clone();

                if (Path.GetFileNameWithoutExtension(path).EndsWith(".clutter", StringComparison.OrdinalIgnoreCase))
                {
                    settings.Clutter = true;
                    path = path.Replace(".clutter", "");
                }
                else if (Path.GetFileNameWithoutExtension(path).EndsWith(".raw", StringComparison.OrdinalIgnoreCase))
                {
                    settings.Format = D3DFormat.A8R8G8B8;
                    path = path.Replace(".raw", "");
                }
                else if (Path.GetFileNameWithoutExtension(path).EndsWith(".dxt1", StringComparison.OrdinalIgnoreCase))
                {
                    settings.Format = D3DFormat.DXT1;
                    path = path.Replace(".dxt1", "");
                }
                else if (Path.GetFileNameWithoutExtension(path).EndsWith(".dxt5", StringComparison.OrdinalIgnoreCase))
                {
                    settings.Format = D3DFormat.DXT5;
                    path = path.Replace(".dxt5", "");
                }

                bmap.Path = Path.GetFileName(path);

                if (settings.Clutter)
                {
                    Console.WriteLine("Cluttering: {0}x{1}", texture.Bitmap.Width, texture.Bitmap.Height);

                    bmap.Mode = 1;
                    bmap.Raw = texture.Bitmap;
                }
                else
                {
                    if (settings.Format == D3DFormat.A8R8G8B8)
                    {
                        Console.WriteLine("Formatting: {0}x{1} (this might take awhile)", texture.Bitmap.Width, texture.Bitmap.Height);
                    }
                    else
                    {
                        Console.WriteLine("Squishing : {0}x{1} (this might take awhile)", texture.Bitmap.Width, texture.Bitmap.Height);
                    }

                    bmap.DDS = new DDS(settings.Format, texture.Bitmap);
                }

                if (!settings.NoRename)
                {

                    if (!Overwrite(Path.GetFullPath(path.Replace(string.Format(".{0}", extension), ".x.bmap")))) { return null; }
                    Console.WriteLine("Saving    : {0}", Path.GetFileName(path.Replace(string.Format(".{0}", extension), ".x.bmap")));

                    bmap.Save(path.Replace(string.Format(".{0}", extension), ".x.bmap"), true);
                }
                else
                {
                    if (!Overwrite(Path.GetFullPath(path.Replace(string.Format(".{0}", extension), ".bmap")))) { return null; }
                    Console.WriteLine("Saving    : {0}", Path.GetFileName(path.Replace(string.Format(".{0}", extension), ".bmap")));
                    bmap.Save(path.Replace(string.Format(".{0}", extension), ".bmap"), true);
                }
                settings = original.Clone();

                return path;
            }

            return null;
        }