forked from simulationcraft/simc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc_main.cpp
374 lines (320 loc) · 9.01 KB
/
sc_main.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// ==========================================================================
// Dedmonwakeen's Raid DPS/TPS Simulator.
// Send questions to [email protected]
// ==========================================================================
#include "simulationcraft.hpp"
#include "util/git_info.hpp"
#include "sim/sc_profileset.hpp"
#include <locale>
// ==========================================================================
// Compiler Minimal Limit Deprecation Warning
// Added here so that we only get 1 warning / build process
// ==========================================================================
#if defined( SC_GCC ) && SC_GCC < 50000
# warning "g++ below version 5 is deprecated for bfa-dev"
#endif
#ifdef SC_SIGACTION
#include <csignal>
#endif
namespace { // anonymous namespace ==========================================
#ifdef SC_SIGACTION
// POSIX-only signal handler ================================================
struct sim_signal_handler_t
{
static sim_t* global_sim;
static void report( int signal )
{
const char* name = strsignal( signal );
std::cerr << "sim_signal_handler: " << name << "!";
const sim_t* crashing_child = nullptr;
#ifndef SC_NO_THREADING
if ( signal == SIGSEGV )
{
for ( auto child : global_sim -> children )
{
if ( std::this_thread::get_id() == child -> thread_id() )
{
crashing_child = child;
break;
}
}
}
#endif
if ( crashing_child )
{
fmt::print(stderr, " Thread={} Iteration={} Seed={} ({}) TargetHealth={}\n",
crashing_child -> thread_index, crashing_child -> current_iteration, crashing_child -> seed,
( crashing_child -> seed + crashing_child -> thread_index ),
crashing_child -> target -> resources.initial[ RESOURCE_HEALTH ]);
}
else
{
fmt::print(stderr, " Iteration={} Seed={} TargetHealth={}\n",
global_sim -> current_iteration, global_sim -> seed,
global_sim -> target -> resources.initial[ RESOURCE_HEALTH ]);
}
auto profileset = global_sim -> profilesets.current_profileset_name();
if ( ! profileset.empty() )
{
std::cerr << " ProfileSet=" << profileset;
}
std::cerr << std::endl;
fflush( stderr );
}
static void sigint( int signal )
{
if ( global_sim )
{
report( signal );
if( global_sim -> scaling -> calculate_scale_factors || global_sim -> reforge_plot -> current_reforge_sim || global_sim -> plot -> current_plot_stat != STAT_NONE )
{
global_sim -> cancel();
}
else if ( global_sim -> single_actor_batch )
{
global_sim -> cancel();
}
else if ( ! global_sim -> profileset_map.empty() )
{
global_sim -> cancel();
}
else
{
global_sim -> interrupt();
}
}
}
static void sigsegv( int signal )
{
if ( global_sim )
{
report( signal );
}
exit( signal );
}
sim_signal_handler_t()
{
assert ( ! global_sim );
struct sigaction sa;
sigemptyset( &sa.sa_mask );
sa.sa_flags = 0;
sa.sa_handler = sigsegv;
sigaction( SIGSEGV, &sa, nullptr );
sa.sa_handler = sigint;
sigaction( SIGINT, &sa, nullptr );
}
~sim_signal_handler_t()
{ global_sim = nullptr; }
};
#else
struct sim_signal_handler_t
{
static sim_t* global_sim;
};
#endif
sim_t* sim_signal_handler_t::global_sim = nullptr;
sim_signal_handler_t handler;
// need_to_save_profiles ====================================================
bool need_to_save_profiles( sim_t* sim )
{
if ( sim -> save_profiles ) { return true;
}
for ( auto& player : sim -> player_list )
{
if ( ! player -> report_information.save_str.empty() ) {
return true;
}
}
return false;
}
/* Obtain a platform specific place to store the http cache file
*/
std::string get_cache_directory()
{
std::string s = ".";
const char* env; // store desired environemental variable in here. getenv returns a null pointer if specified
// environemental variable cannot be found.
#if defined(__linux__) || defined(__APPLE__)
env = getenv( "XDG_CACHE_HOME" );
if ( ! env )
{
env = getenv( "HOME" );
if ( env ) {
s = std::string( env ) + "/.cache";
} else {
s = "/tmp"; // back out
}
}
else {
s = std::string( env );
}
#endif
#ifdef _WIN32
env = getenv( "TMP" );
if ( !env )
{
env = getenv( "TEMP" );
if ( ! env )
{
env = getenv( "HOME" );
}
}
s = std::string( env );
#endif
return s;
}
// RAII-wrapper for dbc init / de-init
struct dbc_initializer_t {
dbc_initializer_t()
{ dbc::init(); }
~dbc_initializer_t()
{ dbc::de_init(); }
};
// RAII-wrapper for http cache load / save
struct cache_initializer_t {
cache_initializer_t( const std::string& fn ) :
_file_name( fn )
{ http::cache_load( _file_name ); }
~cache_initializer_t()
{ http::cache_save( _file_name ); }
private:
std::string _file_name;
};
struct special_effect_initializer_t
{
special_effect_initializer_t()
{
unique_gear::register_special_effects();
unique_gear::sort_special_effects();
}
~special_effect_initializer_t()
{ unique_gear::unregister_special_effects(); }
};
} // anonymous namespace ====================================================
// sim_t::main ==============================================================
int sim_t::main( const std::vector<std::string>& args )
{
try
{
cache_initializer_t cache_init( get_cache_directory() + "/simc_cache.dat" );
dbc_initializer_t dbc_init;
module_t::init();
unique_gear::register_hotfixes();
special_effect_initializer_t special_effect_init;
// Print simc version info
if ( !git_info::available() )
{
util::printf("SimulationCraft %s for World of Warcraft %s %s (wow build %s)\n",
SC_VERSION, dbc.wow_version(), dbc.wow_ptr_status(), util::to_string(dbc.build_level()).c_str());
}
else
{
util::printf("SimulationCraft %s for World of Warcraft %s %s (wow build %s, git build %s %s)\n",
SC_VERSION, dbc.wow_version(), dbc.wow_ptr_status(), util::to_string(dbc.build_level()).c_str(), git_info::branch(), git_info::revision());
}
std::cout << std::endl;
sim_control_t control;
try
{
control.options.parse_args(args);
}
catch (const std::exception& e) {
std::throw_with_nested(std::invalid_argument("Incorrect option format"));
}
// Hotfixes are applies right before the sim context (control) is created, and simulator setup
// begins
hotfix::apply();
try
{
setup( &control );
}
catch( const std::exception& e ){
std::throw_with_nested(std::runtime_error("Setup failure"));
}
if ( display_hotfixes )
{
std::cout << hotfix::to_str( dbc.ptr );
return 0;
}
if ( display_bonus_ids )
{
std::cout << dbc::bonus_ids_str( dbc );
return 0;
}
if ( canceled )
{
return 1;
}
if ( spell_query )
{
try
{
spell_query -> evaluate();
print_spell_query();
}
catch( const std::exception& e ){
std::throw_with_nested(std::runtime_error("Spell Query Error"));
}
}
else if ( need_to_save_profiles( this ) )
{
try
{
init();
std::cout << "\nGenerating profiles... \n";
report::print_profiles( this );
}
catch( const std::exception& e ){
std::throw_with_nested(std::runtime_error("Generating profiles"));
}
}
else
{
util::printf( "\nSimulating... ( iterations=%d, threads=%d, target_error=%.3f, max_time=%.0f, vary_combat_length=%0.2f, optimal_raid=%d, fight_style=%s )\n\n",
iterations, threads, target_error, max_time.total_seconds(), vary_combat_length, optimal_raid, fight_style.c_str() );
progress_bar.set_base( "Baseline" );
if ( execute() )
{
scaling -> analyze();
plot -> analyze();
reforge_plot -> analyze();
if ( canceled == 0 && ! profilesets.iterate( this ))
{
canceled = 1;
}
else
{
report::print_suite( this );
}
}
else
{
util::printf("Simulation was canceled.\n");
canceled = 1;
}
}
std::cout << std::endl;
return canceled;
}
catch (const std::nested_exception& e) {
// Only catch exception we have already re-thrown in init functions.
std::cerr << "Error: ";
util::print_chained_exception(e.nested_ptr());
std::cerr << std::endl;
return 1;
}
}
// ==========================================================================
// MAIN
// ==========================================================================
int main( int argc, char** argv )
{
std::locale::global( std::locale( "C" ) );
#if defined( SC_VS ) && SC_VS < 13
// Ensure unified scientific notation exponent rules between different VS versions
_set_output_format( _TWO_DIGIT_EXPONENT );
#endif
sim_t sim;
sim_signal_handler_t::global_sim = ∼
return sim.main( io::utf8_args( argc, argv ) );
}