private void _createCsm(string savePath, string appPath, string mymPath) { ReportProgress(0, "Unpacking base app..."); tempDir = Path.GetTempPath() + Guid.NewGuid().ToString() + "\\"; string appOut = tempDir + "appOut\\"; string mymOut = tempDir + "mymOut\\"; List<iniEntry> editedContainers = new List<iniEntry>(); Directory.CreateDirectory(appOut); int counter = 1; //Unpack .app try { Wii.U8.UnpackU8(File.ReadAllBytes(appPath), appOut); } catch (Exception ex) { SetControls(true); ErrorBox(ex.Message); return; } //Unpack .mym try { FastZip zFile = new FastZip(); zFile.ExtractZip(mymPath, mymOut, ""); } catch (Exception ex) { SetControls(true); ErrorBox(ex.Message); return; } //Parse ini if (!File.Exists(mymOut + "mym.ini")) { SetControls(true); ErrorBox("mym.ini wasn't found!"); return; } mymini ini = mymini.LoadIni(mymOut + "mym.ini"); foreach (iniEntry tempEntry in ini.Entries) { ReportProgress(counter++ * 100 / ini.Entries.Length, "Parsing mym.ini..."); if (tempEntry.entryType == iniEntry.EntryType.Container) { if (!File.Exists(appOut + tempEntry.file)) continue; bool extracted = false; while (!extracted) { byte[] fourBytes = Wii.Tools.LoadFileToByteArray(appOut + tempEntry.file, 0, 4); if (fourBytes[0] == 'A' && fourBytes[1] == 'S' && fourBytes[2] == 'H' && fourBytes[3] == '0') //ASH0 { try { DeASH(tempEntry, appOut); File.Delete(appOut + tempEntry.file); FileInfo fi = new FileInfo(appOut + tempEntry.file + ".arc"); fi.MoveTo(appOut + tempEntry.file); } catch { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\nASH.exe returned an error!\nYou may try to decompress the ASH files manually..."); return; } } else if (fourBytes[0] == 'L' && fourBytes[1] == 'Z' && fourBytes[2] == '7' && fourBytes[3] == '7') //Lz77 { try { byte[] decompressedFile = Wii.Lz77.Decompress(File.ReadAllBytes(appOut + tempEntry.file), 0); File.Delete(appOut + tempEntry.file); File.WriteAllBytes(appOut + tempEntry.file, decompressedFile); } catch (Exception ex) { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\n" + ex.Message); return; } } else if (fourBytes[0] == 'Y' && fourBytes[1] == 'a' && fourBytes[2] == 'z' && fourBytes[3] == '0') //Yaz0 { //Nothing to do about yet... break; } else if (fourBytes[0] == 0x55 && fourBytes[1] == 0xaa && fourBytes[2] == 0x38 && fourBytes[3] == 0x2d) //U8 { try { Wii.U8.UnpackU8(appOut + tempEntry.file, appOut + tempEntry.file.Replace('.', '_') + "_out"); File.Delete(appOut + tempEntry.file); extracted = true; } catch (Exception ex) { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\n" + ex.Message); return; } } else break; } editedContainers.Add(tempEntry); } else if (tempEntry.entryType == iniEntry.EntryType.CustomImage) { try { if (File.Exists(appOut + tempEntry.file)) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = tempEntry.name; ofd.Filter = "PNG|*.png"; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Image img = Image.FromFile(ofd.FileName); img = ResizeImage(img, tempEntry.width, tempEntry.height); if (File.Exists(appOut + tempEntry.file)) File.Delete(appOut + tempEntry.file); Wii.TPL.ConvertToTPL(img, appOut + tempEntry.file, (int)tempEntry.format); } } } catch (Exception ex) { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\n" + ex.Message); return; } } else if (tempEntry.entryType == iniEntry.EntryType.StaticImage) { try { if (File.Exists(mymOut + tempEntry.source)) { Image img = Image.FromFile(mymOut + tempEntry.source); img = ResizeImage(img, tempEntry.width, tempEntry.height); if (File.Exists(appOut + tempEntry.file)) File.Delete(appOut + tempEntry.file); Wii.TPL.ConvertToTPL(img, appOut + tempEntry.file, (int)tempEntry.format); } } catch (Exception ex) { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\n" + ex.Message); return; } } else if (tempEntry.entryType == iniEntry.EntryType.CustomData) { try { if (File.Exists(appOut + tempEntry.file)) { OpenFileDialog ofd = new OpenFileDialog(); ofd.FileName = tempEntry.name; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { File.Copy(ofd.FileName, appOut + tempEntry.file, true); } } } catch (Exception ex) { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\n" + ex.Message); return; } } else if (tempEntry.entryType == iniEntry.EntryType.StaticData) { try { if (File.Exists(mymOut + tempEntry.source)) { File.Copy(mymOut + tempEntry.source, appOut + tempEntry.file, true); } } catch (Exception ex) { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\n" + ex.Message); return; } } } //Repack Containers foreach (iniEntry tempEntry in editedContainers) { if (!settings.lz77Containers) { Wii.U8.PackU8(appOut + tempEntry.file.Replace('.', '_') + "_out", appOut + tempEntry.file); } else { byte[] u8Container = Wii.U8.PackU8(appOut + tempEntry.file.Replace('.', '_') + "_out"); Wii.Lz77.Compress(u8Container, appOut + tempEntry.file); } Directory.Delete(appOut + tempEntry.file.Replace('.', '_') + "_out", true); } //Repack app Wii.U8.PackU8(appOut, savePath); CsmFinish finish = new CsmFinish(this._csmFinish); this.Invoke(finish, savePath, mymPath); }
private void _createCsm(string savePath, string appPath, string mymPath) { ReportProgress(0, "Unpacking base app..."); tempDir = Path.GetTempPath() + Guid.NewGuid().ToString() + "\\"; string appOut = tempDir + "appOut\\"; string mymOut = tempDir + "mymOut\\"; List <iniEntry> editedContainers = new List <iniEntry>(); Directory.CreateDirectory(appOut); int counter = 1; //Unpack .app try { Wii.U8.UnpackU8(File.ReadAllBytes(appPath), appOut); } catch (Exception ex) { SetControls(true); ErrorBox(ex.Message); return; } //Unpack .mym try { FastZip zFile = new FastZip(); zFile.ExtractZip(mymPath, mymOut, ""); } catch (Exception ex) { SetControls(true); ErrorBox(ex.Message); return; } //Parse ini if (!File.Exists(mymOut + "mym.ini")) { SetControls(true); ErrorBox("mym.ini wasn't found!"); return; } mymini ini = mymini.LoadIni(mymOut + "mym.ini"); foreach (iniEntry tempEntry in ini.Entries) { ReportProgress(counter++ *100 / ini.Entries.Length, "Parsing mym.ini..."); if (tempEntry.entryType == iniEntry.EntryType.Container) { if (!File.Exists(appOut + tempEntry.file)) { continue; } bool extracted = false; while (!extracted) { byte[] fourBytes = Wii.Tools.LoadFileToByteArray(appOut + tempEntry.file, 0, 4); if (fourBytes[0] == 'A' && fourBytes[1] == 'S' && fourBytes[2] == 'H' && fourBytes[3] == '0') //ASH0 { try { DeASH(tempEntry, appOut); File.Delete(appOut + tempEntry.file); FileInfo fi = new FileInfo(appOut + tempEntry.file + ".arc"); fi.MoveTo(appOut + tempEntry.file); } catch { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\nASH.exe returned an error!\nYou may try to decompress the ASH files manually..."); return; } } else if (fourBytes[0] == 'L' && fourBytes[1] == 'Z' && fourBytes[2] == '7' && fourBytes[3] == '7') //Lz77 { try { byte[] decompressedFile = Wii.Lz77.Decompress(File.ReadAllBytes(appOut + tempEntry.file), 0); File.Delete(appOut + tempEntry.file); File.WriteAllBytes(appOut + tempEntry.file, decompressedFile); } catch (Exception ex) { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\n" + ex.Message); return; } } else if (fourBytes[0] == 'Y' && fourBytes[1] == 'a' && fourBytes[2] == 'z' && fourBytes[3] == '0') //Yaz0 { //Nothing to do about yet... break; } else if (fourBytes[0] == 0x55 && fourBytes[1] == 0xaa && fourBytes[2] == 0x38 && fourBytes[3] == 0x2d) //U8 { try { Wii.U8.UnpackU8(appOut + tempEntry.file, appOut + tempEntry.file.Replace('.', '_') + "_out"); File.Delete(appOut + tempEntry.file); extracted = true; } catch (Exception ex) { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\n" + ex.Message); return; } } else { break; } } editedContainers.Add(tempEntry); } else if (tempEntry.entryType == iniEntry.EntryType.CustomImage) { try { if (File.Exists(appOut + tempEntry.file)) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = tempEntry.name; ofd.Filter = "PNG|*.png"; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Image img = Image.FromFile(ofd.FileName); img = ResizeImage(img, tempEntry.width, tempEntry.height); if (File.Exists(appOut + tempEntry.file)) { File.Delete(appOut + tempEntry.file); } Wii.TPL.ConvertToTPL(img, appOut + tempEntry.file, (int)tempEntry.format); } } } catch (Exception ex) { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\n" + ex.Message); return; } } else if (tempEntry.entryType == iniEntry.EntryType.StaticImage) { try { if (File.Exists(mymOut + tempEntry.source)) { Image img = Image.FromFile(mymOut + tempEntry.source); img = ResizeImage(img, tempEntry.width, tempEntry.height); if (File.Exists(appOut + tempEntry.file)) { File.Delete(appOut + tempEntry.file); } Wii.TPL.ConvertToTPL(img, appOut + tempEntry.file, (int)tempEntry.format); } } catch (Exception ex) { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\n" + ex.Message); return; } } else if (tempEntry.entryType == iniEntry.EntryType.CustomData) { try { if (File.Exists(appOut + tempEntry.file)) { OpenFileDialog ofd = new OpenFileDialog(); ofd.FileName = tempEntry.name; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { File.Copy(ofd.FileName, appOut + tempEntry.file, true); } } } catch (Exception ex) { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\n" + ex.Message); return; } } else if (tempEntry.entryType == iniEntry.EntryType.StaticData) { try { if (File.Exists(mymOut + tempEntry.source)) { File.Copy(mymOut + tempEntry.source, appOut + tempEntry.file, true); } } catch (Exception ex) { SetControls(true); ErrorBox("Entry: " + tempEntry.entry + "\n\n" + ex.Message); return; } } } //Repack Containers foreach (iniEntry tempEntry in editedContainers) { if (!settings.lz77Containers) { Wii.U8.PackU8(appOut + tempEntry.file.Replace('.', '_') + "_out", appOut + tempEntry.file); } else { byte[] u8Container = Wii.U8.PackU8(appOut + tempEntry.file.Replace('.', '_') + "_out"); Wii.Lz77.Compress(u8Container, appOut + tempEntry.file); } Directory.Delete(appOut + tempEntry.file.Replace('.', '_') + "_out", true); } //Repack app Wii.U8.PackU8(appOut, savePath); CsmFinish finish = new CsmFinish(this._csmFinish); this.Invoke(finish, savePath, mymPath); }