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 |
using EmuXPortal.Logging; |
10 |
|
11 |
namespace EmuXPortal.Api |
12 |
{ |
13 |
public interface IConfigLoader |
14 |
{ |
15 |
bool LoadConfig(logger log); |
16 |
bool LoadConfig(logger log, string path); |
17 |
} |
18 |
public interface IConfig |
19 |
{ |
20 |
string RomPath { get; } |
21 |
string DisplayDevice { get; } |
22 |
} |
23 |
public static class Config |
24 |
{ |
25 |
private static ConfigLoader loader = new ConfigLoader(); |
26 |
#region public class ConfigLoader : IConfigLoader |
27 |
internal class ConfigLoader : IConfigLoader, IConfig |
28 |
{ |
29 |
private static string APP_PATH = Application.StartupPath; |
30 |
#if CONFIGURATION_TESTING |
31 |
private static string CONFIG_FILE = "config-dbg.ini"; |
32 |
private static string FAVORITES_FILE = "favorites-dbg.ini"; |
33 |
#else |
34 |
private static string CONFIG_FILE = "config.ini"; |
35 |
private static string FAVORITES_FILE = "favorites.ini"; |
36 |
#endif |
37 |
internal static string CONFIG_FILE_PATH = string.Format(@"{0}\", APP_PATH); |
38 |
internal static string CONFIG_FILE_INI = string.Format(@"{0}{1}", CONFIG_FILE_PATH, CONFIG_FILE); |
39 |
internal static string FAVORITES_FILE_INI = string.Format(@"{0}{1}", CONFIG_FILE_PATH, FAVORITES_FILE); |
40 |
#region IConfigLoader Members |
41 |
internal static bool LoadFavorites(logger log) { return RomFavorite.LoadFavorites(log, FAVORITES_FILE_INI); } |
42 |
internal static bool LoadFavorites(logger log, string ini) |
43 |
{ |
44 |
return RomFavorite.LoadFavorites(log, ini); |
45 |
} |
46 |
public bool LoadConfig(logger log) { return LoadConfig(log, CONFIG_FILE_INI); } |
47 |
public bool LoadConfig(logger log, string ini) |
48 |
{ |
49 |
bool loaded = false; |
50 |
try |
51 |
{ |
52 |
log.WriteLine("Loading Config: {0}", ini); |
53 |
FileInfo fi = new FileInfo(ini); |
54 |
if (!fi.Exists) |
55 |
{ |
56 |
loaded = false; |
57 |
log.WriteLine("\tFailed to Load Config (file not found): {0}", ini); |
58 |
return loaded; |
59 |
} |
60 |
using (FileStream fs = new FileStream(ini, FileMode.Open, FileAccess.Read, FileShare.Read)) |
61 |
{ |
62 |
using (XmlReader reader = XmlReader.Create(fs)) |
63 |
{ |
64 |
reader.ReadToFollowing("ROMPATH"); |
65 |
RomPath = reader.ReadElementContentAsString(); |
66 |
if (RomPath == "") |
67 |
{ |
68 |
log.WriteLine("\t\tROMPATH has not been configured..."); |
69 |
MessageBox.Show("Please edit config.ini and update ROMPATH to point to your roms folder", "ROMPATH has not been configured", MessageBoxButtons.OK, MessageBoxIcon.Error); |
70 |
log.WriteLine("Terminating application..."); |
71 |
Application.Exit(); |
72 |
} |
73 |
log.WriteLine("\tROMPATH={0}", RomPath); |
74 |
reader.ReadToFollowing("DISPLAYDEVICE"); |
75 |
DisplayDevice = reader.ReadElementContentAsString(); |
76 |
if (DisplayDevice == "") |
77 |
{ |
78 |
log.WriteLine("\t\tDISPLAYDEVICE was an empty string...using defaults: {0}", new DisplayHelper(log).PhysicalDisplayDevice.DeviceName); |
79 |
DisplayDevice = new DisplayHelper(log).PhysicalDisplayDevice.DeviceName; |
80 |
} |
81 |
log.WriteLine("\tDISPLAYDEVICE={0}", DisplayDevice); |
82 |
} |
83 |
} |
84 |
log.WriteLine("Loaded Config: {0}", ini); |
85 |
loaded = LoadFavorites(log); |
86 |
if (!loaded) |
87 |
{ |
88 |
log.WriteLine("\tFailed to Load Favorites while loading the main config: {0}", ini); |
89 |
} |
90 |
|
91 |
} |
92 |
catch (Exception ex) |
93 |
{ |
94 |
log.WriteLine("\tFailed to Load Config: {0}", ini); |
95 |
Console.WriteLine(ex.ToString()); |
96 |
log.WriteLine("Error: {0}", ex.ToString()); |
97 |
loaded = false; |
98 |
} |
99 |
return loaded; |
100 |
} |
101 |
#endregion |
102 |
#region IConfig Members |
103 |
public string RomPath { get; private set; } |
104 |
public string DisplayDevice { get; private set; } |
105 |
#endregion |
106 |
} |
107 |
#endregion |
108 |
|
109 |
#region IConfigLoader Members |
110 |
public static bool LoadConfig(logger log) { return loader.LoadConfig(log); } |
111 |
public static bool LoadConfig(logger log,string path) { return loader.LoadConfig(log, path); } |
112 |
#endregion |
113 |
|
114 |
#region IConfig Members |
115 |
public static string RomPath { get { return loader.RomPath; } } |
116 |
public static string DisplayDevice { get { return loader.DisplayDevice; } } |
117 |
#endregion |
118 |
public static void InitializePresentationForm(logger log,Form frm) |
119 |
{ |
120 |
DisplayHelper helper = new DisplayHelper(log, DisplayDevice); |
121 |
log.WriteLine("Initializing Form: {0}", frm.Name); |
122 |
Screen s = helper.PhysicalDisplayDevice; |
123 |
frm.Location = new System.Drawing.Point(s.Bounds.X,s.Bounds.Y); |
124 |
log.WriteLine("\tLocation={0}", frm.Location.ToString()); |
125 |
frm.Size = s.Bounds.Size; |
126 |
log.WriteLine("\tSize={0}", frm.Size.ToString()); |
127 |
log.WriteLine("Initialized Form: {0}", frm.Name); |
128 |
} |
129 |
} |
130 |
|
131 |
|
132 |
} |