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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 110 - (show annotations) (download)
Thu Aug 23 02:44:26 2012 UTC (8 years, 1 month ago) by william
File size: 4944 byte(s)
PERFORMANCE OPTIMIZATION:
** only get defaultimage resource once (don't duplicate the resource)

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

  ViewVC Help
Powered by ViewVC 1.1.22