/[EmuXPortal]/branches/mono/EmuXPortal.Api/Api/ConfigLoader.cs
ViewVC logotype

Contents of /branches/mono/EmuXPortal.Api/Api/ConfigLoader.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 396 - (show annotations) (download)
Sun Jun 23 23:32:26 2019 UTC (6 months, 3 weeks ago) by william
File size: 6546 byte(s)
use HashSet instead of List
1 #define CONFIGURATION_TESTING // when defined will use config-dbg.ini, otherwise config.ini
2 //#if CONFIGURATION_TESTING
3 // #define DISABLE_FULLSCREEN // when CONFIGURATION_TESTING is defined, full screen will be disabled
4 //#endif
5
6 #if CONFIGURATION_TESTING
7 #warning CONFIGURATION_TESTING is enabled
8 #endif
9 using System;
10 using System.Collections.Generic;
11 using System.Linq;
12 using System.Text;
13 using System.Windows.Forms;
14 using System.Xml;
15 using System.IO;
16 using Enterprise.Logging;
17 using Enterprise.CrossPlatform;
18 using System.Xml.Linq;
19
20 namespace EmuXPortal.Api
21 {
22 public interface IConfigLoader
23 {
24 bool LoadConfig(IPlatformCache cache);
25 bool LoadConfig(IPlatformCache cache,string path);
26 }
27 public interface IConfig
28 {
29 string RomPath { get; }
30 string LocalRomPath { get; }
31 string DisplayDevice { get; }
32 }
33 public static class Config
34 {
35 private static ConfigLoader loader = new ConfigLoader();
36 #region public class ConfigLoader : IConfigLoader
37 internal class ConfigLoader : IConfigLoader, IConfig
38 {
39 private static string APP_PATH = Application.StartupPath;
40 #if CONFIGURATION_TESTING
41 private static string CONFIG_FILE = "config-dbg.ini";
42 private static string FAVORITES_FILE = "favorites-dbg.ini";
43 #else
44 private static string CONFIG_FILE = "config.ini";
45 private static string FAVORITES_FILE = "favorites.ini";
46 #endif
47 internal static string CONFIG_FILE_PATH = OSInfo.FormatPath(string.Format(@"{0}\", APP_PATH));
48 internal static string CONFIG_FILE_INI = OSInfo.FormatPath(string.Format(@"{0}{1}", CONFIG_FILE_PATH, CONFIG_FILE));
49 internal static string FAVORITES_FILE_INI = OSInfo.FormatPath(string.Format(@"{0}{1}", CONFIG_FILE_PATH, FAVORITES_FILE));
50 #region IConfigLoader Members
51 internal static bool LoadFavorites(IPlatformCache cache) { return RomFavorite.LoadFavorites(cache, FAVORITES_FILE_INI); }
52 internal static bool LoadFavorites(IPlatformCache cache,string ini)
53 {
54 return RomFavorite.LoadFavorites(cache, ini);
55 }
56 public bool LoadConfig(IPlatformCache cache) { return LoadConfig(cache,CONFIG_FILE_INI); }
57 public bool LoadConfig(IPlatformCache cache, string ini)
58 {
59 bool loaded = false;
60 try
61 {
62 //gLog.Debug.WriteLine("Loading Config: {0}", ini);
63 FileInfo fi = new FileInfo(ini);
64 if (!fi.Exists)
65 {
66 loaded = false;
67 gLog.Error.WriteLine("\tFailed to Load Config (file not found): {0}", ini);
68 return loaded;
69 }
70 XDocument xdoc = XDocument.Load (fi.FullName);
71
72 this.RomPath = xdoc.Root.Element (InternalConsts.ROMPATH) == null ? "" : xdoc.Root.Element (InternalConsts.ROMPATH).Value;
73 if (RomPath == "") {
74 gLog.Warn.WriteLine("\t\tROMPATH has not been configured...");
75 MessageBox.Show("Please edit config.ini and update ROMPATH to point to your roms folder", "ROMPATH has not been configured", MessageBoxButtons.OK, MessageBoxIcon.Error);
76 gLog.Warn.WriteLine("Terminating application...");
77 Application.Exit();
78 return false;
79 }
80 this.LocalRomPath = xdoc.Root.Element (InternalConsts.LOCALROMPATH) == null ? "" : xdoc.Root.Element (InternalConsts.LOCALROMPATH).Value;
81 this.DisplayDevice = xdoc.Root.Element (InternalConsts.DISPLAYDEVICE) == null ? "" : xdoc.Root.Element (InternalConsts.DISPLAYDEVICE).Value;
82 if (DisplayDevice == "")
83 {
84 gLog.Warn.WriteLine("\t\tDISPLAYDEVICE was an empty string...using defaults: {0}", new DisplayHelper().PhysicalDisplayDevice.DeviceName);
85 DisplayDevice = new DisplayHelper().PhysicalDisplayDevice.DeviceName;
86 }
87 else
88 {
89 var t = new DisplayHelper(DisplayDevice).PhysicalDisplayDevice.DeviceName;
90 if(DisplayDevice.ToLower() != t.ToLower()) {
91 gLog.Warn.WriteLine("Could not find display device: '{0}' [using device: '{1}' instead]", DisplayDevice, t);
92 DisplayDevice = t;
93 }
94
95 }
96
97 //gLog.Info.WriteLine("Loaded Config: {0}", ini);
98 loaded = LoadFavorites(cache);
99 if (!loaded)
100 {
101 //gLog.Error.WriteLine("\tFailed to Load Favorites while loading the main config: {0}", ini);
102 }
103
104 }
105 catch (Exception ex)
106 {
107 gLog.Error.WriteLine("\tFailed to Load Config: {0}", ini);
108 gLog.Verbose.Error.WriteLine(ex.ToString());
109 return false;
110 }
111 return loaded;
112 }
113 #endregion
114 #region IConfig Members
115 public string LocalRomPath { get; private set; }
116 public string RomPath { get; private set; }
117 public string DisplayDevice { get; private set; }
118 #endregion
119 }
120 #endregion
121
122 #region IConfigLoader Members
123 public static bool LoadConfig(IPlatformCache cache) { return loader.LoadConfig(cache); }
124 public static bool LoadConfig(IPlatformCache cache,string path) { return loader.LoadConfig(cache,path); }
125 #endregion
126
127 #region IConfig Members
128 public static string RomPath { get { return loader.RomPath; } }
129 public static string LocalRomPath { get { return loader.LocalRomPath; } }
130 public static string DisplayDevice { get { return loader.DisplayDevice; } }
131 #endregion
132 public static void InitializePresentationForm(Form frm)
133 {
134 #if !DISABLE_FULLSCREEN
135 DisplayHelper helper = new DisplayHelper(DisplayDevice);
136 //gLog.Debug.WriteLine("Initializing Form: {0}", frm.Name);
137 Screen s = helper.PhysicalDisplayDevice;
138 frm.Location = new System.Drawing.Point(s.Bounds.X,s.Bounds.Y);
139 //gLog.Debug.WriteLine("\tLocation={0}", frm.Location.ToString());
140 frm.Size = s.Bounds.Size;
141 //gLog.Debug.WriteLine("\tSize={0}", frm.Size.ToString());
142 //gLog.Debug.WriteLine("Initialized Form: {0}", frm.Name);
143 #endif
144 }
145 }
146
147
148 }

  ViewVC Help
Powered by ViewVC 1.1.22