mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-08-29 10:49:53 +02:00
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:
@@ -1,30 +1,61 @@
|
|||||||
#include "SaveFile.h"
|
#include "SaveFile.h"
|
||||||
#include "GameSave.h"
|
#include "GameSave.h"
|
||||||
|
#include "common/Platform.h"
|
||||||
|
|
||||||
SaveFile::SaveFile(SaveFile & save):
|
SaveFile::SaveFile(SaveFile & save):
|
||||||
gameSave(NULL),
|
gameSave(NULL),
|
||||||
filename(save.filename),
|
filename(save.filename),
|
||||||
displayName(save.displayName),
|
displayName(save.displayName),
|
||||||
loadingError(save.loadingError)
|
loadingError(save.loadingError),
|
||||||
|
lazyLoad(save.lazyLoad)
|
||||||
{
|
{
|
||||||
if (save.gameSave)
|
if (save.gameSave)
|
||||||
gameSave = new GameSave(*save.gameSave);
|
gameSave = new GameSave(*save.gameSave);
|
||||||
}
|
}
|
||||||
|
|
||||||
SaveFile::SaveFile(ByteString filename):
|
SaveFile::SaveFile(ByteString filename, bool newLazyLoad):
|
||||||
gameSave(NULL),
|
gameSave(NULL),
|
||||||
filename(filename),
|
filename(filename),
|
||||||
displayName(filename.FromUtf8()),
|
displayName(filename.FromUtf8()),
|
||||||
loadingError("")
|
loadingError(""),
|
||||||
|
lazyLoad(newLazyLoad)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GameSave * SaveFile::GetGameSave()
|
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;
|
return gameSave;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SaveFile::LazyUnload()
|
||||||
|
{
|
||||||
|
if (lazyLoad && gameSave)
|
||||||
|
{
|
||||||
|
delete gameSave;
|
||||||
|
gameSave = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void SaveFile::SetGameSave(GameSave * save)
|
void SaveFile::SetGameSave(GameSave * save)
|
||||||
{
|
{
|
||||||
gameSave = save;
|
gameSave = save;
|
||||||
@@ -61,6 +92,8 @@ void SaveFile::SetLoadingError(String error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
SaveFile::~SaveFile() {
|
SaveFile::~SaveFile() {
|
||||||
|
if (gameSave)
|
||||||
|
{
|
||||||
delete gameSave;
|
delete gameSave;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@@ -8,7 +8,7 @@ class GameSave;
|
|||||||
class SaveFile {
|
class SaveFile {
|
||||||
public:
|
public:
|
||||||
SaveFile(SaveFile & save);
|
SaveFile(SaveFile & save);
|
||||||
SaveFile(ByteString filename);
|
SaveFile(ByteString filename, bool newLazyLoad = false);
|
||||||
|
|
||||||
GameSave * GetGameSave();
|
GameSave * GetGameSave();
|
||||||
void SetGameSave(GameSave * save);
|
void SetGameSave(GameSave * save);
|
||||||
@@ -19,12 +19,15 @@ public:
|
|||||||
String GetError();
|
String GetError();
|
||||||
void SetLoadingError(String error);
|
void SetLoadingError(String error);
|
||||||
|
|
||||||
|
void LazyUnload();
|
||||||
|
|
||||||
virtual ~SaveFile();
|
virtual ~SaveFile();
|
||||||
private:
|
private:
|
||||||
GameSave * gameSave;
|
GameSave * gameSave;
|
||||||
ByteString filename;
|
ByteString filename;
|
||||||
String displayName;
|
String displayName;
|
||||||
String loadingError;
|
String loadingError;
|
||||||
|
bool lazyLoad;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* SAVEFILE_H_ */
|
#endif /* SAVEFILE_H_ */
|
||||||
|
@@ -6,6 +6,13 @@
|
|||||||
#include "simulation/SaveRenderer.h"
|
#include "simulation/SaveRenderer.h"
|
||||||
#include "client/GameSave.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) :
|
ThumbnailRendererTask::ThumbnailRendererTask(GameSave *save, int width, int height, bool autoRescale, bool decorations, bool fire) :
|
||||||
Save(new GameSave(*save)),
|
Save(new GameSave(*save)),
|
||||||
Width(width),
|
Width(width),
|
||||||
@@ -14,10 +21,12 @@ ThumbnailRendererTask::ThumbnailRendererTask(GameSave *save, int width, int heig
|
|||||||
Fire(fire),
|
Fire(fire),
|
||||||
AutoRescale(autoRescale)
|
AutoRescale(autoRescale)
|
||||||
{
|
{
|
||||||
|
queueSize += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ThumbnailRendererTask::~ThumbnailRendererTask()
|
ThumbnailRendererTask::~ThumbnailRendererTask()
|
||||||
{
|
{
|
||||||
|
queueSize -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ThumbnailRendererTask::doWork()
|
bool ThumbnailRendererTask::doWork()
|
||||||
|
@@ -16,12 +16,16 @@ class ThumbnailRendererTask : public AbandonableTask
|
|||||||
bool AutoRescale;
|
bool AutoRescale;
|
||||||
std::unique_ptr<VideoBuffer> thumbnail;
|
std::unique_ptr<VideoBuffer> thumbnail;
|
||||||
|
|
||||||
|
static int queueSize;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ThumbnailRendererTask(GameSave *save, int width, int height, bool autoRescale = false, bool decorations = true, bool fire = true);
|
ThumbnailRendererTask(GameSave *save, int width, int height, bool autoRescale = false, bool decorations = true, bool fire = true);
|
||||||
virtual ~ThumbnailRendererTask();
|
virtual ~ThumbnailRendererTask();
|
||||||
|
|
||||||
virtual bool doWork() override;
|
virtual bool doWork() override;
|
||||||
std::unique_ptr<VideoBuffer> Finish();
|
std::unique_ptr<VideoBuffer> Finish();
|
||||||
|
|
||||||
|
static int QueueSize();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // THUMBNAILRENDERER_H
|
#endif // THUMBNAILRENDERER_H
|
||||||
|
@@ -43,25 +43,13 @@ class LoadFilesTask: public Task
|
|||||||
notifyProgress(-1);
|
notifyProgress(-1);
|
||||||
for(std::vector<ByteString>::iterator iter = files.begin(), end = files.end(); iter != end; ++iter)
|
for(std::vector<ByteString>::iterator iter = files.begin(), end = files.end(); iter != end; ++iter)
|
||||||
{
|
{
|
||||||
SaveFile * saveFile = new SaveFile(directory + *iter);
|
SaveFile * saveFile = new SaveFile(directory + *iter, true);
|
||||||
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);
|
saveFiles.push_back(saveFile);
|
||||||
|
|
||||||
ByteString filename = (*iter).SplitFromEndBy(PATH_SEP).After();
|
ByteString filename = (*iter).SplitFromEndBy(PATH_SEP).After();
|
||||||
filename = filename.SplitFromEndBy('.').Before();
|
filename = filename.SplitFromEndBy('.').Before();
|
||||||
saveFile->SetDisplayName(filename.FromUtf8());
|
saveFile->SetDisplayName(filename.FromUtf8());
|
||||||
}
|
}
|
||||||
catch(std::exception & e)
|
|
||||||
{
|
|
||||||
//:(
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,7 +254,7 @@ void FileBrowserActivity::OnTick(float dt)
|
|||||||
if(loadFiles)
|
if(loadFiles)
|
||||||
loadFiles->Poll();
|
loadFiles->Poll();
|
||||||
|
|
||||||
if(files.size())
|
while(files.size())
|
||||||
{
|
{
|
||||||
SaveFile * saveFile = files.back();
|
SaveFile * saveFile = files.back();
|
||||||
files.pop_back();
|
files.pop_back();
|
||||||
@@ -296,7 +284,7 @@ void FileBrowserActivity::OnTick(float dt)
|
|||||||
componentsQueue.push_back(saveButton);
|
componentsQueue.push_back(saveButton);
|
||||||
fileX++;
|
fileX++;
|
||||||
}
|
}
|
||||||
else if(componentsQueue.size())
|
if(componentsQueue.size())
|
||||||
{
|
{
|
||||||
for(std::vector<ui::Component*>::iterator iter = componentsQueue.begin(), end = componentsQueue.end(); iter != end; ++iter)
|
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 (!thumbnail)
|
||||||
{
|
{
|
||||||
if (!triedThumbnail)
|
if (!triedThumbnail && wantsDraw && ThumbnailRendererTask::QueueSize() < 10)
|
||||||
{
|
{
|
||||||
float scaleFactor = (Size.Y-25)/((float)YRES);
|
float scaleFactor = (Size.Y-25)/((float)YRES);
|
||||||
ui::Point thumbBoxSize = ui::Point(int(XRES*scaleFactor), int(YRES*scaleFactor));
|
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);
|
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)
|
void SaveButton::Draw(const Point& screenPos)
|
||||||
|
Reference in New Issue
Block a user