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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (hide annotations) (download)
Wed Apr 4 05:42:03 2012 UTC (8 years, 6 months ago) by william
File size: 10172 byte(s)
reformat Platform name to incorporate both short and long names

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 william 31 public interface IEmuConfig : IComparable
13 william 17 {
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 william 32 string ToString();
22 william 17 }
23    
24 william 23
25 william 17
26     public static class EmuConfigLoader
27     {
28     private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration
29 william 23 #region load
30 william 17 public static IEmuConfig Load(string rom_path) { return Load(string.Empty, rom_path); }
31     public static IEmuConfig Load(string config_path, string rom_path)
32     {
33     EmuConfig config = new EmuConfig();
34     config.EmuRomPath = rom_path;
35     if (config_path == "") { config_path = string.Format(@"{0}\{1}", rom_path, EMU_CONFIG); }
36    
37     // read the actual config emu.config
38     FileInfo fi = new FileInfo(config_path);
39     if (fi.Exists)
40     {
41     logger.WriteLine("Found EMU Config File: {0}", config_path);
42 william 20 logger.WriteLine("\tLoading Config: {0}", config_path);
43     //bool loaded = false;
44     try
45     {
46     using (FileStream fs = new FileStream(config_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
47     {
48     using (XmlReader reader = XmlReader.Create(fs))
49     {
50 william 22 string value = "";
51 william 20 reader.ReadToFollowing("PLATFORMNAMESHORT");
52 william 22 value = reader.ReadElementContentAsString();
53     config.PlatformNameShort = (value == "") ? config.PlatformNameShort: value;
54 william 21 logger.WriteLine("\t\tPLATFORMNAMESHORT={0}", config.PlatformNameShort);
55 william 20
56     reader.ReadToFollowing("PLATFORMNAMELONG");
57 william 22 value = reader.ReadElementContentAsString();
58     config.PlatformNameLong = (value == "") ? config.PlatformNameLong : value;
59 william 21 logger.WriteLine("\t\tPLATFORMNAMELONG={0}", config.PlatformNameLong);
60 william 20
61     reader.ReadToFollowing("PLATFORMIMAGE");
62 william 21 string platform_image = reader.ReadElementContentAsString();
63 william 20 config.PlatformImage = (platform_image == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(string.Format(@"{0}\{1}",rom_path,platform_image));
64     string str_platform_image = (platform_image == "") ? "DefaultPlatformImage" : platform_image;
65 william 21 logger.WriteLine("\t\tPLATFORMIMAGE={0}", str_platform_image);
66 william 20
67     reader.ReadToFollowing("EXTENSIONS");
68 william 22 value = reader.ReadElementContentAsString();
69     config.Extenstions = (value == "") ? config.Extenstions : value;
70 william 21 logger.WriteLine("\t\tEXTENSIONS={0}", config.Extenstions);
71 william 20
72     reader.ReadToFollowing("EMULATORPATH");
73 william 22 value = reader.ReadElementContentAsString();
74     config.EmuPath = (value == "") ? config.EmuPath : value;
75 william 21 logger.WriteLine("\t\tEMULATORPATH={0}", config.EmuPath);
76 william 20
77     reader.ReadToFollowing("EMULATOROPTIONS");
78 william 22 value = reader.ReadElementContentAsString();
79     config.EmuOptions = (value == "") ? config.EmuOptions : value;
80     logger.WriteLine("\tEMULATOROPTIONS={0}", config.EmuOptions);
81 william 20 }
82     }
83     logger.WriteLine("\tLoaded Config: {0}", config_path);
84     //loaded = true;
85     }
86     catch(Exception ex)
87     {
88     logger.WriteLine("\tFailed to Load Config: {0}", config_path);
89     Console.WriteLine(ex.ToString());
90     logger.WriteLine("Error: {0}", ex.ToString());
91     //loaded = false;
92     }
93 william 17 }
94 william 20 else { logger.WriteLine("Could not find EMU Config File: {0}", config_path); }
95 william 31
96 william 17 return config;
97     }
98 william 23 #endregion
99     #region parse emu options
100     public static string GetEMUOptions(IRomConfig config)
101     {
102     EMUOptions EMUOptions = new EMUOptions(config);
103     return EMUOptions.Options;
104     }
105     #endregion
106     #region private class EMUOptions
107     private class EMUOptions
108     {
109 william 24 #region Replaceable Constant Options
110     private const string ROM_FILE = "%ROM_FILE%";
111     private const string ROM_PATH = "%ROM_PATH%";
112     #endregion
113 william 23 private Dictionary<string, string> options_dict = new Dictionary<string, string>();
114     public EMUOptions(IRomConfig config)
115     {
116     init_dict(config);
117     string options = config.Config.EmuOptions;
118     string real_options = options;
119     foreach (KeyValuePair<string, string> pair in options_dict) { if (options.ToLower().Contains(pair.Key.ToLower())) { real_options = real_options.ToLower().Replace(pair.Key.ToLower(), pair.Value); } }
120     Options = real_options;
121     }
122     private void init_dict(IRomConfig config)
123     {
124 william 24 options_dict.Add(ROM_FILE, config.RomFile);
125     options_dict.Add(ROM_PATH, config.Config.EmuRomPath);
126 william 23 }
127     public string Options { get; set; }
128     }
129     #endregion
130    
131 william 17 #region private class EmuConfig : IEmuConfig
132 william 31 private class EmuConfig : IEmuConfig, IComparable
133 william 17 {
134     private const string Unknown_Platform = "Unknown Platform";
135     public EmuConfig() : this("") { }
136     public EmuConfig(string PlatformNameShort) : this(PlatformNameShort, PlatformNameShort) { }
137     public EmuConfig(string PlatformNameShort, string PlatformNameLong) : this(PlatformNameShort, PlatformNameLong, "") { }
138     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage) : this(PlatformNameShort, PlatformNameLong, PlatformImage, "") { }
139     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, "") { }
140     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, EmuPath, "") { }
141     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath, string EmuOptions)
142     {
143     this.PlatformNameShort = PlatformNameShort;
144     this.PlatformNameLong = PlatformNameLong;
145     this.PlatformImage = (PlatformImage == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(PlatformImage);
146     this.Extenstions = (Extenstions == "") ? "*.*" : Extenstions;
147     this.EmuPath = EmuPath;
148     this.EmuOptions = EmuOptions;
149     }
150     #region IEmuConfig members
151     private string _PlatformNameShort;
152     public string PlatformNameShort
153     {
154     get
155     {
156     try
157     {
158     DirectoryInfo t = new DirectoryInfo(EmuRomPath);
159     return (_PlatformNameShort == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameShort;
160     }
161     catch(Exception ex)
162     {
163     logger.WriteLine("PlatformNameShort.get() Error: {0}",ex.ToString());
164     return Unknown_Platform;
165     }
166     }
167 william 20 set { _PlatformNameShort = value; }
168 william 17 }
169     private string _PlatformNameLong;
170     public string PlatformNameLong
171     {
172     get
173     {
174     try
175     {
176     DirectoryInfo t = new DirectoryInfo(EmuRomPath);
177     return (_PlatformNameLong == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameLong;
178     }
179     catch (Exception ex)
180     {
181     logger.WriteLine("PlatformNameLong.get() Error: {0}", ex.ToString());
182     return Unknown_Platform;
183     }
184     }
185 william 20 set { _PlatformNameLong = value; }
186 william 17 }
187 william 20 public Image PlatformImage { get; set; }
188     public string Extenstions { get; set; }
189     public string EmuPath { get; set; }
190     public string EmuOptions { get; set; }
191 william 17 public string EmuRomPath { get; set; }
192     #endregion
193 william 31
194 william 32 public override string ToString()
195     {
196     return string.Format("{2} {0}{3}{1}","{","}", PlatformNameLong,PlatformNameShort);
197     }
198 william 31 #region IComparable Members
199    
200     public int CompareTo(object obj)
201     {
202     return this.PlatformNameLong.CompareTo((obj as EmuConfig).PlatformNameLong);
203     }
204    
205     #endregion
206 william 17 }
207     #endregion
208    
209     }
210     }

  ViewVC Help
Powered by ViewVC 1.1.22