Fix local browser handling large amounts saves badly
So instead of loading every save in sight and rendering the thumbnails for them too, SaveButtons will only do this when they are actually visible, and unload saves and thumbnails when they are not. Also remove the "Rendering thumbnails" progress bar, which did absolutely nothing.
This commit is contained in:
parent
54cd259a18
commit
2cd1f7bad3
@ -1,30 +1,61 @@
|
||||
#include "SaveFile.h"
|
||||
#include "GameSave.h"
|
||||
#include "common/Platform.h"
|
||||
|
||||
SaveFile::SaveFile(SaveFile & save):
|
||||
gameSave(NULL),
|
||||
filename(save.filename),
|
||||
displayName(save.displayName),
|
||||
loadingError(save.loadingError)
|
||||
loadingError(save.loadingError),
|
||||
lazyLoad(save.lazyLoad)
|
||||
{
|
||||
if (save.gameSave)
|
||||
gameSave = new GameSave(*save.gameSave);
|
||||
}
|
||||
|
||||
SaveFile::SaveFile(ByteString filename):
|
||||
SaveFile::SaveFile(ByteString filename, bool newLazyLoad):
|
||||
gameSave(NULL),
|
||||
filename(filename),
|
||||
displayName(filename.FromUtf8()),
|
||||
loadingError("")
|
||||
loadingError(""),
|
||||
lazyLoad(newLazyLoad)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
GameSave * SaveFile::GetGameSave()
|
||||
{
|
||||
if (!gameSave && !loadingError.size() && lazyLoad)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::vector<char> data;
|
||||
if (Platform::ReadFile(data, filename))
|
||||
{
|
||||
gameSave = new GameSave(std::move(data));
|
||||
}
|
||||
else
|
||||
{
|
||||
loadingError = "cannot access file";
|
||||
}
|
||||
}
|
||||
catch(std::exception & e)
|
||||
{
|
||||
loadingError = ByteString(e.what()).FromUtf8();
|
||||
}
|
||||
}
|
||||
return gameSave;
|
||||
}
|
||||
|
||||
void SaveFile::LazyUnload()
|
||||
{
|
||||
if (lazyLoad && gameSave)
|
||||
{
|
||||
delete gameSave;
|
||||
gameSave = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void SaveFile::SetGameSave(GameSave * save)
|
||||
{
|
||||
gameSave = save;
|
||||
@ -61,6 +92,8 @@ void SaveFile::SetLoadingError(String error)
|
||||
}
|
||||
|
||||
SaveFile::~SaveFile() {
|
||||
delete gameSave;
|
||||
if (gameSave)
|
||||
{
|
||||
delete gameSave;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ class GameSave;
|
||||
class SaveFile {
|
||||
public:
|
||||
SaveFile(SaveFile & save);
|
||||
SaveFile(ByteString filename);
|
||||
SaveFile(ByteString filename, bool newLazyLoad = false);
|
||||
|
||||
GameSave * GetGameSave();
|
||||
void SetGameSave(GameSave * save);
|
||||
@ -19,12 +19,15 @@ public:
|
||||
String GetError();
|
||||
void SetLoadingError(String error);
|
||||
|
||||
void LazyUnload();
|
||||
|
||||
virtual ~SaveFile();
|
||||
private:
|
||||
GameSave * gameSave;
|
||||
ByteString filename;
|
||||
String displayName;
|
||||
String loadingError;
|
||||
bool lazyLoad;
|
||||
};
|
||||
|
||||
#endif /* SAVEFILE_H_ */
|
||||
|
@ -6,6 +6,13 @@
|
||||
#include "simulation/SaveRenderer.h"
|
||||
#include "client/GameSave.h"
|
||||
|
||||
int ThumbnailRendererTask::queueSize = 0;
|
||||
|
||||
int ThumbnailRendererTask::QueueSize()
|
||||
{
|
||||
return queueSize;
|
||||
}
|
||||
|
||||
ThumbnailRendererTask::ThumbnailRendererTask(GameSave *save, int width, int height, bool autoRescale, bool decorations, bool fire) :
|
||||
Save(new GameSave(*save)),
|
||||
Width(width),
|
||||
@ -14,10 +21,12 @@ ThumbnailRendererTask::ThumbnailRendererTask(GameSave *save, int width, int heig
|
||||
Fire(fire),
|
||||
AutoRescale(autoRescale)
|
||||
{
|
||||
queueSize += 1;
|
||||
}
|
||||
|
||||
ThumbnailRendererTask::~ThumbnailRendererTask()
|
||||
{
|
||||
queueSize -= 1;
|
||||
}
|
||||
|
||||
bool ThumbnailRendererTask::doWork()
|
||||
|
@ -16,12 +16,16 @@ class ThumbnailRendererTask : public AbandonableTask
|
||||
bool AutoRescale;
|
||||
std::unique_ptr<VideoBuffer> thumbnail;
|
||||
|
||||
static int queueSize;
|
||||
|
||||
public:
|
||||
ThumbnailRendererTask(GameSave *save, int width, int height, bool autoRescale = false, bool decorations = true, bool fire = true);
|
||||
virtual ~ThumbnailRendererTask();
|
||||
|
||||
virtual bool doWork() override;
|
||||
std::unique_ptr<VideoBuffer> Finish();
|
||||
|
||||
static int QueueSize();
|
||||
};
|
||||
|
||||
#endif // THUMBNAILRENDERER_H
|
||||
|
@ -43,24 +43,12 @@ class LoadFilesTask: public Task
|
||||
notifyProgress(-1);
|
||||
for(std::vector<ByteString>::iterator iter = files.begin(), end = files.end(); iter != end; ++iter)
|
||||
{
|
||||
SaveFile * saveFile = new SaveFile(directory + *iter);
|
||||
try
|
||||
{
|
||||
std::vector<char> data;
|
||||
if (!Platform::ReadFile(data, directory + *iter))
|
||||
continue;
|
||||
GameSave * tempSave = new GameSave(std::move(data));
|
||||
saveFile->SetGameSave(tempSave);
|
||||
saveFiles.push_back(saveFile);
|
||||
SaveFile * saveFile = new SaveFile(directory + *iter, true);
|
||||
saveFiles.push_back(saveFile);
|
||||
|
||||
ByteString filename = (*iter).SplitFromEndBy(PATH_SEP).After();
|
||||
filename = filename.SplitFromEndBy('.').Before();
|
||||
saveFile->SetDisplayName(filename.FromUtf8());
|
||||
}
|
||||
catch(std::exception & e)
|
||||
{
|
||||
//:(
|
||||
}
|
||||
ByteString filename = (*iter).SplitFromEndBy(PATH_SEP).After();
|
||||
filename = filename.SplitFromEndBy('.').Before();
|
||||
saveFile->SetDisplayName(filename.FromUtf8());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -266,7 +254,7 @@ void FileBrowserActivity::OnTick(float dt)
|
||||
if(loadFiles)
|
||||
loadFiles->Poll();
|
||||
|
||||
if(files.size())
|
||||
while(files.size())
|
||||
{
|
||||
SaveFile * saveFile = files.back();
|
||||
files.pop_back();
|
||||
@ -296,7 +284,7 @@ void FileBrowserActivity::OnTick(float dt)
|
||||
componentsQueue.push_back(saveButton);
|
||||
fileX++;
|
||||
}
|
||||
else if(componentsQueue.size())
|
||||
if(componentsQueue.size())
|
||||
{
|
||||
for(std::vector<ui::Component*>::iterator iter = componentsQueue.begin(), end = componentsQueue.end(); iter != end; ++iter)
|
||||
{
|
||||
|
@ -126,7 +126,7 @@ void SaveButton::Tick(float dt)
|
||||
{
|
||||
if (!thumbnail)
|
||||
{
|
||||
if (!triedThumbnail)
|
||||
if (!triedThumbnail && wantsDraw && ThumbnailRendererTask::QueueSize() < 10)
|
||||
{
|
||||
float scaleFactor = (Size.Y-25)/((float)YRES);
|
||||
ui::Point thumbBoxSize = ui::Point(int(XRES*scaleFactor), int(YRES*scaleFactor));
|
||||
@ -170,6 +170,14 @@ void SaveButton::Tick(float dt)
|
||||
thumbSize = ui::Point(thumbnail->Width, thumbnail->Height);
|
||||
}
|
||||
}
|
||||
if (!wantsDraw && !thumbnailRenderer)
|
||||
{
|
||||
file->LazyUnload();
|
||||
thumbnail.reset();
|
||||
thumbSize = { 0, 0 };
|
||||
triedThumbnail = false;
|
||||
}
|
||||
wantsDraw = false;
|
||||
}
|
||||
|
||||
void SaveButton::Draw(const Point& screenPos)
|
||||
|
Reference in New Issue
Block a user