Skip to content

Commit

Permalink
Fixing imagecache (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jousway authored and MinaciousGrace committed Jul 20, 2018
1 parent 657d713 commit f3b6bdc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
33 changes: 32 additions & 1 deletion src/ImageCache.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "global.h"
#include "global.h"

#include "ImageCache.h"
#include "Foreach.h"
#include "PrefsManager.h"
#include "RageFileManager.h"
#include "RageDisplay.h"
#include "RageLog.h"
#include "RageSurface.h"
Expand All @@ -14,6 +15,7 @@
#include "RageTexture.h"
#include "RageTextureManager.h"
#include "RageUtil.h"
#include "CommonMetrics.h"
#include "SongCacheIndex.h"
#include "SpecialFiles.h"
#include "Sprite.h"
Expand All @@ -27,6 +29,8 @@ static Preference<bool> g_bPalettedImageCache( "PalettedImageCache", false );
//const std::string IMAGE_CACHE_INDEX = SpecialFiles::CACHE_DIR + "images.cache";
#define IMAGE_CACHE_INDEX (SpecialFiles::CACHE_DIR + "images.cache")

#define IMAGE_CACHE_VERSION 1

/* Call CacheImage to cache a image by path. If the image is already
* cached, it'll be recreated. This is efficient if the image hasn't changed,
* but we still only do this in TidyUpData for songs.
Expand Down Expand Up @@ -174,9 +178,36 @@ ImageCache::~ImageCache()
UnloadAllImages();
}

static void EmptyDir(RString dir)
{
ASSERT(dir[dir.size() - 1] == '/');

vector<RString> asCacheFileNames;
GetDirListing(dir, asCacheFileNames);
for (unsigned i = 0; i<asCacheFileNames.size(); i++)
{
if (!IsADirectory(dir + asCacheFileNames[i]))
FILEMAN->Remove(dir + asCacheFileNames[i]);
}
}

void ImageCache::ReadFromDisk()
{
ImageData.ReadFile( IMAGE_CACHE_INDEX ); // don't care if this fails

int iCacheVersion = -1;
ImageData.GetValue("Cache", "CacheVersion", iCacheVersion);
if (iCacheVersion == IMAGE_CACHE_VERSION)
return;

LOG->Trace( "Cache format is out of date. Deleting all cache files." );
vector<RString> ImageDir;
split( CommonMetrics::IMAGES_TO_CACHE, ",", ImageDir );
for( std::string Image : ImageDir )
EmptyDir( SpecialFiles::CACHE_DIR+Image+"/" );

ImageData.SetValue( "Cache", "CacheVersion", IMAGE_CACHE_VERSION);
ImageData.WriteFile( IMAGE_CACHE_INDEX );
}

struct ImageTexture: public RageTexture
Expand Down
16 changes: 7 additions & 9 deletions src/SongCacheIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* path; we don't have to actually look in the directory (to find out the directory hash)
* in order to find the cache file.
*/
const string CACHE_INDEX = SpecialFiles::CACHE_DIR + "index.cache";
const string CACHE_INDEX = SpecialFiles::CACHE_DIR + "images.cache";
const string CACHE_DB = SpecialFiles::CACHE_DIR + "cache.db";
const unsigned int CACHE_DB_VERSION = 236;

Expand Down Expand Up @@ -714,33 +714,31 @@ static void EmptyDir(RString dir)

void SongCacheIndex::ReadCacheIndex()
{
CacheIndex.ReadFile(CACHE_INDEX); // don't care if this fails
return;
/*CacheIndex.ReadFile(CACHE_INDEX); // don't care if this fails
int iCacheVersion = -1;
CacheIndex.GetValue("Cache", "CacheVersion", iCacheVersion);
if (iCacheVersion == FILE_CACHE_VERSION)
return; // OK
LOG->Trace( "Cache format is out of date. Deleting all cache files." );
EmptyDir(SpecialFiles::CACHE_DIR + "Banners/");
EmptyDir( SpecialFiles::CACHE_DIR+"Songs/" );
EmptyDir( SpecialFiles::CACHE_DIR+"Courses/" );

// comment out until we stop being really bad at dealing with cache versions (revisit asap) - mina
/*
vector<RString> ImageDir;
split( CommonMetrics::IMAGES_TO_CACHE, ",", ImageDir );
for( std::string Image : ImageDir )
EmptyDir( SpecialFiles::CACHE_DIR+Image+"/" );
*/
EmptyDir( SpecialFiles::CACHE_DIR+Image+"/" );*/

CacheIndex.Clear();
//CacheIndex.Clear();
/* This is right now in place because our song file paths are apparently being
* cached in two distinct areas, and songs were loading from paths in FILEMAN.
* This is admittedly a hack for now, but this does bring up a good question on
* whether we really need a dedicated cache for future versions of StepMania.
*/
FILEMAN->FlushDirCache();
//FILEMAN->FlushDirCache();
}

void SongCacheIndex::SaveCacheIndex()
Expand Down

0 comments on commit f3b6bdc

Please sign in to comment.