Fixed compilation times by refactoring: WResourceManager.h gets included either directly or indirectly into every header & cpp file; so does its includes & implementation details. Broke out WResourceManager into a pure virtual class that contains only the required calls, and added a WResourceManagerImpl header that contains all the dirty details that the rest of the app doesn't care about / need to know.
This commit is contained in:
@@ -1,41 +1,9 @@
|
||||
#ifndef _WRESOURCEMANAGER_H_
|
||||
#define _WRESOURCEMANAGER_H_
|
||||
#ifndef _WResourceManager_H_
|
||||
#define _WResourceManager_H_
|
||||
|
||||
#include "WResource_Fwd.h"
|
||||
#include <JResourceManager.h>
|
||||
#include <JSoundSystem.h>
|
||||
#include <JTypes.h>
|
||||
#include "MTGDeck.h"
|
||||
#include "MTGCard.h"
|
||||
#include "utils.h"
|
||||
#include "WCachedResource.h"
|
||||
#include "WFont.h"
|
||||
#include "JLogger.h"
|
||||
#include <sstream>
|
||||
|
||||
#include "Threading.h"
|
||||
|
||||
#define HUGE_CACHE_LIMIT 20000000 // Size of the cache for Windows and Linux
|
||||
#define SAMPLES_CACHE_SIZE 1500000 // Size in bytes of the cached samples
|
||||
#define PSI_CACHE_SIZE 500000 // Size in bytes of the cached particles
|
||||
#define TEXTURES_CACHE_MINSIZE 2000000 // Minimum size of the cache on the PSP. The program should complain if the cache ever gets smaller than this
|
||||
#define OPERATIONAL_SIZE 5000000 // Size required by Wagic for operational stuff. 3MB is not enough. The cache will usually try to take (Total Ram - Operational size)
|
||||
#define MIN_LINEAR_RAM 1500000
|
||||
#ifdef DEBUG_CACHE
|
||||
#define MAX_CACHE_TIME 2000 //The threshold above which we try to prevent nowTime() from looping.
|
||||
#else
|
||||
#define MAX_CACHE_TIME 2000000000
|
||||
#endif
|
||||
|
||||
|
||||
#define THUMBNAILS_OFFSET 100000000
|
||||
#define OTHERS_OFFSET 2000000000
|
||||
|
||||
//Hard Limits.
|
||||
#define MAX_CACHE_OBJECTS 300
|
||||
#define MAX_CACHE_ATTEMPTS 10
|
||||
#define MAX_CACHE_MISSES 200
|
||||
#define MAX_CACHED_SAMPLES 50
|
||||
#define MAX_CACHE_GARBAGE 10
|
||||
|
||||
|
||||
enum ENUM_WRES_INFO
|
||||
{
|
||||
@@ -83,226 +51,75 @@ enum ENUM_CACHE_ERROR
|
||||
CACHE_ERROR_NOT_MANAGED,
|
||||
};
|
||||
|
||||
struct WCacheSort
|
||||
{
|
||||
bool operator()(const WResource * l, const WResource * r); //Predicate for use in sorting. See flatten().
|
||||
};
|
||||
|
||||
template <class cacheItem, class cacheActual>
|
||||
class WCache
|
||||
{
|
||||
public:
|
||||
friend class WResourceManager;
|
||||
friend class ThreadedCardRetriever;
|
||||
friend class UnthreadedCardRetriever;
|
||||
|
||||
WCache();
|
||||
~WCache();
|
||||
|
||||
cacheItem* Retrieve(int id, const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Primary interface function.
|
||||
bool Release(cacheActual* actual); //Releases an item, and deletes it if unlocked.
|
||||
bool RemoveMiss(int id=0); //Removes a cache miss.
|
||||
bool RemoveOldest(); //Remove oldest unlocked item.
|
||||
bool Cleanup(); //Repeats RemoveOldest() until cache fits in size limits
|
||||
void ClearUnlocked(); //Remove all unlocked items.
|
||||
void Refresh(); //Refreshes all cache items.
|
||||
unsigned int Flatten(); //Ensures that the times don't loop. Returns new lastTime.
|
||||
void Resize(unsigned long size, int items); //Sets new limits, then enforces them. Lock safe, so not a "hard limit".
|
||||
|
||||
protected:
|
||||
|
||||
cacheItem* LoadIntoCache(int id, const string& filename, int submode, int style = RETRIEVE_NORMAL);
|
||||
|
||||
/*
|
||||
** Attempts a new cache item, progressively clearing cache if it fails.
|
||||
*/
|
||||
cacheItem* AttemptNew(const string& filename, int submode);
|
||||
|
||||
bool RemoveItem(cacheItem* item, bool force = true); //Removes an item, deleting it. if(force), ignores locks / permanent
|
||||
bool UnlinkCache(cacheItem* item); //Removes an item from our cache, does not delete it. Use with care.
|
||||
bool Delete(cacheItem* item); //SAFE_DELETE and garbage collect. If maxCached == 0, nullify first. (This means you have to free that cacheActual later!)
|
||||
cacheItem* Get(int id, const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Subordinate to Retrieve.
|
||||
|
||||
int makeID(int id, const string& filename, int submode); //Makes an ID appropriate to the submode.
|
||||
|
||||
inline bool RequiresMissCleanup()
|
||||
{
|
||||
return (cacheItems < cache.size() /*&& cache.size() - cacheItems > MAX_CACHE_MISSES*/);
|
||||
}
|
||||
|
||||
inline bool RequiresOldItemCleanup()
|
||||
{
|
||||
if (cacheItems > MAX_CACHE_OBJECTS || cacheItems > maxCached || cacheSize > maxCacheSize)
|
||||
{
|
||||
std::ostringstream stream;
|
||||
if (cacheItems > MAX_CACHE_OBJECTS)
|
||||
{
|
||||
stream << "CacheItems exceeded 300, cacheItems = " << cacheItems;
|
||||
}
|
||||
else if (cacheItems > maxCached)
|
||||
{
|
||||
stream << "CacheItems (" << cacheItems << ") exceeded arbitrary limit of " << maxCached;
|
||||
}
|
||||
else
|
||||
{
|
||||
stream << "Cache size (" << cacheSize << ") exceeded max value of " << maxCacheSize;
|
||||
}
|
||||
|
||||
LOG(stream.str().c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
#if PSPENV
|
||||
if (ramAvailableLineareMax() < MIN_LINEAR_RAM)
|
||||
{
|
||||
DebugTrace("Memory below minimum threshold!!");
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
map<string,int> ids;
|
||||
map<int,cacheItem*> cache;
|
||||
map<int,cacheItem*> managed; //Cache can be arbitrarily large, so managed items are separate.
|
||||
unsigned long totalSize;
|
||||
unsigned long cacheSize;
|
||||
|
||||
//Applies to cacheSize only.
|
||||
unsigned long maxCacheSize;
|
||||
unsigned int maxCached;
|
||||
|
||||
unsigned int cacheItems;
|
||||
int mError;
|
||||
|
||||
// mutex meant for the cache map
|
||||
boost::mutex mCacheMutex;
|
||||
// mutex meant to protect against unthread-safe calls into JFileSystem, etc.
|
||||
boost::mutex mLoadFunctionMutex;
|
||||
};
|
||||
|
||||
struct WManagedQuad
|
||||
{
|
||||
WCachedTexture* texture;
|
||||
string resname;
|
||||
};
|
||||
struct WManagedQuad;
|
||||
class WFont;
|
||||
class MTGCard;
|
||||
struct hgeParticleSystemInfo;
|
||||
|
||||
class WResourceManager
|
||||
{
|
||||
public:
|
||||
static WResourceManager* Instance()
|
||||
{
|
||||
if (sInstance == NULL)
|
||||
{
|
||||
sInstance = NEW WResourceManager;
|
||||
}
|
||||
static WResourceManager* Instance();
|
||||
static void Terminate();
|
||||
|
||||
return sInstance;
|
||||
}
|
||||
virtual ~WResourceManager()
|
||||
{
|
||||
}
|
||||
|
||||
static void Terminate()
|
||||
{
|
||||
if (sInstance)
|
||||
SAFE_DELETE(sInstance);
|
||||
}
|
||||
virtual bool IsThreaded() = 0;
|
||||
|
||||
virtual ~WResourceManager();
|
||||
virtual JQuadPtr RetrieveCard(MTGCard * card, int style = RETRIEVE_NORMAL,int submode = CACHE_NORMAL) = 0;
|
||||
virtual JSample * RetrieveSample(const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL) = 0;
|
||||
virtual JTexture * RetrieveTexture(const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL) = 0;
|
||||
virtual JQuadPtr RetrieveQuad(const string& filename, float offX=0.0f, float offY=0.0f, float width=0.0f, float height=0.0f, string resname="", int style = RETRIEVE_LOCK, int submode = CACHE_NORMAL, int id = 0) = 0;
|
||||
virtual JQuadPtr RetrieveTempQuad(const string& filename, int submode = CACHE_NORMAL) = 0;
|
||||
virtual hgeParticleSystemInfo * RetrievePSI(const string& filename, JQuad * texture, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL) = 0;
|
||||
virtual int RetrieveError() = 0;
|
||||
|
||||
bool IsThreaded();
|
||||
virtual void Release(JTexture * tex) = 0;
|
||||
virtual void Release(JSample * sample) = 0;
|
||||
|
||||
void Unmiss(string filename);
|
||||
JQuadPtr RetrieveCard(MTGCard * card, int style = RETRIEVE_NORMAL,int submode = CACHE_NORMAL);
|
||||
JSample * RetrieveSample(const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
|
||||
JTexture * RetrieveTexture(const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
|
||||
JQuadPtr RetrieveQuad(const string& filename, float offX=0.0f, float offY=0.0f, float width=0.0f, float height=0.0f, string resname="", int style = RETRIEVE_LOCK, int submode = CACHE_NORMAL, int id = 0);
|
||||
JQuadPtr RetrieveTempQuad(const string& filename, int submode = CACHE_NORMAL);
|
||||
hgeParticleSystemInfo * RetrievePSI(const string& filename, JQuad * texture, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
|
||||
int RetrieveError();
|
||||
//Refreshes all files in cache, for when mode/profile changes.
|
||||
virtual void Refresh() = 0;
|
||||
|
||||
void Release(JTexture * tex);
|
||||
void Release(JSample * sample);
|
||||
bool RemoveOldest();
|
||||
virtual unsigned int nowTime() = 0;
|
||||
|
||||
void ClearUnlocked(); //Remove unlocked items.
|
||||
void Refresh(); //Refreshes all files in cache, for when mode/profile changes.
|
||||
|
||||
unsigned int nowTime();
|
||||
|
||||
unsigned long Size();
|
||||
unsigned long SizeCached();
|
||||
unsigned long SizeManaged();
|
||||
|
||||
unsigned int Count();
|
||||
unsigned int CountCached();
|
||||
unsigned int CountManaged();
|
||||
|
||||
int CreateQuad(const string &quadName, const string &textureName, float x, float y, float width, float height);
|
||||
JQuadPtr GetQuad(const string &quadName);
|
||||
JQuadPtr GetQuad(int id);
|
||||
|
||||
int AddQuadToManaged(const WManagedQuad& inManagedQuad);
|
||||
virtual JQuadPtr GetQuad(const string &quadName) = 0;
|
||||
|
||||
//Our file redirect system.
|
||||
string graphicsFile(const string& filename);
|
||||
string avatarFile(const string& filename);
|
||||
string cardFile(const string& filename);
|
||||
string musicFile(const string& filename);
|
||||
string sfxFile(const string& filename);
|
||||
int fileOK(const string&, bool relative = false);
|
||||
int dirOK(const string& dirname);
|
||||
virtual string graphicsFile(const string& filename) = 0;
|
||||
virtual string avatarFile(const string& filename) = 0;
|
||||
virtual string cardFile(const string& filename) = 0;
|
||||
virtual string musicFile(const string& filename) = 0;
|
||||
virtual string sfxFile(const string& filename) = 0;
|
||||
virtual int fileOK(const string&, bool relative = false) = 0;
|
||||
virtual int dirOK(const string& dirname) = 0;
|
||||
|
||||
//For backwards compatibility with JResourceManager. Avoid using these, they're not optimal.
|
||||
int CreateTexture(const string &textureName);
|
||||
JTexture* GetTexture(const string &textureName);
|
||||
JTexture* GetTexture(int id);
|
||||
//For backwards compatibility with JWResourceManager. Avoid using these, they're not optimal.
|
||||
virtual int CreateTexture(const string &textureName) = 0;
|
||||
virtual JTexture* GetTexture(const string &textureName) = 0;
|
||||
|
||||
// Font management functions
|
||||
void InitFonts(const std::string& inLang);
|
||||
int ReloadWFonts();
|
||||
WFont* LoadWFont(const string& inFontname, int inFontHeight, int inFontID);
|
||||
WFont* GetWFont(int id);
|
||||
void RemoveWFonts();
|
||||
virtual void InitFonts(const std::string& inLang) = 0;
|
||||
virtual int ReloadWFonts() = 0;
|
||||
virtual WFont* LoadWFont(const string& inFontname, int inFontHeight, int inFontID) = 0;
|
||||
virtual WFont* GetWFont(int id) = 0;
|
||||
|
||||
//Wrapped from JSoundSystem. TODO: Privatize.
|
||||
JMusic * ssLoadMusic(const char *fileName);
|
||||
virtual JMusic * ssLoadMusic(const char *fileName) = 0;
|
||||
|
||||
//Resets the cache limits on when it starts to purge data.
|
||||
void ResetCacheLimits();
|
||||
virtual void ResetCacheLimits() = 0;
|
||||
|
||||
void DebugRender();
|
||||
virtual void DebugRender() = 0;
|
||||
|
||||
#ifdef DEBUG_CACHE
|
||||
unsigned long menuCached;
|
||||
string debugMessage;
|
||||
#endif
|
||||
|
||||
private:
|
||||
protected:
|
||||
/*
|
||||
** Singleton object only accessibly via Instance(), constructor is private
|
||||
*/
|
||||
WResourceManager();
|
||||
|
||||
bool bThemedCards; //Does the theme have a "sets" directory for overwriting cards?
|
||||
void FlattenTimes(); //To prevent bad cache timing on int overflow
|
||||
|
||||
//For cached stuff
|
||||
WCache<WCachedTexture,JTexture> textureWCache;
|
||||
WCache<WCachedSample,JSample> sampleWCache;
|
||||
WCache<WCachedParticles,hgeParticleSystemInfo> psiWCache;
|
||||
|
||||
typedef std::map<std::string, WManagedQuad> ManagedQuadMap;
|
||||
ManagedQuadMap mManagedQuads;
|
||||
|
||||
typedef std::map<int, std::string> IDLookupMap;
|
||||
IDLookupMap mIDLookupMap;
|
||||
|
||||
//Statistics of record.
|
||||
unsigned int lastTime;
|
||||
int lastError;
|
||||
|
||||
typedef std::map<int, WFont*> FontMap;
|
||||
FontMap mWFontMap;
|
||||
std::string mFontFileExtension;
|
||||
WResourceManager()
|
||||
{
|
||||
}
|
||||
|
||||
static WResourceManager* sInstance;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user