ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/EmuXPortal/trunk/EmuXPortal/Api/ConfigLoader.cs
Revision: 14
Committed: Tue Apr 3 21:35:13 2012 UTC (11 years, 7 months ago) by william
File size: 3557 byte(s)
Log Message:
add logging support

File Contents

# Content
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 reader.ReadToFollowing("DISPLAYDEVICE");
51 DisplayDevice = reader.ReadElementContentAsString();
52 }
53 }
54 logger.WriteLine("Loaded Config: {0}", path);
55 loaded = true;
56 }
57 catch (Exception ex)
58 {
59 logger.WriteLine("\tFailed to Load Config: {0}", path);
60 Console.WriteLine(ex.ToString());
61 logger.WriteLine("Error: {0}", ex.ToString());
62 loaded = false;
63 }
64 return loaded;
65 }
66 #endregion
67 #region IConfig Members
68 public string RomPath { get; private set; }
69 public string DisplayDevice { get; private set; }
70 #endregion
71 }
72 #endregion
73
74 #region IConfigLoader Members
75 public static bool LoadConfig() { return loader.LoadConfig(); }
76 public static bool LoadConfig(string path) { return loader.LoadConfig(path); }
77 #endregion
78
79 #region IConfig Members
80 public static string RomPath { get { return loader.RomPath; } }
81 public static string DisplayDevice { get { return loader.DisplayDevice; } }
82 #endregion
83 public static void InitializePresentationForm(Form frm)
84 {
85 DisplayHelper helper = new DisplayHelper(DisplayDevice);
86 Screen s = helper.PhysicalDisplayDevice;
87 frm.Location = new System.Drawing.Point(s.Bounds.X,s.Bounds.Y);
88 frm.Size = s.Bounds.Size;
89 }
90 }
91
92
93 }