-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainManager.cs
More file actions
112 lines (96 loc) · 3.4 KB
/
MainManager.cs
File metadata and controls
112 lines (96 loc) · 3.4 KB
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
using ReadWriteMemory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CnCMemoryWatchTool
{
class MainManager
{
ProcessMemory ProcMem;
byte[] MemBytes;
int MemoryOffset = -1;
int AdjustedOffset = 0;
public MainManager()
{
LoadMemory();
}
private void LoadMemory()
{
LoadProcessMemory();
LoadMemoryOffset();
LoadMemoryBytes();
}
public void UpdateMemoryBytes()
{
LoadMemoryBytes();
}
public byte[] GetMemoryBytes()
{
return MemBytes;
}
private void LoadMemoryBytes()
{
this.MemBytes = this.ProcMem.ReadMem(this.MemoryOffset + AdjustedOffset, 0x8000, false);
}
public void SetAdJustedOffset(int offset)
{
this.AdjustedOffset = offset;
}
private void LoadMemoryOffset()
{
//byte[] pattern = new byte[] { 0x54, 0x02, 0x61, 0x02, 0x72, 0x02 };
// 30 02 30 02 30 02 30 02 29 02
// 60 10 00 F0 08 00 70 00 08 00 70 00 08 00 70 00 08 00 70 00 60 10
// 60 10 00 F0 08 00 70 00 08 00 70 00 08 00 70 00 08 00 70 00 60 10
byte[] pattern = new byte[] { 0x60, 0x10, 0x00, 0xF0, 0x08, 0x00, 0x70, 0x00, 0x08,
0x00, 0x70, 0x00, 0x08, 0x00, 0x70, 0x00, 0x08, 0x00, 0x70, 0x00, 0x60, 0x10 };
for (int searchOffset = 0x0; searchOffset < (int.MaxValue >> 4); searchOffset += 0x100)
{
byte[] MemB = this.ProcMem.ReadMem(searchOffset, 0x100, false);
var foundList = SearchBytePattern(pattern, MemB);
if (foundList.Count > 0)
{
this.MemoryOffset = searchOffset + foundList[0];
this.MemoryOffset += 0xB0000; // Fix up offset
return;
}
}
this.MemoryOffset = -1;
MessageBox.Show("Can't find memory offsets for the monopages in DOSBox memory. Shutting down."
+ " Consider running this application after starting DOSBox but before starting a DOS application.");
Environment.Exit(-1);
}
private void LoadProcessMemory()
{
this.ProcMem = new ProcessMemory("dosbox");
if (!this.ProcMem.OpenProcess())
{
Environment.Exit(-1);
}
}
static private List<int> SearchBytePattern(byte[] pattern, byte[] bytes)
{
List<int> positions = new List<int>();
int patternLength = pattern.Length;
int totalLength = bytes.Length;
byte firstMatchByte = pattern[0];
for (int i = 0; i < totalLength; i++)
{
if (firstMatchByte == bytes[i] && totalLength - i >= patternLength)
{
byte[] match = new byte[patternLength];
Array.Copy(bytes, i, match, 0, patternLength);
if (match.SequenceEqual<byte>(pattern))
{
positions.Add(i);
i += patternLength - 1;
}
}
}
return positions;
}
}
}