-
Notifications
You must be signed in to change notification settings - Fork 3
/
program.cpp
139 lines (129 loc) · 3.11 KB
/
program.cpp
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
#include "common.hpp"
#include "program.hpp"
#include "joaat.hpp"
namespace upd
{
program::program(const std::filesystem::path& path, const std::filesystem::path& basedir) :
m_program(path),
m_script_base_dir(basedir)
{
}
program::~program()
{
for (auto script : m_open_scripts)
{
delete script;
}
}
void program::load_script(std::string& script)
{
auto hash = joaat(script);
for (const auto& script : m_open_scripts)
{
if (script->m_name_hash == hash)
{
m_active_script = script;
return;
}
}
auto file = new ysc_file(m_script_base_dir / (script + ".ysc"));
m_open_scripts.push_back(file);
m_active_script = file;
}
void program::run()
{
while (char opcode = m_program.get())
{
if (m_program.eof())
{
return;
}
switch (opcode)
{
case 'F': // load file
{
auto file = read_string();
load_script(file);
break;
}
case 'P': // scan pattern, set output to position
{
if (!m_active_script)
throw std::runtime_error("no active file");
auto pattern = memory::pattern(read_string());
auto result = m_active_script->scan_pattern(pattern);
if (!result.has_value())
throw std::runtime_error("failed to find pattern");
m_position = result.value();
break;
}
case '+': // add integer to position
{
m_position += read_int();
break;
}
case '-': // subtract integer from position
{
m_position -= read_int();
break;
}
case 'B': // read byte from position into value
{
if (!m_active_script)
throw std::runtime_error("no active file");
m_value = *m_active_script->get_code_address(m_position);
break;
}
case 'W': // read word from position into value
{
if (!m_active_script)
throw std::runtime_error("no active file");
m_value = *(std::uint16_t*)m_active_script->get_code_address(m_position);
break;
}
case 'A': // read 3 byte value from position into value
{
if (!m_active_script)
throw std::runtime_error("no active file");
m_value = read_uint24_t(m_active_script->get_code_address(m_position));
break;
}
case 'D': // read dword from position into value
{
if (!m_active_script)
throw std::runtime_error("no active file");
m_value = *(std::uint32_t*)m_active_script->get_code_address(m_position);
break;
}
case 'R': // read relative from (position+1) into value (read jump address)
{
if (!m_active_script)
throw std::runtime_error("no active file");
m_value = script_rva(*(std::int16_t*)m_active_script->get_code_address(m_position) + 1, m_position);
break;
}
case '^': // copy value into position (rip)
{
m_position = m_value;
break;
}
case 'O': // output string
{
std::cout << read_string() << std::endl;
break;
}
case '=': // output string with value
{
std::cout << read_string() << " = " << (std::int32_t)m_value << std::endl;
break;
}
case ';': // reset position and value
{
m_position = 0;
m_value = 0;
break;
}
}
}
}
}