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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 52 - (show annotations) (download)
Sun Apr 8 10:33:25 2012 UTC (8 years, 5 months ago) by william
File size: 4940 byte(s)
enable: CONFIGURATION_TESTING (should be done in r52, remove this from any merges)

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();
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 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 #else
33 private static string CONFIG_FILE = "config.ini";
34 #endif
35 private static string CONFIG_FILE_PATH = string.Format(@"{0}\{1}", APP_PATH, CONFIG_FILE);
36 #region IConfigLoader Members
37 public bool LoadConfig() { return LoadConfig(CONFIG_FILE_PATH); }
38 public bool LoadConfig(string path)
39 {
40 bool loaded = false;
41 try
42 {
43 logger.WriteLine("Loading Config: {0}", path);
44 using (FileStream fs = new FileStream(CONFIG_FILE_PATH, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
45 {
46 using (XmlReader reader = XmlReader.Create(fs))
47 {
48 reader.ReadToFollowing("ROMPATH");
49 RomPath = reader.ReadElementContentAsString();
50 if (RomPath == "")
51 {
52 logger.WriteLine("\t\tROMPATH has not been configured...");
53 MessageBox.Show("Please edit config.ini and update ROMPATH to point to your roms folder", "ROMPATH has not been configured", MessageBoxButtons.OK, MessageBoxIcon.Error);
54 logger.WriteLine("Terminating application...");
55 Application.Exit();
56 }
57 logger.WriteLine("\tROMPATH={0}", RomPath);
58 reader.ReadToFollowing("DISPLAYDEVICE");
59 DisplayDevice = reader.ReadElementContentAsString();
60 if (DisplayDevice == "")
61 {
62 logger.WriteLine("\t\tDISPLAYDEVICE was an empty string...using defaults: {0}", new DisplayHelper().PhysicalDisplayDevice.DeviceName);
63 DisplayDevice = new DisplayHelper().PhysicalDisplayDevice.DeviceName;
64 }
65 logger.WriteLine("\tDISPLAYDEVICE={0}", DisplayDevice);
66 }
67 }
68 logger.WriteLine("Loaded Config: {0}", path);
69 loaded = true;
70
71 }
72 catch (Exception ex)
73 {
74 logger.WriteLine("\tFailed to Load Config: {0}", path);
75 Console.WriteLine(ex.ToString());
76 logger.WriteLine("Error: {0}", ex.ToString());
77 loaded = false;
78 }
79 return loaded;
80 }
81 #endregion
82 #region IConfig Members
83 public string RomPath { get; private set; }
84 public string DisplayDevice { get; private set; }
85 #endregion
86 }
87 #endregion
88
89 #region IConfigLoader Members
90 public static bool LoadConfig() { return loader.LoadConfig(); }
91 public static bool LoadConfig(string path) { return loader.LoadConfig(path); }
92 #endregion
93
94 #region IConfig Members
95 public static string RomPath { get { return loader.RomPath; } }
96 public static string DisplayDevice { get { return loader.DisplayDevice; } }
97 #endregion
98 public static void InitializePresentationForm(Form frm)
99 {
100 DisplayHelper helper = new DisplayHelper(DisplayDevice);
101 logger.WriteLine("Initializing Form: {0}", frm.Name);
102 Screen s = helper.PhysicalDisplayDevice;
103 frm.Location = new System.Drawing.Point(s.Bounds.X,s.Bounds.Y);
104 logger.WriteLine("\tLocation={0}", frm.Location.ToString());
105 frm.Size = s.Bounds.Size;
106 logger.WriteLine("\tSize={0}", frm.Size.ToString());
107 logger.WriteLine("Initialized Form: {0}", frm.Name);
108 }
109 }
110
111
112 }

  ViewVC Help
Powered by ViewVC 1.1.22