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 |
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 |
return config; |
95 |
} |
96 |
#endregion |
97 |
#region parse emu options |
98 |
public static string GetEMUOptions(IRomConfig config) |
99 |
{ |
100 |
EMUOptions EMUOptions = new EMUOptions(config); |
101 |
return EMUOptions.Options; |
102 |
} |
103 |
#endregion |
104 |
#region private class EMUOptions |
105 |
private class EMUOptions |
106 |
{ |
107 |
#region Replaceable Constant Options |
108 |
private const string ROM_FILE = "%ROM_FILE%"; |
109 |
private const string ROM_PATH = "%ROM_PATH%"; |
110 |
#endregion |
111 |
private Dictionary<string, string> options_dict = new Dictionary<string, string>(); |
112 |
public EMUOptions(IRomConfig config) |
113 |
{ |
114 |
init_dict(config); |
115 |
string options = config.Config.EmuOptions; |
116 |
string real_options = options; |
117 |
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); } } |
118 |
Options = real_options; |
119 |
} |
120 |
private void init_dict(IRomConfig config) |
121 |
{ |
122 |
options_dict.Add(ROM_FILE, config.RomFile); |
123 |
options_dict.Add(ROM_PATH, config.Config.EmuRomPath); |
124 |
} |
125 |
public string Options { get; set; } |
126 |
} |
127 |
#endregion |
128 |
|
129 |
#region private class EmuConfig : IEmuConfig |
130 |
private class EmuConfig : IEmuConfig |
131 |
{ |
132 |
private const string Unknown_Platform = "Unknown Platform"; |
133 |
public EmuConfig() : this("") { } |
134 |
public EmuConfig(string PlatformNameShort) : this(PlatformNameShort, PlatformNameShort) { } |
135 |
public EmuConfig(string PlatformNameShort, string PlatformNameLong) : this(PlatformNameShort, PlatformNameLong, "") { } |
136 |
public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage) : this(PlatformNameShort, PlatformNameLong, PlatformImage, "") { } |
137 |
public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, "") { } |
138 |
public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, EmuPath, "") { } |
139 |
public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath, string EmuOptions) |
140 |
{ |
141 |
this.PlatformNameShort = PlatformNameShort; |
142 |
this.PlatformNameLong = PlatformNameLong; |
143 |
this.PlatformImage = (PlatformImage == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(PlatformImage); |
144 |
this.Extenstions = (Extenstions == "") ? "*.*" : Extenstions; |
145 |
this.EmuPath = EmuPath; |
146 |
this.EmuOptions = EmuOptions; |
147 |
} |
148 |
#region IEmuConfig members |
149 |
private string _PlatformNameShort; |
150 |
public string PlatformNameShort |
151 |
{ |
152 |
get |
153 |
{ |
154 |
try |
155 |
{ |
156 |
DirectoryInfo t = new DirectoryInfo(EmuRomPath); |
157 |
return (_PlatformNameShort == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameShort; |
158 |
} |
159 |
catch(Exception ex) |
160 |
{ |
161 |
logger.WriteLine("PlatformNameShort.get() Error: {0}",ex.ToString()); |
162 |
return Unknown_Platform; |
163 |
} |
164 |
} |
165 |
set { _PlatformNameShort = value; } |
166 |
} |
167 |
private string _PlatformNameLong; |
168 |
public string PlatformNameLong |
169 |
{ |
170 |
get |
171 |
{ |
172 |
try |
173 |
{ |
174 |
DirectoryInfo t = new DirectoryInfo(EmuRomPath); |
175 |
return (_PlatformNameLong == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameLong; |
176 |
} |
177 |
catch (Exception ex) |
178 |
{ |
179 |
logger.WriteLine("PlatformNameLong.get() Error: {0}", ex.ToString()); |
180 |
return Unknown_Platform; |
181 |
} |
182 |
} |
183 |
set { _PlatformNameLong = value; } |
184 |
} |
185 |
public Image PlatformImage { get; set; } |
186 |
public string Extenstions { get; set; } |
187 |
public string EmuPath { get; set; } |
188 |
public string EmuOptions { get; set; } |
189 |
public string EmuRomPath { get; set; } |
190 |
#endregion |
191 |
} |
192 |
#endregion |
193 |
|
194 |
} |
195 |
} |