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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 107 - (hide annotations) (download)
Thu Aug 23 02:10:16 2012 UTC (8 years, 2 months ago) by william
File size: 4796 byte(s)
+ ensure we only read files that have the correct rom file pattern: ie .iso, .rom,.bin, etc)

1 william 17 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using System.Drawing;
6     using System.IO;
7 william 105 using EmuXPortal.Logging;
8 william 17
9     namespace EmuXPortal.Api
10     {
11 william 105 public interface IRomConfig : IDisposable
12 william 17 {
13     string RomFile { get; }
14     string RomTitle { get; }
15     Image RomImage { get; }
16     IEmuConfig Config { get; }
17     }
18    
19     public static class RomLoader
20     {
21     private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration
22 william 29 public static IRomConfig Load(string rom_file, IEmuConfig EMUConfig)
23 william 17 {
24     IRomConfig config = null;
25 william 29 config = new RomConfig(rom_file, EMUConfig);
26 william 17 return config;
27     }
28     #region private class RomConfig : IRomConfig
29 william 105 private class RomConfig : IRomConfig, IDisposable
30 william 17 {
31 william 29 public RomConfig(string rom_file, IEmuConfig EMUConfig)
32 william 17 {
33     FileInfo fi = new FileInfo(rom_file);
34     this.RomFile =fi.FullName;
35 william 85 this.RomTitle = EMUConfig.GameTitle == "" ? fi.Name : EMUConfig.GameTitle;
36 william 17 string rom_img = "";
37     // load image
38 william 105 try
39     {
40 william 107 if (File.Exists(string.Format("{0}.jpg", fi.FullName.Replace(fi.Extension, "")))) { rom_img = string.Format("{0}.jpg", fi.FullName.Replace(fi.Extension, "")); }
41     }
42     catch (Exception ex)
43     {
44     logger.WriteLine("Error Loading an Image file for: {1}{0}Error:{0}{2}",System.Environment.NewLine, fi.FullName, ex.ToString());
45     }
46     try
47     {
48 william 106 if (rom_img != string.Empty)
49     {
50     try
51     {
52     this.RomImage = Image.FromFile(rom_img);
53     }
54     catch (Exception ex)
55     {
56     throw ex;
57     }
58     }
59     else
60     {
61     this.RomImage = Properties.Resources.DefaultGameImage;
62     }
63 william 105 }
64     catch (Exception ex)
65     {
66     logger.WriteLine("Error loading rom image: {1}{0}Error:{0}{2}", System.Environment.NewLine, (rom_img == "") ? "DefaultGameImage" : rom_img, ex.ToString());
67     this.RomImage = Properties.Resources.DefaultGameImage;
68     }
69 william 85
70    
71     if (EMUConfig.GameImage != string.Empty)
72     {
73     FileInfo fi_gameimage = new FileInfo(EMUConfig.GameImage);
74     if (fi_gameimage.Exists) { this.RomImage = Image.FromFile(fi_gameimage.FullName); }
75     else
76     {
77     FileInfo t = new FileInfo(EMUConfig.ConfigPath);
78     string path = t.FullName.Replace(t.Name, "").Replace(t.Extension, "");
79     string image_path = string.Format(@"{0}{1}", path, fi_gameimage.Name);
80     FileInfo x = new FileInfo(image_path);
81     if (x.Exists) { this.RomImage = Image.FromFile(x.FullName); }
82     else
83     {
84     if (EMUConfig.GameExe != string.Empty)
85     {
86     FileInfo fi_gamexe = new FileInfo(EMUConfig.GameExe);
87     path = fi_gamexe.FullName.Replace(fi_gamexe.Name, "").Replace(fi_gamexe.Extension, "");
88     image_path = string.Format(@"{0}{1}", path, fi_gameimage.Name);
89     x = new FileInfo(image_path);
90     if (x.Exists) { this.RomImage = Image.FromFile(x.FullName); }
91     }
92     }
93     }
94     }
95    
96    
97 william 29 //string config_dir = fi.Directory.Parent.FullName;
98     //Config = EmuConfigLoader.Load(config_dir);
99     Config = EMUConfig;
100 william 17 }
101     #region IRomConfig Members
102     public string RomFile { get; private set; }
103     public string RomTitle { get; private set; }
104     public Image RomImage { get; private set; }
105     public IEmuConfig Config { get; private set; }
106     #endregion
107 william 105
108     public void Dispose()
109     {
110     if (this.RomImage != null)
111     this.RomImage.Dispose();
112     }
113 william 17 }
114     #endregion
115     }
116    
117    
118     }

  ViewVC Help
Powered by ViewVC 1.1.22