- using System;
- using System.Linq;
- using System.Windows.Forms;
- using System.IO;
- using System.Diagnostics;
- using Ionic.Zip;
- using Microsoft.Win32;
- using System.Security.Cryptography;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- string MW3Directory = String.Empty;
- private void button1_Click(object sender, EventArgs e)
- {
- OpenFileDialog openImage = new OpenFileDialog();
- openImage.Filter = "Compatible formats|*.tga;*.bmp;*.gif;*.ppm;*.jpg;*.tif;*.cel;*.dds;*.png;*.psd;*.rgb;*.bw;*.rgba";
- DialogResult result = openImage.ShowDialog();
- if (result == DialogResult.OK) { filePath.Text = openImage.FileName; }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- MW3Directory = MW3Path();
- string iwiFile = textBox1.Text;
- string settings = String.Empty;
- string ddsFormat = String.Empty;
- string[] iwiDB = File.ReadAllLines("iwidb.idf");
- string iwdFile = guessIWD(iwiDB, iwiFile);
- using (ZipFile zip1 = ZipFile.Read(MW3Directory + iwdFile))
- {
- ZipEntry iwiInZip = zip1[@"images\" + iwiFile];
- iwiInZip.Extract("temp", ExtractExistingFileAction.OverwriteSilently);
- }
- Crc32 crc32 = new Crc32();
- String hash = String.Empty;
- using (FileStream fs = File.Open(@"temp\images\" + iwiFile, FileMode.Open))
- foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();
- byte[] iwiFileBytes = File.ReadAllBytes(@"temp\images\" + iwiFile);
- byte[] unknown1 = { iwiFileBytes[4], iwiFileBytes[5], iwiFileBytes[6], iwiFileBytes[7] };
- byte unknown2 = iwiFileBytes[9];
- byte[] unknown3 = { iwiFileBytes[14], iwiFileBytes[15] };
- if (checkBox2.Checked)
- {
- File.Copy(@"temp\images\" + iwiFile, iwiFile.Replace(".iwi", ".bak"), true);
- }
- if (Directory.Exists("temp")) { Directory.Delete("temp", true); }
- byte ddsType = 0x00;
- if (radioButton1.Checked) { ddsType = iwiFileBytes[8]; }
- else
- {
- if (comboBox1.SelectedIndex == 0) { ddsType = 0x01; }
- else if (comboBox1.SelectedIndex == 1) { ddsType = 0x02; }
- else if (comboBox1.SelectedIndex == 2) { ddsType = 0x03; }
- else if (comboBox1.SelectedIndex == 3) { ddsType = 0x04; }
- else if (comboBox1.SelectedIndex == 4) { ddsType = 0x0B; }
- else if (comboBox1.SelectedIndex == 5) { ddsType = 0x0C; }
- else if (comboBox1.SelectedIndex == 6) { ddsType = 0x0D; }
- }
- if (radioButton1.Checked)
- {
- if (iwiFileBytes[8] == 0x01) { ddsFormat = "-u8888"; }
- else if (iwiFileBytes[8] == 0x02) { ddsFormat = "-u888"; }
- else if (iwiFileBytes[8] == 0x03) { ddsFormat = "-A8L8"; }
- else if (iwiFileBytes[8] == 0x04) { ddsFormat = "-a8"; }
- else if (iwiFileBytes[8] == 0x0B) { ddsFormat = "-dxt1c"; }
- else if (iwiFileBytes[8] == 0x0C) { ddsFormat = "-dxt3"; }
- else if (iwiFileBytes[8] == 0x0D) { ddsFormat = "-dxt5"; }
- }
- else
- {
- if (comboBox1.SelectedIndex == 0) { ddsFormat = "-u8888"; }
- else if (comboBox1.SelectedIndex == 1) { ddsFormat = "-u888"; }
- else if (comboBox1.SelectedIndex == 2) { ddsFormat = "-A8L8"; }
- else if (comboBox1.SelectedIndex == 3) { ddsFormat = "-a8"; }
- else if (comboBox1.SelectedIndex == 4) { ddsFormat = "-dxt1c"; }
- else if (comboBox1.SelectedIndex == 5) { ddsFormat = "-dxt3"; }
- else if (comboBox1.SelectedIndex == 6) { ddsFormat = "-dxt5"; }
- }
- if (File.Exists("batchddsprocess_iwi.bat")) { File.Delete("batchddsprocess_iwi.bat"); }
- settings = "nvdxt.exe -file \"" + filePath.Text + "\" " + ddsFormat + " -nmips 1 -outdir \"" + Directory.GetCurrentDirectory() + "\"";
- if (checkBox1.Checked) { settings += " -prescale " + resolution1.Text + " " + resolution2.Text; }
- StreamWriter batch = new StreamWriter("batchddsprocess_iwi.bat");
- batch.WriteLine("@echo off");
- batch.WriteLine(settings);
- batch.Close();
- ProcessStartInfo batchInfo = new ProcessStartInfo("batchddsprocess_iwi.bat");
- Process batchProcess = new Process();
- batchProcess.StartInfo = batchInfo;
- batchProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
- batchProcess.Start();
- batchProcess.WaitForExit();
- if (File.Exists("batchddsprocess_iwi.bat")) { File.Delete("batchddsprocess_iwi.bat"); }
- byte[] oldArray = File.ReadAllBytes(Path.GetFileNameWithoutExtension(filePath.Text) + ".dds");
- byte[] newArray = new byte[oldArray.Length - 96];
- Buffer.BlockCopy(oldArray, 96, newArray, 0, newArray.Length);
- string res1string = String.Empty;
- string res2string = String.Empty;
- if (!checkBox1.Checked)
- {
- byte[] res2hex = { oldArray[12], oldArray[13] };
- byte[] res1hex = { oldArray[16], oldArray[17] };
- Array.Reverse(res1hex);
- Array.Reverse(res2hex);
- res1string = BitConverter.ToString(res1hex).Replace("-", "");
- res2string = BitConverter.ToString(res2hex).Replace("-", "");
- res1string = hex2Decimal(res1string);
- res2string = hex2Decimal(res2string);
- }
- else
- {
- res1string = resolution1.Text;
- res2string = resolution2.Text;
- }
- byte[] iwiCookie = { 0x49, 0x57, 0x69, 0x08 };
- byte[] res1IWI = { 0x00, 0x00 };
- byte[] res2IWI = { 0x00, 0x00 };
- Array.Copy(BitConverter.GetBytes(Int16.Parse(res1string)), res1IWI, 2);
- Array.Copy(BitConverter.GetBytes(Int16.Parse(res2string)), res2IWI, 2);
- byte[] secondLine = { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 };
- int fileSize = newArray.Length;
- int count = 0;
- for (int i = 0; i < 4; i++)
- {
- Array.Copy(BitConverter.GetBytes(fileSize), 0, secondLine, count, 4);
- count += 4;
- }
- byte[] iwiHeader = { iwiCookie[0], iwiCookie[1], iwiCookie[2], iwiCookie[3], unknown1[0], unknown1[1], unknown1[2], unknown1[3], ddsType, unknown2, res1IWI[0], res1IWI[1], res2IWI[0], res2IWI[1], unknown3[0], unknown3[1], secondLine[0], secondLine[1], secondLine[2], secondLine[3], secondLine[4], secondLine[5], secondLine[6], secondLine[7], secondLine[8], secondLine[9], secondLine[10], secondLine[11], secondLine[12], secondLine[13], secondLine[14], secondLine[15] };
- iwiHeader.CopyTo(newArray, 0);
- File.WriteAllBytes(Path.GetFileNameWithoutExtension(filePath.Text) + ".iwi", newArray);
- if (File.Exists(Path.GetFileNameWithoutExtension(filePath.Text) + ".dds")) { File.Delete(Path.GetFileNameWithoutExtension(filePath.Text) + ".dds"); }
- File.Copy(Path.GetFileNameWithoutExtension(filePath.Text) + ".iwi", iwiFile, true);
- if (File.Exists(Path.GetFileNameWithoutExtension(filePath.Text) + ".iwi")) { File.Delete(Path.GetFileNameWithoutExtension(filePath.Text) + ".iwi"); }
- Crc32Fix.FixChecksum(newArray, newArray.Length, newArray.Length - 4, Convert.ToUInt32(hash, 16));
- File.WriteAllBytes(iwiFile, newArray);
- using (var zip = ZipFile.Read(MW3Directory + iwdFile))
- {
- zip.RemoveEntry(@"images\" + iwiFile);
- zip.AddFile(iwiFile, @"images");
- zip.Save();
- }
- if (File.Exists(iwiFile)) { File.Delete(iwiFile); }
- }
- private void checkBox1_CheckedChanged(object sender, EventArgs e)
- {
- if (resolution1.Enabled) { resolution1.Enabled = false; }
- else { resolution1.Enabled = true; }
- if (resolution2.Enabled) { resolution2.Enabled = false; }
- else { resolution2.Enabled = true; }
- if (label2.Enabled) { label2.Enabled = false; }
- else { label2.Enabled = true; }
- }
- public static string hex2Decimal(string hexvalue)
- {
- string binaryval = String.Empty;
- binaryval = Convert.ToString(Convert.ToInt32(hexvalue, 16), 10);
- return binaryval;
- }
- private void radioButton1_CheckedChanged(object sender, EventArgs e)
- {
- if (checkBox2.Enabled) { checkBox2.Enabled = false; }
- else { checkBox2.Enabled = true; }
- if (comboBox1.Enabled) { comboBox1.Enabled = false; }
- else { comboBox1.Enabled = true; }
- if (label3.Enabled) { label3.Enabled = false; }
- else { label3.Enabled = true; }
- }
- private void checkBox2_CheckedChanged(object sender, EventArgs e)
- {
- }
- private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
- {
- Process.Start("http://www.itsmods.com/forum/index.php");
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- try
- {
- comboBox1.SelectedIndex = 0;
- if (File.Exists("iwidb.idf"))
- {
- string[] iwiDataBase = File.ReadAllLines("iwidb.idf");
- int count = 0;
- AutoCompleteStringCollection data = new AutoCompleteStringCollection();
- while (count != iwiDataBase.Length)
- {
- if (iwiDataBase[count].Contains(".iwi"))
- {
- data.Add(iwiDataBase[count]);
- }
- count++;
- }
- textBox1.AutoCompleteCustomSource = data;
- }
- else
- {
- DialogResult result = MessageBox.Show("Cant find the IWI database! Do you want to create it?", "Error", MessageBoxButtons.YesNo);
- if (result == DialogResult.Yes)
- {
- createIWIDB();
- string[] iwiDataBase = File.ReadAllLines("iwidb.idf");
- int count = 0;
- AutoCompleteStringCollection data = new AutoCompleteStringCollection();
- while (count != iwiDataBase.Length)
- {
- if (iwiDataBase[count].Contains(".iwi"))
- {
- data.Add(iwiDataBase[count]);
- }
- count++;
- }
- textBox1.AutoCompleteCustomSource = data;
- }
- }
- }
- catch (Exception x)
- {
- MessageBox.Show(x.ToString());
- }
- Activate();
- }
- void createIWIDB()
- {
- try
- {
- MW3Directory = MW3Path();
- if (MW3Directory != null)
- {
- if (Directory.Exists(MW3Directory))
- {
- string[] filePaths = Directory.GetFiles(MW3Directory, "*.iwd");
- using (StreamWriter sw = new StreamWriter("iwidb.idf"))
- {
- sw.WriteLine("[iwi database file]");
- sw.WriteLine("[do not modify unless you want corrupt shit]");
- sw.WriteLine();
- sw.WriteLine();
- for (int i = 0; i != filePaths.Length; i++)
- {
- sw.WriteLine("[[" + Path.GetFileName(filePaths[i]) + "]]");
- sw.WriteLine("");
- using (var zip = ZipFile.Read(filePaths[i]))
- {
- foreach (ZipEntry zipEntry in zip.Entries)
- {
- if (zipEntry.FileName != "fileSysCheck.cfg")
- {
- string file = zipEntry.FileName;
- if (file.EndsWith(".iwi")) { sw.WriteLine(file.Replace("images/", "")); }
- }
- }
- }
- }
- }
- }
- else { MessageBox.Show("You have selected the wrong folder. IWI database not created", "Error", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); }
- }
- else { MessageBox.Show("You didnt select any folder. IWI database not created", "Error", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); }
- }
- catch (Exception x)
- {
- MessageBox.Show(x.ToString());
- }
- }
- private string MW3Path()
- {
- try
- {
- bool cfgCorrupt = false;
- RegistryKey regKey = Registry.CurrentUser;
- regKey = regKey.OpenSubKey(@"Software\Valve\Steam");
- bool canFindSteamPath = false;
- string steamInstallPath;
- string MW3Directory = String.Empty;
- if (regKey != null)
- {
- steamInstallPath = regKey.GetValue("SteamPath").ToString();
- MW3Directory = steamInstallPath + "/steamapps/common/call of duty modern warfare 3";
- if (File.Exists(MW3Directory + "/main/iw_00.iwd")) { canFindSteamPath = true; }
- else { canFindSteamPath = false; }
- }
- else { canFindSteamPath = false; }
- if (!canFindSteamPath)
- {
- if (File.Exists("settings.cfg"))
- {
- string cfgPath = String.Empty;
- try { cfgPath = GetStringFromTo(File.ReadAllLines("settings.cfg"), "MW3 Directory = ", " ;"); }
- catch (Exception)
- {
- cfgCorrupt = true;
- if (File.Exists("settings.cfg")) { File.Delete("settings.cfg"); }
- MessageBox.Show("File \"settings.cfg\" corrupt, please select the MW3 folder");
- FolderBrowserDialog MW3Folder = new FolderBrowserDialog();
- DialogResult result = MW3Folder.ShowDialog();
- if (result == DialogResult.OK) { MW3Directory = MW3Folder.SelectedPath; }
- else { MW3Directory = null; }
- if (File.Exists(MW3Directory + "/main/iw_00.iwd")) { File.WriteAllText("settings.cfg", "MW3 Directory = " + MW3Directory + " ;"); }
- }
- if (File.Exists("settings.cfg")) { if (File.Exists(cfgPath + "/main/iw_00.iwd")) { MW3Directory = cfgPath; } }
- if (File.Exists("settings.cfg"))
- {
- if (!File.Exists(cfgPath + "/main/iw_00.iwd"))
- {
- if (!cfgCorrupt)
- {
- if (File.Exists("settings.cfg")) { File.Delete("settings.cfg"); }
- MessageBox.Show("The folder in \"settings.cfg\" is wrong, please select the MW3 folder");
- FolderBrowserDialog MW3Folder = new FolderBrowserDialog();
- DialogResult result = MW3Folder.ShowDialog();
- if (result == DialogResult.OK) { MW3Directory = MW3Folder.SelectedPath; }
- else { MW3Directory = null; }
- if (File.Exists(MW3Directory + "/main/iw_00.iwd")) { File.WriteAllText("settings.cfg", "MW3 Directory = " + MW3Directory + " ;"); }
- }
- }
- }
- }
- else
- {
- MessageBox.Show("Please select the MW3 folder");
- FolderBrowserDialog MW3Folder = new FolderBrowserDialog();
- DialogResult result = MW3Folder.ShowDialog();
- if (result == DialogResult.OK) { MW3Directory = MW3Folder.SelectedPath; }
- else { MW3Directory = null; }
- if (File.Exists(MW3Directory + "/main/iw_00.iwd")) { File.WriteAllText("settings.cfg", "MW3 Directory = " + MW3Directory + " ;"); }
- }
- }
- if (MW3Directory != null) { MW3Directory += "/main/"; }
- return MW3Directory;
- }
- catch (Exception x)
- {
- MessageBox.Show(x.ToString());
- return null;
- }
- }
- public static string GetStringFromTo(string[] source, string from, string to)
- {
- string result;
- int line = 0;
- bool lineReached = false;
- while (line != source.Length - 1)
- {
- if (lineReached == false)
- {
- if (source[line].Contains(from) && source[line].Contains(to)) { lineReached = true; }
- else { line++; }
- }
- }
- result = source[line];
- result = result.Replace(source[line].Substring(source[line].IndexOf(to)), "");
- result = result.Substring(result.IndexOf(from)).Replace(from, "");
- return result;
- }
- string guessIWD(string[] iwiDB, string iwiToFind)
- {
- string iwdFile;
- int strNumber = findStringInArray(iwiDB, iwiToFind);
- if (iwiDB[strNumber].EndsWith(".iwi"))
- {
- while (!iwiDB[strNumber].StartsWith("[["))
- {
- strNumber--;
- }
- iwdFile = iwiDB[strNumber].Replace("[", "");
- iwdFile = iwdFile.Replace("]", "");
- return iwdFile;
- }
- else { return null; }
- }
- int findStringInArray(string[] stringArray, string stringToFind)
- {
- int strNumber;
- int strIndex = 0;
- for (strNumber = 0; strNumber < stringArray.Length; strNumber++)
- {
- strIndex = stringArray[strNumber].IndexOf(stringToFind);
- if (strIndex >= 0)
- break;
- }
- return strNumber;
- }
- public class Crc32Fix
- {
- public const uint poly = 0xedb88320;
- public const uint startxor = 0xffffffff;
- public static uint[] table = null;
- public static uint[] revtable = null;
- public static byte[] FixChecksum(byte[] bytes, int length, int fixpos, uint wantcrc)
- {
- try
- {
- if (fixpos + 4 > length) return null;
- if (table == null)
- {
- table = new uint[256];
- revtable = new uint[256];
- uint fwd, rev;
- for (int i = 0; i < table.Length; i++)
- {
- fwd = (uint)i;
- rev = (uint)(i) << (3 * 8);
- for (int j = 8; j > 0; j--)
- {
- if ((fwd & 1) == 1)
- {
- fwd = (uint)((fwd >> 1) ^ poly);
- }
- else
- {
- fwd >>= 1;
- }
- if ((rev & 0x80000000) != 0)
- {
- rev = ((rev ^ poly) << 1) | 1;
- }
- else
- {
- rev <<= 1;
- }
- }
- table[i] = fwd;
- revtable[i] = rev;
- }
- }
- uint crc = startxor;
- for (int i = 0; i < fixpos; i++)
- {
- crc = (crc >> 8) ^ table[(crc ^ bytes[i]) & 0xff];
- }
- Array.Copy(BitConverter.GetBytes(crc), 0, bytes, fixpos, 4);
- crc = wantcrc ^ startxor;
- for (int i = length - 1; i >= fixpos; i--)
- {
- crc = (crc << 8) ^ revtable[crc >> (3 * 8)] ^ bytes[i];
- }
- Array.Copy(BitConverter.GetBytes(crc), 0, bytes, fixpos, 4);
- return BitConverter.GetBytes(crc);
- }
- catch (Exception x)
- {
- MessageBox.Show(x.ToString());
- return null;
- }
- }
- }
- public class Crc32 : HashAlgorithm
- {
- public const UInt32 DefaultPolynomial = 0xedb88320;
- public const UInt32 DefaultSeed = 0xffffffff;
- private UInt32 hash;
- private UInt32 seed;
- private UInt32[] table;
- private static UInt32[] defaultTable;
- public Crc32()
- {
- table = InitializeTable(DefaultPolynomial);
- seed = DefaultSeed;
- Initialize();
- }
- public Crc32(UInt32 polynomial, UInt32 seed)
- {
- table = InitializeTable(polynomial);
- this.seed = seed;
- Initialize();
- }
- public override void Initialize()
- {
- hash = seed;
- }
- protected override void HashCore(byte[] buffer, int start, int length)
- {
- hash = CalculateHash(table, hash, buffer, start, length);
- }
- protected override byte[] HashFinal()
- {
- byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
- this.HashValue = hashBuffer;
- return hashBuffer;
- }
- public override int HashSize
- {
- get { return 32; }
- }
- public static UInt32 Compute(byte[] buffer)
- {
- return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
- }
- public static UInt32 Compute(UInt32 seed, byte[] buffer)
- {
- return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
- }
- public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
- {
- return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
- }
- private static UInt32[] InitializeTable(UInt32 polynomial)
- {
- if (polynomial == DefaultPolynomial && defaultTable != null)
- return defaultTable;
- UInt32[] createTable = new UInt32[256];
- for (int i = 0; i < 256; i++)
- {
- UInt32 entry = (UInt32)i;
- for (int j = 0; j < 8; j++)
- if ((entry & 1) == 1)
- entry = (entry >> 1) ^ polynomial;
- else
- entry = entry >> 1;
- createTable[i] = entry;
- }
- if (polynomial == DefaultPolynomial)
- defaultTable = createTable;
- return createTable;
- }
- private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size)
- {
- UInt32 crc = seed;
- for (int i = start; i < size; i++)
- unchecked
- {
- crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
- }
- return crc;
- }
- private byte[] UInt32ToBigEndianBytes(UInt32 x)
- {
- return new byte[] {
- (byte)((x >> 24) & 0xff),
- (byte)((x >> 16) & 0xff),
- (byte)((x >> 8) & 0xff),
- (byte)(x & 0xff)
- };
- }
- }
- }
- }