1 |
#define FORCE_ALL_LOGGING_FLAGS // when defined will force logging flags to ALL |
2 |
#define SHOW_DEBUG_MENU // when defined will show the debug menu or else it will be hidden |
3 |
using System; |
4 |
using System.Collections.Generic; |
5 |
using System.ComponentModel; |
6 |
using System.Data; |
7 |
using System.Drawing; |
8 |
using System.Linq; |
9 |
using System.Text; |
10 |
using System.Windows.Forms; |
11 |
using RomCheater.Logging; |
12 |
using RomCheater.Properties; |
13 |
using RomCheater.UserSettingsSupport; |
14 |
using RomCheater.PluginFramework.Core; |
15 |
using System.Diagnostics; |
16 |
using RomCheater.PluginFramework.Interfaces; |
17 |
using WeifenLuo.WinFormsUI.Docking; |
18 |
using RomCheater.Docking; |
19 |
using System.IO; |
20 |
using Sojaner.MemoryScanner; |
21 |
using RomCheater.PluginFramework.Events; |
22 |
|
23 |
namespace RomCheater |
24 |
{ |
25 |
public partial class Main : Form |
26 |
{ |
27 |
private bool m_bSaveLayout = true; |
28 |
private Process SelectedProcess = null; |
29 |
private DeserializeDockContent m_deserializeDockContent; |
30 |
private FloatingLogWindow m_LogWindow = new FloatingLogWindow(); |
31 |
private FloatingAboutBox m_AboutBox = new FloatingAboutBox(); |
32 |
private FloatingRamDumperDialog m_RamDump = new FloatingRamDumperDialog(); |
33 |
private PIDSelector m_PIDSelector = new PIDSelector(); |
34 |
private FloatingMemoryView m_memoryview = new FloatingMemoryView(); |
35 |
private FloatingDataTypeConverter m_typeconverter = new FloatingDataTypeConverter(); |
36 |
private FloatingMemorySearcher m_memsearcher = new FloatingMemorySearcher(); |
37 |
//private bool log_window_expanded = false; |
38 |
//private double log_window_splitter_default_position = 1.4045; |
39 |
PluginLoader loader = null; |
40 |
IConfigPlugin ConfigPlugin = null; |
41 |
IInputPlugin InputPlugin = null; |
42 |
IWindowPlugin WindowPlugin = null; |
43 |
static Main() { SettingSubscriber.AddSubscriber(new Main(), Settings.Default); } |
44 |
private const string t = "RomCheater"; |
45 |
#region LogWriterSupport |
46 |
static LogWriter _LoggerInstance; |
47 |
static LogWriter LoggerInstance |
48 |
{ |
49 |
get { return _LoggerInstance; } |
50 |
set { _LoggerInstance = value; } |
51 |
} |
52 |
#endregion |
53 |
|
54 |
|
55 |
private void OnProcessChanged(ProcessChangedEventArgs e) |
56 |
{ |
57 |
if (m_memsearcher.SearchInProgess) |
58 |
{ |
59 |
DialogResult result = MessageBox.Show("Do you want to start a new search with the selected process?", "A memory search is currently in progress", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3); |
60 |
if (result != DialogResult.Yes || result != DialogResult.OK) { return; } |
61 |
m_memsearcher = null; //free memory used by the memory searcher |
62 |
m_memsearcher = new FloatingMemorySearcher(); |
63 |
} |
64 |
SelectedProcess = Process.GetProcessById(e.ProcessID); |
65 |
m_RamDump.AcceptedPlugin = this.ConfigPlugin; |
66 |
m_RamDump.AcceptedProcess = SelectedProcess; |
67 |
|
68 |
m_memoryview.AcceptedPlugin = this.ConfigPlugin; |
69 |
m_memoryview.AcceptedProcess = SelectedProcess; |
70 |
|
71 |
m_memsearcher.AcceptedPlugin = this.ConfigPlugin; |
72 |
m_memsearcher.AcceptedProcess = SelectedProcess; |
73 |
m_memsearcher.OnBrowseMemoryRegion += new BaseEventHandler<BrowseMemoryRegionEvent>(OnBrowseMemoryRegion); |
74 |
} |
75 |
private void OnBrowseMemoryRegion(BrowseMemoryRegionEvent e) |
76 |
{ |
77 |
m_memoryview.BrowseMemoryRegion(e.MemoryRegion); |
78 |
} |
79 |
|
80 |
#region Dock Support |
81 |
void AddDockToWindowList(object sender, EventArgs e) |
82 |
{ |
83 |
DockContent dc; |
84 |
ObjectBinder.Bind(sender, out dc); |
85 |
} |
86 |
void RemoveDockFromWindowList(object sender, FormClosedEventArgs e) |
87 |
{ |
88 |
DockContent dc; |
89 |
ObjectBinder.Bind(sender, out dc); |
90 |
} |
91 |
private IDockContent GetContentFromPersistString(string persistString) |
92 |
{ |
93 |
if (persistString == typeof(FloatingLogWindow).ToString()) { return m_LogWindow; } |
94 |
if (persistString == typeof(FloatingRamDumperDialog).ToString()) { return m_RamDump; } |
95 |
if (persistString == typeof(PIDSelector).ToString()) { return m_PIDSelector; } |
96 |
if (persistString == typeof(FloatingMemoryView).ToString()) { return m_memoryview; } |
97 |
if (persistString == typeof(FloatingDataTypeConverter).ToString()) { return m_typeconverter; } |
98 |
if (persistString == typeof(FloatingMemorySearcher).ToString()) { return m_memsearcher; } |
99 |
else { return null; } |
100 |
} |
101 |
public void SetupDocks() |
102 |
{ |
103 |
m_LogWindow = new FloatingLogWindow(); |
104 |
m_AboutBox = new FloatingAboutBox(); |
105 |
m_RamDump = new FloatingRamDumperDialog(); |
106 |
m_PIDSelector = new PIDSelector(); |
107 |
m_PIDSelector.OnSelectedProcessChanged += new BaseEventHandler<ProcessChangedEventArgs>(OnProcessChanged); |
108 |
m_memoryview = new FloatingMemoryView(); |
109 |
m_typeconverter = new FloatingDataTypeConverter(); |
110 |
m_memsearcher = new FloatingMemorySearcher(); |
111 |
m_memsearcher.OnBrowseMemoryRegion += new BaseEventHandler<BrowseMemoryRegionEvent>(OnBrowseMemoryRegion); |
112 |
m_deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString); |
113 |
} |
114 |
#region SetupDockWindowHandler support |
115 |
public void SetupDockWindowHandler() |
116 |
{ |
117 |
SetupLogWindowHandler(); |
118 |
SetupRamDumpWindowHandler(); |
119 |
SetupMemoryViewWindowHandler(); |
120 |
SetupPIDSelectorWindowHandler(); |
121 |
SetupDataTypeConverterWindowHandler(); |
122 |
SetupMemorySearchWindowHandler(); |
123 |
} |
124 |
private void SetupLogWindowHandler() |
125 |
{ |
126 |
if (m_LogWindow == null) return; |
127 |
m_LogWindow.Shown += new EventHandler(AddDockToWindowList); |
128 |
m_LogWindow.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList); |
129 |
} |
130 |
private void SetupRamDumpWindowHandler() |
131 |
{ |
132 |
if (m_RamDump == null) return; |
133 |
m_RamDump.Shown += new EventHandler(AddDockToWindowList); |
134 |
m_RamDump.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList); |
135 |
} |
136 |
private void SetupMemoryViewWindowHandler() |
137 |
{ |
138 |
if (m_memoryview == null) return; |
139 |
m_memoryview.Shown += new EventHandler(AddDockToWindowList); |
140 |
m_memoryview.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList); |
141 |
} |
142 |
private void SetupPIDSelectorWindowHandler() |
143 |
{ |
144 |
if (m_PIDSelector == null) return; |
145 |
m_PIDSelector.Shown += new EventHandler(AddDockToWindowList); |
146 |
m_PIDSelector.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList); |
147 |
} |
148 |
private void SetupDataTypeConverterWindowHandler() |
149 |
{ |
150 |
if (m_typeconverter == null) return; |
151 |
m_typeconverter.Shown += new EventHandler(AddDockToWindowList); |
152 |
m_typeconverter.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList); |
153 |
} |
154 |
private void SetupMemorySearchWindowHandler() |
155 |
{ |
156 |
if (m_memsearcher == null) return; |
157 |
m_memsearcher.Shown += new EventHandler(AddDockToWindowList); |
158 |
m_memsearcher.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList); |
159 |
} |
160 |
#endregion |
161 |
public void ShowDocks() |
162 |
{ |
163 |
ShowLogWindow(); |
164 |
SetupLogWindowHandler(); |
165 |
//ShowAboutBox(); |
166 |
ShowRamDump(); |
167 |
SetupRamDumpWindowHandler(); |
168 |
ShowMemoryView(); |
169 |
SetupMemoryViewWindowHandler(); |
170 |
ShowPidSelector(); |
171 |
SetupPIDSelectorWindowHandler(); |
172 |
ShowDataTypeConverter(); |
173 |
SetupDataTypeConverterWindowHandler(); |
174 |
ShowMemorySearch(); |
175 |
SetupMemorySearchWindowHandler(); |
176 |
} |
177 |
public void ShowLogWindow() |
178 |
{ |
179 |
if (m_LogWindow == null || m_LogWindow.IsDisposed) { m_LogWindow = new FloatingLogWindow(); } |
180 |
LoggerInstance = m_LogWindow.Logwriter; |
181 |
LoggerInstance.CreateNewLog(false); |
182 |
m_LogWindow.Show(dockPanel, DockState.DockBottom); |
183 |
} |
184 |
public void ShowAboutBox() |
185 |
{ |
186 |
if (m_AboutBox == null || m_AboutBox.IsDisposed) { m_AboutBox = new FloatingAboutBox(); } |
187 |
m_AboutBox.ShowDialog(); |
188 |
} |
189 |
public void ShowRamDump() |
190 |
{ |
191 |
load_plugins(); |
192 |
m_RamDump = new FloatingRamDumperDialog(ConfigPlugin); |
193 |
m_RamDump.AcceptedProcess = SelectedProcess; |
194 |
m_RamDump.Show(dockPanel); |
195 |
} |
196 |
public void ShowMemoryView() |
197 |
{ |
198 |
load_plugins(); |
199 |
m_memoryview = new FloatingMemoryView(ConfigPlugin); |
200 |
m_memoryview.AcceptedProcess = SelectedProcess; |
201 |
m_memoryview.Show(dockPanel); |
202 |
} |
203 |
public void ShowPidSelector() |
204 |
{ |
205 |
load_plugins(); |
206 |
//List<Process> procs = ConfigPlugin.ValidProcessesForPlugin; |
207 |
m_PIDSelector = new PIDSelector(ConfigPlugin); |
208 |
m_PIDSelector.OnSelectedProcessChanged += new BaseEventHandler<ProcessChangedEventArgs>(OnProcessChanged); |
209 |
m_PIDSelector.Show(dockPanel); |
210 |
} |
211 |
public void ShowDataTypeConverter() |
212 |
{ |
213 |
if (m_typeconverter == null || m_typeconverter.IsDisposed) { m_typeconverter = new FloatingDataTypeConverter(); } |
214 |
m_typeconverter.Show(dockPanel, DockState.DockRightAutoHide); |
215 |
} |
216 |
public void ShowMemorySearch() |
217 |
{ |
218 |
load_plugins(); |
219 |
m_memsearcher = new FloatingMemorySearcher(ConfigPlugin); |
220 |
m_memsearcher.AcceptedProcess = SelectedProcess; |
221 |
m_memsearcher.OnBrowseMemoryRegion += new BaseEventHandler<BrowseMemoryRegionEvent>(OnBrowseMemoryRegion); |
222 |
m_memsearcher.Show(dockPanel); |
223 |
} |
224 |
|
225 |
|
226 |
#endregion |
227 |
|
228 |
|
229 |
public Main() : this(false) { } |
230 |
public Main(bool no_console_redirect) |
231 |
{ |
232 |
InitializeComponent(); |
233 |
#if SHOW_DEBUG_MENU |
234 |
mnuDebug.Visible = true; |
235 |
#else |
236 |
mnuDebug.Visible = false; |
237 |
#endif |
238 |
load_loggerflags(); |
239 |
SetupDocks(); |
240 |
LoggerInstance = m_LogWindow.Logwriter; |
241 |
LoggerInstance.CreateNewLog(false); |
242 |
logger.ForceLog.Info.WriteLine("LoggingFlags = 0x{0:x4} ({1})", logger.GetLoggingFlags().Value, logger.GetLoggingFlags().Name); |
243 |
load_plugins(); |
244 |
if (no_console_redirect) |
245 |
LoggerInstance.RedirectConsoleOutput = false; |
246 |
|
247 |
|
248 |
|
249 |
} |
250 |
private void load_loggerflags() |
251 |
{ |
252 |
logger.SetLoggingFlags(Logging.Properties.Settings.Default.LoggingFlags); |
253 |
#if FORCE_ALL_LOGGING_FLAGS |
254 |
logger.SetLoggingFlags(loggerflags.ALL); |
255 |
#endif |
256 |
} |
257 |
private void load_plugins() |
258 |
{ |
259 |
loader = new PluginLoader(); |
260 |
loader.LoadPlugins(); |
261 |
|
262 |
ConfigPlugin = loader.GetConfigPlugin(RomCheater.Properties.Settings.Default.LastConfigPlugin); |
263 |
if (ConfigPlugin != null) |
264 |
logger.Info.WriteLine("Loaded Config Plugin: {0}", ConfigPlugin.ToString()); |
265 |
InputPlugin = loader.GetInputPlugin(RomCheater.Properties.Settings.Default.LastInputPlugin); |
266 |
if (InputPlugin != null) |
267 |
logger.Info.WriteLine("Loaded Input Plugin: {0}", InputPlugin.ToString()); |
268 |
WindowPlugin = loader.GetWindowPlugin(RomCheater.Properties.Settings.Default.LastWindowPlugin); |
269 |
if (WindowPlugin != null) |
270 |
logger.Info.WriteLine("Loaded Window Plugin: {0}", WindowPlugin.ToString()); |
271 |
|
272 |
m_PIDSelector.AcceptedPlugin = ConfigPlugin; |
273 |
m_RamDump.AcceptedPlugin = ConfigPlugin; |
274 |
m_memoryview.AcceptedPlugin = ConfigPlugin; |
275 |
m_memsearcher.AcceptedPlugin = ConfigPlugin; |
276 |
|
277 |
if (this.SelectedProcess != null) |
278 |
{ |
279 |
m_RamDump.AcceptedProcess = SelectedProcess; |
280 |
m_memoryview.AcceptedProcess = SelectedProcess; |
281 |
m_memsearcher.AcceptedProcess = SelectedProcess; |
282 |
} |
283 |
|
284 |
} |
285 |
|
286 |
private void mnuItemExit_Click(object sender, EventArgs e) |
287 |
{ |
288 |
this.Close(); |
289 |
} |
290 |
|
291 |
|
292 |
private void Main_Load(object sender, EventArgs e) |
293 |
{ |
294 |
|
295 |
} |
296 |
|
297 |
private void mnuItemConfig_Click(object sender, EventArgs e) |
298 |
{ |
299 |
RomCheaterConfigDialog dlg = new RomCheaterConfigDialog(loader); |
300 |
dlg.ShowDialog(); |
301 |
logger.ForceLog.Info.WriteLine("LoggingFlags = 0x{0:x4} ({1})", logger.GetLoggingFlags().Value, logger.GetLoggingFlags().Name); |
302 |
// reload plugins |
303 |
load_plugins(); |
304 |
|
305 |
} |
306 |
|
307 |
private void mnuItemOpenProcess_Click(object sender, EventArgs e) |
308 |
{ |
309 |
////List<Process> procs = ConfigPlugin.ValidProcessesForPlugin; |
310 |
//PIDSelector selector = new PIDSelector(ConfigPlugin); |
311 |
//selector.ShowDialog(); |
312 |
} |
313 |
|
314 |
private void Main_Shown(object sender, EventArgs e) |
315 |
{ |
316 |
//dockPanel.SuspendLayout(true); |
317 |
//ShowDocks(); |
318 |
string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config"); |
319 |
if (File.Exists(configFile)) |
320 |
{ |
321 |
try |
322 |
{ |
323 |
dockPanel.LoadFromXml(configFile, m_deserializeDockContent); |
324 |
SetupDockWindowHandler(); |
325 |
} |
326 |
catch (Exception) |
327 |
{ |
328 |
this.Controls.Remove(dockPanel); |
329 |
dockPanel = new DockPanel(); |
330 |
dockPanel.Dock = DockStyle.Fill; |
331 |
dockPanel.DocumentStyle = DocumentStyle.DockingWindow; |
332 |
this.Controls.Add(dockPanel); |
333 |
ShowDocks(); |
334 |
} |
335 |
} |
336 |
else |
337 |
{ |
338 |
ShowDocks(); |
339 |
} |
340 |
|
341 |
//dockPanel.ResumeLayout(true, true); |
342 |
} |
343 |
|
344 |
private void mnuItemShowLogWindow_Click(object sender, EventArgs e) |
345 |
{ |
346 |
ShowLogWindow(); |
347 |
} |
348 |
|
349 |
private void mnuItemHelpAbout_Click(object sender, EventArgs e) |
350 |
{ |
351 |
ShowAboutBox(); |
352 |
} |
353 |
|
354 |
private void mnuItemShowRamDumpDialog_Click(object sender, EventArgs e) |
355 |
{ |
356 |
ShowRamDump(); |
357 |
} |
358 |
|
359 |
private void mnuItemShowPIDSelector_Click(object sender, EventArgs e) |
360 |
{ |
361 |
ShowPidSelector(); |
362 |
} |
363 |
private void mnuItemShowMemoryView_Click(object sender, EventArgs e) |
364 |
{ |
365 |
ShowMemoryView(); |
366 |
} |
367 |
private void Main_FormClosing(object sender, FormClosingEventArgs e) |
368 |
{ |
369 |
string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config"); |
370 |
if (m_bSaveLayout) |
371 |
dockPanel.SaveAsXml(configFile); |
372 |
else if (File.Exists(configFile)) |
373 |
File.Delete(configFile); |
374 |
} |
375 |
|
376 |
private void mnuTestExeParse_Click(object sender, EventArgs e) |
377 |
{ |
378 |
PEReader reader = new PEReader(@"C:\Windows\System32\notepad.exe"); |
379 |
} |
380 |
|
381 |
private void mnuItemFindMaxNonNegativeHexValue_Click(object sender, EventArgs e) |
382 |
{ |
383 |
|
384 |
//uint start = 0xf0000000; |
385 |
uint end = uint.MaxValue; |
386 |
//for (uint i = start; i < end; i++) |
387 |
//{ |
388 |
ulong value = Convert.ToUInt64(end.ToString(), 16); |
389 |
//} |
390 |
} |
391 |
|
392 |
private void mnuItemShowDataTypeConverter_Click(object sender, EventArgs e) |
393 |
{ |
394 |
ShowDataTypeConverter(); |
395 |
} |
396 |
|
397 |
private void mnuItemShowMemorySearch_Click(object sender, EventArgs e) |
398 |
{ |
399 |
ShowMemorySearch(); |
400 |
} |
401 |
|
402 |
private void mnuItemDateShift_Click(object sender, EventArgs e) |
403 |
{ |
404 |
DateTime t = DateTime.Now; |
405 |
int year = Convert.ToInt32(t.ToString("yy")); |
406 |
int y = year << 9; |
407 |
int m = t.Month << 5; |
408 |
int d = t.Day; |
409 |
|
410 |
ushort v1 = (ushort)(y | m | d); |
411 |
|
412 |
t = t.AddDays(1); |
413 |
year = Convert.ToInt32(t.ToString("yy")); |
414 |
y = year << 9; |
415 |
m = t.Month << 5; |
416 |
d = t.Day; |
417 |
ushort v2 = (ushort)(y | m | d); |
418 |
|
419 |
t = t.AddDays(1); |
420 |
year = Convert.ToInt32(t.ToString("yy")); |
421 |
y = year << 9; |
422 |
m = t.Month << 5; |
423 |
d = t.Day; |
424 |
ushort v3 = (ushort)(y | m | d); |
425 |
|
426 |
t = new DateTime(9999,12,31); |
427 |
year = Convert.ToInt32(t.ToString("yy")); |
428 |
y = year << 9; |
429 |
m = t.Month << 5; |
430 |
d = t.Day; |
431 |
ushort v4 = (ushort)(y | m | d); |
432 |
} |
433 |
|
434 |
} |
435 |
} |