private void _convertCsm() { string appDir = tempDir + "\\appOut\\"; string csmDir = tempDir + "\\csmOut\\"; string mymDir = tempDir + "\\mymOut\\"; if (Directory.Exists(appDir)) { Directory.Delete(appDir, true); } if (Directory.Exists(csmDir)) { Directory.Delete(csmDir, true); } if (Directory.Exists(mymDir)) { Directory.Delete(mymDir, true); } List <iniEntry> entryList = new List <iniEntry>(); Wii.U8.UnpackU8(tbCsm.Text, csmDir); Wii.U8.UnpackU8(tbApp.Text, appDir); string[] csmFiles = Directory.GetFiles(csmDir, "*", SearchOption.AllDirectories); if (intensiveAlgorithm) { for (int i = 0; i < csmFiles.Length; i++) { ReportProgress((i * 100 / csmFiles.Length) / 2); byte[] temp = Wii.Tools.LoadFileToByteArray(csmFiles[i], 0, 4); if (temp[0] == 'Y' && temp[1] == 'a' && temp[2] == 'z' && temp[3] == '0') { continue; } bool extracted = false; while (!extracted) { byte[] fourBytes = Wii.Tools.LoadFileToByteArray(csmFiles[i].Replace(csmDir, appDir), 0, 4); if (fourBytes[0] == 'A' && fourBytes[1] == 'S' && fourBytes[2] == 'H' && fourBytes[3] == '0') //ASH0 { try { DeASH(csmFiles[i].Replace(csmDir, appDir)); File.Delete(csmFiles[i].Replace(csmDir, appDir)); FileInfo fi = new FileInfo(csmFiles[i].Replace(csmDir, appDir) + ".arc"); fi.MoveTo(csmFiles[i].Replace(csmDir, appDir)); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else if (fourBytes[0] == 'L' && fourBytes[1] == 'Z' && fourBytes[2] == '7' && fourBytes[3] == '7') //Lz77 { try { byte[] decompressedFile = Wii.Lz77.Decompress(File.ReadAllBytes(csmFiles[i].Replace(csmDir, appDir)), 0); File.Delete(csmFiles[i].Replace(csmDir, appDir)); File.WriteAllBytes(csmFiles[i].Replace(csmDir, appDir), decompressedFile); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 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(csmFiles[i].Replace(csmDir, appDir), csmFiles[i].Replace(csmDir, appDir).Replace(".", "_") + "_out"); File.Delete(csmFiles[i].Replace(csmDir, appDir)); extracted = true; } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { break; } } extracted = false; while (!extracted) { byte[] fourBytes = Wii.Tools.LoadFileToByteArray(csmFiles[i], 0, 4); if (fourBytes[0] == 'A' && fourBytes[1] == 'S' && fourBytes[2] == 'H' && fourBytes[3] == '0') //ASH0 { try { DeASH(csmFiles[i]); File.Delete(csmFiles[i]); FileInfo fi = new FileInfo(csmFiles[i] + ".arc"); fi.MoveTo(csmFiles[i]); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else if (fourBytes[0] == 'L' && fourBytes[1] == 'Z' && fourBytes[2] == '7' && fourBytes[3] == '7') //Lz77 { try { byte[] decompressedFile = Wii.Lz77.Decompress(File.ReadAllBytes(csmFiles[i]), 0); File.Delete(csmFiles[i]); File.WriteAllBytes(csmFiles[i], decompressedFile); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 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(csmFiles[i], csmFiles[i].Replace(".", "_") + "_out"); File.Delete(csmFiles[i]); extracted = true; } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { break; } } } csmFiles = Directory.GetFiles(csmDir, "*", SearchOption.AllDirectories); } for (int i = 0; i < csmFiles.Length; i++) { ReportProgress(((i * 100 / csmFiles.Length) / (intensiveAlgorithm ? 2 : 1)) + (intensiveAlgorithm ? 50 : 0)); if (File.Exists(csmFiles[i].Replace(csmDir, appDir))) { //File exists in original app FileInfo fi = new FileInfo(csmFiles[i]); FileInfo fi2 = new FileInfo(csmFiles[i].Replace(csmDir, appDir)); if (fi.Length == fi2.Length) //Same file { continue; } } iniEntry tempEntry = new iniEntry(); tempEntry.entryType = iniEntry.EntryType.StaticData; tempEntry.file = csmFiles[i].Replace(csmDir, string.Empty); if (!tempEntry.file.StartsWith("\\")) { tempEntry.file = tempEntry.file.Insert(0, "\\"); } if (!Directory.Exists(Path.GetDirectoryName(mymDir + Path.GetExtension(csmFiles[i]).Remove(0, 1) + "\\" + Path.GetFileName(csmFiles[i])))) { Directory.CreateDirectory(Path.GetDirectoryName(mymDir + Path.GetExtension(csmFiles[i]).Remove(0, 1) + "\\" + Path.GetFileName(csmFiles[i]))); } string destFile = mymDir + Path.GetExtension(csmFiles[i]).Remove(0, 1) + "\\" + Path.GetFileName(csmFiles[i]); int counter = 0; FileInfo fi1 = new FileInfo(csmFiles[i]); string tempFile = destFile; while (File.Exists(destFile)) { FileInfo fi2 = new FileInfo(destFile); if (fi1.Length == fi2.Length) { break; } destFile = tempFile.Replace(Path.GetExtension(tempFile), (++counter).ToString() + Path.GetExtension(tempFile)); } File.Copy(csmFiles[i], destFile, true); tempEntry.source = "\\" + Path.GetExtension(destFile).Remove(0, 1) + "\\" + Path.GetFileName(destFile); entryList.Add(tempEntry); } //--- List <string> containersToManage = new List <string>(); List <string> managedContainers = new List <string>(); foreach (iniEntry tempEntry in entryList) { if (tempEntry.entryType == iniEntry.EntryType.Container) { managedContainers.Add(tempEntry.file); } else { if (tempEntry.file.Contains("_out")) { string tmpString = tempEntry.file.Remove(tempEntry.file.IndexOf("_out")); tmpString = tmpString.Substring(0, tmpString.Length - 5) + tmpString.Substring(tmpString.Length - 5).Replace("_", "."); if (!StringExistsInStringArray(tmpString, containersToManage.ToArray())) { containersToManage.Add(tmpString); } } } } List <string> leftContainers = new List <string>(); foreach (string thisContainer in containersToManage) { if (!StringExistsInStringArray(thisContainer, managedContainers.ToArray())) { leftContainers.Add(thisContainer); } } if (leftContainers.Count > 0) { List <iniEntry> newList = new List <iniEntry>(); foreach (string thisContainer in leftContainers) { iniEntry tempEntry = new iniEntry(); tempEntry.entryType = iniEntry.EntryType.Container; tempEntry.file = thisContainer; tempEntry.type = iniEntry.ContainerType.ASH; newList.Add(tempEntry); } newList.AddRange(entryList); entryList = newList; } //--- mymini ini = mymini.CreateIni(entryList.ToArray()); ini.Save(mymDir + "mym.ini"); FastZip fZip = new FastZip(); fZip.CreateZip(saveFile, mymDir, true, ""); if (Directory.Exists(appDir)) { Directory.Delete(appDir, true); } if (Directory.Exists(csmDir)) { Directory.Delete(csmDir, true); } if (Directory.Exists(mymDir)) { Directory.Delete(mymDir, true); } ReportProgress(100); MessageBox.Show("Saved mym to:\n" + saveFile, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); }
private void _saveMym(object _creationInfo) { SetControls(false); CreationInfo cInfo = (CreationInfo)_creationInfo; string outDir = tempDir + "newMym\\"; if (Directory.Exists(outDir)) Directory.Delete(outDir, true); Directory.CreateDirectory(outDir); int counter = 0; int[] counters = new int[5]; //Build new ini List<iniEntry> tempEntries = new List<iniEntry>(); List<string[]> dataSources = new List<string[]>(); List<string[]> imageSources = new List<string[]>(); foreach (object tempObject in cInfo.lbEntries) { ReportProgress(++counter * 100 / cInfo.lbEntries.Length, "Building mym.ini..."); try { iniEntry tempEntry = ini.GetEntry(tempObject.ToString()); if (!CheckEntry(tempEntry)) { if (!settings.ignoreMissing) { ReportProgress(100, " "); SetControls(true); return; } else continue; } if (settings.sourceManage) { //Manage source if (tempEntry.entryType == iniEntry.EntryType.StaticData) { if (Path.HasExtension(tempEntry.filepath)) { tempEntry.source = "\\" + Path.GetExtension(tempEntry.filepath).Remove(0, 1) + "\\" + Path.GetFileName(tempEntry.filepath); string tempSource = "\\" + Path.GetExtension(tempEntry.filepath).Remove(0, 1) + "\\" + Path.GetFileName(tempEntry.filepath); int i = 1; while (EntryExists(tempEntry, dataSources)) { tempEntry.source = tempSource.Insert(tempSource.LastIndexOf('.'), (++i).ToString()); } FileInfo fi = new FileInfo(tempEntry.filepath); dataSources.Add(new string[] { tempEntry.source, fi.Length.ToString() }); } else { tempEntry.source = "\\" + Path.GetFileName(tempEntry.filepath); string tempSource = "\\" + "\\" + Path.GetFileName(tempEntry.filepath); int i = 1; while (EntryExists(tempEntry, dataSources)) { tempEntry.source = tempSource.Insert(tempSource.LastIndexOf('.'), (++i).ToString()); } FileInfo fi = new FileInfo(tempEntry.filepath); dataSources.Add(new string[] { tempEntry.source, fi.Length.ToString() }); } } else if (tempEntry.entryType == iniEntry.EntryType.StaticImage) { tempEntry.source = "\\images\\" + Path.GetFileName(tempEntry.filepath); string tempSource = "\\images\\" + Path.GetFileName(tempEntry.filepath); int i = 1; while (EntryExists(tempEntry, imageSources)) { tempEntry.source = tempSource.Insert(tempSource.LastIndexOf('.'), (++i).ToString()); } FileInfo fi = new FileInfo(tempEntry.filepath); imageSources.Add(new string[] { tempEntry.source, fi.Length.ToString() }); } } if (settings.autoImageSize) { if (tempEntry.entryType == iniEntry.EntryType.CustomImage || tempEntry.entryType == iniEntry.EntryType.StaticImage) { //Get png width and height Image img = Image.FromFile(tempEntry.filepath); tempEntry.width = img.Width; tempEntry.height = img.Height; } } tempEntries.Add(tempEntry); } catch { } } if (tempEntries.Count < 1) { ErrorBox("No entries left..."); SetControls(true); return; } //Manage Containers if (settings.containerManage) { List<string> containersToManage = new List<string>(); List<string> managedContainers = new List<string>(); foreach (iniEntry tempEntry in tempEntries) { if (tempEntry.entryType == iniEntry.EntryType.Container) managedContainers.Add(tempEntry.file); else { if (tempEntry.file.Contains("_out")) { string tmpString = tempEntry.file.Remove(tempEntry.file.IndexOf("_out")); tmpString = tmpString.Substring(0, tmpString.Length - 5) + tmpString.Substring(tmpString.Length - 5).Replace("_", "."); if (!StringExistsInStringArray(tmpString, containersToManage.ToArray())) containersToManage.Add(tmpString); } } } List<string> leftContainers = new List<string>(); foreach (string thisContainer in containersToManage) { if (!StringExistsInStringArray(thisContainer, managedContainers.ToArray())) leftContainers.Add(thisContainer); } if (leftContainers.Count > 0) { List<iniEntry> newList = new List<iniEntry>(); foreach (string thisContainer in leftContainers) { iniEntry tempEntry = new iniEntry(); tempEntry.entryType = iniEntry.EntryType.Container; tempEntry.file = thisContainer; tempEntry.type = iniEntry.ContainerType.ASH; newList.Add(tempEntry); } newList.AddRange(tempEntries); tempEntries = newList; } } ini = new mymini(); ini.EntryList = tempEntries; ini.Save(outDir + "mym.ini"); counter = 0; //Copy files foreach (iniEntry tempEntry in ini.EntryList) { ReportProgress(++counter * 100 / ini.EntryList.Count, "Copying files..."); if (tempEntry.entryType == iniEntry.EntryType.StaticImage || tempEntry.entryType == iniEntry.EntryType.StaticData) { string sourceFile = tempEntry.filepath; string destFile = outDir + tempEntry.source; if (!Directory.Exists(Path.GetDirectoryName(destFile))) Directory.CreateDirectory(Path.GetDirectoryName(destFile)); File.Copy(sourceFile, destFile, true); } } //Zip file ReportProgress(0, "Packing mym..."); ReportProgress(80, ""); // Fake, hm?! FastZip fZip = new FastZip(); fZip.CreateZip((cInfo.createCsm) ? tempDir + "temp.mym" : cInfo.savePath, outDir, true, ""); ReportProgress(100, " "); if (cInfo.closeAfter) { MethodInvoker m = new MethodInvoker(this.ExitApplication); this.Invoke(m); return; } if (!cInfo.createCsm) { InfoBox("Saved mym to:\n" + cInfo.savePath); SetControls(true); return; } _createCsm(cInfo.savePath, cInfo.appFile, tempDir + "temp.mym"); }
private void _saveMym(object _creationInfo) { SetControls(false); CreationInfo cInfo = (CreationInfo)_creationInfo; string outDir = tempDir + "newMym\\"; if (Directory.Exists(outDir)) { Directory.Delete(outDir, true); } Directory.CreateDirectory(outDir); int counter = 0; int[] counters = new int[5]; //Build new ini List <iniEntry> tempEntries = new List <iniEntry>(); List <string[]> dataSources = new List <string[]>(); List <string[]> imageSources = new List <string[]>(); foreach (object tempObject in cInfo.lbEntries) { ReportProgress(++counter * 100 / cInfo.lbEntries.Length, "Building mym.ini..."); try { iniEntry tempEntry = ini.GetEntry(tempObject.ToString()); if (!CheckEntry(tempEntry)) { if (!settings.ignoreMissing) { ReportProgress(100, " "); SetControls(true); return; } else { continue; } } if (settings.sourceManage) { //Manage source if (tempEntry.entryType == iniEntry.EntryType.StaticData) { if (Path.HasExtension(tempEntry.filepath)) { tempEntry.source = "\\" + Path.GetExtension(tempEntry.filepath).Remove(0, 1) + "\\" + Path.GetFileName(tempEntry.filepath); string tempSource = "\\" + Path.GetExtension(tempEntry.filepath).Remove(0, 1) + "\\" + Path.GetFileName(tempEntry.filepath); int i = 1; while (EntryExists(tempEntry, dataSources)) { tempEntry.source = tempSource.Insert(tempSource.LastIndexOf('.'), (++i).ToString()); } FileInfo fi = new FileInfo(tempEntry.filepath); dataSources.Add(new string[] { tempEntry.source, fi.Length.ToString() }); } else { tempEntry.source = "\\" + Path.GetFileName(tempEntry.filepath); string tempSource = "\\" + "\\" + Path.GetFileName(tempEntry.filepath); int i = 1; while (EntryExists(tempEntry, dataSources)) { tempEntry.source = tempSource.Insert(tempSource.LastIndexOf('.'), (++i).ToString()); } FileInfo fi = new FileInfo(tempEntry.filepath); dataSources.Add(new string[] { tempEntry.source, fi.Length.ToString() }); } } else if (tempEntry.entryType == iniEntry.EntryType.StaticImage) { tempEntry.source = "\\images\\" + Path.GetFileName(tempEntry.filepath); string tempSource = "\\images\\" + Path.GetFileName(tempEntry.filepath); int i = 1; while (EntryExists(tempEntry, imageSources)) { tempEntry.source = tempSource.Insert(tempSource.LastIndexOf('.'), (++i).ToString()); } FileInfo fi = new FileInfo(tempEntry.filepath); imageSources.Add(new string[] { tempEntry.source, fi.Length.ToString() }); } } if (settings.autoImageSize) { if (tempEntry.entryType == iniEntry.EntryType.CustomImage || tempEntry.entryType == iniEntry.EntryType.StaticImage) { //Get png width and height Image img = Image.FromFile(tempEntry.filepath); tempEntry.width = img.Width; tempEntry.height = img.Height; } } tempEntries.Add(tempEntry); } catch { } } if (tempEntries.Count < 1) { ErrorBox("No entries left..."); SetControls(true); return; } //Manage Containers if (settings.containerManage) { List <string> containersToManage = new List <string>(); List <string> managedContainers = new List <string>(); foreach (iniEntry tempEntry in tempEntries) { if (tempEntry.entryType == iniEntry.EntryType.Container) { managedContainers.Add(tempEntry.file); } else { if (tempEntry.file.Contains("_out")) { string tmpString = tempEntry.file.Remove(tempEntry.file.IndexOf("_out")); tmpString = tmpString.Substring(0, tmpString.Length - 5) + tmpString.Substring(tmpString.Length - 5).Replace("_", "."); if (!StringExistsInStringArray(tmpString, containersToManage.ToArray())) { containersToManage.Add(tmpString); } } } } List <string> leftContainers = new List <string>(); foreach (string thisContainer in containersToManage) { if (!StringExistsInStringArray(thisContainer, managedContainers.ToArray())) { leftContainers.Add(thisContainer); } } if (leftContainers.Count > 0) { List <iniEntry> newList = new List <iniEntry>(); foreach (string thisContainer in leftContainers) { iniEntry tempEntry = new iniEntry(); tempEntry.entryType = iniEntry.EntryType.Container; tempEntry.file = thisContainer; tempEntry.type = iniEntry.ContainerType.ASH; newList.Add(tempEntry); } newList.AddRange(tempEntries); tempEntries = newList; } } ini = new mymini(); ini.EntryList = tempEntries; ini.Save(outDir + "mym.ini"); counter = 0; //Copy files foreach (iniEntry tempEntry in ini.EntryList) { ReportProgress(++counter * 100 / ini.EntryList.Count, "Copying files..."); if (tempEntry.entryType == iniEntry.EntryType.StaticImage || tempEntry.entryType == iniEntry.EntryType.StaticData) { string sourceFile = tempEntry.filepath; string destFile = outDir + tempEntry.source; if (!Directory.Exists(Path.GetDirectoryName(destFile))) { Directory.CreateDirectory(Path.GetDirectoryName(destFile)); } File.Copy(sourceFile, destFile, true); } } //Zip file ReportProgress(0, "Packing mym..."); ReportProgress(80, ""); // Fake, hm?! FastZip fZip = new FastZip(); fZip.CreateZip((cInfo.createCsm) ? tempDir + "temp.mym" : cInfo.savePath, outDir, true, ""); ReportProgress(100, " "); if (cInfo.closeAfter) { MethodInvoker m = new MethodInvoker(this.ExitApplication); this.Invoke(m); return; } if (!cInfo.createCsm) { InfoBox("Saved mym to:\n" + cInfo.savePath); SetControls(true); return; } _createCsm(cInfo.savePath, cInfo.appFile, tempDir + "temp.mym"); }