/[EmuXPortal]/trunk/EmuXPortal/Api/RomConfig.cs
ViewVC logotype

Diff of /trunk/EmuXPortal/Api/RomConfig.cs

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

--- trunk/EmuXPortal/Api/RomConfig.cs	2012/04/04 03:38:07	27
+++ trunk/EmuXPortal/Api/RomConfig.cs	2012/08/23 02:10:16	107
@@ -4,10 +4,11 @@
 using System.Text;
 using System.Drawing;
 using System.IO;
+using EmuXPortal.Logging;
 
 namespace EmuXPortal.Api
 {
-    public interface IRomConfig
+    public interface IRomConfig : IDisposable
     {
         string RomFile { get; }
         string RomTitle { get; }
@@ -18,26 +19,84 @@
     public static class RomLoader
     {
         private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration
-        public static IRomConfig Load(string rom_file)
+        public static IRomConfig Load(string rom_file, IEmuConfig EMUConfig)
         {
             IRomConfig config = null;
-            config = new RomConfig(rom_file);
+            config = new RomConfig(rom_file, EMUConfig);            
             return config;
         }
         #region private class RomConfig : IRomConfig
-        private class RomConfig : IRomConfig
+        private class RomConfig : IRomConfig, IDisposable
         {
-            public RomConfig(string rom_file)
+            public RomConfig(string rom_file, IEmuConfig EMUConfig)
             {
                 FileInfo fi = new FileInfo(rom_file);
                 this.RomFile =fi.FullName;
-                this.RomTitle = fi.Name;
+                this.RomTitle = EMUConfig.GameTitle == "" ? fi.Name : EMUConfig.GameTitle;                
                 string rom_img = "";
                 // load image
-                if (File.Exists(string.Format("{0}.jpg", fi.FullName.Replace(fi.Extension, "")))) { rom_img = string.Format("{0}.jpg", fi.FullName.Replace(fi.Extension, "")); }
-                this.RomImage = (rom_img == "") ? Properties.Resources.DefaultGameImage : Image.FromFile(rom_img);
-                string config_dir = fi.Directory.Parent.FullName;
-                Config = EmuConfigLoader.Load(config_dir);
+                try
+                {
+                    if (File.Exists(string.Format("{0}.jpg", fi.FullName.Replace(fi.Extension, "")))) { rom_img = string.Format("{0}.jpg", fi.FullName.Replace(fi.Extension, "")); }
+                }
+                catch (Exception ex)
+                {
+                    logger.WriteLine("Error Loading an Image file for: {1}{0}Error:{0}{2}",System.Environment.NewLine, fi.FullName, ex.ToString());
+                }
+                try
+                {
+                    if (rom_img != string.Empty)
+                    {
+                        try
+                        {
+                            this.RomImage = Image.FromFile(rom_img);
+                        }
+                        catch (Exception ex)
+                        {
+                            throw ex;
+                        }
+                    }
+                    else
+                    {
+                        this.RomImage = Properties.Resources.DefaultGameImage;
+                    }
+                }
+                catch (Exception ex)
+                {
+                    logger.WriteLine("Error loading rom image: {1}{0}Error:{0}{2}", System.Environment.NewLine, (rom_img == "") ? "DefaultGameImage" : rom_img, ex.ToString());
+                    this.RomImage = Properties.Resources.DefaultGameImage;
+                }
+
+
+                if (EMUConfig.GameImage != string.Empty)
+                {
+                    FileInfo fi_gameimage = new FileInfo(EMUConfig.GameImage);
+                    if (fi_gameimage.Exists) { this.RomImage = Image.FromFile(fi_gameimage.FullName); }
+                    else
+                    {
+                        FileInfo t = new FileInfo(EMUConfig.ConfigPath);
+                        string path = t.FullName.Replace(t.Name, "").Replace(t.Extension, "");
+                        string image_path = string.Format(@"{0}{1}", path, fi_gameimage.Name);
+                        FileInfo x = new FileInfo(image_path);
+                        if (x.Exists) { this.RomImage = Image.FromFile(x.FullName); }
+                        else
+                        {
+                            if (EMUConfig.GameExe != string.Empty)
+                            {
+                                FileInfo fi_gamexe = new FileInfo(EMUConfig.GameExe);
+                                path = fi_gamexe.FullName.Replace(fi_gamexe.Name, "").Replace(fi_gamexe.Extension, "");
+                                image_path = string.Format(@"{0}{1}", path, fi_gameimage.Name);
+                                x = new FileInfo(image_path);
+                                if (x.Exists) { this.RomImage = Image.FromFile(x.FullName); }
+                            }
+                        }
+                    }
+                }
+
+
+                //string config_dir = fi.Directory.Parent.FullName;
+                //Config = EmuConfigLoader.Load(config_dir);
+                Config = EMUConfig;
             }
             #region IRomConfig Members
             public string RomFile { get; private set; }
@@ -45,6 +104,12 @@
             public Image RomImage { get; private set; }
             public IEmuConfig Config { get; private set; }
             #endregion
+
+            public void Dispose()
+            {
+                if (this.RomImage != null)
+                    this.RomImage.Dispose();
+            }
         }
         #endregion
     }

 

  ViewVC Help
Powered by ViewVC 1.1.22