-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathComputerProgramSession.cpp
More file actions
193 lines (171 loc) · 6.21 KB
/
Copy pathComputerProgramSession.cpp
File metadata and controls
193 lines (171 loc) · 6.21 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
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
/* Computer Program Session
*
* From: https://github.com/PokemonAutomation/
*
*/
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/Json/JsonValue.h"
#include "Common/Cpp/CancellableScope.h"
#include "CommonFramework/Exceptions/FatalProgramException.h"
#include "CommonFramework/Exceptions/ProgramFinishedException.h"
#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h"
#include "CommonFramework/Notifications/ProgramInfo.h"
#include "CommonFramework/Notifications/ProgramNotifications.h"
#include "ComputerProgramSession.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
ComputerProgramSession::ComputerProgramSession(const ComputerProgramDescriptor& descriptor)
: UiState<ComputerProgramSession, PanelSession>(descriptor)
, ProgramSession(descriptor)
, m_descriptor(descriptor)
, m_instance(descriptor.make_instance())
{}
ComputerProgramSession::~ComputerProgramSession(){
ComputerProgramSession::internal_stop_program();
join_program_thread();
}
ConfigOption& ComputerProgramSession::options(){
return m_instance->m_options;
}
std::string ComputerProgramSession::check_validity() const{
return m_instance->check_validity();
}
void ComputerProgramSession::restore_defaults(){
std::lock_guard<Mutex> lg(program_lock());
if (current_state() != ProgramState::STOPPED){
logger().log("Cannot change settings while program is running.", COLOR_RED);
return;
}
logger().log("Restoring settings to defaults...");
m_instance->restore_defaults();
}
JsonValue ComputerProgramSession::to_json() const{
return m_instance->to_json();
}
void ComputerProgramSession::load_json(const JsonValue& json){
m_instance->load_json(json);
}
void ComputerProgramSession::run_program_instance(ProgramEnvironment& env, CancellableScope& scope){
{
std::lock_guard<Mutex> lg(program_lock());
std::string error = check_validity();
if (!error.empty()){
throw UserSetupError(logger(), std::move(error));
}
}
{
WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION);
m_scope = &scope;
}
try{
m_instance->program(env, scope);
}catch (...){
WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION);
m_scope = nullptr;
throw;
}
WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION);
m_scope = nullptr;
}
void ComputerProgramSession::internal_stop_program(){
WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION);
if (m_scope != nullptr){
m_scope->cancel(std::make_exception_ptr(ProgramCancelledException()));
}
}
void ComputerProgramSession::internal_run_program(){
CancellableHolder<CancellableScope> download_scope;
{
WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION);
m_scope = &download_scope;
}
bool success = download_prereqs(download_scope);
{
std::lock_guard<Mutex> lg(program_lock());
m_scope = nullptr;
}
if (!success){
return;
}
options().reset_state();
ProgramInfo program_info(
identifier(),
m_descriptor.category(),
m_descriptor.display_name(),
last_state_change()
);
CancellableHolder<CancellableScope> scope;
ProgramEnvironment env(
program_info,
*this,
current_stats_tracker(), historical_stats_tracker()
);
try{
logger().log("<b>Starting Program: " + identifier() + "</b>");
run_program_instance(env, scope);
// m_setup->wait_for_all_requests();
logger().log("Program finished normally!", COLOR_BLUE);
}catch (OperationCancelledException&){
}catch (ProgramCancelledException&){
}catch (ProgramFinishedException& e){
logger().log("Program finished early!", COLOR_BLUE);
send_program_finished_notification(env, m_instance->NOTIFICATION_PROGRAM_FINISH, e.message(), *e.screenshot());
}catch (InvalidConnectionStateException&){
}catch (OperationFailedExceptionWithScreenshot& e){
logger().log("Program stopped with an exception!", COLOR_RED);
std::string message = e.message();
if (message.empty()){
message = e.name();
}
report_error(message);
e.send_fatal_error_notif_and_telemetry_report(env, m_instance->NOTIFICATION_ERROR_FATAL);
}catch (OperationFailedException& e){ // no screenshot
logger().log("Program stopped with an exception!", COLOR_RED);
std::string message = e.message();
if (message.empty()){
message = e.name();
}
report_error(message);
e.send_fatal_error_notif_and_telemetry_report(env, m_instance->NOTIFICATION_ERROR_FATAL);
}catch (FatalProgramException& e){
logger().log("Program stopped with an exception!", COLOR_RED);
std::string message = e.message();
if (message.empty()){
message = e.name();
}
report_error(message);
e.send_fatal_error_notif_and_telemetry_report(env, m_instance->NOTIFICATION_ERROR_FATAL);
}catch (Exception& e){
logger().log("Program stopped with an exception!", COLOR_RED);
std::string message = e.message();
if (message.empty()){
message = e.name();
}
report_error(message);
send_program_fatal_error_notification(
env, m_instance->NOTIFICATION_ERROR_FATAL,
message
);
}catch (std::exception& e){
logger().log("Program stopped with an exception!", COLOR_RED);
std::string message = e.what();
if (message.empty()){
message = "Unknown std::exception.";
}
report_error(message);
send_program_fatal_error_notification(
env, m_instance->NOTIFICATION_ERROR_FATAL,
message
);
}catch (...){
logger().log("Program stopped with an exception!", COLOR_RED);
report_error("Unknown error.");
send_program_fatal_error_notification(
env, m_instance->NOTIFICATION_ERROR_FATAL,
"Unknown error."
);
}
}
}