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