summaryrefslogtreecommitdiff
path: root/cheat/internal_rewrite/console.cpp
blob: d922af3234aa3fc9e2a053e653f7a928d195a5a7 (plain)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "console.hpp"
#include "interface.hpp"
#include "settings.hpp"
#include "d3d.hpp"
#include "ui_draw.h"

std::shared_ptr< console > g_con = std::make_shared< console >( );


void console::draw_text( int x, int y, bool greyed, const char* fmt, ... ) {
	va_list args;
	char buf[ 512 ];

	__crt_va_start( args, fmt );
	vsprintf_s< 512 >( buf, fmt, args );
	__crt_va_end( args );

	g_d3d.draw_text< ALIGN_LEFT >( d3d::fonts.f_con, greyed ? clr_t( 160, 160, 160 ) : clr_t( 255, 255, 255 ), x, y, D3DFONTFLAG_DROPSHADOW, "%s", buf );
	//g_d3d.draw_text< ALIGN_LEFT >( d3d::fonts.f_con, greyed ? clr_t( 160, 160, 160 ) : clr_t( 255, 255, 255 ), x, y, D3DFONTFLAG_DROPSHADOW, buf );
}

void console::draw_text( int x, int y, const char* fmt, ... ) {
	va_list args;
	char buf[ 512 ];

	__crt_va_start( args, fmt );
	vsprintf_s< 512 >( buf, fmt, args );
	__crt_va_end( args );

	g_d3d.draw_text< ALIGN_LEFT >( d3d::fonts.f_con, clr_t( 255, 255, 255 ), x, y, D3DFONTFLAG_DROPSHADOW, buf );
}

void console::draw_rect( int x, int y, int w, int h, clr_t col ) {
	g_d3d.draw_filled_rect( col, x, y, w, h );
}

void console::register_alias( std::shared_ptr< ISetting >& alias ) {
	m_aliases.push_back( alias );
}

void console::capture_command( ) {
	float current_time = GetTickCount( ) * 0.001f;
	size_t length = strlen( m_cur_cmd );

	for( size_t i{ }; i < 0xfe; ++i ) {
		if( m_input->is_key_pressed( i ) ) {
			float delta_time = current_time - m_last_key_input[ i ];
			if( fabs( delta_time ) > 0.2f ) {
				if( i == KEYS_BACK ) {
					m_cur_cmd[ length - 1 ] = 0;
					m_last_key_input[ i ] = current_time;
					continue;
				}

				m_key_states[ i ] = 0xf0;
				wchar_t pressed_char;
				const auto scan = MapVirtualKeyA( i, 2 );
				auto ret = ToAscii( i, scan, ( BYTE* )m_key_states, ( LPWORD )&pressed_char, 1 );

				if( ret == 1 ) {
					if( length < 200 ) {
						m_cur_cmd[ length ] = ( char )( pressed_char );
						m_cur_cmd[ length + 1 ] = 0;
					}
				}
				m_last_key_input[ i ] = current_time;
			}
		}
		else {
			m_last_key_input[ i ] = 0.f;
			m_key_states[ i ] = 0;
		}
	}
}

ISetting* console::find_var( hash_t var ) {
	for( auto& it : data::holder_.get_nodes( ) ) {
		auto setting = static_cast< ISetting* >( it );
		if( var == setting->get_hash( ) ) {
			return setting;
		}
	}

	return nullptr;
}

ISetting* console::find_alias( hash_t alias ) {
	for( auto& it : m_aliases ) {
		if( it->get_hash( ) == alias )
			return it.get( );
	}

	return nullptr;
}

con_fn_base* console::find_fn( hash_t name ) {
	for( auto& it : m_functions ) {
		if( it->get_hash( ) == name ) {
			return it;
		}
	}

	return nullptr;
}

std::string console::impl_alias_str( std::string str ) {
	for( size_t i{ }; i < str.length( ); ++i ) {
		if( str[ i ] == ' ' && str[ i + 1 ] != ' ' ) {
			size_t end = 0;
			for( size_t i2{ i + 1 }; i2 < str.length( ); ++i ) {
				if( str[ i2 ] == ' ' )
					end = i2;
			}

			if( end ) {
				std::string alias_str( str.c_str( ) + i, end - i );
				auto alias = find_alias( hash::fnv1a( alias_str ) );
				if( alias ) {
					str.replace( alias_str.begin( ), alias_str.end( ), alias->get_string( ) );
				}
				else {
					g_con->log( xors( "couldnt find alias %s" ), alias_str.c_str( ) );
				}
			}
		}
	}

	return str;
}

void console::execute_command( const char* cmd ) {
	if( !cmd[ 0 ] ) return;
	std::string first_param( cmd, strlen( cmd ) );

	size_t end{ };
	bool one_arg = true;
	for( size_t i{ }; i < strlen( cmd ); ++i ) {
		if( cmd[ i ] == ' ' ) {
			first_param.resize( i );
			end = i;
			one_arg = false;
			break;
		}
	}

	if( one_arg ) {
		first_param.resize( first_param.length( ) - 1 );
	}

	//find a command to execute
	auto fn = find_fn( hash::fnv1a( first_param ) );
	if( fn ) {
		//std::string alias_cmd = impl_alias_str( cmd );

		if( !fn->execute( cmd ) ) {
			log( xors( "invalid syntax" ) );
		}
		return;
	}

	//command not found, we're managing vars
	if( !one_arg ) {
		std::string second_param = ( cmd + end + 1 );
		bool is_floating_point = second_param.find( '.' ) != std::string::npos;
		auto var = find_var( hash::fnv1a( first_param ) );

		if( var ) {
			auto setting_cast = static_cast< con_var< void* >* >( var );
			if( !setting_cast->is_integral( ) && !setting_cast->is_floating_point( ) ) {
				auto value = std::strtol( second_param.c_str( ), 0, 16 );
				printf( xors( "value: %08x\n" ), value );
				var->set( value );
			}

			if( is_floating_point ) {
				float value = std::strtof( second_param.c_str( ), 0 );
				var->set( value );
			}
			else {
				int value = std::strtol( second_param.c_str( ), 0, 10 );
				var->set( value );
			}
			log( xors( "%s %s\n" ), first_param.c_str( ), second_param.c_str( ) );
			return;
		}
	}


	log( xors( "unknown command: %s" ), first_param.c_str( ) );
}

void console::input( ) {
	static int drag_x, drag_y;
	int cursor_x, cursor_y;
	int x = m_x, y = m_y;
	bool clicked = m_input->is_key_pressed( KEYS_MOUSE1 );
	static bool is_hovered{ };
	m_input->get_cursor_pos( cursor_x, cursor_y );

	auto is_top_hovered = [ &x, &y, 
		&cursor_x, &cursor_y ]( ) {
		return cursor_x > x && cursor_x < x + WIDTH
			&& cursor_y > y && cursor_y < y + TOP_HEIGHT;
	};

	auto is_window_hovered = [ &x, &y,
		&cursor_x, &cursor_y ]( ) {
		return cursor_x > x && cursor_x < x + WIDTH
			&& cursor_y > y && cursor_y < y + HEIGHT;
	};

	auto is_bottom_hovered = [ &x, &y,
		&cursor_x, &cursor_y ]( ) {
		return cursor_x > x && cursor_x < x + WIDTH
			&& cursor_y > y + HEIGHT - TOP_HEIGHT 
			&& cursor_y < y + HEIGHT;
	};

	if( clicked ) {
		//m_consuming_input = is_window_hovered( );
	}

	if( !clicked && is_top_hovered( ) ) {
		drag_x = cursor_x - m_x;
		drag_y = cursor_y - m_y;
	}

	if( clicked && is_hovered ) {
		is_hovered = true;
		m_x = cursor_x - drag_x;
		m_y = cursor_y - drag_y;
	}
	else {
		is_hovered = is_top_hovered( );
	}

	if( clicked ) {
		m_active = is_bottom_hovered( );
	}

	if( m_active ) {
		capture_command( );

		static bool was_held{ };
		if( m_input->is_key_pressed( KEYS_RETURN ) ) {
			if( !was_held ) {
				execute_command( m_cur_cmd );
				m_cur_cmd[ 0 ] = 0;
			}
			was_held = true;
		}
		else was_held = false;
	}
}

void console::draw( ) {
	if( !m_open ) {
		m_consuming_input = false;
		m_active = false;
		return;
	}

	static clr_t col = clr_t( 31, 31, 31, 255 );

	input( );

	RECT prev_rect{ };
	RECT new_rect{ m_x - 1, m_y - 1,
		m_x + WIDTH + 1, m_y + HEIGHT + 1 };
	g_d3d.get_device( )->GetScissorRect( &prev_rect );
	
	g_d3d.get_device( )->SetScissorRect( &new_rect );

	draw_rect( m_x - 1, m_y - 1, WIDTH + 2, HEIGHT + 2, ui::ui_get_accent_col( ) );
	draw_rect( m_x, m_y, WIDTH, HEIGHT, col );
	draw_rect( m_x + 1, m_y, WIDTH - 2, TOP_HEIGHT, clr_t( 41, 41, 41 ) );

	draw_rect( m_x + 1, m_y + HEIGHT - TOP_HEIGHT, WIDTH - 2, TOP_HEIGHT, clr_t( 41, 41, 41 ) );
	if( m_active ) {
		draw_rect( m_x + 2, m_y + HEIGHT - TOP_HEIGHT + 1, WIDTH - 4, TOP_HEIGHT - 2, clr_t( 31, 31, 31 ) );
		auto fn = find_fn( hash::fnv1a( m_cur_cmd ) );
		if( fn ) {
			std::string fn_str = m_cur_cmd;
			fn_str += ' ';
			fn_str += fn->get_syntax( );

			draw_text( m_x + 2, m_y + HEIGHT - TOP_HEIGHT / 2 - 3, true, "%s", fn_str.c_str( ) );
		}

		else {
			auto var = find_var( hash::fnv1a( m_cur_cmd ) );
			if( var ) {
				std::string var_str = m_cur_cmd;
				var_str += ' ';

				var_str += var->get_string( );

				draw_text( m_x + 2, m_y + HEIGHT - TOP_HEIGHT / 2 - 3, true, var_str.c_str( ) );
			}
		}
	}

	std::string cur_cmd_str = m_cur_cmd;
	if( m_active ) {
		cur_cmd_str += '_';
	}
	draw_text( m_x + 2, m_y + HEIGHT - TOP_HEIGHT / 2 - 3, cur_cmd_str.c_str( ) );
	draw_text( m_x + 3, m_y + TOP_HEIGHT / 2 - 3, xors( "console" ) );

	int cur_y = m_y + HEIGHT - TOP_HEIGHT;
	for( int i = m_logs.size( ) - 1; i >= 0; --i ) {
		constexpr int LINE_HEIGHT = 14;
		auto& log = m_logs.at( i );

		if( cur_y - LINE_HEIGHT < m_y + TOP_HEIGHT ) {
			m_logs.erase( m_logs.begin( ) + i );
			continue;
		}
		
		draw_text( m_x + 2, cur_y -= LINE_HEIGHT, log.m_msg );
	}

	g_d3d.get_device( )->SetScissorRect( &prev_rect );
}