示例#1
0
        public static DDSFile ReadFile(string fileName)
        {
            DDSFile file = new DDSFile();

            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) {
                byte[] buf    = new byte[Marshal.SizeOf(typeof(DDSHEAD))];
                int    length = fs.Read(buf, 0, Marshal.SizeOf(typeof(DDSHEAD)));
                if (length != Marshal.SizeOf(typeof(DDSHEAD)))
                {
                    throw new Exception("Could not read the whole header.");
                }
                file.header = (DDSHEAD)RawDeserialize(buf, typeof(DDSHEAD));

                file.SetSizeAndMipmapCount(file.header.dwWidth, file.header.dwHeight, file.header.dwMipMapCount);
                file.mipmapData = new byte[file.RequiredMipSizeAll];
                length          = fs.Read(file.mipmapData, 0, Convert.ToInt32(file.RequiredMipSizeAll));

                if (length != file.RequiredMipSizeAll)
                {
                    throw new Exception("Could not read the whole mipmaps.");
                }

                if (fs.Length != fs.Position)
                {
                    throw new Exception("There are rest bytes");
                }
            }

            return(file);
        }
示例#2
0
        private void updateUpkButton_Click(object sender, System.EventArgs e)
        {
            string upkPath = this.upkBox.Text;

            File.Copy(upkPath, upkPath + ".mod", true);
            UPK       upk       = new UPK(upkPath + ".mod");
            Hashtable nameTable = loadNameTable(this.nameTablePathBox2.Text);
            Texture2D texture   = new Texture2D(nameTable, this.texturePathBox2.Text);

            upk.ReplaceTexture(texture, DDSFile.ReadFile(this.newDDSBox.Text));
        }
示例#3
0
        internal void Save()
        {
            string  ddsFileName = path.Substring(0, path.Length - ".Texture2D".Length) + ".dds";
            DDSFile file        = new DDSFile();

            int[] mips = GetExportableBulkDataIndices();

            string format = (string)property["Format"];

            file.SetPixelFormat(format);
            file.SetSizeAndMipmapCount(Width, Height, (uint)mips.Length);

            for (int i = 0; i < mips.Length; i++)
            {
                file.SetData(GetExportableBulkData(mips[i]), i);
            }

            file.WriteFile(ddsFileName);
        }
示例#4
0
        internal void ReplaceTexture(Texture2D texture, DDSFile dds)
        {
            for (int mipId = 0; mipId < dds.MipmapCount; mipId++)
            {
                Mipmap mipmap = dds.GetMipmap(mipId);
                Texture2D.BulkDataInfo info = texture.FindCompressedDataHeader(mipmap);;
                if (info.Format == 16)
                {
                    mipmap.Compress();
                }

                if (mipmap.CompressedSize > info.CompressedSize)
                {
                    throw new Exception("New DDS is larger than the old one");
                }

                //fs.Seek(info.OffsetInUpk - 8, SeekOrigin.Begin);
                //writer.Write(Convert.ToInt32(mipmap.CompressedSize));
                fs.Seek(info.OffsetInUpk, SeekOrigin.Begin);
                writer.Write(mipmap.CompressedData);
                writer.Write(info.Footer);
            }
        }