This repository has been archived by the owner on May 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
261 lines (214 loc) · 9.07 KB
/
MainWindow.xaml.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using MapleLib.WzLib;
using MapleRIL.Structure;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
namespace MapleRIL
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public bool LookupReversed = false;
public string SourceWzPath;
public string TargetWzPath;
public string SourceRegion;
public string TargetRegion;
public Dictionary<string, WzFile> SourceWzs;
public Dictionary<string, WzFile> TargetWzs;
public string[] RequiredWzs = new string[] { "String.wz", "Item.wz", "Character.wz" };
public List<WzItemType> ItemTypes;
public ObservableCollection<SearchedItem> SearchResults = new ObservableCollection<SearchedItem>();
public Version AppVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
public string FriendlyAppVersion => $"v{AppVersion.Major}.{AppVersion.Minor}.{AppVersion.Build}";
public MainWindow()
{
InitializeComponent();
checkForSetup();
SourceRegion = Properties.Settings.Default.sourceRegion;
TargetRegion = Properties.Settings.Default.targetRegion;
loadPaths();
try
{
loadWzs();
}
catch (IOException e)
{
MessageBox.Show($"An error occured whilst loading required WZ files -- this can happen as a file is in use somewhere else, such as MapleStory is still using the file. MapleRIL must now close, please try again later.\n\nAdditional information: {e.Message}", "MapleRIL - Error loading required files", MessageBoxButton.OK, MessageBoxImage.Error);
Environment.Exit(1);
return;
}
loadFilters();
dataGrid.ItemsSource = SearchResults;
aboutLabel.Content = $"{FriendlyAppVersion} ~ Click for about info";
}
private void checkForSetup()
{
// Checks if old folders are still OK or if we need a new setup
if (String.IsNullOrWhiteSpace(Properties.Settings.Default.sourceFolder))
(new Setup()).ShowDialog();
if (!File.Exists(Path.Combine(Properties.Settings.Default.sourceFolder, "String.wz"))
|| !File.Exists(Path.Combine(Properties.Settings.Default.targetFolder, "String.wz")))
{
MessageBox.Show("The MapleStory folder(s) seem to not exist anymore. Relaunching setup for you to reconfigure.", "MapleRIL - Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
Properties.Settings.Default.sourceFolder = "";
Properties.Settings.Default.Save();
(new Setup()).ShowDialog();
}
}
private void loadPaths()
{
SourceWzPath = Properties.Settings.Default.sourceFolder;
TargetWzPath = Properties.Settings.Default.targetFolder;
}
private void loadWzs()
{
SourceWzs = new Dictionary<string, WzFile>();
TargetWzs = new Dictionary<string, WzFile>();
foreach (string w in RequiredWzs)
{
// gms, kms, msea all use classic encryption now
WzFile source = new WzFile(Path.Combine(SourceWzPath, w), WzMapleVersion.CLASSIC);
source.ParseWzFile();
SourceWzs.Add(w, source);
WzFile target = new WzFile(Path.Combine(TargetWzPath, w), WzMapleVersion.CLASSIC);
target.ParseWzFile();
TargetWzs.Add(w, target);
}
}
private void loadFilters()
{
ItemTypes = new List<WzItemType> // default Item.wz types
{
new ItemWzItemType("Consume"),
new ItemWzItemType("Etc"),
new PetItemWzItemType(),
new ItemWzItemType("Cash"),
new SetupItemWzItemType()
};
IEnumerable<string> equipProps = SourceWzs["String.wz"].WzDirectory.GetImageByName("Eqp.img")["Eqp"].WzProperties.Select(w => w.Name);
foreach (string e in equipProps)
ItemTypes.Add(new EquipWzItemType(e));
filterBox.Items.Clear();
filterBox.Items.Add("All");
foreach (WzItemType e in ItemTypes)
filterBox.Items.Add(e);
filterBox.Text = "All";
}
private WzFile loadWzFile(string path, WzMapleVersion version)
{
try
{
WzFile w = new WzFile(path, version);
w.ParseWzFile();
return w;
}
catch (IOException e)
{
MessageBox.Show($"An error occured whilst loading WZ file: {path} -- this can happen as the file is in use somewhere else, such as MapleStory is still using the file. MapleRIL must now close, please try again later.\n\nAdditional information: {e.Message}", "MapleRIL - Error loading required files", MessageBoxButton.OK, MessageBoxImage.Error);
Environment.Exit(1);
return null;
}
}
private void searchButton_Click(object sender, RoutedEventArgs e)
{
if (String.IsNullOrWhiteSpace(searchBox.Text))
return;
SearchResults.Clear();
if (filterBox.Text != "All")
{
searchInType((WzItemType)filterBox.SelectedItem);
}
else
{
foreach (WzItemType p in ItemTypes)
searchInType(p);
}
}
private void searchInType(WzItemType type)
{
List<WzImageProperty> searchProperties = type.GetAllStringIdProperties(SourceWzs); // these are the properties we will be looping for the item names
// look up a property in the image, eg 2000000 where inside the property "name"'s string is what the user is looking for.
// loose search so use regexes
Regex r = new Regex("(^| )" + searchBox.Text + "($| |')", RegexOptions.IgnoreCase);
IEnumerable<WzImageProperty> props = searchProperties.Where(w => {
var nameProp = w.WzProperties.Where(p => p.Name == "name");
if (nameProp.Count() < 1)
return false;
return r.IsMatch(nameProp.First().GetString());
});
foreach (SearchedItem i in props.Select(p => new SearchedItem(p, type)))
SearchResults.Add(i);
}
// // https://stackoverflow.com/questions/3120616/wpf-datagrid-selected-row-clicked-event sol #2
private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DataGridRow row = ItemsControl.ContainerFromElement((DataGrid)sender, e.OriginalSource as DependencyObject) as DataGridRow;
if (row == null)
return;
SearchedItem si = row.Item as SearchedItem;
if (si == null)
return;
(new Comparison(this, si)).ShowDialog();
}
private void swapDirectionBox_Toggled(object sender, RoutedEventArgs e)
{
reverseLookup();
}
private void reverseLookup()
{
// switcharoo
swap(ref SourceRegion, ref TargetRegion);
swap(ref SourceWzPath, ref TargetWzPath);
swap(ref SourceWzs, ref TargetWzs);
// reload
//searchBox.Text = "";
SearchResults.Clear();
loadFilters();
regionDirectionLabel.Content = SourceRegion + " -> " + TargetRegion;
searchBox.Focus();
LookupReversed = !LookupReversed;
}
private void swap<T>(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
searchBox.Focus();
}
private void filterBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (filterBox.Text == "All")
warningLabel.Content = "Note: when using All, searches may be slower. Filters are strongly recommended.";
else
warningLabel.Content = "";
}
private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (LookupReversed)
reverseLookup();
(new Setup()).ShowDialog();
}
private void aboutLabel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
(new About(this)).ShowDialog();
}
}
}