forked from altf4/SmashBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmme.cpp
More file actions
146 lines (120 loc) · 4.15 KB
/
Copy pathmme.cpp
File metadata and controls
146 lines (120 loc) · 4.15 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
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
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <csignal>
#include <string>
#include <time.h>
#include "Util/GameState.h"
#include "Util/MemoryWatcher.h"
#include "Util/Logger.h"
#include "Util/Paths.h"
bool isDebug = false;
void FirstTimeSetup()
{
std::string config_path = Paths::GetConfigPath();
std::string mem_watcher_path = config_path + "MemoryWatcher/";
std::string pipe_path = config_path + "Pipes/";
//Create the MemoryWatcher directory if it doesn't already exist
struct stat buffer;
if(stat(mem_watcher_path.c_str(), &buffer) != 0)
{
if(mkdir(mem_watcher_path.c_str(), 0775) != 0)
{
std::cout << "ERROR: Could not create the directory: \"" << mem_watcher_path << "\". Dolphin seems to be installed, " \
"But this is not working for some reason. Maybe permissions?" << std::endl;
exit(-1);
}
std::cout << "WARNING: Had to create a MemoryWatcher directory in Dolphin just now. " \
"You may need to restart Dolphin and SmashBot in order for this to work. (You should only see this warning once)" << std::endl;
}
std::ifstream src("Locations.txt", std::ios::in);
std::ofstream dst(mem_watcher_path + "/Locations.txt", std::ios::out);
dst << src.rdbuf();
//Create the Pipes directory if it doesn't already exist
if(stat(pipe_path.c_str(), &buffer) != 0)
{
if(mkdir(pipe_path.c_str(), 0775) != 0)
{
std::cout << "ERROR: Could not create the directory: \"" << pipe_path << "\". Dolphin seems to be installed, " \
"But this is not working for some reason. Maybe permissions?" << std::endl;
exit(-1);
}
std::cout << "WARNING: Had to create a Pipes directory in Dolphin just now. " \
"You may need to restart Dolphin and SmashBot in order for this to work. (You should only see this warning once)" << std::endl;
}
}
void signal_handler(int signal)
{
std::string logpath = "Logs";
struct stat buffer;
if(isDebug)
{
Logger *logger = Logger::Instance();
std::string logdump = logger->DumpLog();
if(stat(logpath.c_str(), &buffer) != 0)
{
if(mkdir(logpath.c_str(), 0775) != 0)
{
std::cout << "ERROR: Could not create the directory: \"" << logpath << "\". Maybe permissions?" << std::endl;
exit(EXIT_FAILURE);
}
}
//Name the file as a timestamp
static char name[20];
time_t now = time(0);
strftime(name, sizeof(name), "%Y%m%d_%H%M%S", localtime(&now));
std::string filename = std::string(name);
std::cout << "\nINFO: Log file written to: " << logpath + "/" + filename + ".csv" << std::endl;
std::ofstream dst(logpath + "/" + filename + ".csv", std::ios::out);
dst << logdump;
dst.close();
}
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
if(argc > 1)
{
std::string arg = std::string(argv[1]);
if(arg == "--debug")
{
isDebug = true;
}
}
//Do some first-time setup
FirstTimeSetup();
std::signal(SIGINT, signal_handler);
Logger *logger = Logger::Instance();
logger->SetDebug(isDebug);
GameState *state = GameState::Instance();
MemoryWatcher *watcher = new MemoryWatcher();
uint last_frame = 0;
MENU current_menu;
//Main frame loop
for(;;)
{
//If we get a new frame, process it. Otherwise, keep reading memory
if(!watcher->ReadMemory())
{
state->shareData(state->m_memory);
continue;
}
current_menu = (MENU)state->m_memory->menu_state;
if(state->m_memory->frame != last_frame)
{
if(state->m_memory->frame > last_frame+1)
{
logger->Log(WARNING, "FRAME MISSED");
logger->Log(INFO, "FRAME MISSED");
}
last_frame = state->m_memory->frame;
}
}
return EXIT_SUCCESS;
}