private void Form3_Load(object sender, EventArgs e) { string path = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Workspace\\settings.config")); if (!File.Exists(path)) { MessageBox.Show("Set the location of \"DARKSOULS.exe\" in \"Settings -> EXE Location\" to be able to modify it."); this.Close(); } else { BinaryReader exeReader = new BinaryReader(File.Open(path, FileMode.Open), System.Text.Encoding.Unicode); debugChB.Checked = EXE.debCheck(exeReader); EXE.exeCheck(exeReader, debugChB.Checked); dcxChB.Checked = EXE.dcxCheck(exeReader, debugChB.Checked); exeReader.Close(); addCheckboxes(); int j = 0; for (int i = 0; i < checkboxes.Count; i++) { switch (i) { default: checkboxes[i].Checked = EXE.isModified[j]; j++; break; case 8: checkboxes[i].Checked = EXE.isModified[j]; j += 3; break; case 17: checkboxes[i].Checked = EXE.isModified[j]; j += 2; break; } ; } if (dcxChB.Checked) { uncheckAllChB(); } else { uncheckDcxChB(); } } }
private void launchBtn_Click(object sender, EventArgs e) { disableBtns(); Log("line"); Log("Initiating launching sequence.\r\n"); Log("line"); string dsdir = ""; if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Workspace\\settings.config"))) { dsdir = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Workspace\\settings.config")); } if (File.Exists(dsdir) & Path.GetFileName(dsdir) == "DARKSOULS.exe") { BinaryReader exeReader = new BinaryReader(File.Open(dsdir, FileMode.Open), System.Text.Encoding.Unicode); EXE.exeCheck(exeReader, EXE.debCheck(exeReader)); exeReader.Close(); bool l = false; foreach (bool mod in EXE.isModified) { l |= mod; } if (l) { DialogResult dialogresult = MessageBox.Show("One or more of the main directories are loaded from unpacked libraries. Some mods might not show up in-game.\n\nTo change this, go to \"Settings -> Modify EXE\"\n\nDo you wish to continue?", "Launch Dark Souls w/ mods", MessageBoxButtons.OKCancel); if (dialogresult != DialogResult.OK) { Log("User chose to modify \"DARKSOULS.exe\".\r\n"); Log("line"); Log("Program terminated successfully.\r\n"); enableBtns(); return; } } dsdir = Path.GetDirectoryName(dsdir); } else { MessageBox.Show("Please specify the loaction of \"DARKSOULS.exe\" in \"Settings -> EXE Loaction\" to be able to install mods to the game."); Log("Path to \"DARKSOULS.exe\" was not specifed.\r\n"); Log("line"); Log("Program terminated successfully.\r\n"); enableBtns(); return; } if (!canBeOpened(dsdir)) { MessageBox.Show("The dvdbnd libraries of Dark Souls cannot be opened, as it's either opened in another program, or protected."); Log("The dvdbnd libraries cannot be opened.\r\n"); Log("line"); Log("Program terminated successfully.\r\n"); enableBtns(); return; } List <byte[]> backup = new List <byte[]> { File.ReadAllBytes(dsdir + "\\dvdbnd0.bhd5"), File.ReadAllBytes(dsdir + "\\dvdbnd1.bhd5"), File.ReadAllBytes(dsdir + "\\dvdbnd2.bhd5"), File.ReadAllBytes(dsdir + "\\dvdbnd3.bhd5") }; List <BHD5.Bhd5Struct> dvdbnd = new List <BHD5.Bhd5Struct> { BHD5.GetBhd5Data(dsdir + "\\dvdbnd0.bhd5"), BHD5.GetBhd5Data(dsdir + "\\dvdbnd1.bhd5"), BHD5.GetBhd5Data(dsdir + "\\dvdbnd2.bhd5"), BHD5.GetBhd5Data(dsdir + "\\dvdbnd3.bhd5") }; string[] initArchiveSize = new string[4] { new FileInfo(dsdir + "\\dvdbnd0.bdt").Length.ToString(), new FileInfo(dsdir + "\\dvdbnd1.bdt").Length.ToString(), new FileInfo(dsdir + "\\dvdbnd2.bdt").Length.ToString(), new FileInfo(dsdir + "\\dvdbnd3.bdt").Length.ToString() }; if (!Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Workspace\\backup"))) { try { Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Workspace\\backup")); } catch { Log("\"Workspace\\backup\" is missing, and could not create it. Could not create backup files for crashDetect to restore from.\r\n"); Log("line"); } } if (Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Workspace\\backup"))) { File.WriteAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Workspace\\backup\\initarchivesize.txt"), initArchiveSize); for (int i = 0; i < 4; i++) { File.WriteAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Workspace\\backup\\dvdbnd" + i + ".bhd5"), backup[i]); } } List <bool> modifiedBhd5 = new List <bool>(new bool[4]); StringReader reader = new StringReader(Properties.Resources.filenames); string[] filenames = EnumerateLines(reader).ToArray(); string text = ""; int matching = -1; if (listView1.Items.Count == 0 || checkBox1.Checked) { loadFolder(dsdir); } specialCases(); if (listView1.Items.Count == 1) { text = "mod.\r\n"; } else { text = "mods.\r\n"; } Log("Preparing to install " + listView1.Items.Count + text); Log("line"); List <UInt32> special = new List <UInt32>(new UInt32[7]); List <string> possibleDSFixSpecial = new List <string> { }; foreach (ListViewItem mod in listView1.Items) { bool l = false; int id = 0; bool dcx = true; if (File.Exists(mod.Text)) { while (!l & id < filenames.Length) { if (Path.GetFileName(mod.Text) == Path.GetFileName(filenames[id])) { checkIfSpecialCase(Path.GetFileName(mod.Text), ref special, ref id); Log("Found \"" + Path.GetFileName(mod.Text) + "\" in the filenames list at line " + id + ".\r\n"); matching = id; l = true; } else if (Path.GetFileName(mod.Text) + ".dcx" == Path.GetFileName(filenames[id])) { checkIfSpecialCase(Path.GetFileName(mod.Text) + ".dcx", ref special, ref id); Log("Found DCX version of \"" + Path.GetFileName(mod.Text) + "\" in the filenames list at line " + id + ".\r\n"); matching = id; l = true; dcx = false; } else { id++; } } if (!l) { text = "Did not find \"" + Path.GetFileName(mod.Text) + "\" in the filenames list. "; string ext = Path.GetExtension(mod.Text); if (ext == ".png" || ext == ".dds" || ext == ".jpg" || ext == ".tga" || ext == ".bmp") { possibleDSFixSpecial.Add(mod.Text); text += "Handling as possible DSFix texture mod.\r\n"; } else { text += "Ignoring file.\r\n"; } Log(text); log.Text += text; Log("line"); matching = -1; } if (matching != -1) { UInt32 hash = BHD5.GetHash(filenames[matching]); Log("Hash for \"" + Path.GetFileName(filenames[matching]) + "\" is: " + hash + "\r\n"); int bucket = 0; int entry = 0; int bhd5 = 3; l = false; while (!l & bhd5 > -1) { l = BHD5.FindHash(dvdbnd[bhd5], hash, ref bucket, ref entry); if (!l) { Log("Did not find \"" + Path.GetFileName(mod.Text) + "\" in dvdbnd" + bhd5 + ".bhd5\r\n"); bhd5--; } } if (!l) { text = "Could not find hash in any of the bhd5 files (should be impossible).\r\n"; log.Text += text; Log(text); } else { Log("Found \"" + Path.GetFileName(mod.Text) + "\" in \"dvdbnd" + bhd5 + ".bhd5\", in bucket " + bucket + " at entry " + entry + "\r\n"); } BHD5.EntryStruct modEntry = new BHD5.EntryStruct(); BHD5.EntryStruct oldEntry = dvdbnd[bhd5].buckets[bucket].entries[entry]; BinaryWriter bdt = new BinaryWriter(File.Open(dsdir + "\\dvdbnd" + bhd5 + ".bdt", FileMode.Append)); byte[] modFile; if (!dcx) { Log("Compressing \"" + Path.GetFileName(mod.Text) + "\" to \"" + Path.GetFileName(mod.Text) + ".dcx\".\r\n"); modFile = rebuildDCX(File.ReadAllBytes(mod.Text)); } else { modFile = File.ReadAllBytes(mod.Text); } modEntry.hash = dvdbnd[bhd5].buckets[bucket].entries[entry].hash; modEntry.size = (UInt32)modFile.Length; modEntry.offset = (UInt64) new FileInfo(dsdir + "\\dvdbnd" + bhd5 + ".bdt").Length; Log("Modifying \"dvdbnd" + bhd5 + ".bhd5\", bucket " + bucket + ", entry " + entry + "\r\n" + "Old size: " + oldEntry.size + ", old offset: " + oldEntry.offset + "\r\n" + "New size: " + modEntry.size + ", new offset: " + modEntry.offset + "\r\n"); dvdbnd[bhd5].buckets[bucket].entries[entry] = modEntry; bdt.Write(modFile); bdt.Close(); modifiedBhd5[bhd5] |= l; Log("Installed \"" + mod.Text + "\" to \"dvdbnd" + bhd5 + ".bdt\"\r\n"); Log("line"); log.Text += "Installed \"" + Path.GetFileName(mod.Text) + "\".\n"; } } else { text = "Could not install file \"" + mod.Text + "\" as it does not exist.\r\n"; Log(text); log.Text += text; } } foreach (string dsfixmod in possibleDSFixSpecial) { string path = Path.Combine(dsdir, "dsfix\\tex_override\\" + Path.GetFileName(dsfixmod)); if (File.Exists(path)) { if (!File.Exists(path + ".bak")) { File.Copy(path, path + ".bak"); } File.Delete(path); } if (File.Exists(dsfixmod)) { File.Copy(dsfixmod, path); Log("Placed possible DSFix texture mod \"" + dsfixmod + "\" into \"dsfix\\tex_override\".\r\n"); } else { Log("\"" + dsfixmod + "\" does not exist, could not be installed.\r\n"); } } if (possibleDSFixSpecial.Count != 0) { text = "DSFix mods installed. Note that for these mods to show up in-game, you still have to manually set \"enableTextureOverride\" in \"DSFix.ini\".\r\n"; Log("line"); Log(text); log.Text += text; Log("line"); } for (int i = 0; i < 4; i++) { if (modifiedBhd5[i]) { BHD5.RewriteBhd5(dvdbnd[i], dsdir + "\\dvdbnd" + i + ".bhd5"); Log("Rewritten \"dvdbnd" + i + ".bhd5\"\r\n"); } } bool noModified = false; foreach (bool item in modifiedBhd5) { noModified |= item; } if (!noModified) { Log("No mods were installed.\r\n"); } Log("line"); runDS(); Log("Restoring backups.\r\n"); for (int i = 0; i < 4; i++) { if (modifiedBhd5[i]) { BinaryWriter bhd5 = new BinaryWriter(File.Open(dsdir + "\\dvdbnd" + i + ".bhd5", FileMode.Create)); bhd5.Write(backup[i]); bhd5.Close(); Log("Restored \"dvdbnd" + i + ".bhd5\"\r\n"); FileStream bdt = new FileStream(dsdir + "\\dvdbnd" + i + ".bdt", FileMode.Open); bdt.SetLength(long.Parse(initArchiveSize[i])); bdt.Close(); Log("Restored \"dvdbnd" + i + ".bdt\"\r\n"); } else { Log("No need to restore \"dvdbnd" + i + ".bhd5\" and \"dvdbnd" + i + ".bdt\" as they were not modified.\r\n"); } } if (possibleDSFixSpecial.Count != 0) { Log("line"); } foreach (string dsfixmod in possibleDSFixSpecial) { string path = Path.Combine(dsdir, "dsfix\\tex_override\\" + Path.GetFileName(dsfixmod)); if (File.Exists(path)) { File.Delete(path); } if (File.Exists(path + ".bak")) { File.Copy(path + ".bak", path); } Log("Deleted possible DSFix texture mod \"" + Path.GetFileName(dsfixmod) + "\" from \"dsfix\\tex_override\".\r\n"); } GC.Collect(); enableBtns(); Log("line"); Log("Program terminated successfully.\r\n"); }