From be3f236eafbb1990f6db3aa57d244a9c5621c291 Mon Sep 17 00:00:00 2001 From: pelya Date: Tue, 26 Aug 2014 02:04:54 +0300 Subject: [PATCH] Cloudsave support --- src/fileio.cpp | 2 +- src/fios_gui.cpp | 31 +++++++++++++++++++++++++++++++ src/openttd.cpp | 22 ++++++++++++++++++++++ src/screenshot.cpp | 2 +- src/table/gameopt_settings.ini | 4 ++-- 5 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/fileio.cpp b/src/fileio.cpp index 94c6adcbad..467ea0fb92 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -293,7 +293,7 @@ static const char * const _subdirs[] = { "game" PATHSEP, "game" PATHSEP "library" PATHSEP, #ifdef __ANDROID__ - "../../../../../../Pictures/", + "screenshot" PATHSEP, #else "screenshot" PATHSEP, #endif diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 5d4ef0a6de..758268749f 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -35,12 +35,16 @@ #include "table/strings.h" #include "safeguards.h" +#ifdef __ANDROID__ +#include +#endif SaveLoadDialogMode _saveload_mode; LoadCheckData _load_check_data; ///< Data loaded from save during SL_LOAD_CHECK. static bool _fios_path_changed; static bool _savegame_sort_dirty; +static const char *NETWORK_SAVE_FILENAME = "network-save.sav"; /** @@ -285,6 +289,7 @@ public: this->FinishInitNested(0); this->LowerWidget(WID_SL_DRIVES_DIRECTORIES_LIST); + if (mode == SLD_SAVE_GAME) this->SetWidgetLoweredState(WID_SL_SAVE_NETWORK_BUTTON, _settings_client.gui.save_to_network); /* pause is only used in single-player, non-editor mode, non-menu mode. It * will be unpaused in the WE_DESTROY event handler. */ @@ -627,6 +632,32 @@ public: /* Note, this is also called via the OSK; and we need to lower the button. */ this->HandleButtonClick(WID_SL_SAVE_GAME); break; + + case WID_SL_SAVE_NETWORK_BUTTON: + _settings_client.gui.save_to_network = !_settings_client.gui.save_to_network; + this->SetWidgetLoweredState(WID_SL_SAVE_NETWORK_BUTTON, _settings_client.gui.save_to_network); + this->SetDirty(); + break; + + case WID_SL_LOAD_NETWORK_BUTTON: { + char savePath[PATH_MAX]; + FiosMakeSavegameName(savePath, NETWORK_SAVE_FILENAME, sizeof(savePath)); +#ifdef __ANDROID__ + if (!SDL_ANDROID_CloudLoad(savePath, NULL, "OpenTTD")) { + break; + } +#endif + _load_check_data.Clear(); + SaveOrLoadResult res = SaveOrLoad(savePath, SL_LOAD_CHECK, SAVE_DIR, false); + if (res == SL_OK && !_load_check_data.HasErrors()) { + strecpy(_file_to_saveload.name, savePath, lastof(_file_to_saveload.name)); + strecpy(_file_to_saveload.title, "", lastof(_file_to_saveload.title)); + if (!_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs()) { + _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_SCENARIO : SM_LOAD_GAME; + } + } + break; + } } } diff --git a/src/openttd.cpp b/src/openttd.cpp index c430cf67e3..bc5b8cbb82 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -69,6 +69,9 @@ #include #include "safeguards.h" +#ifdef __ANDROID__ +#include +#endif void CallLandscapeTick(); void IncreaseDate(); @@ -81,6 +84,8 @@ bool HandleBootstrap(); extern Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY); extern void ShowOSErrorBox(const char *buf, bool system); extern char *_config_file; +const char *NETWORK_SAVE_SCREENSHOT_FILE = "OpenTTD-network-save"; +const char *NETWORK_SAVE_SCREENSHOT_FILE_PNG = "OpenTTD-network-save.png"; /** * Error handling for fatal user errors. @@ -1161,6 +1166,23 @@ void SwitchToMode(SwitchMode new_mode) ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR); } else { DeleteWindowById(WC_SAVELOAD, 0); +#ifdef __ANDROID__ + if (_settings_client.gui.save_to_network) { + char screenshotFile[PATH_MAX] = ""; + const char* lastPart = strrchr(_file_to_saveload.name, PATHSEPCHAR); + if (!lastPart) { + lastPart = _file_to_saveload.name; + } else { + lastPart++; + } + MakeScreenshot(SC_VIEWPORT, NETWORK_SAVE_SCREENSHOT_FILE); + FioFindFullPath(screenshotFile, sizeof(screenshotFile), SCREENSHOT_DIR, NETWORK_SAVE_SCREENSHOT_FILE_PNG); + int ret = SDL_ANDROID_CloudSave(_file_to_saveload.name, lastPart, "OpenTTD", lastPart, screenshotFile, _date); + if (_settings_client.gui.save_to_network == 2) { + _settings_client.gui.save_to_network = ret ? 1 : 0; + } + } +#endif } break; diff --git a/src/screenshot.cpp b/src/screenshot.cpp index 0974de62b5..2022dca461 100644 --- a/src/screenshot.cpp +++ b/src/screenshot.cpp @@ -871,7 +871,7 @@ bool MakeScreenshot(ScreenshotType t, const char *name) if (ret) { SetDParamStr(0, _screenshot_name); - ShowErrorMessage(STR_MESSAGE_SCREENSHOT_SUCCESSFULLY, INVALID_STRING_ID, WL_WARNING); + //ShowErrorMessage(STR_MESSAGE_SCREENSHOT_SUCCESSFULLY, INVALID_STRING_ID, WL_WARNING); // No need for message when we're doing cloudsave } else { ShowErrorMessage(STR_ERROR_SCREENSHOT_FAILED, INVALID_STRING_ID, WL_ERROR); } diff --git a/src/table/gameopt_settings.ini b/src/table/gameopt_settings.ini index 30b5f667a2..762ccaa3f2 100644 --- a/src/table/gameopt_settings.ini +++ b/src/table/gameopt_settings.ini @@ -19,7 +19,7 @@ static const char *_locale_units = "imperial|metric|si"; static const char *_town_names = "english|french|german|american|latin|silly|swedish|dutch|finnish|polish|slovak|norwegian|hungarian|austrian|romanian|czech|swiss|danish|turkish|italian|catalan"; static const char *_climates = "temperate|arctic|tropic|toyland"; static const char *_autosave_interval = "off|monthly|quarterly|half year|yearly"; -static const char *_save_to_network = "ask|enabled|disabled"; +static const char *_save_to_network = "disabled|enabled|ask"; static const char *_roadsides = "left|right"; static const char *_savegame_date = "long|short|iso"; #ifdef ENABLE_NETWORK @@ -178,7 +178,7 @@ cat = SC_BASIC var = gui.save_to_network type = SLE_UINT8 flags = SLF_NOT_IN_SAVE | SLF_NO_NETWORK_SYNC -def = 0 +def = 2 max = 2 full = _save_to_network cat = SC_BASIC