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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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 Enterprise.Logging;
10
11 namespace EmuXPortal.Api
12 {
13 public interface IConfigLoader
14 {
15 bool LoadConfig();
16 bool LoadConfig(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() { return RomFavorite.LoadFavorites(FAVORITES_FILE_INI); }
42 internal static bool LoadFavorites(string ini)
43 {
44 return RomFavorite.LoadFavorites(ini);
45 }
46 public bool LoadConfig() { return LoadConfig(CONFIG_FILE_INI); }
47 public bool LoadConfig(string ini)
48 {
49 bool loaded = false;
50 try
51 {
52 gLog.Debug.WriteLine("Loading Config: {0}", ini);
53 FileInfo fi = new FileInfo(ini);
54 if (!fi.Exists)
55 {
56 loaded = false;
57 gLog.Error.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 gLog.Warn.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 gLog.Warn.WriteLine("Terminating application...");
71 Application.Exit();
72 return false;
73 }
74 gLog.Debug.WriteLine("\tROMPATH={0}", RomPath);
75 reader.ReadToFollowing("DISPLAYDEVICE");
76 DisplayDevice = reader.ReadElementContentAsString();
77 if (DisplayDevice == "")
78 {
79 gLog.Debug.WriteLine("\t\tDISPLAYDEVICE was an empty string...using defaults: {0}", new DisplayHelper().PhysicalDisplayDevice.DeviceName);
80 DisplayDevice = new DisplayHelper().PhysicalDisplayDevice.DeviceName;
81 }
82 gLog.Debug.WriteLine("\tDISPLAYDEVICE={0}", DisplayDevice);
83 }
84 }
85 gLog.Info.WriteLine("Loaded Config: {0}", ini);
86 loaded = LoadFavorites();
87 if (!loaded)
88 {
89 gLog.Error.WriteLine("\tFailed to Load Favorites while loading the main config: {0}", ini);
90 }
91
92 }
93 catch (Exception ex)
94 {
95 gLog.Error.WriteLine("\tFailed to Load Config: {0}", ini);
96 gLog.Verbose.Error.WriteLine(ex.ToString());
97 return 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() { return loader.LoadConfig(); }
111 public static bool LoadConfig(string path) { return loader.LoadConfig(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(Form frm)
119 {
120 DisplayHelper helper = new DisplayHelper(DisplayDevice);
121 gLog.Debug.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 gLog.Debug.WriteLine("\tLocation={0}", frm.Location.ToString());
125 frm.Size = s.Bounds.Size;
126 gLog.Debug.WriteLine("\tSize={0}", frm.Size.ToString());
127 gLog.Debug.WriteLine("Initialized Form: {0}", frm.Name);
128 }
129 }
130
131
132 }

  ViewVC Help
Powered by ViewVC 1.1.22