CRAW now runs on Windows 7 too - the problem was that Windows 7 has moved some functi...
[craw.git] / craw / console.cpp
blobf2ebfef9014a324e02ed4a9027fcd93be5edc402
1 #include <iostream>
2 #include <vector>
3 #include <boost/thread.hpp>
4 #include <boost/foreach.hpp>
5 #include <ail/string.hpp>
6 #include <ail/time.hpp>
7 #include <windows.h>
8 #include "console.hpp"
9 #include "d2_functions.hpp"
10 #include "reveal_map.hpp"
11 #include "utility.hpp"
12 #include "python.hpp"
14 //#define MAPHACK_TEST
16 #ifdef MAPHACK_TEST
17 //#include "gMap.h"
18 #include "mMap.h"
19 #endif
21 namespace
23 console_command commands[] =
25 console_command("help", "", "Prints this help", 0, &print_help),
26 console_command("?", "", "See 'help'", 0, &print_help),
27 console_command("quit", "", "Terminates the program", 0, &quit_program),
28 console_command("exit", "", "See 'quit'", 0, &quit_program),
29 console_command("life", "", "Print your character's life", 0, &print_life),
30 console_command("reveal", "", "Reveal the map of the act your character is currently in", 0, &reveal_act_command),
31 //console_command("test", "", "Perform maphack test", 0, &maphack_test),
32 console_command("name", "", "Retrieve your character's name", 0, &get_character_name_command),
33 console_command("player", "", "Get player pointer", 0, &get_player_pointer),
34 console_command("move", "<x> <y>", "Move to the specified coordinates.", 2, &move),
35 console_command("pid", "", "Print the process ID", 0, &print_pid),
36 console_command("test", "", "Run test function", 0, &run_test),
37 console_command("print", " <text>", "Print chat text", -1, &print_text),
41 console_command::console_command(std::string const & command, std::string const & argument_description, std::string const & description, long argument_count, command_handler handler):
42 command(command),
43 argument_description(argument_description),
44 description(description),
45 argument_count(argument_count),
46 handler(handler)
50 bool console_command::match(std::string const & match_command, string_vector const & arguments) const
52 return command == match_command && (argument_count == -1 || argument_count == arguments.size());
55 void print_life(string_vector const & arguments)
57 unsigned
58 current_life,
59 maximum_life;
61 if(get_life(current_life, maximum_life))
62 std::cout << "Life: " << current_life << "/" << maximum_life << std::endl;
63 else
64 std::cout << "Your character is not in a game" << std::endl;
67 void get_player_pointer(string_vector const & arguments)
69 unit * player_pointer = d2_get_player_unit();
70 std::cout << ail::hex_string_32(reinterpret_cast<ulong>(player_pointer)) << std::endl;
71 if(player_pointer && player_pointer->path_data_pointer)
73 path_data & data = *(player_pointer->path_data_pointer);
74 std::cout << "Location: " << data.position_x << ", " << data.position_y << std::endl;
78 void maphack_test(string_vector const & arguments)
80 #ifdef MAPHACK_TEST
82 std::cout << "Running the maphack test" << std::endl;
84 DefineOffsets();
86 std::cout << "Defined the offsets" << std::endl;
88 if(!GameReady())
90 std::cout << "The game is not ready" << std::endl;
91 return;
94 thread_controller controller;
95 if(!controller.suspend())
97 std::cout << "Failed to suspend all threads, resuming them" << std::endl;
98 controller.resume();
99 return;
102 std::cout << "Revealing the act" << std::endl;
104 if(RevealAct())
105 std::cout << "Done revealing the map" << std::endl;
106 else
107 std::cout << "Failed to reveal the act" << std::endl;
109 #endif
112 void move(string_vector const & arguments)
118 if(!ail::string_to_number(arguments[0], x) || !ail::string_to_number(arguments[1], y))
120 std::cout << "Invalid coordinates specified" << std::endl;
121 return;
124 //Sleep(2000);
126 std::cout << "Moving to (" << x << ", " << y << ")" << std::endl;
128 move_click(x, y);
131 void reveal_act_command(string_vector const & arguments)
133 std::cout << "Revealing the map" << std::endl;
135 bool const use_suspension = false;
137 thread_controller controller;
139 if(use_suspension && !controller.suspend())
141 std::cout << "Failed to suspend all threads, resuming them" << std::endl;
142 controller.resume();
143 return;
146 ullong start = ail::milliseconds();
148 bool success = reveal_act();
150 ullong duration = ail::milliseconds() - start;
152 if(use_suspension && !controller.resume())
153 return;
155 if(success)
156 std::cout << "Done revealing the map after " << duration << " ms" << std::endl;
157 else
158 std::cout << "Failed to reveal the act" << std::endl;
161 void get_character_name_command(string_vector const & arguments)
163 std::cout << "Attempting to retrieve your character's name" << std::endl;
164 unit * unit_pointer = d2_get_player_unit();
165 if(unit_pointer == 0)
167 std::cout << "You do not appear to be in a game" << std::endl;
168 return;
171 std::string name;
172 if(get_name_by_id(unit_pointer->id, name))
173 std::cout << "Your name is " << name << "." << std::endl;
174 else
175 std::cout << "Failed to retrieve your name." << std::endl;
178 void print_help(string_vector const & arguments)
180 std::cout << "Available commands:" << std::endl;
181 BOOST_FOREACH(console_command const & current_command, commands)
183 std::cout << std::endl << "Command: " << current_command.command << " " << current_command.argument_description << std::endl;
184 std::cout << "Description: " << current_command.description << std::endl;
188 void quit_program(string_vector const & arguments)
190 std::cout << "Terminating..." << std::endl;
191 ExitProcess(0);
194 void print_pid(string_vector const & arguments)
196 std::cout << ail::hex_string_32(GetCurrentProcessId()) << std::endl;
199 void run_test(string_vector const & arguments)
201 unit * unit_pointer = d2_get_player_unit();
202 if(unit_pointer == 0)
204 std::cout << "Not in a game" << std::endl;
205 return;
208 std::vector<unit> minions;
209 if(!get_minions(unit_pointer->id, minions))
211 std::cout << "Failed to retrieve minions" << std::endl;
213 std::cout << "Counted " << minions.size() << " minion(s)" << std::endl;
216 void print_text(string_vector const & arguments)
218 std::string text;
219 bool first = true;
220 BOOST_FOREACH(std::string const & argument, arguments)
222 if(first)
223 first = false;
224 else
225 text += " ";
226 text += argument;
228 std::cout << "Printing \"" << text << "\"" << std::endl;
229 print_chat_text(text);
232 void console_prompt()
234 while(true)
236 std::cout << ">> ";
237 std::string line;
238 std::getline(std::cin, line);
239 if(line.empty())
240 continue;
241 string_vector tokens = ail::tokenise(line, " ");
242 if(tokens.empty())
243 continue;
245 std::string const & command = tokens[0];
247 bool success = false;
249 string_vector arguments = tokens;
250 arguments.erase(arguments.begin());
252 BOOST_FOREACH(console_command const & current_command, commands)
254 if(current_command.match(command, arguments))
256 current_command.handler(arguments);
257 if(command != "help")
258 success = true;
259 break;
263 if(!success && !python::perform_command_callback(line))
264 std::cout << "Unknown command: \"" << command << "\"" << std::endl;
268 void launch_prompt()
270 //dirty!
271 new boost::thread(&console_prompt);