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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 258 - (hide annotations) (download)
Tue Aug 5 13:22:10 2014 UTC (7 years, 2 months ago) by william
File size: 6102 byte(s)

1 william 258 //#define CONFIGURATION_TESTING // when defined will use config-dbg.ini, otherwise config.ini
2 william 12 using System;
3     using System.Collections.Generic;
4     using System.Linq;
5     using System.Text;
6     using System.Windows.Forms;
7 william 13 using System.Xml;
8     using System.IO;
9 william 239 using Enterprise.Logging;
10 william 12
11     namespace EmuXPortal.Api
12     {
13     public interface IConfigLoader
14     {
15 william 239 bool LoadConfig();
16     bool LoadConfig(string path);
17 william 12 }
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 william 169 internal class ConfigLoader : IConfigLoader, IConfig
28 william 12 {
29     private static string APP_PATH = Application.StartupPath;
30     #if CONFIGURATION_TESTING
31     private static string CONFIG_FILE = "config-dbg.ini";
32 william 167 private static string FAVORITES_FILE = "favorites-dbg.ini";
33 william 12 #else
34 william 167 private static string CONFIG_FILE = "config.ini";
35     private static string FAVORITES_FILE = "favorites.ini";
36 william 12 #endif
37 william 169 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 william 12 #region IConfigLoader Members
41 william 239 internal static bool LoadFavorites() { return RomFavorite.LoadFavorites(FAVORITES_FILE_INI); }
42     internal static bool LoadFavorites(string ini)
43 william 12 {
44 william 239 return RomFavorite.LoadFavorites(ini);
45 william 167 }
46 william 239 public bool LoadConfig() { return LoadConfig(CONFIG_FILE_INI); }
47     public bool LoadConfig(string ini)
48 william 167 {
49     bool loaded = false;
50     try
51     {
52 william 239 gLog.Debug.WriteLine("Loading Config: {0}", ini);
53 william 167 FileInfo fi = new FileInfo(ini);
54     if (!fi.Exists)
55     {
56     loaded = false;
57 william 241 gLog.Error.WriteLine("\tFailed to Load Config (file not found): {0}", ini);
58 william 167 return loaded;
59     }
60 william 169 using (FileStream fs = new FileStream(ini, FileMode.Open, FileAccess.Read, FileShare.Read))
61 william 167 {
62     using (XmlReader reader = XmlReader.Create(fs))
63     {
64 william 13 reader.ReadToFollowing("ROMPATH");
65     RomPath = reader.ReadElementContentAsString();
66 william 33 if (RomPath == "")
67     {
68 william 241 gLog.Warn.WriteLine("\t\tROMPATH has not been configured...");
69 william 33 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 william 241 gLog.Warn.WriteLine("Terminating application...");
71 william 33 Application.Exit();
72 william 240 return false;
73 william 33 }
74 william 239 gLog.Debug.WriteLine("\tROMPATH={0}", RomPath);
75 william 13 reader.ReadToFollowing("DISPLAYDEVICE");
76     DisplayDevice = reader.ReadElementContentAsString();
77 william 33 if (DisplayDevice == "")
78     {
79 william 239 gLog.Debug.WriteLine("\t\tDISPLAYDEVICE was an empty string...using defaults: {0}", new DisplayHelper().PhysicalDisplayDevice.DeviceName);
80     DisplayDevice = new DisplayHelper().PhysicalDisplayDevice.DeviceName;
81 william 33 }
82 william 239 gLog.Debug.WriteLine("\tDISPLAYDEVICE={0}", DisplayDevice);
83 william 13 }
84     }
85 william 241 gLog.Info.WriteLine("Loaded Config: {0}", ini);
86 william 239 loaded = LoadFavorites();
87 william 167 if (!loaded)
88     {
89 william 241 gLog.Error.WriteLine("\tFailed to Load Favorites while loading the main config: {0}", ini);
90 william 167 }
91    
92 william 13 }
93     catch (Exception ex)
94     {
95 william 239 gLog.Error.WriteLine("\tFailed to Load Config: {0}", ini);
96 william 241 gLog.Verbose.Error.WriteLine(ex.ToString());
97     return false;
98 william 13 }
99 william 12 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 william 239 public static bool LoadConfig() { return loader.LoadConfig(); }
111     public static bool LoadConfig(string path) { return loader.LoadConfig(path); }
112 william 12 #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 william 239 public static void InitializePresentationForm(Form frm)
119 william 15 {
120 william 239 DisplayHelper helper = new DisplayHelper(DisplayDevice);
121     gLog.Debug.WriteLine("Initializing Form: {0}", frm.Name);
122 william 15 Screen s = helper.PhysicalDisplayDevice;
123 william 13 frm.Location = new System.Drawing.Point(s.Bounds.X,s.Bounds.Y);
124 william 239 gLog.Debug.WriteLine("\tLocation={0}", frm.Location.ToString());
125 william 13 frm.Size = s.Bounds.Size;
126 william 239 gLog.Debug.WriteLine("\tSize={0}", frm.Size.ToString());
127     gLog.Debug.WriteLine("Initialized Form: {0}", frm.Name);
128 william 13 }
129 william 12 }
130    
131    
132     }

  ViewVC Help
Powered by ViewVC 1.1.22