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