Exemple #1
0
        public override void Export(Asset asset, string path)
        {
            Texture texture = (asset as Texture);
            TDX     tdx     = (texture.SupportingDocuments["Source"] as TDX);

            Stopwatch sw = new Stopwatch();

            sw.Start();

            if (tdx == null)
            {
                SceneManager.Current.UpdateProgress($"Saving {texture.Name}");

                tdx = TDX.LoadFromBitmap(texture.SupportingDocuments["Source"] as Bitmap, texture.Name, settings.GetSetting <D3DFormat>("Format"));
                tdx.SetFlags(TDX.Flags.sRGB);
            }

            //tdx.Save(Path.Combine(path, $"{texture.Name}.tdx"));
            tdx.Save(path);

            sw.Stop();
            Console.WriteLine(sw.Elapsed);

            SceneManager.Current.UpdateProgress($"{texture.Name}.tdx saved!");
        }
Exemple #2
0
 public TDX LoadTDXFromZADEntry(ZADEntry entry, ZAD zadFile)
 {
     using (MemoryStream stream = new MemoryStream(zadFile.ExtractToBuffer(entry)))
     {
         TDX output = TDX.Load(stream, entry.Name);
         return(output);
     }
 }
Exemple #3
0
        public override void Export(Asset asset, string path)
        {
            var texture = (asset as Texture);
            var tdx     = (texture.SupportingDocuments["Source"] as TDX);

            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();

            if (tdx == null)
            {
                tdx = new TDX();

                var b = (texture.SupportingDocuments["Source"] as Bitmap);

                SceneManager.Current.UpdateProgress(string.Format("Saving {0}", texture.Name));

                var flags = Squish.SquishFlags.kDxt1;

                tdx.Name   = texture.Name;
                tdx.Format = settings.GetSetting <D3DFormat>("Format");

                switch (tdx.Format)
                {
                case D3DFormat.DXT1:
                    flags = Squish.SquishFlags.kDxt1;
                    break;

                case D3DFormat.DXT5:
                    flags = Squish.SquishFlags.kDxt5;
                    break;
                }

                var mip = new ToxicRagers.Generics.MipMap();
                mip.Width  = b.Width;
                mip.Height = b.Height;

                byte[] data = new byte[b.Width * b.Height * 4];
                byte[] dest = new byte[Squish.Squish.GetStorageRequirements(b.Width, b.Height, flags | Squish.SquishFlags.kColourIterativeClusterFit | Squish.SquishFlags.kWeightColourByAlpha)];

                var bmpdata = b.LockBits(new Rectangle(0, 0, mip.Width, mip.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                Marshal.Copy(bmpdata.Scan0, data, 0, data.Length);
                b.UnlockBits(bmpdata);

                SceneManager.Current.UpdateProgress(string.Format("Compressing {0} (this may take a moment)", texture.Name));
                Squish.Squish.CompressImage(data, b.Width, b.Height, ref dest, flags | Squish.SquishFlags.kColourClusterFit);
                mip.Data = dest;

                tdx.MipMaps.Add(mip);
            }

            tdx.Save(Path.GetDirectoryName(path) + "\\" + texture.Name + ".tdx");

            sw.Stop();
            Console.WriteLine(sw.Elapsed);

            SceneManager.Current.UpdateProgress(string.Format("{0}.tdx saved!", texture.Name));
        }
Exemple #4
0
        public override Asset Import(string path)
        {
            Texture texture = new Texture
            {
                FileName = path
            };

            TDX tdx = TDX.Load(path);

            texture.SetData(tdx.Name, tdx.Format.ToString(), tdx.MipMaps[0].Width, tdx.MipMaps[0].Height, tdx.MipMaps[0].Data);
            texture.SupportingDocuments["Source"] = tdx;


            return(texture);
        }
        public override Asset Import(string path)
        {
            Texture texture = new Texture();

            texture.FileName = path;

            if (string.Compare(Path.GetExtension(path), ".tdx", true) == 0)
            {
                TDX tdx = TDX.Load(path);
                texture.SetData(tdx.Name, tdx.Format.ToString(), tdx.MipMaps[0].Width, tdx.MipMaps[0].Height, tdx.MipMaps[0].Data);
                texture.SupportingDocuments["Source"] = tdx;
            }
            else
            {
                texture = (Texture)(new IMGImporter()).Import(path);
            }

            return(texture);
        }
Exemple #6
0
        public VirtualTextureDef LoadVT(string inputFolder)
        {
            var zadFiles = Directory.EnumerateFiles(inputFolder, "*.zad");
            //bool firstZad = true;
            ZAD EnvironmentsZAD = ZAD.Load(Path.Combine(inputFolder, "Environments.zad"));
            TDX DiffuseTDX      = null;
            TDX SpecularTDX     = null;
            TDX NormalTDX       = null;

            //Console.WriteLine("Loading dictionary TDX files...");
            foreach (ZADEntry entry in EnvironmentsZAD.Contents)
            {
                //Console.WriteLine(entry.Name);
                switch (Path.GetFileName(entry.Name).ToLower())
                {
                case "diffuse_d.tdx":
                    DiffuseTDX = LoadTDXFromZADEntry(entry, EnvironmentsZAD);
                    break;

                case "specular_s.tdx":
                    SpecularTDX = LoadTDXFromZADEntry(entry, EnvironmentsZAD);
                    break;

                case "normal_n.tdx":
                    NormalTDX = LoadTDXFromZADEntry(entry, EnvironmentsZAD);
                    break;
                }
            }

            VirtualTextureDef vtDef = new VirtualTextureDef
            {
                DiffuseMap   = (VTMap)DiffuseTDX.ExtraData,
                SpecularMap  = (VTMap)SpecularTDX.ExtraData,
                NormalMap    = (VTMap)NormalTDX.ExtraData,
                DiffusePages = new List <VTPage>()
            };

            for (int i = 0; i < vtDef.DiffuseMap.PageCount + 1; i++)
            {
                int pageWidth  = vtDef.DiffuseMap.GetWidth(i);
                int pageHeight = vtDef.DiffuseMap.GetHeight(i);
                vtDef.DiffusePages.Add(new VTPage(pageWidth, pageHeight, i, vtDef.DiffuseMap));
                //Console.WriteLine("\tDiffuse Page {0} created", i);
            }

            vtDef.SpecularPages = new List <VTPage>();

            for (int i = 0; i < vtDef.SpecularMap.PageCount + 1; i++)
            {
                int pageWidth  = vtDef.SpecularMap.GetWidth(i);
                int pageHeight = vtDef.SpecularMap.GetHeight(i);
                vtDef.SpecularPages.Add(new VTPage(pageWidth, pageHeight, i, vtDef.SpecularMap));
                //Console.WriteLine("\tSpecular Page {0} created", i);
            }

            vtDef.NormalPages = new List <VTPage>();

            for (int i = 0; i < vtDef.NormalMap.PageCount + 1; i++)
            {
                int pageWidth  = vtDef.NormalMap.GetWidth(i);
                int pageHeight = vtDef.NormalMap.GetHeight(i);
                vtDef.NormalPages.Add(new VTPage(pageWidth, pageHeight, i, vtDef.NormalMap));
                //Console.WriteLine("\tNormal Page {0} created", i);
            }

            foreach (string zadFile in zadFiles)
            {
                if (Path.GetFileNameWithoutExtension(zadFile).ToLower() == "environments")
                {
                    continue;
                }
                //Console.Write("Loading ZAD: " + zadFile);

                /*if(Path.GetFileNameWithoutExtension(zadFile).ToLower() == "pages_5")
                 * {
                 *  Console.WriteLine("This is page 5");
                 * }*/
                ZAD currentZAD = ZAD.Load(zadFile);

                foreach (ZADEntry entry in currentZAD.Contents)
                {
                    if (entry.CompressionMethod != CompressionMethods.LZ4)
                    {
                        //Console.WriteLine("This entry isnt compressed using lz4! wtf? {0}", entry.Name);
                    }

                    string tdxName  = Path.GetFileNameWithoutExtension(entry.Name).ToLower();
                    string tileName = tdxName.Split(new Char[] { '_' })[0].ToUpper();

                    /*if (tileName == "E4C7607E")
                     * {
                     *  Console.WriteLine("This is E4C7607E");
                     * }*/
                    if (vtDef.DiffuseMap.TilesByName.ContainsKey(tileName))
                    {
                        VTMapTileTDX tileTDX = vtDef.DiffuseMap.TilesByName[tileName];
                        //tileTDX.Texture = LoadTDXFromZADEntry(entry, currentZAD);
                        tileTDX.ZADFile          = zadFile;
                        tileTDX.ZADEntryLocation = entry.Name;
                        for (int i = 0; i < tileTDX.Coords.Count; i++)
                        {
                            VTMapTile tile = tileTDX.Coords[i];
                            tile.TDXTile = tileTDX;
                            //if (tile.Row < diffusePages[tile.Page].maxTilesToStitch && tile.Column < diffusePages[tile.Page].maxTilesToStitch)
                            {
                                vtDef.DiffusePages[tile.Page].Tiles[tile.Row][tile.Column] = tileTDX;// LoadTDXFromZADEntry(entry, currentZAD);
                            }
                        }
                    }

                    if (vtDef.SpecularMap.TilesByName.ContainsKey(tileName))
                    {
                        VTMapTileTDX tileTDX = vtDef.SpecularMap.TilesByName[tileName];
                        //tileTDX.Texture = LoadTDXFromZADEntry(entry, currentZAD);
                        tileTDX.ZADFile          = zadFile;
                        tileTDX.ZADEntryLocation = entry.Name;

                        for (int i = 0; i < tileTDX.Coords.Count; i++)
                        {
                            VTMapTile tile = tileTDX.Coords[i];
                            tile.TDXTile = tileTDX;

                            if (tile.Row < vtDef.SpecularPages[tile.Page].maxTilesToStitch && tile.Column < vtDef.SpecularPages[tile.Page].maxTilesToStitch)
                            {
                                vtDef.SpecularPages[tile.Page].Tiles[tile.Row][tile.Column] = tileTDX;// LoadTDXFromZADEntry(entry, currentZAD);
                            }
                        }
                    }

                    if (vtDef.NormalMap.TilesByName.ContainsKey(tileName))
                    {
                        //currentZAD.Extract(entry, Path.Combine(outputFolder, "Normal", "TDX")+"/");
                        VTMapTileTDX tileTDX = vtDef.NormalMap.TilesByName[tileName];
                        //tileTDX.Texture = LoadTDXFromZADEntry(entry, currentZAD);
                        tileTDX.ZADFile          = zadFile;
                        tileTDX.ZADEntryLocation = entry.Name;
                        for (int i = 0; i < tileTDX.Coords.Count; i++)
                        {
                            VTMapTile tile = tileTDX.Coords[i];
                            tile.TDXTile = tileTDX;
                            if (tile.Row < vtDef.NormalPages[tile.Page].maxTilesToStitch && tile.Column < vtDef.NormalPages[tile.Page].maxTilesToStitch)
                            {
                                vtDef.NormalPages[tile.Page].Tiles[tile.Row][tile.Column] = tileTDX;// LoadTDXFromZADEntry(entry, currentZAD);
                            }
                        }
                    }
                }

                continue;

                foreach (ZADEntry entry in currentZAD.Contents)
                {
                    if (entry.CompressionMethod != CompressionMethods.LZ4)
                    {
                        //Console.WriteLine("This entry isnt compressed using lz4! wtf? {0}", entry.Name);
                    }

                    string tdxName  = Path.GetFileNameWithoutExtension(entry.Name).ToLower();
                    string tileName = tdxName.Split(new Char[] { '_' })[0].ToUpper();

                    /*if (tileName == "E4C7607E")
                     * {
                     *  Console.WriteLine("This is E4C7607E");
                     * }*/
                    if (vtDef.DiffuseMap.TilesByName.ContainsKey(tileName))
                    {
                        VTMapTileTDX tileTDX = vtDef.DiffuseMap.TilesByName[tileName];
                        //tileTDX.Texture = LoadTDXFromZADEntry(entry, currentZAD);
                        tileTDX.ZADFile          = zadFile;
                        tileTDX.ZADEntryLocation = entry.Name;
                        for (int i = 0; i < tileTDX.Coords.Count; i++)
                        {
                            VTMapTile tile = tileTDX.Coords[i];
                            tile.TDXTile = tileTDX;
                            //if (tile.Row < diffusePages[tile.Page].maxTilesToStitch && tile.Column < diffusePages[tile.Page].maxTilesToStitch)
                            {
                                vtDef.DiffusePages[tile.Page].Tiles[tile.Row][tile.Column] =
                                    tileTDX; // LoadTDXFromZADEntry(entry, currentZAD);
                            }
                        }
                    }

                    if (vtDef.SpecularMap.TilesByName.ContainsKey(tileName))
                    {
                        VTMapTileTDX tileTDX = vtDef.SpecularMap.TilesByName[tileName];
                        //tileTDX.Texture = LoadTDXFromZADEntry(entry, currentZAD);
                        tileTDX.ZADFile          = zadFile;
                        tileTDX.ZADEntryLocation = entry.Name;
                        for (int i = 0; i < tileTDX.Coords.Count; i++)
                        {
                            VTMapTile tile = tileTDX.Coords[i];
                            tile.TDXTile = tileTDX;
                            if (tile.Row < vtDef.SpecularPages[tile.Page].maxTilesToStitch &&
                                tile.Column < vtDef.SpecularPages[tile.Page].maxTilesToStitch)
                            {
                                vtDef.SpecularPages[tile.Page].Tiles[tile.Row][tile.Column] =
                                    tileTDX; // LoadTDXFromZADEntry(entry, currentZAD);
                            }
                        }
                    }

                    if (vtDef.NormalMap.TilesByName.ContainsKey(tileName))
                    {
                        //currentZAD.Extract(entry, Path.Combine(outputFolder, "Normal", "TDX")+"/");
                        VTMapTileTDX tileTDX = vtDef.NormalMap.TilesByName[tileName];
                        //tileTDX.Texture = LoadTDXFromZADEntry(entry, currentZAD);
                        tileTDX.ZADFile          = zadFile;
                        tileTDX.ZADEntryLocation = entry.Name;
                        for (int i = 0; i < tileTDX.Coords.Count; i++)
                        {
                            VTMapTile tile = tileTDX.Coords[i];
                            tile.TDXTile = tileTDX;
                            if (tile.Row < vtDef.NormalPages[tile.Page].maxTilesToStitch &&
                                tile.Column < vtDef.NormalPages[tile.Page].maxTilesToStitch)
                            {
                                vtDef.NormalPages[tile.Page].Tiles[tile.Row][tile.Column] =
                                    tileTDX; // LoadTDXFromZADEntry(entry, currentZAD);
                            }
                        }
                    }
                }
            }

            return(vtDef);
        }
Exemple #7
0
        public void SaveTexture(VTMapEntry textureEntry, string outputPath, bool SaveTDX, bool SaveTGA, bool SaveOther, ImageFormat format)
        {
            int divisor = GetDivisor();
            int xPos    = PageNum == 0 ? textureEntry.Column : textureEntry.Column / divisor;
            int yPos    = PageNum == 0 ? textureEntry.Row : textureEntry.Row / divisor;
            int tileRow = (int)Math.Floor(yPos / (float)TileSize);
            int tileCol = (int)Math.Floor(xPos / (float)TileSize);

            tileRow = tileRow < 0 ? 0 : tileRow;
            tileCol = tileCol < 0 ? 0 : tileCol;

            int numTilesX = (int)Math.Ceiling(textureEntry.GetWidth(PageNum) / (float)TileSize);
            int numTilesY = (int)Math.Ceiling(textureEntry.GetHeight(PageNum) / (float)TileSize);

            int    bitmapheight = textureEntry.GetHeight(PageNum);
            int    bitmapwidth  = textureEntry.GetWidth(PageNum);
            Bitmap stitched     = new Bitmap(bitmapwidth, bitmapheight);

            D3DFormat guessedFormat = D3DFormat.DXT5;
            bool      formatGuessed = false;

            for (int row = tileRow; row < Tiles.Count && row <= tileRow + numTilesY; row++)
            {
                int rowStart = row * TileSize;
                int rowEnd   = (row + 1) * TileSize;

                for (int col = tileCol; col < Tiles[row].Count && col <= tileCol + numTilesX; col++)
                {
                    if (Tiles[row][col] == null)
                    {
                        continue;
                    }

                    int colStart = col * TileSize;
                    int colEnd   = (col + 1) * TileSize;

                    if (Tiles[row][col].Texture == null)
                    {
                        Tiles[row][col].GetTextureFromZAD();
                    }
                    if (Tiles[row][col].Texture == null)
                    {
                        continue;
                    }
                    if (!formatGuessed)
                    {
                        guessedFormat = Tiles[row][col].Texture.Format;
                    }

                    Bitmap decompressed = Tiles[row][col].Texture.GetBitmap();

                    int firstPixelRow = rowStart < yPos ? yPos - rowStart : TilePadding;
                    int lastPixelRow  = TileSize + TilePadding;

                    int firstPixelCol = colStart < xPos ? xPos - colStart : TilePadding;
                    int lastPixelCol  = TileSize + TilePadding;

                    for (int y = firstPixelRow; y < lastPixelRow; y++)
                    {
                        if ((row * TileSize + (y - TilePadding)) - (yPos) >= bitmapheight)
                        {
                            break;
                        }
                        for (int x = firstPixelCol; x < lastPixelCol; x++)
                        {
                            if ((col * TileSize + (x - TilePadding)) - xPos >= bitmapwidth)
                            {
                                break;
                            }

                            Color pixelColour = decompressed.GetPixel(x, y);

                            stitched.SetPixel((col * TileSize + (x - TilePadding)) - xPos, (row * TileSize + (y - TilePadding)) - (yPos), pixelColour);
                        }
                    }
                }
            }

            if (!Directory.Exists(Path.GetDirectoryName(outputPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
            }

            if (SaveTDX)
            {
                TDX newtdx = TDX.LoadFromBitmap(stitched, Path.GetFileNameWithoutExtension(outputPath), guessedFormat);
                newtdx.Save(outputPath);
            }

            if (SaveTGA)
            {
                if (File.Exists(outputPath))
                {
                    File.Delete(outputPath);
                }

                using (FileStream stream = new FileStream(outputPath, FileMode.OpenOrCreate))
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        int tgaWidth        = bitmapwidth;
                        int tgaHeight       = bitmapheight;
                        int tgaTileRowCount = Tiles.Count - 1;

                        writer.Write((byte)0);
                        writer.Write((byte)0);
                        writer.Write((byte)2);
                        writer.Write((short)0);
                        writer.Write((short)0);
                        writer.Write((byte)0);
                        writer.Write((short)0);
                        writer.Write((short)0);
                        writer.Write((short)bitmapwidth);
                        writer.Write((short)bitmapheight);
                        writer.Write((byte)32);
                        writer.Write((byte)0);
                        BitmapData data = stitched.LockBits(new Rectangle(0, 0, stitched.Width, stitched.Height), ImageLockMode.ReadOnly, stitched.PixelFormat);
                        unsafe
                        {
                            // important to use the BitmapData object's Width and Height
                            // properties instead of the Bitmap's.
                            for (int x = data.Height - 1; x >= 0; x--)
                            {
                                byte *row = (byte *)data.Scan0 + (x * data.Stride);
                                for (int y = 0; y < data.Width; y++)
                                {
                                    int  columnOffset = y * 4;
                                    byte B            = row[columnOffset];
                                    byte G            = row[columnOffset + 1];
                                    byte R            = row[columnOffset + 2];
                                    byte alpha        = row[columnOffset + 3];
                                    writer.Write(B);
                                    writer.Write(G);
                                    writer.Write(R);
                                    writer.Write(alpha);
                                }
                            }
                        }
                        stitched.UnlockBits(data);
                    }
            }

            if (SaveOther)
            {
                using (FileStream str = File.OpenWrite(outputPath))
                {
                    stitched.Save(str, format);
                }
            }

            stitched.Dispose();
        }
Exemple #8
0
        public List <VTMapTileTDX> ImportTexture(Bitmap image, VTMapEntry textureEntry)
        {
            List <VTMapTileTDX> output = new List <VTMapTileTDX>();

            int divisor = GetDivisor();
            int xPos    = PageNum == 0 ? textureEntry.Column : textureEntry.Column / divisor;
            int yPos    = PageNum == 0 ? textureEntry.Row : textureEntry.Row / divisor;
            int tileRow = (int)Math.Floor(yPos / (float)TileSize);
            int tileCol = (int)Math.Floor(xPos / (float)TileSize);

            tileRow = tileRow < 0 ? 0 : tileRow;
            tileCol = tileCol < 0 ? 0 : tileCol;

            int numTilesX = (int)Math.Ceiling(textureEntry.GetWidth(PageNum) / (float)TileSize);
            int numTilesY = (int)Math.Ceiling(textureEntry.GetHeight(PageNum) / (float)TileSize);

            int bitmapheight = textureEntry.GetHeight(PageNum);
            int bitmapwidth  = textureEntry.GetWidth(PageNum);

            for (int row = tileRow; row < Tiles.Count && row <= tileRow + numTilesY; row++)
            {
                int rowStart = row * TileSize;
                int rowEnd   = (row + 1) * TileSize;

                for (int col = tileCol; col < Tiles[row].Count && col <= tileCol + numTilesX; col++)
                {
                    if (Tiles[row][col] == null)
                    {
                        continue;
                    }

                    int colStart = col * TileSize;
                    int colEnd   = (col + 1) * TileSize;

                    if (Tiles[row][col].Texture == null)
                    {
                        Tiles[row][col].GetTextureFromZAD();
                    }
                    if (Tiles[row][col].Texture == null)
                    {
                        continue;
                    }

                    Bitmap decompressed = Tiles[row][col].Texture.GetBitmap();

                    int firstPixelRow = rowStart < yPos ? yPos - rowStart : 0;
                    int lastPixelRow  = TileSize + TilePadding + TilePadding;

                    int firstPixelCol = colStart < xPos ? xPos - colStart : 0;
                    int lastPixelCol  = TileSize + TilePadding + TilePadding;

                    for (int y = firstPixelRow; y < lastPixelRow; y++)
                    {
                        if ((row * TileSize + (y - TilePadding)) - (yPos) >= bitmapheight + TilePadding)
                        {
                            break;
                        }

                        for (int x = firstPixelCol; x < lastPixelCol; x++)
                        {
                            if ((col * TileSize + (x - TilePadding)) - xPos >= bitmapwidth + TilePadding)
                            {
                                break;
                            }

                            int originalX = (col * TileSize + (x - TilePadding)) - xPos;
                            int originalY = (row * TileSize + (y - TilePadding)) - (yPos);

                            if (originalX < 0)
                            {
                                originalX = 0;
                            }
                            else if (originalX >= bitmapwidth)
                            {
                                originalX = bitmapwidth - 1;
                            }

                            if (originalY < 0)
                            {
                                originalY = 0;
                            }
                            else if (originalY >= bitmapheight)
                            {
                                originalY = bitmapheight - 1;
                            }

                            Color colour = image.GetPixel(originalX, originalY);
                            decompressed.SetPixel(x, y, colour);
                        }
                    }

                    TDX newTileTDX = TDX.LoadFromBitmap(decompressed, Tiles[row][col].Texture.Name, Tiles[row][col].Texture.Format);
                    newTileTDX.Flags        = TDX.TDXFlags.AlphaNbit | TDX.TDXFlags.DoNot16bit;
                    Tiles[row][col].Texture = newTileTDX;
                    output.Add(Tiles[row][col]);
                }
            }

            return(output);
        }
Exemple #9
0
        void LoadVT(string inputFolder)
        {
            var  zadFiles        = Directory.EnumerateFiles(inputFolder, "*.zad");
            bool firstZad        = true;
            ZAD  EnvironmentsZAD = ZAD.Load(Path.Combine(inputFolder, "Environments.zad"));
            TDX  DiffuseTDX      = null;
            TDX  SpecularTDX     = null;
            TDX  NormalTDX       = null;

            Console.WriteLine("Loading dictionary TDX files...");
            foreach (ZADEntry entry in EnvironmentsZAD.Contents)
            {
                Console.WriteLine(entry.Name);
                switch (Path.GetFileName(entry.Name).ToLower())
                {
                case "diffuse_d.tdx":
                    DiffuseTDX = LoadTDXFromZADEntry(entry, EnvironmentsZAD);
                    break;

                case "specular_s.tdx":
                    SpecularTDX = LoadTDXFromZADEntry(entry, EnvironmentsZAD);
                    break;

                case "normal_n.tdx":
                    NormalTDX = LoadTDXFromZADEntry(entry, EnvironmentsZAD);
                    break;
                }
            }

            DiffuseMap = (crVTMap)DiffuseTDX.ExtraData;

            SpecularMap = (crVTMap)SpecularTDX.ExtraData;

            NormalMap = (crVTMap)NormalTDX.ExtraData;

            textureListSource.DataSource = DiffuseMap.Entries;
            TextureList.DataSource       = textureListSource;
            diffusePages = new List <crVTPage>();
            for (int i = 0; i < DiffuseMap.PageCount + 1; i++)
            {
                int pageWidth  = DiffuseMap.GetWidth(i);
                int pageHeight = DiffuseMap.GetHeight(i);
                diffusePages.Add(new crVTPage(pageWidth, pageHeight, i, DiffuseMap));
                Console.WriteLine("\tDiffuse Page {0} created", i);
            }
            specularPages = new List <crVTPage>();
            for (int i = 0; i < SpecularMap.PageCount + 1; i++)
            {
                int pageWidth  = SpecularMap.GetWidth(i);
                int pageHeight = SpecularMap.GetHeight(i);
                specularPages.Add(new crVTPage(pageWidth, pageHeight, i, SpecularMap));
                Console.WriteLine("\tDiffuse Page {0} created", i);
            }
            normalPages = new List <crVTPage>();
            for (int i = 0; i < NormalMap.PageCount + 1; i++)
            {
                int pageWidth  = NormalMap.GetWidth(i);
                int pageHeight = NormalMap.GetHeight(i);
                normalPages.Add(new crVTPage(pageWidth, pageHeight, i, NormalMap));
                Console.WriteLine("\tDiffuse Page {0} created", i);
            }
            if (PageType.SelectedItem == "Diffuse")
            {
                pageNumSource.DataSource = diffusePages;
            }
            else if (PageType.SelectedItem == "Specular")
            {
                pageNumSource.DataSource = specularPages;
            }
            else if (PageType.SelectedItem == "Normal")
            {
                pageNumSource.DataSource = normalPages;
            }
            PageNumSelect.SelectedIndex = 1;

            foreach (string zadFile in zadFiles)
            {
                if (Path.GetFileNameWithoutExtension(zadFile).ToLower() == "environments")
                {
                    continue;
                }
                //Console.Write("Loading ZAD: " + zadFile);

                /*if(Path.GetFileNameWithoutExtension(zadFile).ToLower() == "pages_5")
                 * {
                 *  Console.WriteLine("This is page 5");
                 * }*/
                ZAD currentZAD = ZAD.Load(zadFile);

                foreach (ZADEntry entry in currentZAD.Contents)
                {
                    if (entry.CompressionMethod != CompressionMethods.LZ4)
                    {
                        //Console.WriteLine("This entry isnt compressed using lz4! wtf? {0}", entry.Name);
                    }
                    string tdxName  = Path.GetFileNameWithoutExtension(entry.Name).ToLower();
                    string tileName = tdxName.Split(new Char[] { '_' })[0].ToUpper();

                    /*if (tileName == "E4C7607E")
                     * {
                     *  Console.WriteLine("This is E4C7607E");
                     * }*/
                    if (DiffuseMap.TilesByName.ContainsKey(tileName))
                    {
                        crVTMapTileTDX tileTDX = DiffuseMap.TilesByName[tileName];
                        //tileTDX.Texture = LoadTDXFromZADEntry(entry, currentZAD);
                        tileTDX.ZADFile          = zadFile;
                        tileTDX.ZADEntryLocation = entry.Name;
                        for (int i = 0; i < tileTDX.Coords.Count; i++)
                        {
                            crVTMapTile tile = tileTDX.Coords[i];
                            tile.TDXTile = tileTDX;
                            //if (tile.Row < diffusePages[tile.Page].maxTilesToStitch && tile.Column < diffusePages[tile.Page].maxTilesToStitch)
                            {
                                diffusePages[tile.Page].Tiles[tile.Row][tile.Column] = tileTDX;// LoadTDXFromZADEntry(entry, currentZAD);
                            }
                        }
                    }
                    if (SpecularMap.TilesByName.ContainsKey(tileName))
                    {
                        crVTMapTileTDX tileTDX = SpecularMap.TilesByName[tileName];
                        //tileTDX.Texture = LoadTDXFromZADEntry(entry, currentZAD);
                        tileTDX.ZADFile          = zadFile;
                        tileTDX.ZADEntryLocation = entry.Name;
                        for (int i = 0; i < tileTDX.Coords.Count; i++)
                        {
                            crVTMapTile tile = tileTDX.Coords[i];
                            tile.TDXTile = tileTDX;
                            if (tile.Row < specularPages[tile.Page].maxTilesToStitch && tile.Column < specularPages[tile.Page].maxTilesToStitch)
                            {
                                specularPages[tile.Page].Tiles[tile.Row][tile.Column] = tileTDX;// LoadTDXFromZADEntry(entry, currentZAD);
                            }
                        }
                    }
                    if (NormalMap.TilesByName.ContainsKey(tileName))
                    {
                        //currentZAD.Extract(entry, Path.Combine(outputFolder, "Normal", "TDX")+"/");
                        crVTMapTileTDX tileTDX = NormalMap.TilesByName[tileName];
                        //tileTDX.Texture = LoadTDXFromZADEntry(entry, currentZAD);
                        tileTDX.ZADFile          = zadFile;
                        tileTDX.ZADEntryLocation = entry.Name;
                        for (int i = 0; i < tileTDX.Coords.Count; i++)
                        {
                            crVTMapTile tile = tileTDX.Coords[i];
                            tile.TDXTile = tileTDX;
                            if (tile.Row < normalPages[tile.Page].maxTilesToStitch && tile.Column < normalPages[tile.Page].maxTilesToStitch)
                            {
                                normalPages[tile.Page].Tiles[tile.Row][tile.Column] = tileTDX;// LoadTDXFromZADEntry(entry, currentZAD);
                            }
                        }
                    }
                }

                if (firstZad && 1 == 0)
                {
                    firstZad = false;
                    foreach (var vtentry in DiffuseMap.Entries)
                    {
                        bool breakloops = false;
                        foreach (var vtpage in diffusePages)
                        {
                            var tiles = vtpage.GetTiles(vtentry);
                            foreach (var vttile in tiles)
                            {
                                if (vttile.TDXTile.ZADFile == zadFile)
                                {
                                    Logger.LogToFile(Logger.LogLevel.Debug, "{0} = {1} (Diffuse / Page {2})", vttile.TDXTile.ZADEntryLocation, vtentry.FileName, vttile.Page);
                                    // breakloops = true;
                                    //break;
                                }
                            }
                            //if (breakloops) break;
                        }
                        //if (breakloops) break;
                    }

                    foreach (var vtentry in SpecularMap.Entries)
                    {
                        bool breakloops = false;
                        foreach (var vtpage in specularPages)
                        {
                            var tiles = vtpage.GetTiles(vtentry);
                            foreach (var vttile in tiles)
                            {
                                if (vttile.TDXTile.ZADFile == zadFile)
                                {
                                    Logger.LogToFile(Logger.LogLevel.Debug, "{0} = {1} (Specular / Page {2})", vttile.TDXTile.ZADEntryLocation, vtentry.FileName, vttile.Page);
                                    //breakloops = true;
                                    //break;
                                }
                            }
                            //if (breakloops) break;
                        }
                        //if (breakloops) break;
                    }

                    foreach (var vtentry in NormalMap.Entries)
                    {
                        bool breakloops = false;
                        foreach (var vtpage in normalPages)
                        {
                            var tiles = vtpage.GetTiles(vtentry);
                            foreach (var vttile in tiles)
                            {
                                if (vttile.TDXTile.ZADFile == zadFile)
                                {
                                    Logger.LogToFile(Logger.LogLevel.Debug, "{0} = {1} (Normal / Page {2})", vttile.TDXTile.ZADEntryLocation, vtentry.FileName, vttile.Page);
                                    //breakloops = true;
                                    //break;
                                }
                            }
                            // if (breakloops) break;
                        }
                        //if (breakloops) break;
                    }
                }
                //Thread zadThread = new Thread(Program.ExtractZADContent);
                //zadThread.Start(currentZAD);
                //Threads.Add(zadThread);
                //ThreadsAlive++;
                //break;
            }
            TextureList.SetSelected(0, true);
        }
Exemple #10
0
        void ReplaceSelectedTexture_Click(object sender, EventArgs e)
        {
            var            textureToReplace = (crVTMapEntry)TextureList.SelectedItem;
            OpenFileDialog dialog           = new OpenFileDialog();

            dialog.InitialDirectory = Properties.Settings.Default.LastImportDirectory;
            dialog.Title            = "Export Texture";
            dialog.Filter           = "PNG Image (*.PNG)|*.png|JPEG Image (*.JPG, *.JPEG)|*.jpg;*.jpeg|BMP Image (*.BMP)|*.bmp|TIFF Image (*.TIF, *.TIFF)|*.tif;*.tiff|TGA Image (*.TGA)|*.tga|TDX Texture (*.TDX)|*.tdx";
            dialog.FileName         = Path.GetFileName(textureToReplace.FileName);
            var result = dialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK && Directory.Exists(Path.GetDirectoryName(dialog.FileName)))
            {
                Properties.Settings.Default.LastImportDirectory = Path.GetDirectoryName(dialog.FileName);
                Properties.Settings.Default.Save();
                var         vtPage    = (crVTPage)PageNumSelect.SelectedItem;
                var         fileType  = Path.GetExtension(dialog.FileName).ToUpper();
                ImageFormat imgFormat = ImageFormat.Png;
                Bitmap      image     = null;
                if (fileType == ".TGA")
                {
                    image = Paloma.TargaImage.LoadTargaImage(dialog.FileName);
                }
                else if (fileType == ".TDX")
                {
                    image = TDX.Load(dialog.FileName).Decompress();
                }
                else
                {
                    image = (Bitmap)Bitmap.FromFile(dialog.FileName);
                }
                //if (image.Width != textureToReplace.GetWidth(vtPage.PageNum) || image.Height != textureToReplace.GetHeight(vtPage.PageNum)) MessageBox.Show("Error: Image dimensions need to match the original texture!", "Texture Size Mismatch", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //else
                {
                    List <crVTPage> pages = null;
                    if (PageType.SelectedItem == "Diffuse")
                    {
                        pages = diffusePages;
                    }
                    else if (PageType.SelectedItem == "Specular")
                    {
                        pages = specularPages;
                    }
                    else if (PageType.SelectedItem == "Normal")
                    {
                        pages = normalPages;
                    }
                    foreach (var page in pages)
                    {
                        if (page.PageNum == 0)
                        {
                            continue;
                        }
                        int    targetWidth  = textureToReplace.GetWidth(page.PageNum);
                        int    targetHeight = textureToReplace.GetHeight(page.PageNum);
                        Bitmap mipimage     = null;
                        if (image.Width != targetWidth || image.Height != targetHeight)
                        {
                            mipimage = new Bitmap(targetWidth, targetHeight);
                            var      srcRect  = new RectangleF(0, 0, image.Width, image.Height);
                            var      destRect = new RectangleF(0, 0, targetWidth, targetHeight);
                            Graphics grfx     = Graphics.FromImage(mipimage);
                            grfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                            grfx.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);
                        }
                        else
                        {
                            mipimage = image;
                        }
                        var tiles = page.ImportTexture(mipimage, textureToReplace);
                        foreach (var tile in tiles)
                        {
                            ZAD      currentZAD = ZAD.Load(tile.ZADFile);
                            ZADEntry tileEntry  = currentZAD.Contents.Find(possibleEntry => possibleEntry.Name == tile.ZADEntryLocation);
                            currentZAD.ReplaceEntryFromBuffer(tileEntry, tile.Texture.SaveToBuffer());
                        }

                        /*foreach(var tile in tiles)
                         * {
                         *  using (ZipArchive zip = ZipFile.Open(tile.ZADFile, ZipArchiveMode.Update))
                         *  {
                         *
                         *      var zipentry = zip.GetEntry(tile.ZADEntryLocation);
                         *      zipentry.Delete();
                         *      zipentry = zip.CreateEntry(tile.ZADEntryLocation, CompressionLevel.NoCompression);
                         *      using (Stream stream = zipentry.Open())
                         *      {
                         *          tile.Texture.Save(stream);
                         *      }
                         *
                         *  }
                         * }*/
                    }
                }
            }
        }