// MassBuilderSaveTool // Copyright (C) 2021-2022 Guillaume Jacquemin // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . #include #include #include #include #include "SaveTool.h" void SaveTool::handleFileAction(efsw::WatchID watch_id, const std::string&, const std::string& filename, efsw::Action action, std::string old_filename) { SDL_Event event; SDL_zero(event); event.type = _fileEventId; if(watch_id == _watchIDs[StagingDir] && Utility::String::endsWith(filename, ".sav")) { event.user.code = StagedUpdate; SDL_PushEvent(&event); return; } if(Utility::String::endsWith(filename, "Config.sav")) { return; } // TODO: actually do something when config files will finally be handled if(!Utility::String::endsWith(filename, _currentProfile->account() + ".sav")) { return; } event.user.code = action; event.user.data1 = Containers::String{Containers::AllocatedInit, filename.c_str()}.release(); if(action == efsw::Actions::Moved) { event.user.data2 = Containers::String{Containers::AllocatedInit, old_filename.c_str()}.release(); } SDL_PushEvent(&event); } void SaveTool::fileUpdateEvent(SDL_Event& event) { if(event.user.code == StagedUpdate) { _massManager->refreshStagedMasses(); return; } Containers::String filename{static_cast(event.user.data1), std::strlen(static_cast(event.user.data1)), nullptr}; Containers::String old_filename; Int index = 0; Int old_index = 0; bool is_current_profile = filename == _currentProfile->filename(); bool is_unit = filename.hasPrefix(_currentProfile->isDemo() ? "DemoUnit"_s : "Unit"_s); if(is_unit) { index = ((filename[_currentProfile->isDemo() ? 8 : 4] - 0x30) * 10) + (filename[_currentProfile->isDemo() ? 9 : 5] - 0x30); } if(event.user.code == FileMoved) { old_filename = Containers::String{static_cast(event.user.data2), std::strlen(static_cast(event.user.data2)), nullptr}; old_index = ((old_filename[_currentProfile->isDemo() ? 8 : 4] - 0x30) * 10) + (old_filename[_currentProfile->isDemo() ? 9 : 5] - 0x30); } switch(event.user.code) { case FileAdded: if(is_unit) { if(!_currentMass || _currentMass != &(_massManager->hangar(index))) { _massManager->refreshHangar(index); } else { _currentMass->setDirty(); } } break; case FileDeleted: if(is_current_profile) { _currentProfile = nullptr; _uiState = UiState::ProfileManager; _profileManager->refreshProfiles(); } else if(is_unit) { if(!_currentMass || _currentMass != &(_massManager->hangar(index))) { _massManager->refreshHangar(index); } } break; case FileModified: if(is_current_profile) { _currentProfile->refreshValues(); } else if(is_unit) { if(!_currentMass || _currentMass != &(_massManager->hangar(index))) { _massManager->refreshHangar(index); } else { if(_modifiedBySaveTool && _currentMass->filename() == filename) { auto handle = CreateFileW(Utility::Unicode::widen(Containers::StringView{filename}).data(), GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr); if(handle && handle != INVALID_HANDLE_VALUE) { CloseHandle(handle); _modifiedBySaveTool = false; } } else { _currentMass->setDirty(); } } } break; case FileMoved: if(is_unit) { if(old_filename.hasSuffix(".sav"_s)) { _massManager->refreshHangar(index); _massManager->refreshHangar(old_index); } } break; default: _queue.addToast(Toast::Type::Warning, "Unknown file action type"_s); } }