Ejemplo n.º 1
0
        void XmlToTree(string xmlString)
        {
            GamesCollection.Unsplit();
            var oldCollection = new NesMenuCollection();

            oldCollection.AddRange(GamesCollection);
            var xml = new XmlDocument();

            xml.LoadXml(xmlString);
            GamesCollection.Clear();
            XmlToNode(xml, xml.SelectSingleNode("/Tree").ChildNodes, oldCollection, GamesCollection);
            // oldCollection has only unsorted (new) games
            if (oldCollection.Count > 0)
            {
                var unsorted = new NesMenuFolder(Resources.FolderNameUnsorted);
                unsorted.Position = NesMenuFolder.Priority.Leftmost;
                foreach (var game in oldCollection)
                {
                    unsorted.ChildMenuCollection.Add(game);
                }
                GamesCollection.Add(unsorted);
                MessageBox.Show(this, Resources.NewGamesUnsorted, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            DrawTree();
        }
        void XmlToTree(string xmlString)
        {
            gamesCollection.Unsplit();
            var oldCollection = new NesMenuCollection();

            oldCollection.AddRange(gamesCollection);
            var xml = new XmlDocument();

            xml.LoadXml(xmlString);
            gamesCollection.Clear();
            XmlToNode(xml, xml.SelectSingleNode("/Tree").ChildNodes, oldCollection, gamesCollection);
            // oldCollection has only unsorted (new) games
            if (oldCollection.Count > 0)
            {
                NesMenuFolder unsorted;
                var           unsorteds = from f in gamesCollection where f is NesMenuFolder && f.Name == Resources.FolderNameUnsorted select f;
                if (unsorteds.Count() > 0)
                {
                    unsorted = unsorteds.First() as NesMenuFolder;
                }
                else
                {
                    unsorted          = new NesMenuFolder(Resources.FolderNameUnsorted);
                    unsorted.Position = NesMenuFolder.Priority.Leftmost;
                    gamesCollection.Add(unsorted);
                }
                foreach (var game in oldCollection)
                {
                    unsorted.ChildMenuCollection.Add(game);
                }
                MessageBox.Show(this, Resources.NewGamesUnsorted, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            DrawTree();
        }
        void XmlToNode(XmlDocument xml, XmlNodeList elements, NesMenuCollection rootMenuCollection, NesMenuCollection nesMenuCollection = null)
        {
            if (nesMenuCollection == null)
            {
                nesMenuCollection = rootMenuCollection;
            }
            foreach (XmlNode element in elements)
            {
                switch (element.Name)
                {
                case "Folder":
                    var folder = new NesMenuFolder(element.Attributes["name"].Value, element.Attributes["icon"].Value);
                    folder.Position = (NesMenuFolder.Priority) byte.Parse(element.Attributes["position"].Value);
                    nesMenuCollection.Add(folder);
                    XmlToNode(xml, element.ChildNodes, rootMenuCollection, folder.ChildMenuCollection);
                    break;

                case "Game":
                case "OriginalGame":
                    var code  = element.Attributes["code"].Value;
                    var games = from n in rootMenuCollection where ((n is NesMiniApplication || n is NesDefaultGame) && (n.Code == code)) select n;
                    if (games.Count() > 0)
                    {
                        var game = games.First();
                        nesMenuCollection.Add(game);
                        rootMenuCollection.Remove(game);
                    }
                    break;
                }
            }
        }
        void AddNodes(TreeNodeCollection treeNodeCollection, NesMenuCollection nesMenuCollection, List <NesMenuCollection> usedFolders = null)
        {
            if (usedFolders == null)
            {
                usedFolders = new List <NesMenuCollection>();
            }
            if (usedFolders.Contains(nesMenuCollection))
            {
                return;
            }
            usedFolders.Add(nesMenuCollection);
            var sorted = nesMenuCollection.OrderBy(o => o.Name).OrderBy(o => (o is NesMenuFolder) ? (byte)(o as NesMenuFolder).Position : 2);

            foreach (var nesElement in sorted)
            {
                var newNode = new TreeNode();
                if (nesElement is NesMenuFolder)
                {
                    if (usedFolders.Contains((nesElement as NesMenuFolder).ChildMenuCollection))
                    {
                        nesMenuCollection.Remove(nesElement); // We don't need any "back" folders
                        continue;
                    }
                }
                newNode.SelectedImageIndex = newNode.ImageIndex = getImageIndex(nesElement as INesMenuElement);
                newNode.Text = nesElement.Name;
                newNode.Tag  = nesElement;
                treeNodeCollection.Add(newNode);
                if (nesElement is NesMenuFolder)
                {
                    AddNodes(newNode.Nodes, (nesElement as NesMenuFolder).ChildMenuCollection, usedFolders);
                }
            }
        }
Ejemplo n.º 5
0
        DialogResult FoldersManagerFromThread(NesMenuCollection collection)
        {
            if (InvokeRequired)
            {
                return((DialogResult)Invoke(new Func <NesMenuCollection, DialogResult>(FoldersManagerFromThread), new object[] { collection }));
            }
            var constructor = new FoldersManagerForm(collection, MainForm);

            TaskbarProgress.SetState(this, TaskbarProgress.TaskbarStates.Paused);
            var result = constructor.ShowDialog();

            TaskbarProgress.SetState(this, TaskbarProgress.TaskbarStates.Normal);
            return(result);
        }
Ejemplo n.º 6
0
        public FoldersManagerForm(NesMenuCollection nesMenuCollection, MainForm mainForm = null)
        {
            try
            {
                InitializeComponent();
                gamesCollection = nesMenuCollection;
                this.mainForm   = mainForm;
                if (File.Exists(FoldersXmlPath))
                {
                    try
                    {
                        XmlToTree(File.ReadAllText(FoldersXmlPath));
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.Message + ex.StackTrace);
                        Tasks.ErrorForm.Show(mainForm, ex);
                        File.Delete(FoldersXmlPath);
                        return;
                    }
                }
                else
                {
                    DrawTree();
                }
                splitContainer.Panel2MinSize       = 485;
                comboBoxPosition.Left              = labelPosition1.Left + labelPosition1.Width;
                treeView.TreeViewNodeSorter        = new NodeSorter();
                listViewContent.ListViewItemSorter = new NodeSorter();
                pictureBoxLeft = pictureBoxArt.Left;

                switch (ConfigIni.Instance.BackFolderPosition)
                {
                case NesMenuFolder.Priority.LeftBack: comboBoxBackPosition.SelectedIndex = 0; break;

                case NesMenuFolder.Priority.Back: comboBoxBackPosition.SelectedIndex = 1; break;
                }
                checkBoxAddHome.Checked = ConfigIni.Instance.HomeFolder;
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message + ex.StackTrace);
                Tasks.ErrorForm.Show(mainForm, ex);
            }
        }
Ejemplo n.º 7
0
        public FoldersManagerForm(NesMenuCollection nesMenuCollection, MainForm mainForm = null)
        {
            try
            {
                InitializeComponent();
                Icon            = Resources.icon;
                gamesCollection = nesMenuCollection;
                this.mainForm   = mainForm;
                if (File.Exists(FoldersXmlPath))
                {
                    try
                    {
                        XmlToTree(File.ReadAllText(FoldersXmlPath));
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message + ex.StackTrace);
                        File.Delete(FoldersXmlPath);
                        throw ex;
                    }
                }
                else
                {
                    DrawTree();
                }
                splitContainer.Panel2MinSize       = 485;
                comboBoxPosition.Left              = labelPosition.Left + labelPosition.Width;
                treeView.TreeViewNodeSorter        = new NodeSorter();
                listViewContent.ListViewItemSorter = new NodeSorter();
                pictureBoxLeft = pictureBoxArt.Left;
            }
            catch (Exception ex)
            {
                var message = ex.Message;
#if DEBUG
                message += ex.StackTrace;
#endif
                Debug.WriteLine(ex.Message + ex.StackTrace);
                MessageBox.Show(this, message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 8
0
        public TreeContructorForm(NesMenuCollection nesMenuCollection)
        {
            try
            {
                InitializeComponent();
                GamesCollection = nesMenuCollection;
                if (File.Exists(FoldersXmlPath))
                {
                    try
                    {
                        XmlToTree(File.ReadAllText(FoldersXmlPath));
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message + ex.StackTrace);
                        File.Delete(FoldersXmlPath);
                        throw ex;
                    }
                }
                else
                {
                    DrawTree();
                }
                splitContainer.Panel2MinSize       = 485;
                treeView.TreeViewNodeSorter        = new NodeSorter();
                listViewContent.ListViewItemSorter = new NodeSorter();
            }
            catch (Exception ex)
            {
                var message = ex.Message;
#if DEBUG
                message += ex.StackTrace;
#endif
                Debug.WriteLine(ex.Message + ex.StackTrace);
                MessageBox.Show(this, message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 9
0
        void TrimFolderNames(NesMenuCollection nesMenuCollection)
        {
            const int minChars = 3;
            const int maxChars = 8;
            var       folders  = nesMenuCollection.Where(o => o is NesMenuFolder).OrderBy(o => o.Name).ToArray();

            for (int i = 1; i < folders.Length; i++)
            {
                var prevFolder    = i > 0 ? (folders[i - 1] as NesMenuFolder) : null;
                var currentFolder = folders[i] as NesMenuFolder;
                var nameA         = prevFolder.NameParts[1];
                var nameB         = currentFolder.NameParts[0];
                int l             = Math.Min(maxChars - 1, Math.Max(nameA.Length, nameB.Length));
                while ((nameA.Substring(0, Math.Min(l, nameA.Length)) !=
                        nameB.Substring(0, Math.Min(l, nameB.Length))) && l >= minChars)
                {
                    l--;
                }
                nameA = nameA.Substring(0, Math.Min(l + 1, nameA.Length));
                nameB = nameB.Substring(0, Math.Min(l + 1, nameB.Length));
                if (nameA == nameB) // There is no point to make long name
                {
                    nameA = nameB = nameA.Substring(0, Math.Min(minChars, nameA.Length));
                }
                prevFolder.NameParts    = new string[] { prevFolder.NameParts[0], nameA };
                currentFolder.NameParts = new string[] { nameB, currentFolder.NameParts[1] };
            }
            if (folders.Length > 0)
            {
                var firstFolder = folders[0] as NesMenuFolder;
                firstFolder.NameParts = new string[] { firstFolder.NameParts[0].Substring(0, Math.Min(firstFolder.NameParts[0].Length, minChars)), firstFolder.NameParts[1] };

                var lastFolder = folders[folders.Length - 1] as NesMenuFolder;
                lastFolder.NameParts = new string[] { lastFolder.NameParts[0], lastFolder.NameParts[1].Substring(0, Math.Min(lastFolder.NameParts[1].Length, minChars)), };
            }
        }
Ejemplo n.º 10
0
        private void AddMenu(NesMenuCollection menuCollection, Dictionary <string, string> originalGames, GamesTreeStats stats = null)
        {
            if (stats == null)
            {
                stats = new GamesTreeStats();
            }
            if (!stats.allMenus.Contains(menuCollection))
            {
                stats.allMenus.Add(menuCollection);
            }
            int    menuIndex = stats.allMenus.IndexOf(menuCollection);
            string targetDirectory;

            if (menuIndex == 0)
            {
                targetDirectory = tempGamesDirectory;
            }
            else
            {
                targetDirectory = Path.Combine(tempGamesDirectory, string.Format("{0:D3}", menuIndex));
            }
            foreach (var element in menuCollection)
            {
                if (element is NesMiniApplication)
                {
                    if (element is NesDefaultGame)
                    {
                        var game = element as NesDefaultGame;
                        stats.TotalSize         += game.Size;
                        originalGames[game.Code] = menuIndex == 0 ? "." : string.Format("{0:D3}", menuIndex);
                    }
                    else
                    {
                        stats.TotalGames++;
                        var game     = element as NesMiniApplication;
                        var gameSize = game.Size();
                        Debug.WriteLine(string.Format("Processing {0} ('{1}'), size: {2}KB", game.Code, game.Name, gameSize / 1024));
                        var gameCopy = game.CopyTo(targetDirectory);
                        stats.TotalSize    += gameSize;
                        stats.TransferSize += gameSize;
                        stats.TotalGames++;
                        try
                        {
                            if (gameCopy is NesGame && File.Exists((gameCopy as NesGame).GameGeniePath))
                            {
                                (gameCopy as NesGame).ApplyGameGenie();
                                File.Delete((gameCopy as NesGame).GameGeniePath);
                            }
                        }
                        catch (GameGenieFormatException ex)
                        {
                            ShowError(new Exception(string.Format(Resources.GameGenieFormatError, ex.Code, game.Name)), dontStop: true);
                        }
                        catch (GameGenieNotFoundException ex)
                        {
                            ShowError(new Exception(string.Format(Resources.GameGenieNotFound, ex.Code, game.Name)), dontStop: true);
                        }
                    }
                }
                if (element is NesMenuFolder)
                {
                    var folder = element as NesMenuFolder;
                    if (!stats.allMenus.Contains(folder.ChildMenuCollection))
                    {
                        stats.allMenus.Add(folder.ChildMenuCollection);
                        AddMenu(folder.ChildMenuCollection, originalGames, stats);
                    }
                    folder.ChildIndex = stats.allMenus.IndexOf(folder.ChildMenuCollection);
                    var folderDir  = Path.Combine(targetDirectory, folder.Code);
                    var folderSize = folder.Save(folderDir);
                    stats.TotalSize    += folderSize;
                    stats.TransferSize += folderSize;
                }
            }
        }
Ejemplo n.º 11
0
        private void AddMenu(NesMenuCollection menuCollection, GamesTreeStats stats = null)
        {
            if (stats == null)
            {
                stats = new GamesTreeStats();
            }
            if (!stats.allMenus.Contains(menuCollection))
            {
                stats.allMenus.Add(menuCollection);
            }
            int    menuIndex = stats.allMenus.IndexOf(menuCollection);
            string targetDirectory;

            if (menuIndex == 0)
            {
                targetDirectory = tempGamesDirectory;
            }
            else
            {
                targetDirectory = Path.Combine(tempGamesDirectory, string.Format("{0:D3}", menuIndex));
            }
            foreach (var element in menuCollection)
            {
                if (element is NesMiniApplication)
                {
                    stats.GamesTotal++;
                    if (stats.Size >= maxRamfsSize)
                    {
                        continue;
                    }
                    stats.GamesProceed++;
                    if (stats.GamesStart >= stats.GamesProceed)
                    {
                        continue;
                    }
                    var game = element as NesMiniApplication;
                    Debug.Write(string.Format("Processing {0} ('{1}'), #{2}", game.Code, game.Name, stats.GamesProceed));
                    var gameCopy = game.CopyTo(targetDirectory);
                    stats.Size += gameCopy.Size();
                    if (stats.Size >= maxRamfsSize)
                    {
                        // Rollback. Just in case of huge last game
                        stats.GamesProceed--;
                        Directory.Delete(gameCopy.GamePath, true);
                        continue;
                    }
                    Debug.WriteLine(string.Format(", total size: {0}", stats.Size));
                    try
                    {
                        if (gameCopy is NesGame && File.Exists((gameCopy as NesGame).GameGeniePath))
                        {
                            (gameCopy as NesGame).ApplyGameGenie();
                            File.Delete((gameCopy as NesGame).GameGeniePath);
                        }
                    }
                    catch (GameGenieFormatException ex)
                    {
                        ShowError(new GameGenieFormatException(string.Format(Resources.GameGenieFormatError, ex.Code, game)), dontStop: true);
                    }
                    catch (GameGenieNotFoundException ex)
                    {
                        ShowError(new GameGenieNotFoundException(string.Format(Resources.GameGenieNotFound, ex.Code, game.Name)), dontStop: true);
                    }
                }
                if (element is NesMenuFolder)
                {
                    var folder = element as NesMenuFolder;
                    if (!stats.allMenus.Contains(folder.ChildMenuCollection))
                    {
                        stats.allMenus.Add(folder.ChildMenuCollection);
                        AddMenu(folder.ChildMenuCollection, stats);
                    }
                    if (stats.GamesStart == 0)
                    {
                        folder.ChildIndex = stats.allMenus.IndexOf(folder.ChildMenuCollection);
                        var folderDir = Path.Combine(targetDirectory, folder.Code);
                        folder.Save(folderDir);
                    }
                }
                if (element is NesDefaultGame)
                {
                    if (stats.GamesStart == 0)
                    {
                        var game      = element as NesDefaultGame;
                        var gfilePath = Path.Combine(originalGamesConfigDirectory, string.Format("gpath-{0}", game.Code));
                        File.WriteAllText(gfilePath, menuIndex == 0 ? "." : string.Format("{0:D3}", menuIndex));
                    }
                }
            }
        }
Ejemplo n.º 12
0
 public FoldersManagerForm(NesMenuCollection nesMenuCollection, bool SkipUnsorted, MainForm mainForm = null)
 {
     Init(nesMenuCollection, SkipUnsorted, mainForm);
 }
Ejemplo n.º 13
0
        public void Split(SplitStyle style, int maxElements = 35)
        {
            bool originalToRoot = false;
            int  originalCount  = 0;

            switch (style)
            {
            case SplitStyle.Original_NoSplit:
            case SplitStyle.Original_Auto:
            case SplitStyle.Original_FoldersAlphabetic_FoldersEqual:
            case SplitStyle.Original_FoldersAlphabetic_PagesEqual:
            case SplitStyle.Original_FoldersEqual:
            case SplitStyle.Original_PagesEqual:
                style--;
                originalCount = this.Where(o => (o is NesApplication) && (o as NesApplication).IsOriginalGame).Count();
                if (originalCount > 0)
                {
                    originalToRoot = true;
                }
                break;
            }
            if (style == SplitStyle.NoSplit && !originalToRoot)
            {
                return;
            }
            if (((style == SplitStyle.Auto && !originalToRoot) ||
                 (style == SplitStyle.FoldersEqual && !originalToRoot) ||
                 (style == SplitStyle.PagesEqual) && !originalToRoot) &&
                (Count <= maxElements))
            {
                return;
            }
            var total      = Count - originalCount;
            var partsCount = (int)Math.Ceiling((float)total / (float)maxElements);
            var perPart    = (int)Math.Ceiling((float)total / (float)partsCount);
            var alphaNum   = new Regex(@"[^\p{L}\p{Nd}0-9]", RegexOptions.Compiled); //var alphaNum = new Regex("[^a-zA-Z0-9]");

            NesMenuCollection root;

            if (!originalToRoot)
            {
                root = this;
            }
            else
            {
                root = new NesMenuCollection();
                root.AddRange(this.Where(o => (o is NesApplication) && !(o as NesApplication).IsOriginalGame));
                if (root.Count == 0)
                {
                    return;
                }
                this.RemoveAll(o => root.Contains(o));
                this.Add(new NesMenuFolder()
                {
                    Name                = Resources.FolderNameMoreGames,
                    Position            = NesMenuFolder.Priority.Rightmost,
                    ChildMenuCollection = root
                });
            }

            var sorted      = root.OrderBy(o => o.SortName);
            var collections = new List <NesMenuCollection>();
            int i           = 0;

            if (style == SplitStyle.Auto || style == SplitStyle.FoldersEqual || style == SplitStyle.PagesEqual)
            {
                var collection = new NesMenuCollection();
                foreach (var game in sorted)
                {
                    collection.Add(game);
                    i++;
                    if (((i % perPart) == 0) || (i == sorted.Count()))
                    {
                        collections.Add(collection);
                        collection = new NesMenuCollection();
                    }
                }
            }

            if (style == SplitStyle.Auto)
            {
                if (collections.Count >= 12)
                {
                    style = SplitStyle.FoldersEqual;
                }
                else
                {
                    style = SplitStyle.PagesEqual;
                }
            }

            // Folders, equal
            if (style == SplitStyle.FoldersEqual) // minimum amount of games/folders on screen without glitches
            {
                root.Clear();
                foreach (var coll in collections)
                {
                    var fname = alphaNum.Replace(coll.Where(o => o is NesApplication).First().SortName.ToUpper(), "");
                    var lname = alphaNum.Replace(coll.Where(o => o is NesApplication).Last().SortName.ToUpper(), "");

                    System.Diagnostics.Debug.WriteLine($"fname:\"{fname}\", lname:\"{lname}\"");

                    var folder = new NesMenuFolder()
                    {
                        ChildMenuCollection = coll, NameParts = new string[] { fname, lname }, Position = NesMenuFolder.Priority.Right
                    };
                    coll.Add(new NesMenuFolder()
                    {
                        Name = Resources.FolderNameBack, ImageId = "folder_back", Position = ConfigIni.Instance.BackFolderPosition, ChildMenuCollection = root
                    });
                    root.Add(folder);
                }
                TrimFolderNames(root);
            }
            else if (style == SplitStyle.PagesEqual)
            // Pages, equal
            {
                root.Clear();
                root.AddRange(collections[0]);
                collections[0] = root;
                for (i = 0; i < collections.Count; i++)
                {
                    for (int j = i - 1; j >= 0; j--)
                    {
                        var fname  = alphaNum.Replace(collections[j].Where(o => o is NesApplication).First().SortName.ToUpper(), "");
                        var lname  = alphaNum.Replace(collections[j].Where(o => o is NesApplication).Last().SortName.ToUpper(), "");
                        var folder = new NesMenuFolder()
                        {
                            ChildMenuCollection = collections[j],
                            NameParts           = new string[] { fname, lname },
                            Position            = NesMenuFolder.Priority.Left
                        };
                        collections[i].Insert(0, folder);
                    }
                    for (int j = i + 1; j < collections.Count; j++)
                    {
                        var fname  = alphaNum.Replace(collections[j].Where(o => o is NesApplication).First().SortName.ToUpper(), "");
                        var lname  = alphaNum.Replace(collections[j].Where(o => o is NesApplication).Last().SortName.ToUpper(), "");
                        var folder = new NesMenuFolder()
                        {
                            ChildMenuCollection = collections[j],
                            NameParts           = new string[] { fname, lname },
                            Position            = NesMenuFolder.Priority.Right
                        };
                        collections[i].Insert(collections[i].Count, folder);
                    }
                    TrimFolderNames(collections[i]);
                }
            }
            else if (style == SplitStyle.FoldersAlphabetic_PagesEqual || style == SplitStyle.FoldersAlphabetic_FoldersEqual)
            {
                var letters = new Dictionary <char, NesMenuCollection>();
                for (char ch = 'A'; ch <= 'Z'; ch++)
                {
                    letters[ch] = new NesMenuCollection();
                }
                letters['#'] = new NesMenuCollection();
                foreach (var game in root)
                {
                    if (!(game is NesApplication))
                    {
                        continue;
                    }
                    var letter = game.SortName.Substring(0, 1).ToUpper()[0];
                    if (letter < 'A' || letter > 'Z')
                    {
                        letter = '#';
                    }
                    letters[letter].Add(game);
                }

                root.Clear();
                foreach (var letter in letters.Keys)
                {
                    if (letters[letter].Count > 0)
                    {
                        string folderImageId = "folder_" + letter.ToString().ToLower();
                        if (letter < 'A' || letter > 'Z')
                        {
                            folderImageId = "folder_number";
                        }
                        var folder = new NesMenuFolder()
                        {
                            ChildMenuCollection = letters[letter], Name = letter.ToString(), Position = NesMenuFolder.Priority.Right, ImageId = folderImageId
                        };
                        if (style == SplitStyle.FoldersAlphabetic_PagesEqual)
                        {
                            folder.ChildMenuCollection.Split(SplitStyle.PagesEqual, maxElements);
                            folder.ChildMenuCollection.Add(new NesMenuFolder()
                            {
                                Name = Resources.FolderNameBack, ImageId = "folder_back", Position = ConfigIni.Instance.BackFolderPosition, ChildMenuCollection = root
                            });
                            foreach (NesMenuFolder f in folder.ChildMenuCollection.Where(o => o is NesMenuFolder))
                            {
                                if (f.ChildMenuCollection != root)
                                {
                                    f.ChildMenuCollection.Add(new NesMenuFolder()
                                    {
                                        Name = Resources.FolderNameBack, ImageId = "folder_back", Position = ConfigIni.Instance.BackFolderPosition, ChildMenuCollection = root
                                    });
                                }
                            }
                        }
                        else if (style == SplitStyle.FoldersAlphabetic_FoldersEqual)
                        {
                            folder.ChildMenuCollection.Split(SplitStyle.FoldersEqual, maxElements);
                            folder.ChildMenuCollection.Add(new NesMenuFolder()
                            {
                                Name = Resources.FolderNameBack, ImageId = "folder_back", Position = ConfigIni.Instance.BackFolderPosition, ChildMenuCollection = root
                            });
                        }
                        root.Add(folder);
                    }
                }
            }
            else if (style == SplitStyle.FoldersGroupByApp)
            {
                var apps       = new SortedDictionary <string, NesMenuCollection>();
                var customApps = new Dictionary <string, NesMenuCollection>();
                foreach (var system in CoreCollection.Systems)
                {
                    apps[system] = new NesMenuCollection();
                }
                foreach (var ai in AppTypeCollection.Apps)
                {
                    if (!apps.ContainsKey(ai.Name))
                    {
                        apps[ai.Name] = new NesMenuCollection();
                    }
                }
                apps[AppTypeCollection.UnknownApp.Name] = new NesMenuCollection();

                foreach (var game in root)
                {
                    if (!(game is NesApplication))
                    {
                        continue;
                    }
                    NesApplication app = game as NesApplication;

                    AppTypeCollection.AppInfo ai = app.Metadata.AppInfo;
                    if (!ai.Unknown && apps.ContainsKey(ai.Name))
                    {
                        apps[ai.Name].Add(game);
                    }
                    else if (!string.IsNullOrEmpty(app.Metadata.System) && apps.ContainsKey(app.Metadata.System))
                    {
                        apps[app.Metadata.System].Add(game);
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(app.Desktop.Bin))
                        {
                            if (!customApps.ContainsKey(app.Desktop.Bin))
                            {
                                customApps.Add(app.Desktop.Bin, new NesMenuCollection());
                            }
                            customApps[app.Desktop.Bin].Add(game);
                        }
                        else
                        {
                            apps[AppTypeCollection.UnknownApp.Name].Add(game);
                        }
                    }
                }

                root.Clear();
                foreach (var app in apps)
                {
                    if (app.Value.Count > 0)
                    {
                        string folderImageId = "folder";
                        var    folder        = new NesMenuFolder()
                        {
                            ChildMenuCollection = app.Value, Name = app.Key, Position = NesMenuFolder.Priority.Right, ImageId = folderImageId
                        };
                        //folder.ChildMenuCollection.Split(SplitStyle.FoldersEqual, maxElements);
                        folder.ChildMenuCollection.Add(new NesMenuFolder()
                        {
                            Name = Resources.FolderNameBack, ImageId = "folder_back", Position = ConfigIni.Instance.BackFolderPosition, ChildMenuCollection = root
                        });
                        root.Add(folder);
                    }
                }
                foreach (var app in customApps)
                {
                    if (app.Value.Count > 0)
                    {
                        string folderImageId = "folder";
                        var    folder        = new NesMenuFolder()
                        {
                            ChildMenuCollection = app.Value, Name = app.Key, Position = NesMenuFolder.Priority.Right, ImageId = folderImageId
                        };
                        //folder.ChildMenuCollection.Split(SplitStyle.FoldersEqual, maxElements);
                        folder.ChildMenuCollection.Add(new NesMenuFolder()
                        {
                            Name = Resources.FolderNameBack, ImageId = "folder_back", Position = ConfigIni.Instance.BackFolderPosition, ChildMenuCollection = root
                        });
                        root.Add(folder);
                    }
                }
            }
            if (originalToRoot)
            {
                if (style != SplitStyle.PagesEqual)
                {
                    root.Add(new NesMenuFolder()
                    {
                        Name = Resources.FolderNameOriginalGames, ImageId = "folder_back", Position = ConfigIni.Instance.BackFolderPosition, ChildMenuCollection = this
                    });
                }
                else
                {
                    foreach (var collection in collections)
                    {
                        collection.Add(new NesMenuFolder()
                        {
                            Name = Resources.FolderNameOriginalGames, ImageId = "folder_back", Position = ConfigIni.Instance.BackFolderPosition, ChildMenuCollection = this
                        });
                    }
                }
            }
        }
Ejemplo n.º 14
0
        public void Split(SplitStyle style, int maxElements = 35)
        {
            bool originalToRoot = false;

            switch (style)
            {
            case SplitStyle.Original_NoSplit:
            case SplitStyle.Original_Auto:
            case SplitStyle.Original_FoldersAlphabetic_FoldersEqual:
            case SplitStyle.Original_FoldersAlphabetic_PagesEqual:
            case SplitStyle.Original_FoldersEqual:
            case SplitStyle.Original_PagesEqual:
                style--;
                if (this.Where(o => o is NesDefaultGame).Count() > 0)
                {
                    originalToRoot = true;
                }
                break;
            }
            if (style == SplitStyle.NoSplit && !originalToRoot)
            {
                return;
            }
            if (((style == SplitStyle.Auto && !originalToRoot) || style == SplitStyle.FoldersEqual || style == SplitStyle.PagesEqual) &&
                (Count <= maxElements))
            {
                return;
            }
            var total      = Count;
            var partsCount = (int)Math.Ceiling((float)total / (float)maxElements);
            var perPart    = (int)Math.Ceiling((float)total / (float)partsCount);
            var alphaNum   = new Regex("[^a-zA-Z0-9]");

            NesMenuCollection root;

            if (!originalToRoot)
            {
                root = this;
            }
            else
            {
                root = new NesMenuCollection();
                root.AddRange(this.Where(o => !(o is NesDefaultGame)));
                if (root.Count == 0)
                {
                    return;
                }
                this.RemoveAll(o => !(o is NesDefaultGame));
                this.Add(new NesMenuFolder()
                {
                    Name                = Resources.FolderNameMoreGames,
                    Position            = NesMenuFolder.Priority.Rightmost,
                    ChildMenuCollection = root
                });
            }

            var sorted      = root.OrderBy(o => o.Name);
            var collections = new List <NesMenuCollection>();
            int i           = 0;

            if (style == SplitStyle.Auto || style == SplitStyle.FoldersEqual || style == SplitStyle.PagesEqual)
            {
                var collection = new NesMenuCollection();
                foreach (var game in sorted)
                {
                    collection.Add(game);
                    i++;
                    if (((i % perPart) == 0) || (i == sorted.Count()))
                    {
                        collections.Add(collection);
                        collection = new NesMenuCollection();
                    }
                }
            }

            if (style == SplitStyle.Auto)
            {
                if (collections.Count >= 12)
                {
                    style = SplitStyle.FoldersEqual;
                }
                else
                {
                    style = SplitStyle.PagesEqual;
                }
            }

            // Folders, equal
            if (style == SplitStyle.FoldersEqual) // minimum amount of games/folders on screen without glitches
            {
                root.Clear();
                foreach (var coll in collections)
                {
                    var fname = alphaNum.Replace(coll.Where(o => (o is NesMiniApplication) || (o is NesDefaultGame)).First().Name.ToUpper(), "");
                    var lname = alphaNum.Replace(coll.Where(o => (o is NesMiniApplication) || (o is NesDefaultGame)).Last().Name.ToUpper(), "");

                    var folder = new NesMenuFolder()
                    {
                        ChildMenuCollection = coll, NameParts = new string[] { fname, lname }, Position = NesMenuFolder.Priority.Right
                    };
                    coll.Add(new NesMenuFolder()
                    {
                        Name = Resources.FolderNameBack, ImageId = "folder_back", Position = NesMenuFolder.Priority.Back, ChildMenuCollection = root
                    });
                    root.Add(folder);
                }
                TrimFolderNames(root);
            }
            else if (style == SplitStyle.PagesEqual)
            // Pages, equal
            {
                root.Clear();
                root.AddRange(collections[0]);
                collections[0] = root;
                for (i = 0; i < collections.Count; i++)
                {
                    for (int j = i - 1; j >= 0; j--)
                    {
                        var fname  = alphaNum.Replace(collections[j].Where(o => (o is NesMiniApplication) || (o is NesDefaultGame)).First().Name.ToUpper(), "");
                        var lname  = alphaNum.Replace(collections[j].Where(o => (o is NesMiniApplication) || (o is NesDefaultGame)).Last().Name.ToUpper(), "");
                        var folder = new NesMenuFolder()
                        {
                            ChildMenuCollection = collections[j],
                            NameParts           = new string[] { fname, lname },
                            Position            = NesMenuFolder.Priority.Left
                        };
                        collections[i].Insert(0, folder);
                    }
                    for (int j = i + 1; j < collections.Count; j++)
                    {
                        var fname  = alphaNum.Replace(collections[j].Where(o => (o is NesMiniApplication) || (o is NesDefaultGame)).First().Name.ToUpper(), "");
                        var lname  = alphaNum.Replace(collections[j].Where(o => (o is NesMiniApplication) || (o is NesDefaultGame)).Last().Name.ToUpper(), "");
                        var folder = new NesMenuFolder()
                        {
                            ChildMenuCollection = collections[j],
                            NameParts           = new string[] { fname, lname },
                            Position            = NesMenuFolder.Priority.Right
                        };
                        collections[i].Insert(collections[i].Count, folder);
                    }
                    TrimFolderNames(collections[i]);
                }
            }
            else if (style == SplitStyle.FoldersAlphabetic_PagesEqual || style == SplitStyle.FoldersAlphabetic_FoldersEqual)
            {
                var letters = new Dictionary <char, NesMenuCollection>();
                for (char ch = 'A'; ch <= 'Z'; ch++)
                {
                    letters[ch] = new NesMenuCollection();
                }
                letters['#'] = new NesMenuCollection();
                foreach (var game in root)
                {
                    if (!(game is NesMiniApplication || game is NesDefaultGame))
                    {
                        continue;
                    }
                    var letter = game.Name.Substring(0, 1).ToUpper()[0];
                    if (letter < 'A' || letter > 'Z')
                    {
                        letter = '#';
                    }
                    letters[letter].Add(game);
                }

                root.Clear();
                foreach (var letter in letters.Keys)
                {
                    if (letters[letter].Count > 0)
                    {
                        string folderImageId = "folder_" + letter.ToString().ToLower();
                        if (letter < 'A' || letter > 'Z')
                        {
                            folderImageId = "folder_number";
                        }
                        var folder = new NesMenuFolder()
                        {
                            ChildMenuCollection = letters[letter], Name = letter.ToString(), Position = NesMenuFolder.Priority.Right, ImageId = folderImageId
                        };
                        if (style == SplitStyle.FoldersAlphabetic_PagesEqual)
                        {
                            folder.ChildMenuCollection.Split(SplitStyle.PagesEqual, maxElements);
                            folder.ChildMenuCollection.Add(new NesMenuFolder()
                            {
                                Name = Resources.FolderNameBack, ImageId = "folder_back", Position = NesMenuFolder.Priority.Back, ChildMenuCollection = root
                            });
                            foreach (NesMenuFolder f in folder.ChildMenuCollection.Where(o => o is NesMenuFolder))
                            {
                                if (f.ChildMenuCollection != root)
                                {
                                    f.ChildMenuCollection.Add(new NesMenuFolder()
                                    {
                                        Name = Resources.FolderNameBack, ImageId = "folder_back", Position = NesMenuFolder.Priority.Back, ChildMenuCollection = root
                                    });
                                }
                            }
                        }
                        else if (style == SplitStyle.FoldersAlphabetic_FoldersEqual)
                        {
                            folder.ChildMenuCollection.Split(SplitStyle.FoldersEqual, maxElements);
                            folder.ChildMenuCollection.Add(new NesMenuFolder()
                            {
                                Name = Resources.FolderNameBack, ImageId = "folder_back", Position = NesMenuFolder.Priority.Back, ChildMenuCollection = root
                            });
                        }
                        root.Add(folder);
                    }
                }
            }
            if (originalToRoot)
            {
                if (style != SplitStyle.PagesEqual)
                {
                    root.Add(new NesMenuFolder()
                    {
                        Name = Resources.FolderNameOriginalGames, ImageId = "folder_back", Position = NesMenuFolder.Priority.Back, ChildMenuCollection = this
                    });
                }
                else
                {
                    foreach (var collection in collections)
                    {
                        collection.Add(new NesMenuFolder()
                        {
                            Name = Resources.FolderNameOriginalGames, ImageId = "folder_back", Position = NesMenuFolder.Priority.Back, ChildMenuCollection = this
                        });
                    }
                }
            }
        }
Ejemplo n.º 15
0
 public FoldersManagerForm(NesMenuCollection nesMenuCollection, MainForm mainForm = null)
 {
     Init(nesMenuCollection, false, mainForm);
 }
Ejemplo n.º 16
0
        public void Split(int maxElements)
        {
            if (Count <= maxElements)
            {
                return;
            }
            var total      = Count;
            var partsCount = (int)Math.Ceiling((float)total / (float)maxElements);
            var perPart    = (int)Math.Ceiling((float)total / (float)partsCount);

            var sorted = this.OrderBy(o => o.Name);

            var collections = new List <NesMenuCollection>();
            var collection  = new NesMenuCollection();
            int i           = 0;

            foreach (var game in sorted)
            {
                collection.Add(game);
                i++;
                if (((i % perPart) == 0) || (i == sorted.Count()))
                {
                    collections.Add(collection);
                    collection = new NesMenuCollection();
                }
            }

            // Method A
            if (collections.Count >= 12) // minimum amount of games/folders on screen without glitches
            {
                var root = this;
                root.Clear();
                foreach (var coll in collections)
                {
                    var fname  = coll.Where(o => (o is NesGame) || (o is NesDefaultGame)).First().Name;
                    var lname  = coll.Where(o => (o is NesGame) || (o is NesDefaultGame)).Last().Name;
                    var folder = new NesMenuFolder();
                    folder.Child = coll;
                    folder.Name  = fname.Substring(0, Math.Min(Letters, fname.Length)) + (fname.Length > Letters ? "..." : "") + " - " + lname.Substring(0, Math.Min(Letters, lname.Length)) + (lname.Length > Letters ? "..." : "");
                    root.Add(folder);
                    coll.Add(new NesMenuFolder()
                    {
                        Name = "<- Back", Image = Resources.back, Child = root
                    });
                }
            }
            else
            // Method B
            {
                for (i = 0; i < collections.Count; i++)
                {
                    for (int j = i - 1; j >= 0; j--)
                    {
                        var folder = new NesMenuFolder();
                        var fname  = collections[j].Where(o => (o is NesGame) || (o is NesDefaultGame)).First().Name /*.ToUpper().Replace(" ", "")*/;
                        var lname  = collections[j].Where(o => (o is NesGame) || (o is NesDefaultGame)).Last().Name /*.ToUpper().Replace(" ", "")*/;
                        folder.Child   = collections[j];
                        folder.Name    = fname.Substring(0, Math.Min(Letters, fname.Length)) + (fname.Length > Letters ? "..." : "") + " - " + lname.Substring(0, Math.Min(Letters, lname.Length)) + (lname.Length > Letters ? "..." : "");
                        folder.Initial = collections[j].Where(o => (o is NesGame) || (o is NesDefaultGame)).First().Code;
                        folder.First   = true;
                        collections[i].Insert(0, folder);
                    }
                    for (int j = i + 1; j < collections.Count; j++)
                    {
                        var folder = new NesMenuFolder();
                        var fname  = collections[j].Where(o => (o is NesGame) || (o is NesDefaultGame)).First().Name /*.ToUpper().Replace(" ", "")*/;
                        var lname  = collections[j].Where(o => (o is NesGame) || (o is NesDefaultGame)).Last().Name /*.ToUpper().Replace(" ", "")*/;
                        folder.Child   = collections[j];
                        folder.Name    = fname.Substring(0, Math.Min(Letters, fname.Length)) + (fname.Length > Letters ? "..." : "") + " - " + lname.Substring(0, Math.Min(Letters, lname.Length)) + (lname.Length > Letters ? "..." : "");
                        folder.Initial = collections[j].Where(o => (o is NesGame) || (o is NesDefaultGame)).First().Code;
                        folder.First   = false;
                        collections[i].Insert(collections[i].Count, folder);
                    }
                }
                Clear();
                AddRange(collections[0]);
            }
        }
Ejemplo n.º 17
0
        private void AddMenu(NesMenuCollection menuCollection, List <NesMenuCollection> allMenus = null)
        {
            if (allMenus == null)
            {
                allMenus = new List <NesMenuCollection>();
            }
            if (!allMenus.Contains(menuCollection))
            {
                allMenus.Add(menuCollection);
            }
            int    menuIndex = allMenus.IndexOf(menuCollection);
            string targetDirectory;

            if (menuIndex == 0)
            {
                targetDirectory = gamesDirectory;
            }
            else
            {
                targetDirectory = Path.Combine(gamesDirectory, string.Format("sub{0:D3}", menuIndex));
            }
            if (Directory.Exists(targetDirectory))
            {
                return;
            }
            foreach (var element in menuCollection)
            {
                if (element is NesGame)
                {
                    var game    = element as NesGame;
                    var gameDir = Path.Combine(targetDirectory, game.Code);
                    Debug.WriteLine(string.Format("Processing {0}/{1}", game.Code, game.Name));
                    DirectoryCopy(game.GamePath, gameDir, true);
                    if (!string.IsNullOrEmpty(game.GameGenie))
                    {
                        var codes          = game.GameGenie.Split(new char[] { ',', '\t', ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
                        var newNesFilePath = Path.Combine(gameDir, game.Code + ".nes");
                        try
                        {
                            var nesFile = new NesFile(newNesFilePath);
                            foreach (var code in codes)
                            {
                                try
                                {
                                    nesFile.PRG = GameGenie.Patch(nesFile.PRG, code.Trim());
                                }
                                catch (GameGenieFormatException)
                                {
                                    ShowError(new GameGenieFormatException(string.Format(Resources.GameGenieFormatError, code, game)), dontStop: true);
                                }
                                catch (GameGenieNotFoundException)
                                {
                                    ShowError(new GameGenieNotFoundException(string.Format(Resources.GameGenieNotFound, code, game.Name)), dontStop: true);
                                }
                            }
                            nesFile.Save(newNesFilePath);
                            var ggFilePath = Path.Combine(gameDir, NesGame.GameGenieFileName);
                            if (File.Exists(ggFilePath))
                            {
                                File.Delete(ggFilePath);
                            }
                        }
                        catch // in case of FDS game... just ignore
                        {
                        }
                    }
                }
                if (element is NesMenuFolder)
                {
                    var folder = element as NesMenuFolder;
                    if (!allMenus.Contains(folder.Child))
                    {
                        allMenus.Add(folder.Child);
                    }
                    int childIndex = allMenus.IndexOf(folder.Child);
                    var folderDir  = Path.Combine(targetDirectory, folder.Code);
                    folder.Save(folderDir, childIndex);
                    AddMenu(folder.Child, allMenus);
                }
                if (element is NesDefaultGame)
                {
                    var game      = element as NesDefaultGame;
                    var gfilePath = Path.Combine(gamesDirectory, string.Format("gpath-{0}-{1}", game.Code, menuIndex));
                    Directory.CreateDirectory(Path.GetDirectoryName(gfilePath));
                    File.WriteAllText(gfilePath, menuIndex == 0 ? "." : string.Format("sub{0:D3}", menuIndex));
                }
            }
        }