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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (show annotations) (download)
Wed Apr 4 02:13:41 2012 UTC (8 years, 5 months ago) by william
File size: 7777 byte(s)
slight ouput format change

1 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 using System.Xml;
9
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 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("\t\tPLATFORMNAMESHORT={0}", config.PlatformNameShort);
50
51 reader.ReadToFollowing("PLATFORMNAMELONG");
52 config.PlatformNameLong = reader.ReadElementContentAsString();
53 logger.WriteLine("\t\tPLATFORMNAMELONG={0}", config.PlatformNameLong);
54
55 reader.ReadToFollowing("PLATFORMIMAGE");
56 string platform_image = reader.ReadElementContentAsString();
57 config.PlatformImage = (platform_image == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(string.Format(@"{0}\{1}",rom_path,platform_image));
58 string str_platform_image = (platform_image == "") ? "DefaultPlatformImage" : platform_image;
59 logger.WriteLine("\t\tPLATFORMIMAGE={0}", str_platform_image);
60
61 reader.ReadToFollowing("EXTENSIONS");
62 config.Extenstions = reader.ReadElementContentAsString();
63 logger.WriteLine("\t\tEXTENSIONS={0}", config.Extenstions);
64
65 reader.ReadToFollowing("EMULATORPATH");
66 config.EmuPath = reader.ReadElementContentAsString();
67 logger.WriteLine("\t\tEMULATORPATH={0}", config.EmuPath);
68
69 reader.ReadToFollowing("EMULATOROPTIONS");
70 config.EmuOptions = reader.ReadElementContentAsString();
71 logger.WriteLine("\t\tEMULATOROPTIONS={0}", config.EmuOptions);
72 }
73 }
74 logger.WriteLine("\tLoaded Config: {0}", config_path);
75 //loaded = true;
76 }
77 catch(Exception ex)
78 {
79 logger.WriteLine("\tFailed to Load Config: {0}", config_path);
80 Console.WriteLine(ex.ToString());
81 logger.WriteLine("Error: {0}", ex.ToString());
82 //loaded = false;
83 }
84 }
85 else { logger.WriteLine("Could not find EMU Config File: {0}", config_path); }
86 return config;
87 }
88 #region private class EmuConfig : IEmuConfig
89 private class EmuConfig : IEmuConfig
90 {
91 private const string Unknown_Platform = "Unknown Platform";
92 public EmuConfig() : this("") { }
93 public EmuConfig(string PlatformNameShort) : this(PlatformNameShort, PlatformNameShort) { }
94 public EmuConfig(string PlatformNameShort, string PlatformNameLong) : this(PlatformNameShort, PlatformNameLong, "") { }
95 public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage) : this(PlatformNameShort, PlatformNameLong, PlatformImage, "") { }
96 public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, "") { }
97 public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, EmuPath, "") { }
98 public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath, string EmuOptions)
99 {
100 this.PlatformNameShort = PlatformNameShort;
101 this.PlatformNameLong = PlatformNameLong;
102 this.PlatformImage = (PlatformImage == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(PlatformImage);
103 this.Extenstions = (Extenstions == "") ? "*.*" : Extenstions;
104 this.EmuPath = EmuPath;
105 this.EmuOptions = EmuOptions;
106 }
107 #region IEmuConfig members
108 private string _PlatformNameShort;
109 public string PlatformNameShort
110 {
111 get
112 {
113 try
114 {
115 DirectoryInfo t = new DirectoryInfo(EmuRomPath);
116 return (_PlatformNameShort == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameShort;
117 }
118 catch(Exception ex)
119 {
120 logger.WriteLine("PlatformNameShort.get() Error: {0}",ex.ToString());
121 return Unknown_Platform;
122 }
123 }
124 set { _PlatformNameShort = value; }
125 }
126 private string _PlatformNameLong;
127 public string PlatformNameLong
128 {
129 get
130 {
131 try
132 {
133 DirectoryInfo t = new DirectoryInfo(EmuRomPath);
134 return (_PlatformNameLong == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameLong;
135 }
136 catch (Exception ex)
137 {
138 logger.WriteLine("PlatformNameLong.get() Error: {0}", ex.ToString());
139 return Unknown_Platform;
140 }
141 }
142 set { _PlatformNameLong = value; }
143 }
144 public Image PlatformImage { get; set; }
145 public string Extenstions { get; set; }
146 public string EmuPath { get; set; }
147 public string EmuOptions { get; set; }
148 public string EmuRomPath { get; set; }
149 #endregion
150 }
151 #endregion
152
153 }
154 }

  ViewVC Help
Powered by ViewVC 1.1.22