/[EmuXPortal]/branches/mono/EmuXPortal/Form1.cs
ViewVC logotype

Diff of /branches/mono/EmuXPortal/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- branches/mono/EmuXPortal/Form1.cs	2017/12/19 00:10:33	324
+++ branches/mono/EmuXPortal/Form1.cs	2017/12/19 00:38:33	327
@@ -34,6 +34,7 @@
         PlatformControl CurrentPlatformControl = null;
 		int SavedPlatformIndex= -1;
         GameControl CurrentGameControl = null;
+		private CustomProgressBar progress_bar;
 
         #region unhandled exception support
         static void Application_Unhandled_ThreadException(object sender, ThreadExceptionEventArgs e)
@@ -69,7 +70,7 @@
         public Form1()
         {
             InitializeComponent();
-
+			this.progress_bar = new CustomProgressBar();
 			this.KeyPreview = true;
          
             #region logging support
@@ -717,28 +718,77 @@
         #region gameWorker
         private static Image DefaultGameImage = Properties.Resources.DefaultGameImage;
         private object gameimage_lock = new object();
-        private void gameWorker_DoWork(object sender, DoWorkEventArgs e)
-        {
-            Stopwatch t = e.Argument as Stopwatch;
-            RomParser parser = new RomParser(CurrentSelectedRom);
+		private void worker_progress_event (object event_source, ProgressEventArgs event_args)
+		{
+			//gLog.Debug.WriteLine ("worker_progress_event called!");
 
-            CustomProgressBar bar = new CustomProgressBar();
-            bar.Font = ResizeFont(GetFormFont(), PROGRESS_BAR_FONT_SIZE);
+			object user_object = event_args.GetUserObject ();
+			int progress = event_args.GetProgress();
+			string message = event_args.GetUserMessage() == null ? "" : event_args.GetUserMessage();
+			if (user_object != null) {				
+				IRomConfig irc = (user_object as IRomConfig);
+				if (irc != null) {
+					gLog.Verbose.Debug.WriteLine ("Read Rom[{0}] ({1}%): '{2}'", event_args.GetCount (), progress, irc);
+				} else {
+					IEmuConfig iec = (user_object as IEmuConfig);
+					if (iec != null) {
+						gLog.Verbose.Debug.WriteLine ("Read Platform[{0}] ({1}%): '{2}'", event_args.GetCount (), progress, iec);
+					}
+				}
+			}
+			if (progress_bar.InvokeRequired) {
+				progress_bar.Invoke (new MethodInvoker (delegate {
+					progress_bar.Message = message;
+					progress_bar.Value = progress;
+					progress_bar.Update();
+				}));
+			} else {
+				progress_bar.Message = message;
+				progress_bar.Value = progress;
+				progress_bar.Update();
+			}
+		}
+        private void gameWorker_DoWork (object sender, DoWorkEventArgs e)
+		{
+			Stopwatch t = e.Argument as Stopwatch;
+			progress_bar.Font = ResizeFont (GetFormFont (), PROGRESS_BAR_FONT_SIZE);
 #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
-            bar.ShowPercentageLabel = false;
+            progress_bar.ShowPercentageLabel = false;
 #endif
-            bar.ProgressColor = Color.LimeGreen;
-            bar.Dock = DockStyle.Top;
+			progress_bar.ProgressColor = Color.LimeGreen;
+			progress_bar.Dock = DockStyle.Top;
 
-            if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { this.Controls.Add(bar); }); }
-            else { this.Controls.Add(bar); }
-            bar.Invoke(new MethodInvoker(delegate { bar.Margin = new System.Windows.Forms.Padding(0); bar.Size = new Size(GetFormWidth() - 25, 100); }));
-            
-            double count = 0;
-            double total_count = parser.Roms.Count;
-            foreach (IRomConfig config in parser.Roms)
+			if (this.InvokeRequired) {
+				this.Invoke ((MethodInvoker)delegate() {
+					this.Controls.Add (progress_bar);
+				});
+			} else {
+				this.Controls.Add (progress_bar);
+			}
+			progress_bar.Invoke (new MethodInvoker (delegate {
+				progress_bar.Margin = new System.Windows.Forms.Padding (0);
+				progress_bar.Size = new Size (GetFormWidth () - 25, 100);
+			}));
+
+			HashSet<IRomConfig> roms = new HashSet<IRomConfig> ();
+			using (RomParser parser = new RomParser (CurrentSelectedRom, new ProgressEvent (worker_progress_event))) {				
+				foreach (var rom in parser.Roms) {
+					roms.Add(rom);
+				}
+			}
+
+			double count = 0;
+			double total_count = roms.Count;
+			if (total_count > 0) {
+				progress_bar.Invoke (new MethodInvoker (delegate {
+					progress_bar.Message = string.Format ("Please Wait Adding {0} {1} Roms...", total_count, CurrentSelectedRom.PlatformNameLong);
+				}));
+			}
+
+
+			foreach (IRomConfig config in roms)
             {
-                bar.Invoke(new MethodInvoker(delegate { bar.Value = (int)(100.0 * (count / total_count)); }));
+                progress_bar.Invoke(new MethodInvoker(delegate { progress_bar.Value = (int)(100.0 * (count / total_count)); }));
                 GameControl game_ctrl = new GameControl();
                 game_ctrl.Font = GetFormFont();
                 game_ctrl.Dock = DockStyle.Top;
@@ -748,12 +798,20 @@
                 {
                     lock (gameimage_lock)
                     {
-                        game_ctrl.GameImage = config.RomImage == null ? (Image)DefaultGameImage.Clone() : (Image)config.RomImage.Clone();
-                        config.ReleaseRomImageResource();
+						//gLog.Debug.WriteLine ("handling rom: {0}", config.ToString ());
+						if(String.IsNullOrEmpty(config.RomImage)) {
+							game_ctrl.GameImage = (Image)DefaultGameImage.Clone ();
+						}
+						else {
+							string path = config.GetFullRomImage();
+							game_ctrl.GameImage = Image.FromFile(path);
+						}
                     }
                 }
                 catch (Exception ex)
                 {
+					gLog.Error.WriteLine (ex.Message);
+					gLog.Verbose.Error.WriteLine (ex.ToString());
                     throw ex;
                 }
                 if (CurrentSelectedRom.PlatformNameShort == "Favorites")
@@ -772,15 +830,23 @@
                 else { rom_flow.Controls.Add(game_ctrl); }
                 count++;
             }
-            bar.Invoke(new MethodInvoker(delegate { bar.Value = 1; bar.Update(); }));
-            if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { this.Controls.Remove(bar); }); } else { this.Controls.Remove(bar); }
-            e.Result = t;            
-            parser.Dispose();
+            progress_bar.Invoke(new MethodInvoker(delegate { progress_bar.Value = 0; progress_bar.Update(); }));
+            e.Result = t;          
         }
         private void gameWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
         private void gameWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
         {
-            Stopwatch t = e.Result as Stopwatch;
+			Stopwatch t = e.Result as Stopwatch;
+			if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { 
+				progress_bar.Message = ""; 
+				this.Controls.Remove(progress_bar); });
+				this.Refresh();
+			} else { 
+				progress_bar.Message = "";
+				this.Controls.Remove(progress_bar); 
+				this.Refresh();
+
+			}
             if (rom_flow.Controls.Count == 0)
             {
                 GameControl game_ctrl = new GameControl();
@@ -800,48 +866,73 @@
             }
             rom_flow.Controls[0].Select();
             t.Stop();
-            //gLog.Profiler.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
+            gLog.Profiler.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
         }
         #endregion
         #region platformWorker
         private static Image DefaultPlatformImage = Properties.Resources.DefaultPlatformImage;
         private object platformimage_lock = new object();
-        private void platformWorker_DoWork(object sender, DoWorkEventArgs e)
-        {
-            Stopwatch t = e.Argument as Stopwatch;
-            PlatformParser parser = new PlatformParser(Config.RomPath);
-            double count = 0;
-            double total_count = parser.Platforms.Count;
-            CustomProgressBar bar = new CustomProgressBar();
-            bar.Font = ResizeFont(GetFormFont(), PROGRESS_BAR_FONT_SIZE);
+        private void platformWorker_DoWork (object sender, DoWorkEventArgs e)
+		{
+			Stopwatch t = e.Argument as Stopwatch;
+			progress_bar.Font = ResizeFont (GetFormFont (), PROGRESS_BAR_FONT_SIZE);
 #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
-            bar.ShowPercentageLabel = false;
+            progress_bar.ShowPercentageLabel = false;
 #endif
-            bar.ProgressColor = Color.LimeGreen;
-            bar.Dock = DockStyle.Top;
+			progress_bar.ProgressColor = Color.LimeGreen;
+			progress_bar.Dock = DockStyle.Top;
             
-            if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { this.Controls.Add(bar); }); }
-            else { this.Controls.Add(bar); }
-            bar.Invoke(new MethodInvoker(delegate { bar.Margin = new System.Windows.Forms.Padding(0); bar.Size = new Size(GetFormWidth() - 25, 100); }));
-            foreach (IEmuConfig config in parser.Platforms)
-            {
-                bar.Invoke(new MethodInvoker(delegate { bar.Value = (int)(100.0 * (count / total_count)); }));
+			if (this.InvokeRequired) { 
+				this.Invoke ((MethodInvoker)delegate() {
+					this.Controls.Add (progress_bar); 
+				}); 
+			} else {
+				this.Controls.Add (progress_bar);
+			}
+			progress_bar.Invoke (new MethodInvoker (delegate {
+				progress_bar.Margin = new System.Windows.Forms.Padding (0);
+				progress_bar.Size = new Size (GetFormWidth () - 25, 100);
+			}));
+
+			HashSet<IEmuConfig> platforms = new HashSet<IEmuConfig>();
+			using (PlatformParser parser = new PlatformParser (Config.RomPath, new ProgressEvent (worker_progress_event))) {
+				foreach (var platform in parser.Platforms) {
+					platforms.Add(platform);
+				}
+			}
+
+
+			double count = 0;
+			double total_count = platforms.Count;
+			if (total_count > 0) {
+				progress_bar.Invoke (new MethodInvoker (delegate {
+					progress_bar.Message = string.Format ("Please Wait Loading {0} Rom Platforms...", total_count);
+				}));
+			}
+			foreach (IEmuConfig config in platforms) {
+				progress_bar.Invoke (new MethodInvoker (delegate {
+					progress_bar.Value = (int)(100.0 * (count / total_count));
+				}));
                 
-                PlatformControl platform_ctrl = new PlatformControl();
-                platform_ctrl.Font = GetFormFont();
-                platform_ctrl.Dock = DockStyle.Top;
-                platform_ctrl.Width = this.Width - 10;
-                platform_ctrl.Tag = config;
-                try
-                {
-                    lock (platformimage_lock)
-                    {
-                        platform_ctrl.PlatformImage = config.PlatformImage == null ? (Image)DefaultPlatformImage.Clone() : (Image)config.PlatformImage.Clone();
-                        config.ReleasePlatformImageResource();
-                    }
-                }
-                catch (Exception ex)
-                {
+				PlatformControl platform_ctrl = new PlatformControl ();
+				platform_ctrl.Font = GetFormFont ();
+				platform_ctrl.Dock = DockStyle.Top;
+				platform_ctrl.Width = this.Width - 10;
+				platform_ctrl.Tag = config;
+				try {
+					lock (platformimage_lock) {
+						//gLog.Debug.WriteLine ("handling platform: {0}", config.ToString ());
+						if(String.IsNullOrEmpty(config.PlatformImage)) {
+							platform_ctrl.PlatformImage = (Image)DefaultPlatformImage.Clone ();
+						}
+						else {
+							string path = config.GetFullPlatformImage();
+							platform_ctrl.PlatformImage = Image.FromFile(path);
+						}
+					}
+				} catch (Exception ex) {
+					gLog.Error.WriteLine (ex.Message);
+					gLog.Verbose.Error.WriteLine (ex.ToString());
                     throw ex;
                 } 
                 platform_ctrl.PlatformName = config.ToString();
@@ -852,14 +943,25 @@
                 else { platform_flow.Controls.Add(platform_ctrl); }                  
                 count++;
             }
-            bar.Invoke(new MethodInvoker(delegate { bar.Value = 1; bar.Update(); }));
-            if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { this.Controls.Remove(bar); }); } else { this.Controls.Remove(bar); }
-            e.Result = t;            
-            parser.Dispose();
+            progress_bar.Invoke(new MethodInvoker(delegate { 
+            	progress_bar.Value = 0; progress_bar.Update();
+            }));         
+            e.Result = t;
         }
         private void platformWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
         private void platformWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
         {
+			gLog.Debug.WriteLine("platformWorker_RunWorkerCompleted called");
+			if (this.InvokeRequired) {
+             this.Invoke((MethodInvoker)delegate() { 
+					progress_bar.Message = "";
+					this.Controls.Remove(progress_bar); 
+             	}); 
+            } 
+            else {
+				progress_bar.Message = "";
+				this.Controls.Remove(progress_bar); 
+           	}
             Stopwatch t = e.Result as Stopwatch;
             if (platform_flow.Controls.Count == 0)
             {
@@ -884,7 +986,8 @@
 			} else {
 				platform_flow.Controls[0].Select();
 			}
-           //gLog.Profiler.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
+			t.Stop();
+           gLog.Profiler.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
         }
         #endregion
 

 

  ViewVC Help
Powered by ViewVC 1.1.22