48 return recentFiles.at(index);
53 return QList<QString>(recentFiles);
58 return recentFiles.size();
68 auto eFrom = recentFiles.at(from);
69 auto eTo = recentFiles.at(to);
71 recentFiles.replace(from, eTo);
72 recentFiles.replace(to, eFrom);
74 processRecentFileChanges();
79 recentFiles.removeAt(ind);
80 processRecentFileChanges();
99 QString file{openFileDialog(
"Open Save File")};
138bool FileManagement::loadData(
const QString& filePath)
140 var8* newData{readSaveData(filePath)};
141 if(newData ==
nullptr) {
146 data->setData(newData);
151void FileManagement::reportLoadError()
156 case LoadErrorCannotOpen:
158 "This save file couldn't be opened.\n\n"
159 "It may be open in another program, or you may not have permission to "
160 "read it. Check that the file still exists and try again.";
163 case LoadErrorTooShort:
165 "This save file looks truncated or corrupted.\n\n"
166 "It's too small to be a valid save, so nothing was loaded and the "
167 "current file was left untouched.";
171 lastErrorMessage =
"This save file couldn't be loaded.";
180 return lastErrorMessage;
185 return lastErrorDetail;
191 recentFiles.prepend(path);
194 processRecentFileChanges();
200 if(path == this->path)
204 QString oldPath{this->path};
223 writeSaveData(path,
data->data);
229 QString filename{saveFileDialog(
"Save File As...")};
234 writeSaveData(filename,
data->data);
241 QString filename{saveFileDialog(
"Save Copy As...")};
246 writeSaveData(filename,
data->data);
252 data->resetData(
true);
258 processRecentFileChanges();
261void FileManagement::processRecentFileChanges()
265 QList<QString> newList;
267 for(
var8 i{0}; i < recentFiles.size(); ++i) {
268 QString file{recentFiles.at(i)};
269 file = file.trimmed();
270 if(file ==
"" || newList.contains(file))
273 newList.append(file);
283 recentFiles = newList;
286 QString compacted{newList.join(
';')};
293void FileManagement::pruneRecentFiles()
304 for(
const QString& file : recentFiles) {
306 if(probe.open(QIODevice::ReadOnly)) {
313 processRecentFileChanges();
316QString FileManagement::openFileDialog(QString title)
318 QString curPath{path};
323 return QFileDialog::getOpenFileName(
328 "Save Files (*.sav);;All Files (*)");
331QString FileManagement::saveFileDialog(QString title)
333 QString curPath{path};
338 return QFileDialog::getSaveFileName(
343 "Save Files (*.sav);;All Files (*)");
346var8* FileManagement::readSaveData(QString filePath)
348 lastError = LoadErrorNone;
349 lastErrorDetail.clear();
352 QFile file(filePath);
353 if(!file.open(QIODevice::ReadOnly)) {
354 lastError = LoadErrorCannotOpen;
355 lastErrorDetail = file.errorString();
362 const qint64 fileSize{file.size()};
364 lastError = LoadErrorTooShort;
365 lastErrorDetail = QStringLiteral(
"File is %1 bytes; a save must be at least %2 bytes (32 KB).")
372 QDataStream in(&file);
384 return reinterpret_cast<var8*
>(rawSaveData);
387void FileManagement::writeSaveData(QString filePath,
var8* data)
390 this->
data->toolset->recalcChecksums();
393 QFile file(filePath);
394 if(!file.open(QIODevice::WriteOnly))
396 QDataStream out(&file);
399 char* dataChar{
reinterpret_cast<char*
>(
data)};
405void FileManagement::expandRecentFiles(QString files)
409 QStringList recentFiles{files.split(
';')};
411 for(
var8 i{0}; i < recentFiles.size(); ++i) {
412 this->recentFiles.append(recentFiles[i]);
416 processRecentFileChanges();
void setPath(QString path)
Set the active path (backs the path property).
void newFile()
Start a fresh blank save.
int recentFilesMax()
The cap (MAX_RECENT_FILES).
QString getLastErrorDetail()
void clearRecentFiles()
Forget the entire recent-files list.
SaveFile * data
The live save file these operations load into / save from.
QString getLastErrorMessage()
bool saveFileAs()
Prompt for a new path and save there.
void recentFilesRemove(int ind)
Drop one entry from the recent list.
QList< QString > getRecentFiles()
The whole recent-files list.
protected::void pathChanged(QString newPath, QString oldPath)
The active path changed.
int recentFilesCount()
How many recent files are currently remembered.
void recentFilesChanged(QList< QString > files)
The recent-files list changed.
QString getRecentFile(int index=0)
Recent path at index (0 = most recent).
void addRecentFile(QString path)
Push a path onto the recent list (de-duped, capped).
virtual ~FileManagement()
void loadError()
A load failed on a file that exists (unreadable / truncated).
FileManagement(QObject *parent=nullptr)
Current file path. Setting it triggers a load via setPath().
void wipeUnusedSpace()
Zero out save regions that aren't meaningfully used.
bool openFileRecent(int index)
Open an entry from the recent-files list.
void reset()
Clear path + data back to a blank starting state.
void reopenFile()
Reload the current path from disk, discarding edits.
bool saveFile()
Save to the current path.
void recentFilesSwap(int from, int to)
Reorder the recent list (e.g. drag to reorder).
bool saveFileCopy()
Save a copy elsewhere without changing the active path.
QString getPath()
Current file path.
bool openFile()
Prompt for and open a save.
One loaded save: the raw 32 KB bytes, their expanded object tree, and the tools that move between the...
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
constexpr const char * KEY_LAST_FILE
QSettings key for the most-recent path.
constexpr var8 MAX_RECENT_FILES
How many recent paths to remember.
constexpr const char * KEY_RECENT_FILES
QSettings key for the recent-files list.
constexpr var16 SAV_DATA_SIZE
Size of a Gen 1 save in bytes (32 KB).