From 107a98686a105468b5f200ebcbd27343c1210ce4 Mon Sep 17 00:00:00 2001 From: RogueAI42 Date: Sun, 11 Jun 2017 17:34:38 +1000 Subject: ReflectMan Saves The Day.............. Refactored every part in the code that used that damn foreach loop. All assemblies are now loaded on startup into an array, and the results of GetType() on each are concatenated into another array. The parts of the code that were loading the assemblies and scanning them themselves now look to ReflectMan.Types, and all disk I/O is limited to the first time ReflectMan is accessed. While I was there I also replaced some other foreach loops with array comprehensions and such to speed things up - there is a noticeable improvement. It doesn't seem to have broken anything, but I'd appreciate if someone could do more stress testing for me. --- ShiftOS.WinForms/Applications/IconManager.cs | 99 ++++++++---------- ShiftOS.WinForms/Applications/Shiftnet.cs | 113 +++++++++------------ .../Applications/ShiftoriumFrontend.cs | 7 +- 3 files changed, 93 insertions(+), 126 deletions(-) (limited to 'ShiftOS.WinForms/Applications') diff --git a/ShiftOS.WinForms/Applications/IconManager.cs b/ShiftOS.WinForms/Applications/IconManager.cs index 0c6e119..7bbeb34 100644 --- a/ShiftOS.WinForms/Applications/IconManager.cs +++ b/ShiftOS.WinForms/Applications/IconManager.cs @@ -92,70 +92,51 @@ namespace ShiftOS.WinForms.Applications { flbody.Controls.Clear(); //Clear the icon list. - List types = new List(); + Type[] types = Array.FindAll(ReflectMan.Types, x => x.GetCustomAttributes(false).FirstOrDefault(y => y is DefaultIconAttribute) != null); - foreach(var exe in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) - { - if(exe.ToLower().EndsWith(".exe") || exe.ToLower().EndsWith(".dll")) - { - try - { - var asm = Assembly.LoadFile(exe); + pageCount = types.GetPageCount(pageSize); - var typeList = asm.GetTypes().Where(x => x.GetCustomAttributes(false).FirstOrDefault(y => y is DefaultIconAttribute) != null); - types.AddRange(typeList); - - } - catch { } - } - } - - pageCount = types.ToArray().GetPageCount(pageSize); - - foreach (var type in types.ToArray().GetItemsOnPage(currentPage, pageSize)) + foreach (var type in Array.FindAll(types.GetItemsOnPage(currentPage, pageSize), t => Shiftorium.UpgradeAttributesUnlocked(t))) { - if (Shiftorium.UpgradeAttributesUnlocked(type)) + var pnl = new Panel(); + pnl.Height = 30; + pnl.Width = flbody.Width - 15; + flbody.Controls.Add(pnl); + pnl.Show(); + var pic = new PictureBox(); + pic.SizeMode = PictureBoxSizeMode.StretchImage; + pic.Size = new Size(24, 24); + pic.Image = GetIcon(type.Name); + pnl.Controls.Add(pic); + pic.Left = 5; + pic.Top = (pnl.Height - pic.Height) / 2; + pic.Show(); + var lbl = new Label(); + lbl.Tag = "header3"; + lbl.AutoSize = true; + lbl.Text = NameChangerBackend.GetNameRaw(type); + ControlManager.SetupControl(lbl); + pnl.Controls.Add(lbl); + lbl.CenterParent(); + lbl.Show(); + var btn = new Button(); + btn.Text = "Change..."; + btn.AutoSize = true; + btn.AutoSizeMode = AutoSizeMode.GrowAndShrink; + pnl.Controls.Add(btn); + btn.Left = (pnl.Width - btn.Width) - 5; + btn.Top = (pnl.Height - btn.Height) / 2; + btn.Click += (o, a) => { - var pnl = new Panel(); - pnl.Height = 30; - pnl.Width = flbody.Width - 15; - flbody.Controls.Add(pnl); - pnl.Show(); - var pic = new PictureBox(); - pic.SizeMode = PictureBoxSizeMode.StretchImage; - pic.Size = new Size(24, 24); - pic.Image = GetIcon(type.Name); - pnl.Controls.Add(pic); - pic.Left = 5; - pic.Top = (pnl.Height - pic.Height) / 2; - pic.Show(); - var lbl = new Label(); - lbl.Tag = "header3"; - lbl.AutoSize = true; - lbl.Text = NameChangerBackend.GetNameRaw(type); - ControlManager.SetupControl(lbl); - pnl.Controls.Add(lbl); - lbl.CenterParent(); - lbl.Show(); - var btn = new Button(); - btn.Text = "Change..."; - btn.AutoSize = true; - btn.AutoSizeMode = AutoSizeMode.GrowAndShrink; - pnl.Controls.Add(btn); - btn.Left = (pnl.Width - btn.Width) - 5; - btn.Top = (pnl.Height - btn.Height) / 2; - btn.Click += (o, a) => + var gfp = new GraphicPicker(pic.Image, lbl.Text + " icon", ImageLayout.Stretch, (raw, img, layout) => { - var gfp = new GraphicPicker(pic.Image, lbl.Text + " icon", ImageLayout.Stretch, (raw, img, layout) => - { - pic.Image = img; - SetIcon(type.Name, raw); - }); - AppearanceManager.SetupDialog(gfp); - }; - btn.Show(); - ControlManager.SetupControls(pnl); - } + pic.Image = img; + SetIcon(type.Name, raw); + }); + AppearanceManager.SetupDialog(gfp); + }; + btn.Show(); + ControlManager.SetupControls(pnl); } btnnext.Visible = (currentPage < pageCount - 1); diff --git a/ShiftOS.WinForms/Applications/Shiftnet.cs b/ShiftOS.WinForms/Applications/Shiftnet.cs index 6ccdb19..136f680 100644 --- a/ShiftOS.WinForms/Applications/Shiftnet.cs +++ b/ShiftOS.WinForms/Applications/Shiftnet.cs @@ -150,79 +150,60 @@ namespace ShiftOS.WinForms.Applications { txturl.Text = url; - foreach(var exe in Directory.GetFiles(Environment.CurrentDirectory)) + try { - if(exe.EndsWith(".exe") || exe.EndsWith(".dll")) + foreach (var type in Array.FindAll(ReflectMan.Types, t => t.GetInterfaces().Contains(typeof(IShiftnetSite)) && t.BaseType == typeof(UserControl) && Shiftorium.UpgradeAttributesUnlocked(t))) { - try + var attribute = type.GetCustomAttributes(false).FirstOrDefault(x => x is ShiftnetSiteAttribute && (x as ShiftnetSiteAttribute).Url == url) as ShiftnetSiteAttribute; + if (attribute != null) { - var asm = Assembly.LoadFile(exe); - foreach (var type in asm.GetTypes()) - { - if (type.GetInterfaces().Contains(typeof(IShiftnetSite))) + var obj = (IShiftnetSite)Activator.CreateInstance(type, null); + obj.GoToUrl += (u) => { - if (type.BaseType == typeof(UserControl)) - { - var attribute = type.GetCustomAttributes(false).FirstOrDefault(x => x is ShiftnetSiteAttribute) as ShiftnetSiteAttribute; - if (attribute != null) - { - if (attribute.Url == url) - { - if (Shiftorium.UpgradeAttributesUnlocked(type)) - { - var obj = (IShiftnetSite)Activator.CreateInstance(type, null); - obj.GoToUrl += (u) => - { - History.Push(CurrentUrl); - NavigateToUrl(u); - }; - obj.GoBack += () => - { - string u = History.Pop(); - Future.Push(u); - NavigateToUrl(u); - }; - CurrentPage = obj; - this.pnlcanvas.Controls.Clear(); - this.pnlcanvas.Controls.Add((UserControl)obj); - ((UserControl)obj).Show(); - ((UserControl)obj).Dock = DockStyle.Fill; - obj.OnUpgrade(); - obj.OnSkinLoad(); - obj.Setup(); - AppearanceManager.SetWindowTitle(this, attribute.Name + " - Shiftnet"); - return; - } - } - } - } - } - } - } - catch (Exception ex) - { - pnlcanvas.Controls.Clear(); - var tlbl = new Label(); - tlbl.Text = "Server error in \"" + url + "\" application."; - tlbl.Tag = "header1"; - tlbl.AutoSize = true; - tlbl.Location = new Point(10, 10); - tlbl.Dock = DockStyle.Top; - pnlcanvas.Controls.Add(tlbl); - tlbl.Show(); - - var crash = new Label(); - crash.Dock = DockStyle.Fill; - crash.AutoSize = false; - crash.Text = ex.ToString(); - pnlcanvas.Controls.Add(crash); - crash.Show(); - crash.BringToFront(); - ControlManager.SetupControls(pnlcanvas); - return; + History.Push(CurrentUrl); + NavigateToUrl(u); + }; + obj.GoBack += () => + { + string u = History.Pop(); + Future.Push(u); + NavigateToUrl(u); + }; + CurrentPage = obj; + this.pnlcanvas.Controls.Clear(); + this.pnlcanvas.Controls.Add((UserControl)obj); + ((UserControl)obj).Show(); + ((UserControl)obj).Dock = DockStyle.Fill; + obj.OnUpgrade(); + obj.OnSkinLoad(); + obj.Setup(); + AppearanceManager.SetWindowTitle(this, attribute.Name + " - Shiftnet"); + return; } } } + catch (Exception ex) + { + pnlcanvas.Controls.Clear(); + var tlbl = new Label(); + tlbl.Text = "Server error in \"" + url + "\" application."; + tlbl.Tag = "header1"; + tlbl.AutoSize = true; + tlbl.Location = new Point(10, 10); + tlbl.Dock = DockStyle.Top; + pnlcanvas.Controls.Add(tlbl); + tlbl.Show(); + + var crash = new Label(); + crash.Dock = DockStyle.Fill; + crash.AutoSize = false; + crash.Text = ex.ToString(); + pnlcanvas.Controls.Add(crash); + crash.Show(); + crash.BringToFront(); + ControlManager.SetupControls(pnlcanvas); + return; + } pnlcanvas.Controls.Clear(); var lbl = new Label(); lbl.Text = "Page not found!"; diff --git a/ShiftOS.WinForms/Applications/ShiftoriumFrontend.cs b/ShiftOS.WinForms/Applications/ShiftoriumFrontend.cs index eebd897..6886d98 100644 --- a/ShiftOS.WinForms/Applications/ShiftoriumFrontend.cs +++ b/ShiftOS.WinForms/Applications/ShiftoriumFrontend.cs @@ -114,8 +114,12 @@ namespace ShiftOS.WinForms.Applications private void SetList() { - lbnoupgrades.Hide(); lbupgrades.Items.Clear(); + if (upgrades.Length == 0) + return; + lbnoupgrades.Hide(); + if (CategoryId > upgrades.Length) + CategoryId = 0; try { lbupgrades.Items.AddRange(upgrades[CategoryId].Keys.ToArray()); @@ -250,6 +254,7 @@ namespace ShiftOS.WinForms.Applications private void moveCat(short direction) // direction is -1 to move backwards or 1 to move forwards { + if (cats.Length == 0) return; CategoryId += direction; CategoryId %= cats.Length; if (CategoryId < 0) CategoryId += cats.Length; // fix modulo on negatives -- cgit v1.2.3