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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 46 - (show annotations) (download)
Fri Apr 6 12:14:21 2012 UTC (8 years, 7 months ago) by william
File size: 10774 byte(s)
+ add support for refreshing emu config options before launching rom, incase the emu's commandline arguments have changed

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 using System.Xml;
9
10 namespace EmuXPortal.Api
11 {
12 public interface IEmuConfig : IComparable
13 {
14 string PlatformNameShort { get; }
15 string PlatformNameLong { get; }
16 Image PlatformImage { get; }
17 string Extenstions { get; }
18 string EmuPath { get; }
19 string EmuOptions { get; }
20 string EmuRomPath { get; set; }
21 string ToString();
22
23 void RefreshConfig();
24 }
25
26
27
28 public static class EmuConfigLoader
29 {
30 private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration
31 #region load
32 public static IEmuConfig Load(string rom_path) { return new EmuConfig().Create(rom_path); }
33 public static IEmuConfig Load(string config_path, string rom_path) { return new EmuConfig().Create(config_path, rom_path); }
34 #endregion
35 #region parse emu options
36 public static string GetEMUOptions(IRomConfig config)
37 {
38 EMUOptions EMUOptions = new EMUOptions(config);
39 return EMUOptions.Options;
40 }
41 #endregion
42 #region private class EMUOptions
43 private class EMUOptions
44 {
45 #region Replaceable Constant Options
46 private const string ROM_FILE = "%ROM_FILE%";
47 private const string ROM_PATH = "%ROM_PATH%";
48 #endregion
49 private Dictionary<string, string> options_dict = new Dictionary<string, string>();
50 public EMUOptions(IRomConfig config)
51 {
52 init_dict(config);
53 config.Config.RefreshConfig();
54 string options = config.Config.EmuOptions;
55 string real_options = options;
56 foreach (KeyValuePair<string, string> pair in options_dict) { if (options.ToLower().Contains(pair.Key.ToLower())) { real_options = real_options.ToLower().Replace(pair.Key.ToLower(), pair.Value); } }
57 Options = real_options;
58 }
59 private void init_dict(IRomConfig config)
60 {
61 options_dict.Add(ROM_FILE, config.RomFile);
62 options_dict.Add(ROM_PATH, config.Config.EmuRomPath);
63 }
64 public string Options { get; set; }
65 }
66 #endregion
67
68 #region private class EmuConfig : IEmuConfig
69 private class EmuConfig : IEmuConfig, IComparable
70 {
71 public IEmuConfig Create(string rom_path) { return this.Create(string.Empty, rom_path); }
72 public IEmuConfig Create(string config_path, string rom_path)
73 {
74 EmuRomPath = rom_path;
75 if (config_path == "") { config_path = string.Format(@"{0}\{1}", rom_path, EMU_CONFIG); }
76
77 // read the actual config emu.config
78 FileInfo fi = new FileInfo(config_path);
79 if (fi.Exists)
80 {
81 logger.WriteLine("Found EMU Config File: {0}", config_path);
82 logger.WriteLine("\tLoading Config: {0}", config_path);
83 //bool loaded = false;
84 try
85 {
86 using (FileStream fs = new FileStream(config_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
87 {
88 using (XmlReader reader = XmlReader.Create(fs))
89 {
90 string value = "";
91 reader.ReadToFollowing("PLATFORMNAMESHORT");
92 value = reader.ReadElementContentAsString();
93 PlatformNameShort = (value == "") ? PlatformNameShort : value;
94 logger.WriteLine("\t\tPLATFORMNAMESHORT={0}", PlatformNameShort);
95
96 reader.ReadToFollowing("PLATFORMNAMELONG");
97 value = reader.ReadElementContentAsString();
98 PlatformNameLong = (value == "") ? PlatformNameLong : value;
99 logger.WriteLine("\t\tPLATFORMNAMELONG={0}", PlatformNameLong);
100
101 reader.ReadToFollowing("PLATFORMIMAGE");
102 string platform_image = reader.ReadElementContentAsString();
103 PlatformImage = (platform_image == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(string.Format(@"{0}\{1}", rom_path, platform_image));
104 string str_platform_image = (platform_image == "") ? "DefaultPlatformImage" : platform_image;
105 logger.WriteLine("\t\tPLATFORMIMAGE={0}", str_platform_image);
106
107 reader.ReadToFollowing("EXTENSIONS");
108 value = reader.ReadElementContentAsString();
109 Extenstions = (value == "") ? Extenstions : value;
110 logger.WriteLine("\t\tEXTENSIONS={0}", Extenstions);
111
112 reader.ReadToFollowing("EMULATORPATH");
113 value = reader.ReadElementContentAsString();
114 EmuPath = (value == "") ? EmuPath : value;
115 logger.WriteLine("\t\tEMULATORPATH={0}", EmuPath);
116
117 reader.ReadToFollowing("EMULATOROPTIONS");
118 value = reader.ReadElementContentAsString();
119 EmuOptions = (value == "") ? EmuOptions : value;
120 logger.WriteLine("\tEMULATOROPTIONS={0}", EmuOptions);
121 }
122 }
123 logger.WriteLine("\tLoaded Config: {0}", config_path);
124 //loaded = true;
125 }
126 catch (Exception ex)
127 {
128 logger.WriteLine("\tFailed to Load Config: {0}", config_path);
129 Console.WriteLine(ex.ToString());
130 logger.WriteLine("Error: {0}", ex.ToString());
131 //loaded = false;
132 }
133 }
134 else { logger.WriteLine("Could not find EMU Config File: {0}", config_path); }
135
136 return this;
137 }
138
139 private const string Unknown_Platform = "Unknown Platform";
140 public EmuConfig() : this("") { }
141 public EmuConfig(string PlatformNameShort) : this(PlatformNameShort, PlatformNameShort) { }
142 public EmuConfig(string PlatformNameShort, string PlatformNameLong) : this(PlatformNameShort, PlatformNameLong, "") { }
143 public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage) : this(PlatformNameShort, PlatformNameLong, PlatformImage, "") { }
144 public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, "") { }
145 public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, EmuPath, "") { }
146 public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath, string EmuOptions)
147 {
148 this.PlatformNameShort = PlatformNameShort;
149 this.PlatformNameLong = PlatformNameLong;
150 this.PlatformImage = (PlatformImage == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(PlatformImage);
151 this.Extenstions = (Extenstions == "") ? "*.*" : Extenstions;
152 this.EmuPath = EmuPath;
153 this.EmuOptions = EmuOptions;
154 }
155 #region IEmuConfig members
156 private string _PlatformNameShort;
157 public string PlatformNameShort
158 {
159 get
160 {
161 try
162 {
163 DirectoryInfo t = new DirectoryInfo(EmuRomPath);
164 return (_PlatformNameShort == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameShort;
165 }
166 catch(Exception ex)
167 {
168 logger.WriteLine("PlatformNameShort.get() Error: {0}",ex.ToString());
169 return Unknown_Platform;
170 }
171 }
172 set { _PlatformNameShort = value; }
173 }
174 private string _PlatformNameLong;
175 public string PlatformNameLong
176 {
177 get
178 {
179 try
180 {
181 DirectoryInfo t = new DirectoryInfo(EmuRomPath);
182 return (_PlatformNameLong == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameLong;
183 }
184 catch (Exception ex)
185 {
186 logger.WriteLine("PlatformNameLong.get() Error: {0}", ex.ToString());
187 return Unknown_Platform;
188 }
189 }
190 set { _PlatformNameLong = value; }
191 }
192 public Image PlatformImage { get; set; }
193 public string Extenstions { get; set; }
194 public string EmuPath { get; set; }
195 public string EmuOptions { get; set; }
196 public string EmuRomPath { get; set; }
197 #endregion
198
199 public override string ToString()
200 {
201 return string.Format("{2} {0}{3}{1}","{","}", PlatformNameLong,PlatformNameShort);
202 }
203 #region IComparable Members
204
205 public int CompareTo(object obj)
206 {
207 return this.PlatformNameLong.CompareTo((obj as EmuConfig).PlatformNameLong);
208 }
209 public void RefreshConfig()
210 {
211 logger.WriteLine("Refreshing config for: {0} from {1}", this.ToString(), string.Format(@"{0}\{1}", EmuRomPath, EMU_CONFIG));
212 this.Create(EmuRomPath);
213 }
214 #endregion
215 }
216 #endregion
217
218 }
219 }

  ViewVC Help
Powered by ViewVC 1.1.22