1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-21 17:17:06 +02:00

Merge pull request #4857 from Laserlicht/delete

Delete saves / random maps
This commit is contained in:
Ivan Savenko 2024-11-16 17:28:23 +02:00 committed by GitHub
commit c3b63a1bef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 178 additions and 19 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 997 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 986 B

View File

@ -0,0 +1,8 @@
{
"basepath" : "lobby/",
"images" :
[
{ "frame" : 0, "file" : "delete-normal.png"},
{ "frame" : 1, "file" : "delete-pressed.png"}
]
}

View File

@ -114,6 +114,12 @@
"vcmi.lobby.handicap.resource" : "Gives players appropriate resources to start with in addition to the normal starting resources. Negative values are allowed, but are limited to 0 in total (the player never starts with negative resources).",
"vcmi.lobby.handicap.income" : "Changes the player's various incomes by the percentage. Is rounded up.",
"vcmi.lobby.handicap.growth" : "Changes the growth rate of creatures in the towns owned by the player. Is rounded up.",
"vcmi.lobby.deleteUnsupportedSave" : "{Unsupported saves found}\n\nVCMI has found %d saved games that are no longer supported, possibly due to differences in VCMI versions.\n\nDo you want to delete them?",
"vcmi.lobby.deleteSaveGameTitle" : "Select a Saved Game to delete",
"vcmi.lobby.deleteMapTitle" : "Select a Scenario to delete",
"vcmi.lobby.deleteFile" : "Do you want to delete following file?",
"vcmi.lobby.deleteFolder" : "Do you want to delete following folder?",
"vcmi.lobby.deleteMode" : "Switch to delete mode and back",
"vcmi.lobby.login.title" : "VCMI Online Lobby",
"vcmi.lobby.login.username" : "Username:",

View File

@ -226,7 +226,7 @@ void ApplyOnLobbyScreenNetPackVisitor::visitLobbyUpdateState(LobbyUpdateState &
else
lobby->updateAfterStateChange();
if(pack.hostChanged)
if(pack.hostChanged || pack.refreshList)
lobby->toggleMode(handler.isHost());
}

View File

@ -42,9 +42,11 @@
#include "../../lib/mapping/CMapInfo.h"
#include "../../lib/mapping/CMapHeader.h"
#include "../../lib/mapping/MapFormat.h"
#include "../../lib/networkPacks/PacksForLobby.h"
#include "../../lib/texts/CGeneralTextHandler.h"
#include "../../lib/texts/TextOperations.h"
#include "../../lib/TerrainHandler.h"
#include "../../lib/UnlockGuard.h"
bool mapSorter::operator()(const std::shared_ptr<ElementInfo> aaa, const std::shared_ptr<ElementInfo> bbb)
{
@ -152,7 +154,7 @@ static ESortBy getSortBySelectionScreen(ESelectionScreen Type)
}
SelectionTab::SelectionTab(ESelectionScreen Type)
: CIntObject(LCLICK | SHOW_POPUP | KEYBOARD | DOUBLECLICK), callOnSelect(nullptr), tabType(Type), selectionPos(0), sortModeAscending(true), inputNameRect{32, 539, 350, 20}, curFolder(""), currentMapSizeFilter(0), showRandom(false)
: CIntObject(LCLICK | SHOW_POPUP | KEYBOARD | DOUBLECLICK), callOnSelect(nullptr), tabType(Type), selectionPos(0), sortModeAscending(true), inputNameRect{32, 539, 350, 20}, curFolder(""), currentMapSizeFilter(0), showRandom(false), deleteMode(false)
{
OBJECT_CONSTRUCTION;
@ -192,20 +194,23 @@ SelectionTab::SelectionTab(ESelectionScreen Type)
int positionsToShow = 18;
std::string tabTitle;
std::string tabTitleDelete;
switch(tabType)
{
case ESelectionScreen::newGame:
tabTitle = CGI->generaltexth->arraytxt[229];
tabTitle = "{" + CGI->generaltexth->arraytxt[229] + "}";
tabTitleDelete = "{red|" + CGI->generaltexth->translate("vcmi.lobby.deleteMapTitle") + "}";
break;
case ESelectionScreen::loadGame:
tabTitle = CGI->generaltexth->arraytxt[230];
tabTitle = "{" + CGI->generaltexth->arraytxt[230] + "}";
tabTitleDelete = "{red|" + CGI->generaltexth->translate("vcmi.lobby.deleteSaveGameTitle") + "}";
break;
case ESelectionScreen::saveGame:
positionsToShow = 16;
tabTitle = CGI->generaltexth->arraytxt[231];
tabTitle = "{" + CGI->generaltexth->arraytxt[231] + "}";
break;
case ESelectionScreen::campaignList:
tabTitle = CGI->generaltexth->allTexts[726];
tabTitle = "{" + CGI->generaltexth->allTexts[726] + "}";
setRedrawParent(true); // we use parent background so we need to make sure it's will be redrawn too
pos.w = parent->pos.w;
pos.h = parent->pos.h;
@ -225,12 +230,26 @@ SelectionTab::SelectionTab(ESelectionScreen Type)
auto sortByDate = std::make_shared<CButton>(Point(371, 85), AnimationPath::builtin("selectionTabSortDate"), CButton::tooltip("", CGI->generaltexth->translate("vcmi.lobby.sortDate")), std::bind(&SelectionTab::sortBy, this, ESortBy::_changeDate), EShortcut::MAPS_SORT_CHANGEDATE);
sortByDate->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("lobby/selectionTabSortDate")));
buttonsSortBy.push_back(sortByDate);
if(tabType == ESelectionScreen::loadGame || tabType == ESelectionScreen::newGame)
{
buttonDeleteMode = std::make_shared<CButton>(Point(367, 18), AnimationPath::builtin("lobby/deleteButton"), CButton::tooltip("", CGI->generaltexth->translate("vcmi.lobby.deleteMode")), [this, tabTitle, tabTitleDelete](){
deleteMode = !deleteMode;
if(deleteMode)
labelTabTitle->setText(tabTitleDelete);
else
labelTabTitle->setText(tabTitle);
});
if(tabType == ESelectionScreen::newGame)
buttonDeleteMode->setEnabled(false);
}
}
for(int i = 0; i < positionsToShow; i++)
listItems.push_back(std::make_shared<ListItem>(Point(30, 129 + i * 25)));
labelTabTitle = std::make_shared<CLabel>(205, 28, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, tabTitle);
labelTabTitle = std::make_shared<CLabel>(205, 28, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, tabTitle);
slider = std::make_shared<CSlider>(Point(372, 86 + (enableUiEnhancements ? 30 : 0)), (tabType != ESelectionScreen::saveGame ? 480 : 430) - (enableUiEnhancements ? 30 : 0), std::bind(&SelectionTab::sliderMove, this, _1), positionsToShow, (int)curItems.size(), 0, Orientation::VERTICAL, CSlider::BLUE);
slider->setPanningStep(24);
@ -242,10 +261,10 @@ SelectionTab::SelectionTab(ESelectionScreen Type)
void SelectionTab::toggleMode()
{
allItems.clear();
curItems.clear();
if(CSH->isGuest())
{
allItems.clear();
curItems.clear();
if(slider)
slider->block(true);
}
@ -263,9 +282,12 @@ void SelectionTab::toggleMode()
}
case ESelectionScreen::loadGame:
inputName->disable();
parseSaves(getFiles("Saves/", EResType::SAVEGAME));
break;
{
inputName->disable();
auto unsupported = parseSaves(getFiles("Saves/", EResType::SAVEGAME));
handleUnsupportedSavegames(unsupported);
break;
}
case ESelectionScreen::saveGame:
parseSaves(getFiles("Saves/", EResType::SAVEGAME));
@ -309,7 +331,35 @@ void SelectionTab::clickReleased(const Point & cursorPosition)
if(line != -1 && curItems.size() > line)
{
select(line);
if(!deleteMode)
select(line);
else
{
int py = line + slider->getValue();
vstd::amax(py, 0);
vstd::amin(py, curItems.size() - 1);
if(curItems[py]->isFolder && boost::algorithm::starts_with(curItems[py]->folderName, ".."))
{
select(line);
return;
}
if(!curItems[py]->isFolder)
CInfoWindow::showYesNoDialog(CGI->generaltexth->translate("vcmi.lobby.deleteFile") + "\n\n" + curItems[py]->fullFileURI, std::vector<std::shared_ptr<CComponent>>(), [this, py](){
LobbyDelete ld;
ld.type = tabType == ESelectionScreen::newGame ? LobbyDelete::EType::RANDOMMAP : LobbyDelete::EType::SAVEGAME;
ld.name = curItems[py]->fileURI;
CSH->sendLobbyPack(ld);
}, nullptr);
else
CInfoWindow::showYesNoDialog(CGI->generaltexth->translate("vcmi.lobby.deleteFolder") + "\n\n" + curFolder + curItems[py]->folderName, std::vector<std::shared_ptr<CComponent>>(), [this, py](){
LobbyDelete ld;
ld.type = LobbyDelete::EType::SAVEGAME_FOLDER;
ld.name = curFolder + curItems[py]->folderName;
CSH->sendLobbyPack(ld);
}, nullptr);
}
}
#ifdef VCMI_MOBILE
// focus input field if clicked inside it
@ -475,6 +525,9 @@ void SelectionTab::filter(int size, bool selectFirst)
curItems.clear();
if(buttonDeleteMode)
buttonDeleteMode->setEnabled(tabType != ESelectionScreen::newGame || showRandom);
for(auto elem : allItems)
{
if((elem->mapHeader && (!size || elem->mapHeader->width == size)) || tabType == ESelectionScreen::campaignList)
@ -826,8 +879,10 @@ void SelectionTab::parseMaps(const std::unordered_set<ResourcePath> & files)
}
}
void SelectionTab::parseSaves(const std::unordered_set<ResourcePath> & files)
std::vector<ResourcePath> SelectionTab::parseSaves(const std::unordered_set<ResourcePath> & files)
{
std::vector<ResourcePath> unsupported;
for(auto & file : files)
{
try
@ -866,10 +921,35 @@ void SelectionTab::parseSaves(const std::unordered_set<ResourcePath> & files)
allItems.push_back(mapInfo);
}
catch(const std::exception & e)
catch(const IdentifierResolutionException & e)
{
logGlobal->error("Error: Failed to process %s: %s", file.getName(), e.what());
}
catch(const std::exception & e)
{
unsupported.push_back(file); // IdentifierResolutionException is not relevant -> not ask to delete, when mods are disabled
logGlobal->error("Error: Failed to process %s: %s", file.getName(), e.what());
}
}
return unsupported;
}
void SelectionTab::handleUnsupportedSavegames(const std::vector<ResourcePath> & files)
{
if(CSH->isHost() && files.size())
{
MetaString text = MetaString::createFromTextID("vcmi.lobby.deleteUnsupportedSave");
text.replaceNumber(files.size());
CInfoWindow::showYesNoDialog(text.toString(), std::vector<std::shared_ptr<CComponent>>(), [files](){
for(auto & file : files)
{
LobbyDelete ld;
ld.type = LobbyDelete::EType::SAVEGAME;
ld.name = file.getName();
CSH->sendLobbyPack(ld);
}
}, nullptr);
}
}

View File

@ -72,6 +72,8 @@ class SelectionTab : public CIntObject
// FIXME: CSelectionBase use them too!
std::shared_ptr<CAnimation> iconsVictoryCondition;
std::shared_ptr<CAnimation> iconsLossCondition;
std::vector<std::shared_ptr<ListItem>> unSupportedSaves;
public:
std::vector<std::shared_ptr<ElementInfo>> allItems;
std::vector<std::shared_ptr<ElementInfo>> curItems;
@ -120,11 +122,16 @@ private:
ESelectionScreen tabType;
Rect inputNameRect;
std::shared_ptr<CButton> buttonDeleteMode;
bool deleteMode;
auto checkSubfolder(std::string path);
bool isMapSupported(const CMapInfo & info);
void parseMaps(const std::unordered_set<ResourcePath> & files);
void parseSaves(const std::unordered_set<ResourcePath> & files);
std::vector<ResourcePath> parseSaves(const std::unordered_set<ResourcePath> & files);
void parseCampaigns(const std::unordered_set<ResourcePath> & files);
std::unordered_set<ResourcePath> getFiles(std::string dirURI, EResType resType);
void handleUnsupportedSavegames(const std::vector<ResourcePath> & files);
};

View File

@ -178,6 +178,7 @@ public:
virtual void visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack) {}
virtual void visitLobbyShowMessage(LobbyShowMessage & pack) {}
virtual void visitLobbyPvPAction(LobbyPvPAction & pack) {}
virtual void visitLobbyDelete(LobbyDelete & pack) {}
virtual void visitSaveLocalState(SaveLocalState & pack) {}
};

View File

@ -842,6 +842,11 @@ void LobbyPvPAction::visitTyped(ICPackVisitor & visitor)
visitor.visitLobbyPvPAction(*this);
}
void LobbyDelete::visitTyped(ICPackVisitor & visitor)
{
visitor.visitLobbyDelete(*this);
}
void SetResources::applyGs(CGameState *gs)
{
assert(player.isValidPlayer());

View File

@ -170,12 +170,14 @@ struct DLL_LINKAGE LobbyUpdateState : public CLobbyPackToPropagate
{
LobbyState state;
bool hostChanged = false; // Used on client-side only
bool refreshList = false;
void visitTyped(ICPackVisitor & visitor) override;
template <typename Handler> void serialize(Handler &h)
{
h & state;
h & refreshList;
}
};
@ -381,4 +383,22 @@ struct DLL_LINKAGE LobbyPvPAction : public CLobbyPackToServer
}
};
struct DLL_LINKAGE LobbyDelete : public CLobbyPackToServer
{
enum class EType : ui8 {
SAVEGAME, SAVEGAME_FOLDER, RANDOMMAP
};
EType type = EType::SAVEGAME;
std::string name;
void visitTyped(ICPackVisitor & visitor) override;
template <typename Handler> void serialize(Handler &h)
{
h & type;
h & name;
}
};
VCMI_LIB_NAMESPACE_END

View File

@ -66,7 +66,7 @@ enum class ESerializationVersion : int32_t
REMOVE_TOWN_PTR, // 867 - removed pointer to CTown from CGTownInstance
REMOVE_OBJECT_TYPENAME, // 868 - remove typename from CGObjectInstance
REMOVE_VLC_POINTERS, // 869 removed remaining pointers to VLC entities
FOLDER_NAME_REWORK, // 870 - rework foldername
FOLDER_NAME_REWORK, // 870 - rework foldername
CURRENT = FOLDER_NAME_REWORK
};

View File

@ -294,6 +294,7 @@ void registerTypes(Serializer &s)
s.template registerType<SpellResearch>(241);
s.template registerType<SetResearchedSpells>(242);
s.template registerType<SaveLocalState>(243);
s.template registerType<LobbyDelete>(244);
}
VCMI_LIB_NAMESPACE_END

View File

@ -39,6 +39,7 @@ public:
void visitLobbyChatMessage(LobbyChatMessage & pack) override;
void visitLobbyGuiAction(LobbyGuiAction & pack) override;
void visitLobbyPvPAction(LobbyPvPAction & pack) override;
void visitLobbyDelete(LobbyDelete & pack) override;
};
class ApplyOnServerAfterAnnounceNetPackVisitor : public VCMI_LIB_WRAP_NAMESPACE(ICPackVisitor)
@ -96,4 +97,5 @@ public:
void visitLobbySetDifficulty(LobbySetDifficulty & pack) override;
void visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack) override;
void visitLobbyPvPAction(LobbyPvPAction & pack) override;
void visitLobbyDelete(LobbyDelete & pack) override;
};

View File

@ -22,6 +22,7 @@
#include "../lib/serializer/Connection.h"
#include "../lib/mapping/CMapInfo.h"
#include "../lib/mapping/CMapHeader.h"
#include "../lib/filesystem/Filesystem.h"
void ClientPermissionsCheckerNetPackVisitor::visitForLobby(CPackForLobby & pack)
{
@ -383,7 +384,6 @@ void ApplyOnServerNetPackVisitor::visitLobbyForceSetPlayer(LobbyForceSetPlayer &
result = true;
}
void ClientPermissionsCheckerNetPackVisitor::visitLobbyPvPAction(LobbyPvPAction & pack)
{
result = true;
@ -433,3 +433,32 @@ void ApplyOnServerNetPackVisitor::visitLobbyPvPAction(LobbyPvPAction & pack)
}
result = true;
}
void ClientPermissionsCheckerNetPackVisitor::visitLobbyDelete(LobbyDelete & pack)
{
result = srv.isClientHost(pack.c->connectionID);
}
void ApplyOnServerNetPackVisitor::visitLobbyDelete(LobbyDelete & pack)
{
if(pack.type == LobbyDelete::EType::SAVEGAME || pack.type == LobbyDelete::EType::RANDOMMAP)
{
auto res = ResourcePath(pack.name, pack.type == LobbyDelete::EType::SAVEGAME ? EResType::SAVEGAME : EResType::MAP);
auto file = boost::filesystem::canonical(*CResourceHandler::get()->getResourceName(res));
boost::filesystem::remove(file);
if(boost::filesystem::is_empty(file.parent_path()))
boost::filesystem::remove(file.parent_path());
}
else if(pack.type == LobbyDelete::EType::SAVEGAME_FOLDER)
{
auto res = ResourcePath("Saves/" + pack.name, EResType::DIRECTORY);
auto folder = boost::filesystem::canonical(*CResourceHandler::get()->getResourceName(res));
boost::filesystem::remove_all(folder);
}
LobbyUpdateState lus;
lus.state = srv;
lus.refreshList = true;
srv.announcePack(lus);
}