-
Notifications
You must be signed in to change notification settings - Fork 9
/
Utils.cs
201 lines (189 loc) · 7.04 KB
/
Utils.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
namespace TempAR
{
public static class Utils
{
/// <summary>
/// Convert DEC to HEX
/// 十六进制地址转10进制数值
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static uint ParseNum(string s)
{
return ParseNum(s, NumberStyles.None);
}
/// <summary>
/// Convert DEC to HEX
/// 十六进制地址转10进制数值
/// </summary>
/// <param name="s"></param>
/// <param name="numstyle"></param>
/// <returns></returns>
public static uint ParseNum(string s, NumberStyles numstyle,
string message = "Unable to parse, please make sure the value is a valid hexadecimal number.",
string title = "Unable to parse: Wrong Format!")
{
try
{
if (s.Trim().Length == 0)
return 0;
if (s.StartsWith("0x"))
return uint.Parse(s.Remove(0, 2), NumberStyles.AllowHexSpecifier);
return uint.Parse(s, numstyle);
}
catch (Exception)
{
MessageBox.Show(message, title);
return 0;
}
}
public static void ParseGroupNums(GroupBox groupBox)
{
foreach (Control x in groupBox.Controls)
{
if (x is TextBox box && x.Enabled)
{
Utils.ParseNum(box.Text, NumberStyles.AllowHexSpecifier, "Unable to Parse an Offset, make sure value is a valid hexadecimal number.");
}
}
}
/// <summary>
/// Open Directory
/// 打开目录
/// </summary>
/// <param name="defaultdir"></param>
/// <param name="description"></param>
/// <returns></returns>
public static string OpenDirectory(string defaultdir, string description)
{
using (var folderBrowserDialog = new FolderBrowserDialog
{
SelectedPath = defaultdir.Length > 0 ? defaultdir : Directory.GetCurrentDirectory(),
Description = description,
ShowNewFolderButton = false
})
{
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
return folderBrowserDialog.SelectedPath;
return defaultdir;
}
}
/// <summary>
/// Open a File
/// 打开文件
/// </summary>
/// <param name="defaultfile"></param>
/// <param name="filter"></param>
/// <param name="title"></param>
/// <returns></returns>
public static string OpenFile(string defaultfile, string filter = null, string title = "Open file")
{
using (var openFileDialog = new OpenFileDialog
{
FileName = defaultfile.Length > 0 ? defaultfile : null,
InitialDirectory = defaultfile.Length > 0 ? defaultfile : Directory.GetCurrentDirectory(),
Filter = filter,
Title = title
})
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
return openFileDialog.FileName;
return defaultfile;
}
}
/// <summary>
/// Save File
/// 保存文件
/// </summary>
/// <param name="defaultfile"></param>
/// <param name="filter"></param>
/// <param name="title"></param>
/// <returns></returns>
public static string SaveFile(string defaultfile, string filter = null, string title = "Save File")
{
using (var saveFileDialog = new SaveFileDialog
{
FileName = defaultfile.Length > 0 ? defaultfile : null,
InitialDirectory = defaultfile.Length > 0 ? defaultfile : Directory.GetCurrentDirectory(),
Filter = filter,
Title = title
})
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
return saveFileDialog.FileName;
return defaultfile;
}
}
/// <summary>
/// Data Sorting
/// 数据排序
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataSource"></param>
/// <param name="fieldName"></param>
/// <param name="asc"></param>
public static void SortList<T>(List<T> dataSource, string fieldName, bool asc)
{
var propInfo = typeof(T).GetProperty(fieldName);
int comparison(T a, T b)
{
var obj1 = asc ? propInfo.GetValue(a, null) : propInfo.GetValue(b, null);
var obj2 = asc ? propInfo.GetValue(b, null) : propInfo.GetValue(a, null);
if (!(obj1 is IComparable)) return 0;
return ((IComparable)obj1).CompareTo(obj2);
}
dataSource.Sort(comparison);
}
///
/// Count lines for Conditionals and ButtonPad
///
public static int RelatedLines(int PtrLvl, string CodeType, uint ButtonType, int ConCheck)
{
var Lines = 0;
if (ConCheck == 1)
{
if (ButtonType == 9) { ButtonType = 1; } // If ButtonPad is set to 'None', still count lines as if it wasn't
else { Lines = 1; }
}
switch (ButtonType)
{
case 9:
return 0;
default:
switch (CodeType)
{
case "Compress ($4...)":
Lines += 2;
return Lines;
case "Pointer Write ($3...)":
Lines += PtrLvl + 2;
return Lines;
case "Pointer MOV ($8...)":
Lines += (2 * (PtrLvl + 1)) + 2;
return Lines;
case "Pointer Compress ($7...)":
Lines += PtrLvl + 3;
return Lines;
default:
return Lines + 1;
}
}
}
public static bool CheckInsideSegments(uint targetAddress, string segStart, string segRange)
{
uint start = ParseNum(segStart, NumberStyles.AllowHexSpecifier);
uint end = ParseNum(segRange, NumberStyles.AllowHexSpecifier) + start;
return targetAddress >= start && targetAddress <= end;
}
public static uint OffsetFromSegment(uint targetAddress, string segStart)
{
uint start = ParseNum(segStart, NumberStyles.AllowHexSpecifier);
return targetAddress - start;
}
}
}