1 |
#define CONFIGURATION_TESTING // when defined will use config-dbg.ini, otherwise config.ini |
2 |
using System; |
3 |
using System.Collections.Generic; |
4 |
using System.Linq; |
5 |
using System.Text; |
6 |
using System.Windows.Forms; |
7 |
using System.Xml; |
8 |
using System.IO; |
9 |
|
10 |
namespace EmuXPortal.Api |
11 |
{ |
12 |
public interface IConfigLoader |
13 |
{ |
14 |
bool LoadConfig(); |
15 |
bool LoadConfig(string path); |
16 |
} |
17 |
public interface IConfig |
18 |
{ |
19 |
string RomPath { get; } |
20 |
string DisplayDevice { get; } |
21 |
} |
22 |
public static class Config |
23 |
{ |
24 |
private static ConfigLoader loader = new ConfigLoader(); |
25 |
#region public class ConfigLoader : IConfigLoader |
26 |
private class ConfigLoader : IConfigLoader, IConfig |
27 |
{ |
28 |
private static string APP_PATH = Application.StartupPath; |
29 |
#if CONFIGURATION_TESTING |
30 |
private static string CONFIG_FILE = "config-dbg.ini"; |
31 |
#else |
32 |
private static string CONFIG_FILE = "config.ini"; |
33 |
#endif |
34 |
private static string CONFIG_FILE_PATH = string.Format(@"{0}\{1}", APP_PATH, CONFIG_FILE); |
35 |
#region IConfigLoader Members |
36 |
public bool LoadConfig() { return LoadConfig(CONFIG_FILE_PATH); } |
37 |
public bool LoadConfig(string path) |
38 |
{ |
39 |
bool loaded = false; |
40 |
try |
41 |
{ |
42 |
using (FileStream fs = new FileStream(CONFIG_FILE_PATH, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
43 |
{ |
44 |
using (XmlReader reader = XmlReader.Create(fs)) |
45 |
{ |
46 |
reader.ReadToFollowing("ROMPATH"); |
47 |
RomPath = reader.ReadElementContentAsString(); |
48 |
reader.ReadToFollowing("DISPLAYDEVICE"); |
49 |
DisplayDevice = reader.ReadElementContentAsString(); |
50 |
} |
51 |
} |
52 |
loaded = true; |
53 |
} |
54 |
catch (Exception ex) |
55 |
{ |
56 |
Console.WriteLine(ex.ToString()); |
57 |
loaded = false; |
58 |
} |
59 |
return loaded; |
60 |
} |
61 |
#endregion |
62 |
#region IConfig Members |
63 |
public string RomPath { get; private set; } |
64 |
public string DisplayDevice { get; private set; } |
65 |
#endregion |
66 |
} |
67 |
#endregion |
68 |
|
69 |
#region IConfigLoader Members |
70 |
public static bool LoadConfig() { return loader.LoadConfig(); } |
71 |
public static bool LoadConfig(string path) { return loader.LoadConfig(path); } |
72 |
#endregion |
73 |
|
74 |
#region IConfig Members |
75 |
public static string RomPath { get { return loader.RomPath; } } |
76 |
public static string DisplayDevice { get { return loader.DisplayDevice; } } |
77 |
#endregion |
78 |
public static void InitializePresentationForm(Form frm) |
79 |
{ |
80 |
DisplayHelper helper = new DisplayHelper(DisplayDevice); |
81 |
Screen s = helper.PhysicalDisplayDevice; |
82 |
frm.Location = new System.Drawing.Point(s.Bounds.X,s.Bounds.Y); |
83 |
frm.Size = s.Bounds.Size; |
84 |
} |
85 |
} |
86 |
|
87 |
|
88 |
} |