/[EmuXPortal]/trunk/EmuXPortal/Api/ConfigLoader.cs
ViewVC logotype

Contents of /trunk/EmuXPortal/Api/ConfigLoader.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 167 - (show annotations) (download)
Mon Aug 4 03:10:12 2014 UTC (6 years, 1 month ago) by william
File size: 8679 byte(s)
+ add support for loading favorites -- this feature is a WIP (work in progress)

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 private 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 private static string CONFIG_FILE_PATH = string.Format(@"{0}\", APP_PATH);
38 private static string CONFIG_FILE_INI = string.Format(@"{0}{1}", CONFIG_FILE_PATH, CONFIG_FILE);
39 private static string FAVORITES_FILE_INI = string.Format(@"{0}{1}", CONFIG_FILE_PATH, FAVORITES_FILE);
40 #region IConfigLoader Members
41 private bool LoadFavorites(logger log) { return LoadFavorites(log, FAVORITES_FILE_INI); }
42 private bool LoadFavorites(logger log, string ini)
43 {
44 bool loaded = false;
45 try
46 {
47 log.WriteLine("Loading Favorites: {0}", ini);
48 FileInfo fi = new FileInfo(ini);
49 if (!fi.Exists)
50 {
51 loaded = false;
52 log.WriteLine("\tFailed to Load Favorites (file not found): {0}", ini);
53 return loaded;
54 }
55 using (FileStream fs = new FileStream(ini, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
56 {
57 using (XmlReader reader = XmlReader.Create(fs))
58 {
59 //reader.ReadToFollowing("ROMPATH");
60 //RomPath = reader.ReadElementContentAsString();
61 //if (RomPath == "")
62 //{
63 // log.WriteLine("\t\tROMPATH has not been configured...");
64 // MessageBox.Show("Please edit config.ini and update ROMPATH to point to your roms folder", "ROMPATH has not been configured", MessageBoxButtons.OK, MessageBoxIcon.Error);
65 // log.WriteLine("Terminating application...");
66 // Application.Exit();
67 //}
68 //log.WriteLine("\tROMPATH={0}", RomPath);
69 //reader.ReadToFollowing("DISPLAYDEVICE");
70 //DisplayDevice = reader.ReadElementContentAsString();
71 //if (DisplayDevice == "")
72 //{
73 // log.WriteLine("\t\tDISPLAYDEVICE was an empty string...using defaults: {0}", new DisplayHelper(log).PhysicalDisplayDevice.DeviceName);
74 // DisplayDevice = new DisplayHelper(log).PhysicalDisplayDevice.DeviceName;
75 //}
76 //log.WriteLine("\tDISPLAYDEVICE={0}", DisplayDevice);
77 }
78 }
79 log.WriteLine("Loaded Favorites: {0}", ini);
80 loaded = true;
81
82 }
83 catch (Exception ex)
84 {
85 log.WriteLine("\tFailed to Load Favorites: {0}", ini);
86 Console.WriteLine(ex.ToString());
87 log.WriteLine("Error: {0}", ex.ToString());
88 loaded = false;
89 }
90 return loaded;
91 }
92 public bool LoadConfig(logger log) { return LoadConfig(log, CONFIG_FILE_INI); }
93 public bool LoadConfig(logger log, string ini)
94 {
95 bool loaded = false;
96 try
97 {
98 log.WriteLine("Loading Config: {0}", ini);
99 FileInfo fi = new FileInfo(ini);
100 if (!fi.Exists)
101 {
102 loaded = false;
103 log.WriteLine("\tFailed to Load Config (file not found): {0}", ini);
104 return loaded;
105 }
106 using (FileStream fs = new FileStream(ini, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
107 {
108 using (XmlReader reader = XmlReader.Create(fs))
109 {
110 reader.ReadToFollowing("ROMPATH");
111 RomPath = reader.ReadElementContentAsString();
112 if (RomPath == "")
113 {
114 log.WriteLine("\t\tROMPATH has not been configured...");
115 MessageBox.Show("Please edit config.ini and update ROMPATH to point to your roms folder", "ROMPATH has not been configured", MessageBoxButtons.OK, MessageBoxIcon.Error);
116 log.WriteLine("Terminating application...");
117 Application.Exit();
118 }
119 log.WriteLine("\tROMPATH={0}", RomPath);
120 reader.ReadToFollowing("DISPLAYDEVICE");
121 DisplayDevice = reader.ReadElementContentAsString();
122 if (DisplayDevice == "")
123 {
124 log.WriteLine("\t\tDISPLAYDEVICE was an empty string...using defaults: {0}", new DisplayHelper(log).PhysicalDisplayDevice.DeviceName);
125 DisplayDevice = new DisplayHelper(log).PhysicalDisplayDevice.DeviceName;
126 }
127 log.WriteLine("\tDISPLAYDEVICE={0}", DisplayDevice);
128 }
129 }
130 log.WriteLine("Loaded Config: {0}", ini);
131 loaded = LoadFavorites(log);
132 if (!loaded)
133 {
134 log.WriteLine("\tFailed to Load Favorites while loading the main config: {0}", ini);
135 }
136
137 }
138 catch (Exception ex)
139 {
140 log.WriteLine("\tFailed to Load Config: {0}", ini);
141 Console.WriteLine(ex.ToString());
142 log.WriteLine("Error: {0}", ex.ToString());
143 loaded = false;
144 }
145 return loaded;
146 }
147 #endregion
148 #region IConfig Members
149 public string RomPath { get; private set; }
150 public string DisplayDevice { get; private set; }
151 #endregion
152 }
153 #endregion
154
155 #region IConfigLoader Members
156 public static bool LoadConfig(logger log) { return loader.LoadConfig(log); }
157 public static bool LoadConfig(logger log,string path) { return loader.LoadConfig(log, path); }
158 #endregion
159
160 #region IConfig Members
161 public static string RomPath { get { return loader.RomPath; } }
162 public static string DisplayDevice { get { return loader.DisplayDevice; } }
163 #endregion
164 public static void InitializePresentationForm(logger log,Form frm)
165 {
166 DisplayHelper helper = new DisplayHelper(log, DisplayDevice);
167 log.WriteLine("Initializing Form: {0}", frm.Name);
168 Screen s = helper.PhysicalDisplayDevice;
169 frm.Location = new System.Drawing.Point(s.Bounds.X,s.Bounds.Y);
170 log.WriteLine("\tLocation={0}", frm.Location.ToString());
171 frm.Size = s.Bounds.Size;
172 log.WriteLine("\tSize={0}", frm.Size.ToString());
173 log.WriteLine("Initialized Form: {0}", frm.Name);
174 }
175 }
176
177
178 }

  ViewVC Help
Powered by ViewVC 1.1.22