Merge branch 'cpp-11' into crashlog_improvements

# Conflicts:
#	Makefile.src.in
#	projects/determineversion.vbs
#	source.list
#	src/crashlog.cpp
#	src/misc.cpp
#	src/os/unix/crashlog_unix.cpp
#	src/os/windows/crashlog_win.cpp
#	src/rev.h
#	src/thread/thread.h
#	src/thread/thread_morphos.cpp
#	src/thread/thread_none.cpp
#	src/thread/thread_os2.cpp
#	src/thread/thread_pthread.cpp
#	src/thread/thread_win32.cpp
This commit is contained in:
Jonathan G Rennison
2019-04-09 19:21:39 +01:00
820 changed files with 29187 additions and 22975 deletions

View File

@@ -64,11 +64,12 @@
#include "gfx_layout.h"
#include "viewport_sprite_sorter.h"
#include "framerate_type.h"
#include "thread/thread.h"
#include "thread.h"
#include "linkgraph/linkgraphschedule.h"
#include <stdarg.h>
#include <system_error>
#include "safeguards.h"
@@ -77,7 +78,7 @@ void IncreaseDate();
void DoPaletteAnimations();
void MusicLoop();
void ResetMusic();
void CallWindowTickEvent();
void CallWindowGameTickEvent();
bool HandleBootstrap();
extern Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY);
@@ -118,7 +119,9 @@ void CDECL error(const char *s, ...)
vseprintf(buf, lastof(buf), s, va);
va_end(va);
ShowOSErrorBox(buf, true);
if (VideoDriver::GetInstance() == NULL || VideoDriver::GetInstance()->HasGUI()) {
ShowOSErrorBox(buf, true);
}
/* Set the error message for the crash log and then invoke it. */
CrashLog::SetErrorMessage(buf);
@@ -192,16 +195,14 @@ static void ShowHelp()
" -e = Start Editor\n"
" -g [savegame] = Start new/save game immediately\n"
" -G seed = Set random seed\n"
#if defined(ENABLE_NETWORK)
" -n [ip:port#company]= Join network game\n"
" -p password = Password to join server\n"
" -P password = Password to join company\n"
" -D [ip][:port] = Start dedicated server\n"
" -l ip[:port] = Redirect DEBUG()\n"
#if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
#if !defined(_WIN32)
" -f = Fork into the background (dedicated only)\n"
#endif
#endif /* ENABLE_NETWORK */
" -I graphics_set = Force the graphics set (see below)\n"
" -S sounds_set = Force the sounds set (see below)\n"
" -M music_set = Force the music set (see below)\n"
@@ -242,7 +243,7 @@ static void ShowHelp()
/* ShowInfo put output to stderr, but version information should go
* to stdout; this is the only exception */
#if !defined(WIN32) && !defined(WIN64)
#if !defined(_WIN32)
printf("%s\n", buf);
#else
ShowInfo(buf);
@@ -251,7 +252,7 @@ static void ShowHelp()
static void WriteSavegameInfo(const char *name)
{
extern uint16 _sl_version;
extern SaveLoadVersion _sl_version;
uint32 last_ottd_rev = 0;
byte ever_modified = 0;
bool removed_newgrfs = false;
@@ -280,7 +281,7 @@ static void WriteSavegameInfo(const char *name)
/* ShowInfo put output to stderr, but version information should go
* to stdout; this is the only exception */
#if !defined(WIN32) && !defined(WIN64)
#if !defined(_WIN32)
printf("%s\n", buf);
#else
ShowInfo(buf);
@@ -308,7 +309,7 @@ static void ParseResolution(Dimension *res, const char *s)
/**
* Unitializes drivers, frees allocated memory, cleans pools, ...
* Uninitializes drivers, frees allocated memory, cleans pools, ...
* Generally, prepares the game for shutting down
*/
static void ShutdownGame()
@@ -328,9 +329,7 @@ static void ShutdownGame()
/* Uninitialize variables that are allocated dynamically */
GamelogReset();
#ifdef ENABLE_NETWORK
free(_config_file);
#endif
LinkGraphSchedule::Clear();
PoolBase::Clean(PT_ALL);
@@ -397,6 +396,9 @@ void MakeNewgameSettingsLive()
_settings_game.ai_config[c] = NULL;
if (_settings_newgame.ai_config[c] != NULL) {
_settings_game.ai_config[c] = new AIConfig(_settings_newgame.ai_config[c]);
if (!AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->HasScript()) {
AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(NULL);
}
}
}
_settings_game.game_config = NULL;
@@ -417,7 +419,7 @@ void OpenBrowser(const char *url)
/** Callback structure of statements to be executed after the NewGRF scan. */
struct AfterNewGRFScan : NewGRFScanCallback {
Year startyear; ///< The start year.
uint generation_seed; ///< Seed for the new game.
uint32 generation_seed; ///< Seed for the new game.
char *dedicated_host; ///< Hostname for the dedicated server.
uint16 dedicated_port; ///< Port for the dedicated server.
char *network_conn; ///< Information about the server to connect to, or NULL.
@@ -437,6 +439,9 @@ struct AfterNewGRFScan : NewGRFScanCallback {
join_server_password(NULL), join_company_password(NULL),
save_config_ptr(save_config_ptr), save_config(true)
{
/* Visual C++ 2015 fails compiling this line (AfterNewGRFScan::generation_seed undefined symbol)
* if it's placed outside a member function, directly in the struct body. */
assert_compile(sizeof(generation_seed) == sizeof(_settings_game.game_creation.generation_seed));
}
virtual void OnNewGRFsScanned()
@@ -458,7 +463,6 @@ struct AfterNewGRFScan : NewGRFScanCallback {
Game::Uninitialize(true);
AI::Uninitialize(true);
CheckConfig();
LoadFromHighScore();
LoadHotkeysFromConfig();
WindowDesc::LoadFromConfig();
@@ -472,13 +476,11 @@ struct AfterNewGRFScan : NewGRFScanCallback {
if (startyear != INVALID_YEAR) _settings_newgame.game_creation.starting_year = startyear;
if (generation_seed != GENERATE_NEW_SEED) _settings_newgame.game_creation.generation_seed = generation_seed;
#if defined(ENABLE_NETWORK)
if (dedicated_host != NULL) {
_network_bind_list.Clear();
*_network_bind_list.Append() = stredup(dedicated_host);
_network_bind_list.push_back(stredup(dedicated_host));
}
if (dedicated_port != 0) _settings_client.network.server_port = dedicated_port;
#endif /* ENABLE_NETWORK */
/* initialize the ingame console */
IConsoleInit();
@@ -488,7 +490,6 @@ struct AfterNewGRFScan : NewGRFScanCallback {
/* Make sure _settings is filled with _settings_newgame if we switch to a game directly */
if (_switch_mode != SM_NONE) MakeNewgameSettingsLive();
#ifdef ENABLE_NETWORK
if (_network_available && network_conn != NULL) {
const char *port = NULL;
const char *company = NULL;
@@ -514,14 +515,13 @@ struct AfterNewGRFScan : NewGRFScanCallback {
_switch_mode = SM_NONE;
NetworkClientConnectGame(NetworkAddress(network_conn, rport), join_as, join_server_password, join_company_password);
}
#endif /* ENABLE_NETWORK */
/* After the scan we're not used anymore. */
delete this;
}
};
#if defined(UNIX) && !defined(__MORPHOS__)
#if defined(UNIX)
extern void DedicatedFork();
#endif
@@ -534,16 +534,14 @@ static const OptionData _options[] = {
GETOPT_SHORT_VALUE('s'),
GETOPT_SHORT_VALUE('v'),
GETOPT_SHORT_VALUE('b'),
#if defined(ENABLE_NETWORK)
GETOPT_SHORT_OPTVAL('D'),
GETOPT_SHORT_OPTVAL('n'),
GETOPT_SHORT_VALUE('l'),
GETOPT_SHORT_VALUE('p'),
GETOPT_SHORT_VALUE('P'),
#if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
#if !defined(_WIN32)
GETOPT_SHORT_NOVAL('f'),
#endif
#endif /* ENABLE_NETWORK */
GETOPT_SHORT_VALUE('r'),
GETOPT_SHORT_VALUE('t'),
GETOPT_SHORT_OPTVAL('d'),
@@ -577,13 +575,14 @@ int openttd_main(int argc, char *argv[])
/* AfterNewGRFScan sets save_config to true after scanning completed. */
bool save_config = false;
AfterNewGRFScan *scanner = new AfterNewGRFScan(&save_config);
#if defined(ENABLE_NETWORK)
bool dedicated = false;
char *debuglog_conn = NULL;
extern bool _dedicated_forks;
_dedicated_forks = false;
#endif /* ENABLE_NETWORK */
std::unique_lock<std::mutex> modal_work_lock(_modal_progress_work_mutex, std::defer_lock);
std::unique_lock<std::mutex> modal_paint_lock(_modal_progress_paint_mutex, std::defer_lock);
_game_mode = GM_MENU;
_switch_mode = SM_MENU;
@@ -602,7 +601,6 @@ int openttd_main(int argc, char *argv[])
case 's': free(sounddriver); sounddriver = stredup(mgo.opt); break;
case 'v': free(videodriver); videodriver = stredup(mgo.opt); break;
case 'b': free(blitter); blitter = stredup(mgo.opt); break;
#if defined(ENABLE_NETWORK)
case 'D':
free(musicdriver);
free(sounddriver);
@@ -637,11 +635,10 @@ int openttd_main(int argc, char *argv[])
case 'P':
scanner->join_company_password = mgo.opt;
break;
#endif /* ENABLE_NETWORK */
case 'r': ParseResolution(&resolution, mgo.opt); break;
case 't': scanner->startyear = atoi(mgo.opt); break;
case 'd': {
#if defined(WIN32)
#if defined(_WIN32)
CreateConsole();
#endif
if (mgo.opt != NULL) SetDebugString(mgo.opt);
@@ -699,7 +696,7 @@ int openttd_main(int argc, char *argv[])
goto exit_noshutdown;
}
case 'G': scanner->generation_seed = atoi(mgo.opt); break;
case 'G': scanner->generation_seed = strtoul(mgo.opt, NULL, 10); break;
case 'c': free(_config_file); _config_file = stredup(mgo.opt); break;
case 'x': scanner->save_config = false; break;
case 'h':
@@ -728,14 +725,12 @@ int openttd_main(int argc, char *argv[])
DeterminePaths(argv[0]);
TarScanner::DoScan(TarScanner::BASESET);
#if defined(ENABLE_NETWORK)
if (dedicated) DEBUG(net, 0, "Starting dedicated version %s", _openttd_revision);
if (_dedicated_forks && !dedicated) _dedicated_forks = false;
#if defined(UNIX) && !defined(__MORPHOS__)
#if defined(UNIX)
/* We must fork here, or we'll end up without some resources we need (like sockets) */
if (_dedicated_forks) DedicatedFork();
#endif
#endif
LoadFromConfig(true);
@@ -813,7 +808,6 @@ int openttd_main(int argc, char *argv[])
NetworkStartUp(); // initialize network-core
#if defined(ENABLE_NETWORK)
if (debuglog_conn != NULL && _network_available) {
const char *not_used = NULL;
const char *port = NULL;
@@ -826,7 +820,6 @@ int openttd_main(int argc, char *argv[])
NetworkStartDebugLog(NetworkAddress(debuglog_conn, rport));
}
#endif /* ENABLE_NETWORK */
if (!HandleBootstrap()) {
ShutdownGame();
@@ -874,8 +867,14 @@ int openttd_main(int argc, char *argv[])
free(musicdriver);
/* Take our initial lock on whatever we might want to do! */
_modal_progress_paint_mutex->BeginCritical();
_modal_progress_work_mutex->BeginCritical();
try {
modal_work_lock.lock();
modal_paint_lock.lock();
} catch (const std::system_error&) {
/* If there is some error we assume that threads aren't usable on the system we run. */
extern bool _use_threaded_modal_progress; // From progress.cpp
_use_threaded_modal_progress = false;
}
GenerateWorld(GWM_EMPTY, 64, 64); // Make the viewport initialization happy
WaitTillGeneratedWorld();
@@ -893,6 +892,7 @@ int openttd_main(int argc, char *argv[])
CrashLog::MainThreadExitCheckPendingCrashlog();
WaitTillSaved();
WaitTillGeneratedWorld(); // Make sure any generate world threads have been joined.
/* only save config if we have to */
if (save_config) {
@@ -931,12 +931,10 @@ exit_normal:
delete scanner;
#ifdef ENABLE_NETWORK
extern FILE *_log_fd;
if (_log_fd != NULL) {
fclose(_log_fd);
}
#endif /* ENABLE_NETWORK */
return ret;
}
@@ -971,19 +969,25 @@ static void MakeNewGameDone()
Company *c = Company::Get(COMPANY_FIRST);
c->settings = _settings_client.company;
/* Overwrite color from settings if needed
* COLOUR_END corresponds to Random colour */
if (_settings_client.gui.starting_colour != COLOUR_END) {
c->colour = _settings_client.gui.starting_colour;
ResetCompanyLivery(c);
_company_colours[c->index] = (Colours)c->colour;
}
IConsoleCmdExec("exec scripts/game_start.scr 0");
SetLocalCompany(COMPANY_FIRST);
InitializeRailGUI();
#ifdef ENABLE_NETWORK
/* We are the server, we start a new company (not dedicated),
* so set the default password *if* needed. */
if (_network_server && !StrEmpty(_settings_client.network.default_company_pass)) {
NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass);
}
#endif /* ENABLE_NETWORK */
if (_settings_client.gui.pause_on_newgame) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
@@ -1021,9 +1025,9 @@ static void MakeNewEditorWorld()
* Load the specified savegame but on error do different things.
* If loading fails due to corrupt savegame, bad version, etc. go back to
* a previous correct state. In the menu for example load the intro game again.
* @param mode mode of loading, either SL_LOAD or SL_OLD_LOAD
* @param newgm switch to this mode of loading fails due to some unknown error
* @param filename file to be loaded
* @param fop mode of loading, always SLO_LOAD
* @param newgm switch to this mode of loading fails due to some unknown error
* @param subdir default directory to look for filename, set to 0 if not needed
* @param lf Load filter to use, if NULL: use filename + subdir.
*/
@@ -1039,7 +1043,6 @@ bool SafeLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft,
case SL_OK: return true;
case SL_REINIT:
#ifdef ENABLE_NETWORK
if (_network_dedicated) {
/*
* We need to reinit a network map...
@@ -1055,7 +1058,6 @@ bool SafeLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft,
/* We can't load the intro game as server, so disconnect first. */
NetworkDisconnect();
}
#endif /* ENABLE_NETWORK */
switch (ogm) {
default:
@@ -1072,7 +1074,6 @@ bool SafeLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft,
void SwitchToMode(SwitchMode new_mode)
{
#ifdef ENABLE_NETWORK
/* If we are saving something, the network stays in his current state */
if (new_mode != SM_SAVE_GAME) {
/* If the network is active, make it not-active */
@@ -1101,7 +1102,7 @@ void SwitchToMode(SwitchMode new_mode)
}
}
}
#endif /* ENABLE_NETWORK */
/* Make sure all AI controllers are gone at quitting game */
if (new_mode != SM_SAVE_GAME) AI::KillAll();
@@ -1112,11 +1113,9 @@ void SwitchToMode(SwitchMode new_mode)
case SM_RESTARTGAME: // Restart --> 'Random game' with current settings
case SM_NEWGAME: // New Game --> 'Random game'
#ifdef ENABLE_NETWORK
if (_network_server) {
seprintf(_network_game_info.map_name, lastof(_network_game_info.map_name), "Random Map");
}
#endif /* ENABLE_NETWORK */
MakeNewGame(false, new_mode == SM_NEWGAME);
break;
@@ -1139,21 +1138,17 @@ void SwitchToMode(SwitchMode new_mode)
IConsoleCmdExec("exec scripts/game_start.scr 0");
/* Decrease pause counter (was increased from opening load dialog) */
DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE);
#ifdef ENABLE_NETWORK
if (_network_server) {
seprintf(_network_game_info.map_name, lastof(_network_game_info.map_name), "%s (Loaded game)", _file_to_saveload.title);
}
#endif /* ENABLE_NETWORK */
}
break;
}
case SM_START_HEIGHTMAP: // Load a heightmap and start a new game from it
#ifdef ENABLE_NETWORK
if (_network_server) {
seprintf(_network_game_info.map_name, lastof(_network_game_info.map_name), "%s (Heightmap)", _file_to_saveload.title);
}
#endif /* ENABLE_NETWORK */
MakeNewGame(true, true);
break;
@@ -1225,10 +1220,10 @@ static void CheckCaches()
if (_debug_desync_level <= 1) return;
/* Check the town caches. */
SmallVector<TownCache, 4> old_town_caches;
std::vector<TownCache> old_town_caches;
Town *t;
FOR_ALL_TOWNS(t) {
MemCpyT(old_town_caches.Append(), &t->cache);
old_town_caches.push_back(t->cache);
}
extern void RebuildTownCaches();
@@ -1237,23 +1232,23 @@ static void CheckCaches()
uint i = 0;
FOR_ALL_TOWNS(t) {
if (MemCmpT(old_town_caches.Get(i), &t->cache) != 0) {
if (MemCmpT(old_town_caches.data() + i, &t->cache) != 0) {
DEBUG(desync, 2, "town cache mismatch: town %i", (int)t->index);
}
i++;
}
/* Check company infrastructure cache. */
SmallVector<CompanyInfrastructure, 4> old_infrastructure;
std::vector<CompanyInfrastructure> old_infrastructure;
Company *c;
FOR_ALL_COMPANIES(c) MemCpyT(old_infrastructure.Append(), &c->infrastructure);
FOR_ALL_COMPANIES(c) old_infrastructure.push_back(c->infrastructure);
extern void AfterLoadCompanyStats();
AfterLoadCompanyStats();
i = 0;
FOR_ALL_COMPANIES(c) {
if (MemCmpT(old_infrastructure.Get(i), &c->infrastructure) != 0) {
if (MemCmpT(old_infrastructure.data() + i, &c->infrastructure) != 0) {
DEBUG(desync, 2, "infrastructure cache mismatch: company %i", (int)c->index);
}
i++;
@@ -1384,7 +1379,6 @@ void StateGameLoop()
#ifndef DEBUG_DUMP_COMMANDS
Game::GameLoop();
#endif
CallWindowTickEvent();
return;
}
@@ -1402,7 +1396,7 @@ void StateGameLoop()
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP);
UpdateLandscapingLimits();
CallWindowTickEvent();
CallWindowGameTickEvent();
NewsLoop();
} else {
if (_debug_desync_level > 2 && _date_fract == 0 && (_date & 0x1F) == 0) {
@@ -1427,12 +1421,15 @@ void StateGameLoop()
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP);
#ifndef DEBUG_DUMP_COMMANDS
AI::GameLoop();
Game::GameLoop();
{
PerformanceMeasurer framerate(PFE_ALLSCRIPTS);
AI::GameLoop();
Game::GameLoop();
}
#endif
UpdateLandscapingLimits();
CallWindowTickEvent();
CallWindowGameTickEvent();
NewsLoop();
cur_company.Restore();
}
@@ -1469,10 +1466,8 @@ static void DoAutosave()
void GameLoop()
{
if (_game_mode == GM_BOOTSTRAP) {
#ifdef ENABLE_NETWORK
/* Check for UDP stuff */
if (_network_available) NetworkBackgroundLoop();
#endif
InputLoop();
return;
}
@@ -1495,11 +1490,6 @@ void GameLoop()
IncreaseSpriteLRU();
InteractiveRandom();
extern int _caret_timer;
_caret_timer += 3;
CursorTick();
#ifdef ENABLE_NETWORK
/* Check for UDP stuff */
if (_network_available) NetworkBackgroundLoop();
@@ -1516,20 +1506,8 @@ void GameLoop()
StateGameLoop();
}
/* Check chat messages roughly once a second. */
static uint check_message = 0;
if (++check_message > 1000 / MILLISECONDS_PER_TICK) {
check_message = 0;
NetworkChatMessageLoop();
}
#else
StateGameLoop();
#endif /* ENABLE_NETWORK */
if (!_pause_mode && HasBit(_display_opt, DO_FULL_ANIMATION)) DoPaletteAnimations();
if (!_pause_mode || _game_mode == GM_EDITOR || _settings_game.construction.command_pause_level > CMDPL_NO_CONSTRUCTION) MoveAllTextEffects();
InputLoop();
SoundDriver::GetInstance()->MainLoop();