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 Enterprise.Logging; |
8 |
|
9 |
namespace EmuXPortal.Api |
10 |
{ |
11 |
public interface IRomConfig : IDisposable, IComparable<IRomConfig> |
12 |
{ |
13 |
string RomFile { get; } |
14 |
string RomTitle { get; } |
15 |
Image RomImage { get; } |
16 |
IEmuConfig Config { get; } |
17 |
void ReleaseRomImageResource(); |
18 |
|
19 |
string ToString(); |
20 |
} |
21 |
|
22 |
public static class RomLoader |
23 |
{ |
24 |
private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration |
25 |
public static IRomConfig Load(string rom_file, IEmuConfig EMUConfig) |
26 |
{ |
27 |
IRomConfig config = null; |
28 |
config = new RomConfig(rom_file, EMUConfig); |
29 |
return config; |
30 |
} |
31 |
#region private class RomConfig : IRomConfig |
32 |
private class RomConfig : IRomConfig, IDisposable |
33 |
{ |
34 |
public RomConfig(string rom_file, IEmuConfig EMUConfig) |
35 |
{ |
36 |
FileInfo fi = new FileInfo(rom_file); |
37 |
this.RomFile =fi.FullName; |
38 |
this.RomTitle = EMUConfig.GameTitle == "" ? fi.Name : EMUConfig.GameTitle; |
39 |
string rom_img = ""; |
40 |
// load image |
41 |
try |
42 |
{ |
43 |
if (File.Exists(string.Format("{0}.jpg", fi.FullName.Replace(fi.Extension, "")))) { rom_img = string.Format("{0}.jpg", fi.FullName.Replace(fi.Extension, "")); } |
44 |
} |
45 |
catch (Exception ex) |
46 |
{ |
47 |
gLog.Debug.WriteLine("Error Loading an Image file for: {1}{0}Error:{0}{2}", System.Environment.NewLine, fi.FullName, ex.ToString()); |
48 |
} |
49 |
try |
50 |
{ |
51 |
if (rom_img != string.Empty) |
52 |
{ |
53 |
try |
54 |
{ |
55 |
this.RomImage = Image.FromFile(rom_img); |
56 |
} |
57 |
catch (Exception ex) |
58 |
{ |
59 |
throw ex; |
60 |
} |
61 |
} |
62 |
else |
63 |
{ |
64 |
this.RomImage = null; |
65 |
} |
66 |
} |
67 |
catch (Exception ex) |
68 |
{ |
69 |
gLog.Debug.WriteLine("Error loading rom image: {1}{0}Error:{0}{2}", System.Environment.NewLine, (rom_img == "") ? "DefaultGameImage" : rom_img, ex.ToString()); |
70 |
this.RomImage = null; |
71 |
} |
72 |
|
73 |
|
74 |
if (EMUConfig.GameImage != string.Empty) |
75 |
{ |
76 |
FileInfo fi_gameimage = new FileInfo(EMUConfig.GameImage); |
77 |
if (fi_gameimage.Exists) { this.RomImage = Image.FromFile(fi_gameimage.FullName); } |
78 |
else |
79 |
{ |
80 |
FileInfo t = new FileInfo(EMUConfig.ConfigPath); |
81 |
string path = t.FullName.Replace(t.Name, "").Replace(t.Extension, ""); |
82 |
string image_path = string.Format(@"{0}{1}", path, fi_gameimage.Name); |
83 |
FileInfo x = new FileInfo(image_path); |
84 |
if (x.Exists) { this.RomImage = Image.FromFile(x.FullName); } |
85 |
else |
86 |
{ |
87 |
if (EMUConfig.GameExe != string.Empty) |
88 |
{ |
89 |
FileInfo fi_gamexe = new FileInfo(EMUConfig.GameExe); |
90 |
path = fi_gamexe.FullName.Replace(fi_gamexe.Name, "").Replace(fi_gamexe.Extension, ""); |
91 |
image_path = string.Format(@"{0}{1}", path, fi_gameimage.Name); |
92 |
x = new FileInfo(image_path); |
93 |
if (x.Exists) { this.RomImage = Image.FromFile(x.FullName); } |
94 |
} |
95 |
} |
96 |
} |
97 |
} |
98 |
|
99 |
|
100 |
//string config_dir = fi.Directory.Parent.FullName; |
101 |
//Config = EmuConfigLoader.Load(config_dir); |
102 |
Config = EMUConfig; |
103 |
} |
104 |
#region IRomConfig Members |
105 |
public string RomFile { get; private set; } |
106 |
public string RomTitle { get; private set; } |
107 |
public Image RomImage { get; private set; } |
108 |
public IEmuConfig Config { get; private set; } |
109 |
public void ReleaseRomImageResource() |
110 |
{ |
111 |
if (this.RomImage != null) |
112 |
this.RomImage.Dispose(); |
113 |
} |
114 |
|
115 |
public override string ToString() |
116 |
{ |
117 |
return string.Format("{2} - {0}{3}{1}", "{", "}", Config.PlatformNameShort, RomFile); |
118 |
} |
119 |
#endregion |
120 |
|
121 |
public void Dispose() |
122 |
{ |
123 |
if (this.RomImage != null) |
124 |
this.RomImage.Dispose(); |
125 |
} |
126 |
|
127 |
public int CompareTo(IRomConfig other) |
128 |
{ |
129 |
return this.RomFile.CompareTo(other.RomFile); |
130 |
} |
131 |
} |
132 |
#endregion |
133 |
} |
134 |
|
135 |
|
136 |
} |