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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 20 - (hide annotations) (download)
Wed Apr 4 02:11:37 2012 UTC (8 years, 7 months ago) by william
File size: 7767 byte(s)
implement actual reading of emu.config

1 william 17 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using System.Drawing;
6     using System.IO;
7     using EmuXPortal.Logging;
8 william 20 using System.Xml;
9 william 17
10     namespace EmuXPortal.Api
11     {
12     public interface IEmuConfig
13     {
14     string PlatformNameShort { get; }
15     string PlatformNameLong { get; }
16     Image PlatformImage { get; }
17     string Extenstions { get; }
18     string EmuPath { get; }
19     string EmuOptions { get; }
20     string EmuRomPath { get; set; }
21     }
22    
23    
24     public static class EmuConfigLoader
25     {
26     private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration
27     public static IEmuConfig Load(string rom_path) { return Load(string.Empty, rom_path); }
28     public static IEmuConfig Load(string config_path, string rom_path)
29     {
30     EmuConfig config = new EmuConfig();
31     config.EmuRomPath = rom_path;
32     if (config_path == "") { config_path = string.Format(@"{0}\{1}", rom_path, EMU_CONFIG); }
33    
34     // read the actual config emu.config
35     FileInfo fi = new FileInfo(config_path);
36     if (fi.Exists)
37     {
38     logger.WriteLine("Found EMU Config File: {0}", config_path);
39 william 20 logger.WriteLine("\tLoading Config: {0}", config_path);
40     //bool loaded = false;
41     try
42     {
43     using (FileStream fs = new FileStream(config_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
44     {
45     using (XmlReader reader = XmlReader.Create(fs))
46     {
47     reader.ReadToFollowing("PLATFORMNAMESHORT");
48     config.PlatformNameShort = reader.ReadElementContentAsString();
49     logger.WriteLine("\tPLATFORMNAMESHORT={0}", config.PlatformNameShort);
50    
51     reader.ReadToFollowing("PLATFORMNAMELONG");
52     config.PlatformNameLong = reader.ReadElementContentAsString();
53     logger.WriteLine("\tPLATFORMNAMELONG={0}", config.PlatformNameLong);
54    
55     reader.ReadToFollowing("PLATFORMIMAGE");
56     string platform_image = reader.ReadElementContentAsString();
57    
58     config.PlatformImage = (platform_image == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(string.Format(@"{0}\{1}",rom_path,platform_image));
59     string str_platform_image = (platform_image == "") ? "DefaultPlatformImage" : platform_image;
60     logger.WriteLine("\tPLATFORMIMAGE={0}", str_platform_image);
61    
62     reader.ReadToFollowing("EXTENSIONS");
63     config.Extenstions = reader.ReadElementContentAsString();
64     logger.WriteLine("\tEXTENSIONS={0}", config.Extenstions);
65    
66     reader.ReadToFollowing("EMULATORPATH");
67     config.EmuPath = reader.ReadElementContentAsString();
68     logger.WriteLine("\tEMULATORPATH={0}", config.EmuPath);
69    
70     reader.ReadToFollowing("EMULATOROPTIONS");
71     config.EmuOptions = reader.ReadElementContentAsString();
72     logger.WriteLine("\tEMULATOROPTIONS={0}", config.EmuOptions);
73     }
74     }
75     logger.WriteLine("\tLoaded Config: {0}", config_path);
76     //loaded = true;
77     }
78     catch(Exception ex)
79     {
80     logger.WriteLine("\tFailed to Load Config: {0}", config_path);
81     Console.WriteLine(ex.ToString());
82     logger.WriteLine("Error: {0}", ex.ToString());
83     //loaded = false;
84     }
85 william 17 }
86 william 20 else { logger.WriteLine("Could not find EMU Config File: {0}", config_path); }
87 william 17 return config;
88     }
89     #region private class EmuConfig : IEmuConfig
90     private class EmuConfig : IEmuConfig
91     {
92     private const string Unknown_Platform = "Unknown Platform";
93     public EmuConfig() : this("") { }
94     public EmuConfig(string PlatformNameShort) : this(PlatformNameShort, PlatformNameShort) { }
95     public EmuConfig(string PlatformNameShort, string PlatformNameLong) : this(PlatformNameShort, PlatformNameLong, "") { }
96     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage) : this(PlatformNameShort, PlatformNameLong, PlatformImage, "") { }
97     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, "") { }
98     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, EmuPath, "") { }
99     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath, string EmuOptions)
100     {
101     this.PlatformNameShort = PlatformNameShort;
102     this.PlatformNameLong = PlatformNameLong;
103     this.PlatformImage = (PlatformImage == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(PlatformImage);
104     this.Extenstions = (Extenstions == "") ? "*.*" : Extenstions;
105     this.EmuPath = EmuPath;
106     this.EmuOptions = EmuOptions;
107     }
108     #region IEmuConfig members
109     private string _PlatformNameShort;
110     public string PlatformNameShort
111     {
112     get
113     {
114     try
115     {
116     DirectoryInfo t = new DirectoryInfo(EmuRomPath);
117     return (_PlatformNameShort == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameShort;
118     }
119     catch(Exception ex)
120     {
121     logger.WriteLine("PlatformNameShort.get() Error: {0}",ex.ToString());
122     return Unknown_Platform;
123     }
124     }
125 william 20 set { _PlatformNameShort = value; }
126 william 17 }
127     private string _PlatformNameLong;
128     public string PlatformNameLong
129     {
130     get
131     {
132     try
133     {
134     DirectoryInfo t = new DirectoryInfo(EmuRomPath);
135     return (_PlatformNameLong == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameLong;
136     }
137     catch (Exception ex)
138     {
139     logger.WriteLine("PlatformNameLong.get() Error: {0}", ex.ToString());
140     return Unknown_Platform;
141     }
142     }
143 william 20 set { _PlatformNameLong = value; }
144 william 17 }
145 william 20 public Image PlatformImage { get; set; }
146     public string Extenstions { get; set; }
147     public string EmuPath { get; set; }
148     public string EmuOptions { get; set; }
149 william 17 public string EmuRomPath { get; set; }
150     #endregion
151     }
152     #endregion
153    
154     }
155     }

  ViewVC Help
Powered by ViewVC 1.1.22