-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
223 lines (192 loc) · 8.15 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Retro
{
public partial class Form1 : Form
{
// Owned by the entire system for caching purposes.
List<FileInfo> masterFilesList;
Dictionary<string, Game> masterGamesList;
Dictionary<string, Machine> machineList = new Dictionary<string, Machine>();
Dictionary<string, int> regionRanking = new Dictionary<string, int>();
public Form1()
{
InitializeComponent();
this.Visible = true;
masterFilesList = new List<FileInfo>();
masterGamesList = new Dictionary<string, Game>();
regionRanking = new RegionRanking().RegionDictionary;
MachineDb machineDb = new MachineDb();
machineList = machineDb.GenerateMachineList();
masterGamesList = ReadRomsFolders();
PopulateListView();
}
public void status(string status)
{
label1.Text = status;
}
public Dictionary<string, Game> ReadRomsFolders()
{
status("Reading Files");
Dictionary<string, Game> result = new Dictionary<string, Game>();
if (masterFilesList.Count == 0)
{
string path = Application.StartupPath + "\\roms";
foreach (string directory in Directory.GetDirectories(path))
{
DirectoryInfo dir = new DirectoryInfo(directory);
masterFilesList.AddRange(dir.GetFiles("*.*"));
}
}
if (masterGamesList.Count == 0)
{
foreach (FileInfo file in masterFilesList)
{
Game game = new Game();
game.Path = file.DirectoryName + "\\" + file.Name;
string[] fileSections = file.Name.Split('.');
StringBuilder displayName = new StringBuilder();
for (int i = 0; i < fileSections.Count() - 1; i++)
{
displayName.Append(fileSections[i]);
}
game.DisplayName = displayName.ToString().Split('(')[0];
game.Extension = fileSections[fileSections.Count() - 1];
game.Region = GetRegionString(file);
string machineName = GetMachine(game).DisplayName;
game.UniqueID = game.DisplayName + machineName;
if (game.Extension != "sav" && machineName != "Unknown")
{
Game existingGame;
if (!result.TryGetValue(game.DisplayName, out existingGame) || GetRegionInt(game.Region) < GetRegionInt(existingGame.Region))
{
result[game.UniqueID] = game;
}
}
}
}
status("Files Read : " + result.Count());
return result;
}
/// <summary>
/// Attempts to get the region for the game. It assumes that region info is in () in the file name
/// It looks at all such occurances and assumes the one that has the best score is the one to go with.
/// </summary>
/// <param name="file"></param>
/// <returns>The region we've gone with for the game</returns>
private string GetRegionString(FileInfo file)
{
string result = "ERR";
string[] regionList = file.Name.Split('(');
if (regionList.Length > 1)
{
int regionScore = 10000;
for (int i = 1; i < regionList.Count(); i++)
{
string region = file.Name.Split('(')[i];
int newRegionScore;
region = region.Split(')')[0];
newRegionScore = GetRegionInt(region);
if (newRegionScore < regionScore)
{
result = region;
regionScore = newRegionScore;
}
}
}
return result;
}
/// <summary>Gets the "score" for the region of this file. Retro uses this to determine the best version of the game to show.</summary>
/// <param name="regionString"></param>
/// <returns>The "score" (lowest will win) of the particular region of the file.</returns>
private int GetRegionInt(string regionString)
{
int result;
if (regionRanking.TryGetValue(regionString, out result))
{
return result;
}
else
{
return 999;
}
}
public void PopulateListView()
{
status("Refreshing games list...");
string searchString = txtSearch.Text.ToUpper();
bool searching = !String.IsNullOrEmpty(txtSearch.Text);
// Skip the whole damn thing if we didn't search last time and aren't now.
if (searching || listView1.Items.Count != masterGamesList.Count)
{
listView1.Items.Clear();
List<ListViewItem> items = new List<ListViewItem>();
foreach (Game file in masterGamesList.Values)
{
ListViewItem item = new ListViewItem();
item.Text = file.DisplayName;
// Here's where the narrowing down happens, we add it to the list if we're not searching or if there's a case insensitive match for the search string
// This is where you'd refactor out less dumb searching.
if (!searching || item.Text.ToUpper().Contains(searchString))
{
Machine machine = GetMachine(file);
string machineDisplayName = machine.DisplayName;
item.SubItems.Add(machineDisplayName);
item.ForeColor = machine.Colour;
item.Tag = file;
items.Add(item);
}
}
listView1.Items.AddRange(items.ToArray());
}
status("Done : " + listView1.Items.Count + " shown of " + masterGamesList.Count());
}
private Machine GetMachine(Game file)
{
Machine machine;
if (!machineList.TryGetValue(file.Extension, out machine))
{
machine = machineList["unknown"];
}
return machine;
}
public void Search()
{
using (Cursors.WaitCursor)
{
PopulateListView();
}
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
Game gameTorun = (Game)listView1.SelectedItems[0].Tag;
status("Launching : " + gameTorun.DisplayName);
// An asumption is made here that the emulators are all under /emu but given we're portable
// and it'd be really ugly if you didn't, you will.
string emuPath = Application.StartupPath + "\\emu\\" + GetMachine(gameTorun).EmuLocation;
string romname = gameTorun.Path;
// The assumption here is that extra parmas can go before the gam's file name.
System.Diagnostics.Process.Start(emuPath, GetMachine(gameTorun).ExtraParams + " \""+romname);
status("Ready.");
}
private void btnSearch_Click(object sender, EventArgs e)
{
Search();
}
private void txtSearch_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
}
}
}
}