From 0a79012458d6517438a9851f5bdb663ba9f44e67 Mon Sep 17 00:00:00 2001 From: Elie Rodrigue Date: Sun, 28 May 2017 08:09:29 -0400 Subject: Working Hakchi2 to book save feature --- FoldersManagerForm.cs | 21 +++-- Manager/BookManager.cs | 34 +++++-- Manager/CoverManager.cs | 4 + Manager/RomManager.cs | 19 ++++ Tooling/Tasks/SaveCurrentConfigAsBook.cs | 147 +++++++++++++++++++++++++++++-- UI/Components/EntryMenuItem.cs | 11 ++- UI/Components/PageMenuItem.cs | 2 +- UI/Forms/EntryCreator.Designer.cs | 74 ++++++++-------- UI/Forms/EntryCreator.cs | 31 ++++++- 9 files changed, 286 insertions(+), 57 deletions(-) diff --git a/FoldersManagerForm.cs b/FoldersManagerForm.cs index 105386d8..e134fb3f 100644 --- a/FoldersManagerForm.cs +++ b/FoldersManagerForm.cs @@ -14,6 +14,7 @@ namespace com.clusterrr.hakchi_gui { public partial class FoldersManagerForm : Form { + public bool _SkipUnsortedFiltering = false; public static string FoldersXmlPath = Path.Combine(Path.Combine(Program.BaseDirectoryExternal, ConfigIni.ConfigDir), "folders.xml"); List cuttedNodes = new List(); List deletedGames = new List(); @@ -39,10 +40,15 @@ namespace com.clusterrr.hakchi_gui if (pos1 != pos2) return pos1.CompareTo(pos2); return el1.Name.CompareTo(el2.Name); } - } - + } + public FoldersManagerForm(NesMenuCollection nesMenuCollection, MainForm mainForm = null) - { + { + Init(nesMenuCollection, false, mainForm); + } + private void Init(NesMenuCollection nesMenuCollection, bool SkipUnsorted, MainForm mainForm = null) + { + _SkipUnsortedFiltering = SkipUnsorted; try { InitializeComponent(); @@ -75,7 +81,11 @@ namespace com.clusterrr.hakchi_gui #endif Debug.WriteLine(ex.Message + ex.StackTrace); MessageBox.Show(this, message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); - } + } + } + public FoldersManagerForm(NesMenuCollection nesMenuCollection,bool SkipUnsorted, MainForm mainForm = null) + { + Init(nesMenuCollection, SkipUnsorted, mainForm); } void DrawTree() @@ -921,8 +931,9 @@ namespace com.clusterrr.hakchi_gui xml.LoadXml(xmlString); gamesCollection.Clear(); XmlToNode(xml, xml.SelectSingleNode("/Tree").ChildNodes, oldCollection, gamesCollection); + // oldCollection has only unsorted (new) games - if (oldCollection.Count > 0) + if (oldCollection.Count > 0 && !_SkipUnsortedFiltering) { NesMenuFolder unsorted; var unsorteds = from f in gamesCollection where f is NesMenuFolder && f.Name == Resources.FolderNameUnsorted select f; diff --git a/Manager/BookManager.cs b/Manager/BookManager.cs index ba4c3212..5a686809 100644 --- a/Manager/BookManager.cs +++ b/Manager/BookManager.cs @@ -90,25 +90,49 @@ namespace com.clusterrr.hakchi_gui.Manager } return currentHigh + 1; } - public void AddPage(string name) + public Page GetPageById(int pageId) { + Page ret = null; + foreach(Page p in Pages) + { + if(p.Id == pageId) + { + ret = p; + break; + } + } + return ret; + } + public Page AddPage(string name, bool forceNew) + { + Page ret = null; bool found = false; - foreach(Page p in Pages) + if (!forceNew) { - if(p.FriendlyName ==name) + foreach (Page p in Pages) { - found = true; + if (p.FriendlyName == name) + { + found = true; + ret = p; + } } } - if(!found) + if (!found) { Page p = new Page(); p.FriendlyName = name; p.Id = GetNextPageId(); Pages.Add(p); + ret = p; BookManager.getInstance().SaveSettings(); } + return ret; + } + public Page AddPage(string name) + { + return AddPage(name, true); } } public class Page diff --git a/Manager/CoverManager.cs b/Manager/CoverManager.cs index c30f2671..15b28b9b 100644 --- a/Manager/CoverManager.cs +++ b/Manager/CoverManager.cs @@ -174,6 +174,10 @@ namespace com.clusterrr.hakchi_gui.Manager new Rectangle(0, 0, outImage.Width, outImage.Height), GraphicsUnit.Pixel); gr.Flush(); outImageSmall.Save(smallDestPath, System.Drawing.Imaging.ImageFormat.Png); + image.Dispose(); + outImage.Dispose(); + outImageSmall.Dispose(); + gr.Dispose(); } ret = new Cover(destinationPath); diff --git a/Manager/RomManager.cs b/Manager/RomManager.cs index 24eb2f67..00ab1904 100644 --- a/Manager/RomManager.cs +++ b/Manager/RomManager.cs @@ -83,6 +83,25 @@ namespace com.clusterrr.hakchi_gui.Manager } return ret; } + public Rom GetRomByFileName(string fileName) + { + Rom ret = null; + + foreach (Rom r in _RomLibrary) + { + if (fileName.ToLower() == System.IO.Path.GetFileName(r.LocalPath.ToLower())) + { + ret = r; + break; + } + } + /* if(ret == null) + { + ret = AddRom(filePath); + } + */ + return ret; + } public Rom GetRom(string filePath) { Rom ret = null; diff --git a/Tooling/Tasks/SaveCurrentConfigAsBook.cs b/Tooling/Tasks/SaveCurrentConfigAsBook.cs index ded8651c..619d518d 100644 --- a/Tooling/Tasks/SaveCurrentConfigAsBook.cs +++ b/Tooling/Tasks/SaveCurrentConfigAsBook.cs @@ -7,6 +7,9 @@ namespace com.clusterrr.hakchi_gui.Tooling.Tasks { public class SaveCurrentConfigAsBook : TaskableTool { + Dictionary _AppToRom = new Dictionary(); + Dictionary _AppToCover = new Dictionary(); + public string TempRomFolder = System.IO.Path.Combine(Program.BaseDirectoryExternal, "tempRom"); public SaveCurrentConfigAsBook() : base("Saving current config as book") { } @@ -15,21 +18,155 @@ namespace com.clusterrr.hakchi_gui.Tooling.Tasks ReportStatus("Importing all selected games in library"); IOrderedEnumerable selectedGames = Manager.GameManager.GetInstance().getSelectedGames(); int processed = 0; - foreach(NesMiniApplication mn in selectedGames) + NesMenuCollection nmc = new NesMenuCollection(); + foreach (NesMiniApplication mn in selectedGames) { + nmc.Add(mn); string localRom = System.IO.Path.Combine(mn.GamePath, mn.RomFile); - if(System.IO.File.Exists(localRom)) + if(!System.IO.Directory.Exists(TempRomFolder)) + { + System.IO.Directory.CreateDirectory(TempRomFolder); + } + string gameName = mn.Name; + string fileName = System.IO.Path.GetFileNameWithoutExtension(mn.RomFile); + string ext = System.IO.Path.GetExtension(mn.RomFile); + string destRom = System.IO.Path.Combine(TempRomFolder, MakeValidFileName( gameName + ext)); + if (mn.RomFile.Trim() != "") { - Manager.RomManager.getInstance().AddRom(localRom); + System.IO.File.Copy(localRom, destRom); + _AppToRom[mn]= Manager.RomManager.getInstance().AddRom(destRom); + + System.IO.File.Delete(destRom); + } + string coverExt = System.IO.Path.GetExtension(mn.IconPath); + string destIcon = System.IO.Path.Combine(TempRomFolder, MakeValidFileName( gameName + coverExt)); + if (System.IO.File.Exists(mn.IconPath)) + { + if(System.IO.File.Exists(destIcon)) + { + System.IO.File.Delete(destIcon); + } + System.IO.File.Copy(mn.IconPath, destIcon); + _AppToCover[mn]=Manager.CoverManager.getInstance().AddCover(destIcon); + System.IO.File.Delete(destIcon); + } + else + { + } - Manager.CoverManager.getInstance().AddCover(mn.IconPath); processed++; ReportProgress(processed * 100 / selectedGames.Count()); + Console.Write(mn.RomFile); } - NesMenuCollection nmc = new NesMenuCollection(); + + ReportStatus("Loading folder structure"); + ReportProgress(0); + if (ConfigIni.FoldersMode != NesMenuCollection.SplitStyle.Custom) + { + nmc.Split(ConfigIni.FoldersMode, ConfigIni.MaxGamesPerFolder); + } + else + { + FoldersManagerForm fm = new FoldersManagerForm(nmc,true); + + nmc.AddBack(); + } + Manager.BookManager.Book theBook = Manager.BookManager.getInstance().GetBookByName("HakchiImport"); + theBook.Pages.Clear(); + Manager.BookManager.Page rootPage = theBook.AddPage("0"); + ReportStatus("Creating pages"); + AddMenu(nmc, rootPage,theBook); ReportCompleted(); + + } + Dictionary pagePerCollection = new Dictionary(); + List processedPages = new List(); + private void AddMenu(NesMenuCollection menuCollection,Manager.BookManager.Page currentPage, Manager.BookManager.Book theBook) + { + pagePerCollection[menuCollection]= currentPage; + processedPages.Add(menuCollection); + foreach (var element in menuCollection) + { + if (element is NesMiniApplication) + { + var app = element as NesMiniApplication; + Manager.BookManager.Entry entr = new Manager.BookManager.Entry(); + entr.Emulator = app.GetEmulator(); + if (_AppToRom.ContainsKey(app)) + { + entr.Rom = _AppToRom[app]; + } + if (_AppToCover.ContainsKey(app)) + { + entr.Cover = _AppToCover[app]; + } + entr.Label = app.Name; + currentPage.Entries.Add(entr); + } + if (element is NesMenuFolder) + { + + var folder = element as NesMenuFolder; + if(!pagePerCollection.ContainsKey(folder.ChildMenuCollection)) + { + Manager.BookManager.Page p = theBook.AddPage(folder.Name); + Manager.BookManager.Entry entr = new Manager.BookManager.Entry(); + entr.IsLink = true; + entr.Label = folder.Name; + entr.PageId = p.Id; + currentPage.Entries.Add(entr); + AddMenu(folder.ChildMenuCollection, p, theBook); + } + else + { + Manager.BookManager.Entry entr = new Manager.BookManager.Entry(); + entr.IsLink = true; + entr.Label = folder.Name; + entr.PageId = pagePerCollection[folder.ChildMenuCollection].Id; + currentPage.Entries.Add(entr); + } + /* + + if (folder.ChildMenuCollection.Count() > 0) + { + if (!processedPages.Contains(folder.ChildMenuCollection)) + { + Manager.BookManager.Page p = theBook.AddPage(folder.Name); + Manager.BookManager.Entry entr = new Manager.BookManager.Entry(); + entr.IsLink = true; + entr.Label = folder.Name; + entr.PageId = p.Id; + currentPage.Entries.Add(entr); + AddMenu(folder.ChildMenuCollection, p,currentPage, theBook); + } + } + else + { + //Most likely a back, but not quite sure... + if(parent != null) + { + Manager.BookManager.Entry entr = new Manager.BookManager.Entry(); + entr.IsLink = true; + entr.Label = folder.Name; + entr.PageId = parent.Id; + currentPage.Entries.Add(entr); + } + }*/ + + + } + + } + } + + private static string MakeValidFileName(string name) + { + string invalidChars = System.Text.RegularExpressions.Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars())); + string invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars); + + return System.Text.RegularExpressions.Regex.Replace(name, invalidRegStr, "_"); } } } diff --git a/UI/Components/EntryMenuItem.cs b/UI/Components/EntryMenuItem.cs index d9f75515..fd027009 100644 --- a/UI/Components/EntryMenuItem.cs +++ b/UI/Components/EntryMenuItem.cs @@ -8,9 +8,13 @@ namespace com.clusterrr.hakchi_gui.UI.Components public class EntryMenuItem : System.Windows.Forms.ToolStripMenuItem { Manager.BookManager.Entry _TheEntry; - public EntryMenuItem(Manager.BookManager.Entry entr):base(entr.Label) + Manager.BookManager.Book _TheBook; + Manager.BookManager.Page _ThePage; + public EntryMenuItem(Manager.BookManager.Entry entr,Manager.BookManager.Page page,Manager.BookManager.Book book):base(entr.Label) { _TheEntry = entr; + _TheBook = book; + _ThePage = page; ToolStripMenuItem entryEdititm = new ToolStripMenuItem("Edit"); entryEdititm.Click += EntryEdititm_Click; @@ -28,7 +32,10 @@ namespace com.clusterrr.hakchi_gui.UI.Components private void EntryEdititm_Click(object sender, EventArgs e) { - throw new NotImplementedException(); + UI.EntryCreator ec = new EntryCreator(_TheBook); + ec.EditEntry(_TheEntry); + ec.ShowDialog(); + } } } diff --git a/UI/Components/PageMenuItem.cs b/UI/Components/PageMenuItem.cs index 481b3107..207c6a34 100644 --- a/UI/Components/PageMenuItem.cs +++ b/UI/Components/PageMenuItem.cs @@ -23,7 +23,7 @@ namespace com.clusterrr.hakchi_gui.UI.Components foreach (Manager.BookManager.Entry ee in _ThePage.Entries) { - DropDownItems.Add(new EntryMenuItem(ee)); + DropDownItems.Add(new EntryMenuItem(ee,_ThePage,_theBook)); } this.DropDownItems.Add(new ToolStripSeparator()); ToolStripMenuItem ts = new ToolStripMenuItem("Add Entry"); diff --git a/UI/Forms/EntryCreator.Designer.cs b/UI/Forms/EntryCreator.Designer.cs index 4a59dd36..cb9423f3 100644 --- a/UI/Forms/EntryCreator.Designer.cs +++ b/UI/Forms/EntryCreator.Designer.cs @@ -29,8 +29,8 @@ private void InitializeComponent() { this.tabControl1 = new System.Windows.Forms.TabControl(); - this.tabPage1 = new System.Windows.Forms.TabPage(); - this.tabPage2 = new System.Windows.Forms.TabPage(); + this.tpLink = new System.Windows.Forms.TabPage(); + this.tpGame = new System.Windows.Forms.TabPage(); this.label1 = new System.Windows.Forms.Label(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.label2 = new System.Windows.Forms.Label(); @@ -44,8 +44,8 @@ this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.tabControl1.SuspendLayout(); - this.tabPage1.SuspendLayout(); - this.tabPage2.SuspendLayout(); + this.tpLink.SuspendLayout(); + this.tpGame.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // @@ -53,39 +53,39 @@ // this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.tabControl1.Controls.Add(this.tabPage1); - this.tabControl1.Controls.Add(this.tabPage2); + this.tabControl1.Controls.Add(this.tpLink); + this.tabControl1.Controls.Add(this.tpGame); this.tabControl1.Location = new System.Drawing.Point(12, 12); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; this.tabControl1.Size = new System.Drawing.Size(223, 137); this.tabControl1.TabIndex = 0; // - // tabPage1 - // - this.tabPage1.Controls.Add(this.comboBox1); - this.tabPage1.Controls.Add(this.label1); - this.tabPage1.Location = new System.Drawing.Point(4, 22); - this.tabPage1.Name = "tabPage1"; - this.tabPage1.Padding = new System.Windows.Forms.Padding(3); - this.tabPage1.Size = new System.Drawing.Size(215, 111); - this.tabPage1.TabIndex = 0; - this.tabPage1.Text = "Link"; - this.tabPage1.UseVisualStyleBackColor = true; - // - // tabPage2 - // - this.tabPage2.Controls.Add(this.cmbRoms); - this.tabPage2.Controls.Add(this.label4); - this.tabPage2.Controls.Add(this.cmbEmulator); - this.tabPage2.Controls.Add(this.label3); - this.tabPage2.Location = new System.Drawing.Point(4, 22); - this.tabPage2.Name = "tabPage2"; - this.tabPage2.Padding = new System.Windows.Forms.Padding(3); - this.tabPage2.Size = new System.Drawing.Size(215, 111); - this.tabPage2.TabIndex = 1; - this.tabPage2.Text = "Game"; - this.tabPage2.UseVisualStyleBackColor = true; + // tpLink + // + this.tpLink.Controls.Add(this.comboBox1); + this.tpLink.Controls.Add(this.label1); + this.tpLink.Location = new System.Drawing.Point(4, 22); + this.tpLink.Name = "tpLink"; + this.tpLink.Padding = new System.Windows.Forms.Padding(3); + this.tpLink.Size = new System.Drawing.Size(215, 111); + this.tpLink.TabIndex = 0; + this.tpLink.Text = "Link"; + this.tpLink.UseVisualStyleBackColor = true; + // + // tpGame + // + this.tpGame.Controls.Add(this.cmbRoms); + this.tpGame.Controls.Add(this.label4); + this.tpGame.Controls.Add(this.cmbEmulator); + this.tpGame.Controls.Add(this.label3); + this.tpGame.Location = new System.Drawing.Point(4, 22); + this.tpGame.Name = "tpGame"; + this.tpGame.Padding = new System.Windows.Forms.Padding(3); + this.tpGame.Size = new System.Drawing.Size(215, 111); + this.tpGame.TabIndex = 1; + this.tpGame.Text = "Game"; + this.tpGame.UseVisualStyleBackColor = true; // // label1 // @@ -218,10 +218,10 @@ this.Name = "EntryCreator"; this.Text = "EntryCreator"; this.tabControl1.ResumeLayout(false); - this.tabPage1.ResumeLayout(false); - this.tabPage1.PerformLayout(); - this.tabPage2.ResumeLayout(false); - this.tabPage2.PerformLayout(); + this.tpLink.ResumeLayout(false); + this.tpLink.PerformLayout(); + this.tpGame.ResumeLayout(false); + this.tpGame.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -231,14 +231,14 @@ #endregion private System.Windows.Forms.TabControl tabControl1; - private System.Windows.Forms.TabPage tabPage1; + private System.Windows.Forms.TabPage tpLink; private System.Windows.Forms.Button button1; private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.ComboBox cmbCover; private System.Windows.Forms.Label label2; private System.Windows.Forms.ComboBox comboBox1; private System.Windows.Forms.Label label1; - private System.Windows.Forms.TabPage tabPage2; + private System.Windows.Forms.TabPage tpGame; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.ComboBox cmbRoms; private System.Windows.Forms.Label label4; diff --git a/UI/Forms/EntryCreator.cs b/UI/Forms/EntryCreator.cs index b1c6be8d..e4c563ce 100644 --- a/UI/Forms/EntryCreator.cs +++ b/UI/Forms/EntryCreator.cs @@ -11,6 +11,7 @@ namespace com.clusterrr.hakchi_gui.UI { public partial class EntryCreator : Form { + private bool EditMode = false; private Manager.BookManager.Book _theBook; public EntryCreator(Manager.BookManager.Book theBook) { @@ -29,6 +30,24 @@ namespace com.clusterrr.hakchi_gui.UI cmbEmulator.Items.Add(emu); } } + public void EditEntry(Manager.BookManager.Entry _entr) + { + EditMode = true; + entr = _entr; + if(entr.IsLink) + { + tabControl1.SelectedTab = tpLink; + comboBox1.SelectedItem = _theBook.GetPageById(_entr.PageId); + } + else + { + tabControl1.SelectedTab = tpGame; + cmbEmulator.SelectedItem = _entr.Emulator; + cmbRoms.SelectedItem = _entr.Rom; + } + textBox1.Text = _entr.Label; + cmbCover.SelectedItem = _entr.Cover; + } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { @@ -55,10 +74,18 @@ namespace com.clusterrr.hakchi_gui.UI { if(textBox1.Text.Trim() != "") { - Manager.BookManager.Entry le = new Manager.BookManager.Entry(); + Manager.BookManager.Entry le = null; + if (EditMode) + { + le = entr; + } + else + { + le= new Manager.BookManager.Entry(); + } le.Cover = (Manager.CoverManager.Cover)cmbCover.SelectedItem; le.Label = textBox1.Text; - if (tabControl1.SelectedTab.Text == "Link") + if (tabControl1.SelectedTab == tpLink) { le.PageId = ((Manager.BookManager.Page)comboBox1.SelectedItem).Id; le.IsLink = true; -- cgit v1.2.3