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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Wed Apr 4 05:12:58 2012 UTC (8 years, 7 months ago) by william
File size: 9967 byte(s)
+ fix issues with selecting entries based on key press (a-z, 0-9)

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

  ViewVC Help
Powered by ViewVC 1.1.22