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 |
public static class EmuConfigLoader |
25 |
{ |
26 |
private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration |
27 |
public static IEmuConfig Load(string rom_path) { return Load(string.Empty, rom_path); } |
28 |
public static IEmuConfig Load(string config_path, string rom_path) |
29 |
{ |
30 |
EmuConfig config = new EmuConfig(); |
31 |
config.EmuRomPath = rom_path; |
32 |
if (config_path == "") { config_path = string.Format(@"{0}\{1}", rom_path, EMU_CONFIG); } |
33 |
|
34 |
// read the actual config emu.config |
35 |
FileInfo fi = new FileInfo(config_path); |
36 |
if (fi.Exists) |
37 |
{ |
38 |
logger.WriteLine("Found EMU Config File: {0}", config_path); |
39 |
logger.WriteLine("\tLoading Config: {0}", config_path); |
40 |
//bool loaded = false; |
41 |
try |
42 |
{ |
43 |
using (FileStream fs = new FileStream(config_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
44 |
{ |
45 |
using (XmlReader reader = XmlReader.Create(fs)) |
46 |
{ |
47 |
string value = ""; |
48 |
reader.ReadToFollowing("PLATFORMNAMESHORT"); |
49 |
value = reader.ReadElementContentAsString(); |
50 |
config.PlatformNameShort = (value == "") ? config.PlatformNameShort: value; |
51 |
logger.WriteLine("\t\tPLATFORMNAMESHORT={0}", config.PlatformNameShort); |
52 |
|
53 |
reader.ReadToFollowing("PLATFORMNAMELONG"); |
54 |
value = reader.ReadElementContentAsString(); |
55 |
config.PlatformNameLong = (value == "") ? config.PlatformNameLong : value; |
56 |
logger.WriteLine("\t\tPLATFORMNAMELONG={0}", config.PlatformNameLong); |
57 |
|
58 |
reader.ReadToFollowing("PLATFORMIMAGE"); |
59 |
string platform_image = reader.ReadElementContentAsString(); |
60 |
config.PlatformImage = (platform_image == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(string.Format(@"{0}\{1}",rom_path,platform_image)); |
61 |
string str_platform_image = (platform_image == "") ? "DefaultPlatformImage" : platform_image; |
62 |
logger.WriteLine("\t\tPLATFORMIMAGE={0}", str_platform_image); |
63 |
|
64 |
reader.ReadToFollowing("EXTENSIONS"); |
65 |
value = reader.ReadElementContentAsString(); |
66 |
config.Extenstions = (value == "") ? config.Extenstions : value; |
67 |
logger.WriteLine("\t\tEXTENSIONS={0}", config.Extenstions); |
68 |
|
69 |
reader.ReadToFollowing("EMULATORPATH"); |
70 |
value = reader.ReadElementContentAsString(); |
71 |
config.EmuPath = (value == "") ? config.EmuPath : value; |
72 |
logger.WriteLine("\t\tEMULATORPATH={0}", config.EmuPath); |
73 |
|
74 |
reader.ReadToFollowing("EMULATOROPTIONS"); |
75 |
value = reader.ReadElementContentAsString(); |
76 |
config.EmuOptions = (value == "") ? config.EmuOptions : value; |
77 |
logger.WriteLine("\tEMULATOROPTIONS={0}", config.EmuOptions); |
78 |
} |
79 |
} |
80 |
logger.WriteLine("\tLoaded Config: {0}", config_path); |
81 |
//loaded = true; |
82 |
} |
83 |
catch(Exception ex) |
84 |
{ |
85 |
logger.WriteLine("\tFailed to Load Config: {0}", config_path); |
86 |
Console.WriteLine(ex.ToString()); |
87 |
logger.WriteLine("Error: {0}", ex.ToString()); |
88 |
//loaded = false; |
89 |
} |
90 |
} |
91 |
else { logger.WriteLine("Could not find EMU Config File: {0}", config_path); } |
92 |
return config; |
93 |
} |
94 |
#region private class EmuConfig : IEmuConfig |
95 |
private class EmuConfig : IEmuConfig |
96 |
{ |
97 |
private const string Unknown_Platform = "Unknown Platform"; |
98 |
public EmuConfig() : this("") { } |
99 |
public EmuConfig(string PlatformNameShort) : this(PlatformNameShort, PlatformNameShort) { } |
100 |
public EmuConfig(string PlatformNameShort, string PlatformNameLong) : this(PlatformNameShort, PlatformNameLong, "") { } |
101 |
public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage) : this(PlatformNameShort, PlatformNameLong, PlatformImage, "") { } |
102 |
public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, "") { } |
103 |
public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, EmuPath, "") { } |
104 |
public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath, string EmuOptions) |
105 |
{ |
106 |
this.PlatformNameShort = PlatformNameShort; |
107 |
this.PlatformNameLong = PlatformNameLong; |
108 |
this.PlatformImage = (PlatformImage == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(PlatformImage); |
109 |
this.Extenstions = (Extenstions == "") ? "*.*" : Extenstions; |
110 |
this.EmuPath = EmuPath; |
111 |
this.EmuOptions = EmuOptions; |
112 |
} |
113 |
#region IEmuConfig members |
114 |
private string _PlatformNameShort; |
115 |
public string PlatformNameShort |
116 |
{ |
117 |
get |
118 |
{ |
119 |
try |
120 |
{ |
121 |
DirectoryInfo t = new DirectoryInfo(EmuRomPath); |
122 |
return (_PlatformNameShort == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameShort; |
123 |
} |
124 |
catch(Exception ex) |
125 |
{ |
126 |
logger.WriteLine("PlatformNameShort.get() Error: {0}",ex.ToString()); |
127 |
return Unknown_Platform; |
128 |
} |
129 |
} |
130 |
set { _PlatformNameShort = value; } |
131 |
} |
132 |
private string _PlatformNameLong; |
133 |
public string PlatformNameLong |
134 |
{ |
135 |
get |
136 |
{ |
137 |
try |
138 |
{ |
139 |
DirectoryInfo t = new DirectoryInfo(EmuRomPath); |
140 |
return (_PlatformNameLong == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameLong; |
141 |
} |
142 |
catch (Exception ex) |
143 |
{ |
144 |
logger.WriteLine("PlatformNameLong.get() Error: {0}", ex.ToString()); |
145 |
return Unknown_Platform; |
146 |
} |
147 |
} |
148 |
set { _PlatformNameLong = value; } |
149 |
} |
150 |
public Image PlatformImage { get; set; } |
151 |
public string Extenstions { get; set; } |
152 |
public string EmuPath { get; set; } |
153 |
public string EmuOptions { get; set; } |
154 |
public string EmuRomPath { get; set; } |
155 |
#endregion |
156 |
} |
157 |
#endregion |
158 |
|
159 |
} |
160 |
} |