예제 #1
0
파일: Program.cs 프로젝트: cyberis/KWADTool
        private static void ExtractBlobs(KWAD kwad, string outputPath)
        {
            var namedBlobList = (from aliasInfo in kwad.GetAliasInfoList()
                                 where kwad.GetResourceInfoList()[(int) aliasInfo.ResourceIdx].GetType().SequenceEqual(KLEIResource.SIGNATURE_KLEI_BLOB_4)
                                 select new {name = aliasInfo.AliasPath.GetString(), blob = kwad.GetResourceAt<KLEIBlob>((int) aliasInfo.ResourceIdx)}).ToList();

            Console.WriteLine("Extracting {0} blobs...", namedBlobList.Count);

            foreach (var namedBlob in namedBlobList)
            {
                Console.WriteLine(namedBlob.name);

                var path = Path.GetFullPath(Path.Combine(outputPath, namedBlob.name));

                var parentDirectory = Path.GetDirectoryName(path);
                if (!string.IsNullOrWhiteSpace(parentDirectory))
                {
                    Directory.CreateDirectory(parentDirectory);
                }

                using (var fileStream = File.Open(path, FileMode.Create))
                {
                    var blobData = namedBlob.blob.GetData();
                    fileStream.Write(blobData, 0, blobData.Length);
                }
            }
        }
예제 #2
0
파일: Program.cs 프로젝트: cyberis/KWADTool
        private static void ExtractTextures(KWAD kwad, string outputPath)
        {
            var namedTextureList = (from aliasInfo in kwad.GetAliasInfoList()
                                    where kwad.GetResourceInfoList()[(int) aliasInfo.ResourceIdx].GetType().SequenceEqual(KLEIResource.SIGNATURE_KLEI_TEXTURE_4)
                                    select new {name = aliasInfo.AliasPath.GetString(), texture = kwad.GetResourceAt<KLEITexture>((int) aliasInfo.ResourceIdx)}).ToList();

            Console.WriteLine("Extracting {0} textures...", namedTextureList.Count);

            var groupedNamedTexture = namedTextureList.GroupBy(namedTexture => namedTexture.texture.ParentSurfaceResourceIdx);
            foreach (var surfaceGroup in groupedNamedTexture)
            {
                var surfaceName = kwad.GetAliasInfoList()
                                      .First(aliasInfo => aliasInfo.ResourceIdx == surfaceGroup.Key)
                                      .AliasPath.GetString();

                Console.WriteLine(surfaceName + ":");

                var surface = kwad.GetResourceAt<KLEISurface>((int) surfaceGroup.Key);
                var mipmap = surface.GetMipmaps()[0];
                var imageData = DecompressMipmap(mipmap);

                if (surface.IsDXTCompressed)
                {
                    imageData = Squish.DecompressImage(imageData, (int) mipmap.Width, (int) mipmap.Height, SquishFlags.Dxt5);
                }

                RgbaToBgra(imageData);

                GCHandle pinnedImageData = GCHandle.Alloc(imageData, GCHandleType.Pinned);
                try
                {
                    IntPtr imageDataIntPtr = pinnedImageData.AddrOfPinnedObject();

                    using (Bitmap image = new Bitmap((int) mipmap.Width, (int) mipmap.Height, (int) mipmap.Width * 4, PixelFormat.Format32bppArgb, imageDataIntPtr))
                    {

                        foreach (var namedTexture in surfaceGroup)
                        {
                            var texture = namedTexture.texture;

                            Console.WriteLine("\t{0}", namedTexture.name);

                            var path = Path.GetFullPath(Path.Combine(outputPath, namedTexture.name));

                            var parentDirectory = Path.GetDirectoryName(path);
                            if (!string.IsNullOrWhiteSpace(parentDirectory))
                            {
                                Directory.CreateDirectory(parentDirectory);
                            }

                            using (var fileStream = File.Open(path, FileMode.Create))
                            {
                                // ReSharper disable CompareOfFloatsByEqualityOperator
                                if (texture.Affine2d.TranslateX == 0 && texture.Affine2d.TranslateY == 0 &&
                                    texture.Affine2d.C1R2 == 0 && texture.Affine2d.C2R1 == 0 &&
                                    texture.Affine2d.ScaleX == 1 && texture.Affine2d.ScaleY == 1)
                                {
                                    image.Save(fileStream, ImageFormat.Png);
                                }
                                else
                                {
                                    image.Clone(new RectangleF(texture.Affine2d.TranslateX * mipmap.Width, texture.Affine2d.TranslateY * mipmap.Height,
                                        texture.Width, texture.Height), image.PixelFormat).Save(fileStream, ImageFormat.Png);
                                }
                            }
                        }
                    }
                }
                finally
                {
                    pinnedImageData.Free();
                }
            }
        }
예제 #3
0
파일: Program.cs 프로젝트: Psimage/KWADTool
        private static void ExtractTextures(KWAD kwad, string outputPath)
        {
            var namedTextureList = (from aliasInfo in kwad.GetAliasInfoList()
                                    where kwad.GetResourceInfoList()[(int)aliasInfo.ResourceIdx].GetType().SequenceEqual(KLEITexture.KLEI_TYPE)
                                    select new { name = aliasInfo.AliasPath.GetString(), texture = kwad.GetResourceAt <KLEITexture>((int)aliasInfo.ResourceIdx) }).ToList();

            Console.WriteLine("Extracting {0} textures...", namedTextureList.Count);

            var groupedNamedTexture = namedTextureList.GroupBy(namedTexture => namedTexture.texture.ParentSurfaceResourceIdx);

            foreach (var surfaceGroup in groupedNamedTexture)
            {
                var surfaceName = kwad.GetAliasInfoList()
                                  .First(aliasInfo => aliasInfo.ResourceIdx == surfaceGroup.Key)
                                  .AliasPath.GetString();

                Console.WriteLine(surfaceName + ":");

                var surface   = kwad.GetResourceAt <KLEISurface>((int)surfaceGroup.Key);
                var mipmap    = surface.GetMipmaps()[0];
                var imageData = mipmap.CompressedSize > 0 ? DecompressMipmap(mipmap) : mipmap.GetData();

                if (surface.IsDXTCompressed)
                {
                    imageData = Squish.DecompressImage(imageData, (int)mipmap.Width, (int)mipmap.Height, SquishFlags.Dxt5);
                }

                RgbaToBgra(imageData);

                GCHandle pinnedImageData = GCHandle.Alloc(imageData, GCHandleType.Pinned);
                try
                {
                    IntPtr imageDataIntPtr = pinnedImageData.AddrOfPinnedObject();

                    using (Bitmap image = new Bitmap((int)mipmap.Width, (int)mipmap.Height, (int)mipmap.Width * 4, PixelFormat.Format32bppArgb, imageDataIntPtr))
                    {
                        foreach (var namedTexture in surfaceGroup)
                        {
                            var texture = namedTexture.texture;

                            Console.WriteLine("\t{0}", namedTexture.name);

                            var path = Path.GetFullPath(Path.Combine(outputPath, namedTexture.name.TrimStart(@"\/".ToCharArray())));

                            var parentDirectory = Path.GetDirectoryName(path);
                            if (!string.IsNullOrWhiteSpace(parentDirectory))
                            {
                                Directory.CreateDirectory(parentDirectory);
                            }

                            using (var fileStream = File.Open(path, FileMode.Create))
                            {
                                // ReSharper disable CompareOfFloatsByEqualityOperator
                                if (texture.Affine2d.TranslateX == 0 && texture.Affine2d.TranslateY == 0 &&
                                    texture.Affine2d.C1R2 == 0 && texture.Affine2d.C2R1 == 0 &&
                                    texture.Affine2d.ScaleX == 1 && texture.Affine2d.ScaleY == 1)
                                {
                                    image.Save(fileStream, ImageFormat.Png);
                                }
                                else
                                {
                                    image.Clone(new RectangleF(texture.Affine2d.TranslateX * mipmap.Width, texture.Affine2d.TranslateY * mipmap.Height,
                                                               texture.Width, texture.Height), image.PixelFormat).Save(fileStream, ImageFormat.Png);
                                }
                            }
                        }
                    }
                }
                finally
                {
                    pinnedImageData.Free();
                }
            }
        }
예제 #4
0
파일: Program.cs 프로젝트: Psimage/KWADTool
        private static void ExtractBlobs(KWAD kwad, string outputPath)
        {
            var namedBlobList = (from aliasInfo in kwad.GetAliasInfoList()
                                 where kwad.GetResourceInfoList()[(int)aliasInfo.ResourceIdx].GetType().SequenceEqual(KLEIBlob.KLEI_TYPE)
                                 select new { name = aliasInfo.AliasPath.GetString(), blob = kwad.GetResourceAt <KLEIBlob>((int)aliasInfo.ResourceIdx) }).ToList();

            Console.WriteLine("Extracting {0} blobs...", namedBlobList.Count);

            foreach (var namedBlob in namedBlobList)
            {
                Console.WriteLine(namedBlob.name);

                var path = Path.GetFullPath(Path.Combine(outputPath, namedBlob.name.TrimStart(@"\/".ToCharArray())));

                var parentDirectory = Path.GetDirectoryName(path);
                if (!string.IsNullOrWhiteSpace(parentDirectory))
                {
                    Directory.CreateDirectory(parentDirectory);
                }

                using (var fileStream = File.Open(path, FileMode.Create))
                {
                    var blobData = namedBlob.blob.GetData();
                    fileStream.Write(blobData, 0, blobData.Length);
                }
            }
        }
예제 #5
0
파일: Program.cs 프로젝트: Psimage/KWADTool
        private static void ExtractAnimationBuild(KWAD kwad, KLEIBuild animBld, string outputPath, string texturesPath)
        {
            if (animBld == null)
            {
                throw new ArgumentNullException("animBld");
            }

            Console.WriteLine("\tAnimation build \"{0}\":", animBld.Name.GetString());

            XmlDocument buildXml     = new XmlDocument();
            XmlElement  buildElement = buildXml.CreateElement("Build");

            buildElement.SetAttribute("name", animBld.Name.GetString());
            buildXml.AppendChild(buildElement);

            foreach (var symbol in animBld.GetSymbols())
            {
                Console.WriteLine("\t\tSymbol \"{0}\":", symbol.GetNameString());

                XmlElement symbolElement = buildXml.CreateElement("Symbol");
                symbolElement.SetAttribute("name", symbol.GetNameString());

                uint frameNum = 0;
                for (var frameIdx = (int)symbol.FrameIdx; frameIdx < symbol.FrameIdx + symbol.FrameCount; frameIdx++)
                {
                    Console.WriteLine("\t\t\tFrame {0}", frameIdx);

                    var frame = animBld.GetSymbolFrames()[frameIdx];

                    //Some frames have no references to the actual model.
                    if (frame.ModelResourceIdx == uint.MaxValue)
                    {
                        Console.WriteLine("\t\t\tSkipping Frame {0}: Model reference not found", frameIdx);
                        frameNum++;
                        continue;
                    }

                    XmlElement symbolFrameElement = buildXml.CreateElement("Frame");

                    symbolFrameElement.SetAttribute("framenum", frameNum.ToString());
                    symbolFrameElement.SetAttribute("duration", "1");

                    var model = kwad.GetResourceAt <KLEIModel>((int)frame.ModelResourceIdx);

                    var leftOffset = 0;
                    var topOffset  = 0;

                    //We only care about meshes that are "quads"
                    if (model.Mesh.VertexCount == 4)
                    {
                        var vertecies = model.Mesh.GetVertices();
                        if (IsRectangle(vertecies[0].X, vertecies[0].Y,
                                        vertecies[1].X, vertecies[1].Y,
                                        vertecies[2].X, vertecies[2].Y,
                                        vertecies[3].X, vertecies[3].Y))
                        {
                            //Since we already know it's a rectangle it is equivalent to getting that info from a top left vertex
                            leftOffset = (int)vertecies.Min(vertex => vertex.X);
                            topOffset  = (int)vertecies.Min(vertex => vertex.Y);
                        }
                    }

                    var imageRelativePath  = kwad.GetAliasInfoList().First(aliasInfo => aliasInfo.ResourceIdx == model.TextureResourceIdx).AliasPath.GetString();
                    var imageFullPath      = Path.GetFullPath(Path.Combine(texturesPath, imageRelativePath.TrimStart(@"\/".ToCharArray())));
                    var baseframeImageName = Path.GetFileNameWithoutExtension(imageRelativePath);
                    var frameImageName     = baseframeImageName;

                    int frameImageWidth;
                    int frameImageHeight;

                    using (var textureImage = Image.FromFile(imageFullPath))
                    {
                        frameImageWidth  = ((textureImage.Width % 2) == 0 ? textureImage.Width : (textureImage.Width + 1)) + leftOffset * 2;
                        frameImageHeight = ((textureImage.Height % 2) == 0 ? textureImage.Height : (textureImage.Height + 1)) + topOffset * 2;

                        using (var bitmap = new Bitmap(frameImageWidth, frameImageHeight, PixelFormat.Format32bppArgb))
                        {
                            bitmap.MakeTransparent();
                            using (var graphics = Graphics.FromImage(bitmap))
                            {
                                graphics.DrawImage(textureImage, leftOffset, topOffset);
                            }

                            int imagePostfix = 1;
                            do
                            {
                                var frameImagePath = Path.GetFullPath(Path.Combine(outputPath, frameImageName + ".png"));

                                if (File.Exists(frameImagePath))
                                {
                                    using (var img = Image.FromFile(frameImagePath))
                                    {
                                        if (img.Width == frameImageWidth && img.Height == frameImageHeight)
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            frameImageName = baseframeImageName + "-" + imagePostfix;
                                            imagePostfix++;
                                        }
                                    }
                                }
                                else
                                {
                                    bitmap.Save(frameImagePath, ImageFormat.Png);
                                    break;
                                }
                            } while (true);
                        }
                    }

                    symbolFrameElement.SetAttribute("image", frameImageName);
                    symbolFrameElement.SetAttribute("w", frameImageWidth.ToString());
                    symbolFrameElement.SetAttribute("h", frameImageHeight.ToString());

                    symbolFrameElement.SetAttribute("x", (frame.Affine3D.GetTx() + (float)frameImageWidth / 2).ToString(CultureInfo.InvariantCulture));
                    symbolFrameElement.SetAttribute("y", (frame.Affine3D.GetTy() + (float)frameImageHeight / 2).ToString(CultureInfo.InvariantCulture));

                    symbolElement.AppendChild(symbolFrameElement);
                    frameNum++;
                }

                buildElement.AppendChild(symbolElement);
            }

            buildXml.Save(Path.Combine(outputPath, "build.xml"));
        }
예제 #6
0
        private static void ExtractAnims(KWAD kwad, string outputBasePath, string texturesPath)
        {
            var animBundleList = (from aliasInfo in kwad.GetAliasInfoList()
                                  where kwad.GetResourceInfoList()[(int)aliasInfo.ResourceIdx].GetType().SequenceEqual(KLEIAnimation.KLEI_TYPE)
                                  select new
            {
                name = Path.GetFileNameWithoutExtension(aliasInfo.AliasPath.GetString()),
                path = Path.GetDirectoryName(aliasInfo.AliasPath.GetString()),
                animDef = kwad.GetResourceAt <KLEIAnimation>((int)aliasInfo.ResourceIdx),
                animBld = kwad.GetResourceByAlias <KLEIBuild>(Path.ChangeExtension(aliasInfo.AliasPath.GetString(), ".abld"))
            }).ToList();

            Console.WriteLine("Extracting {0} animation bundles...", animBundleList.Count);
            int buildCount = 0;
            int animCount  = 0;

            foreach (var animBundle in animBundleList)
            {
                if (animBundle.animBld != null)
                {
                    buildCount++;
                }
                if (animBundle.animDef != null)
                {
                    animCount++;
                }
            }

            Console.WriteLine("Extracting {0} animation builds...", buildCount);

            foreach (var animBundle in animBundleList)
            {
                //TODO: log any issues for a current animBundle to a corresponding log.txt file in that animBundle directory
                var animBuildOutputPath = Path.Combine(outputBasePath, animBundle.path, animBundle.name + ".anim");
                if (!string.IsNullOrWhiteSpace(animBuildOutputPath))
                {
                    Directory.CreateDirectory(animBuildOutputPath);
                }

                if (animBundle.animBld != null)
                {
                    ExtractAnimationBuild(kwad, animBundle.animBld, animBuildOutputPath, texturesPath);
                    //ZipFile.CreateFromDirectory(tempOutputPath, Path.Combine(Path.GetFullPath(outputPath), animBundle.path, animBundle.name + ".anmbdl"));
                }
            }

            Console.WriteLine("Extracting {0} animation definitions...", animCount);

            foreach (var animBundle in animBundleList)
            {
                //TODO: log any issues for a current animBundle to a corresponding log.txt file in that animBundle directory
                var animBuildOutputPath = Path.Combine(outputBasePath, animBundle.path, animBundle.name + ".anim");
                if (!string.IsNullOrWhiteSpace(animBuildOutputPath))
                {
                    Directory.CreateDirectory(animBuildOutputPath);
                }

                if (animBundle.animDef != null)
                {
                    Console.WriteLine(string.Format("\tAnimation Def \"{0}\"", animBundle.name + ".anim"));
                    ExtractAnimationDef(kwad, animBundle.animDef, animBuildOutputPath);
                }
            }
        }