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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 107 - (show annotations) (download)
Thu Aug 23 02:10:16 2012 UTC (8 years, 1 month 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 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Drawing;
6 using System.IO;
7 using EmuXPortal.Logging;
8
9 namespace EmuXPortal.Api
10 {
11 public interface IRomConfig : IDisposable
12 {
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 public static IRomConfig Load(string rom_file, IEmuConfig EMUConfig)
23 {
24 IRomConfig config = null;
25 config = new RomConfig(rom_file, EMUConfig);
26 return config;
27 }
28 #region private class RomConfig : IRomConfig
29 private class RomConfig : IRomConfig, IDisposable
30 {
31 public RomConfig(string rom_file, IEmuConfig EMUConfig)
32 {
33 FileInfo fi = new FileInfo(rom_file);
34 this.RomFile =fi.FullName;
35 this.RomTitle = EMUConfig.GameTitle == "" ? fi.Name : EMUConfig.GameTitle;
36 string rom_img = "";
37 // load image
38 try
39 {
40 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 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 }
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
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 //string config_dir = fi.Directory.Parent.FullName;
98 //Config = EmuConfigLoader.Load(config_dir);
99 Config = EMUConfig;
100 }
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
108 public void Dispose()
109 {
110 if (this.RomImage != null)
111 this.RomImage.Dispose();
112 }
113 }
114 #endregion
115 }
116
117
118 }

  ViewVC Help
Powered by ViewVC 1.1.22