-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathCacheManager.cpp
More file actions
165 lines (137 loc) · 4.31 KB
/
Copy pathCacheManager.cpp
File metadata and controls
165 lines (137 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "Precompile.h"
#include "Engine/CacheManager.h"
#include "Platform/Process.h"
#include "Foundation/FilePath.h"
#include "Engine/Asset.h"
#include "Engine/FileLocations.h"
using namespace Helium;
static uint32_t g_InitCount = 0;
CacheManager* CacheManager::sm_pInstance = NULL;
/// Constructor.
CacheManager::CacheManager( const FilePath& rBaseDirectory )
: m_cachePool( CACHE_POOL_BLOCK_SIZE )
{
m_platformDataDirectories[ Cache::PLATFORM_PC ] = rBaseDirectory.Data();
m_platformDataDirectories[ Cache::PLATFORM_PC ] += "DataPC/";
m_platformDataDirectories[ Cache::PLATFORM_PC ].Trim();
}
/// Destructor.
CacheManager::~CacheManager()
{
}
/// Get the specified cache, creating the cache instance if necessary.
///
/// @param[in] name Cache name.
/// @param[in] platform Cache platform, or Cache::PLATFORM_INVALID to resolve the current platform's cache.
///
/// @return Cache instance.
Cache* CacheManager::GetCache( Name name, Cache::EPlatform platform )
{
HELIUM_ASSERT(
platform == Cache::PLATFORM_INVALID ||
static_cast< size_t >( platform ) < static_cast< size_t >( Cache::PLATFORM_MAX ) );
// Default to the current platform if PLATFORM_INVALID was specified.
if( platform == Cache::PLATFORM_INVALID )
{
platform = GetCurrentPlatform();
}
// Try to locate an existing cache instance first.
ConcurrentHashMap< Name, Cache* >::Accessor cacheAccessor;
if( m_cacheMaps[ platform ].Find( cacheAccessor, name ) )
{
Cache *pCache = cacheAccessor->Second();
HELIUM_ASSERT( pCache );
return pCache;
}
// Cache instance wasn't found, so create a new one and add it.
Cache* pCache = m_cachePool.Allocate();
HELIUM_ASSERT( pCache );
const String& rPlatformDataDirectory = GetPlatformDataDirectory( platform );
#if HELIUM_TOOLS
// If the cache directory doesn't exist, attempt to create it.
FilePath platformDataPath( rPlatformDataDirectory.GetData() );
platformDataPath.MakePath();
#endif
String cacheFileName = rPlatformDataDirectory;
cacheFileName += *name;
String tocFileName = cacheFileName;
tocFileName += "." HELIUM_CACHE_TOC_EXTENSION;
cacheFileName += "." HELIUM_CACHE_EXTENSION;
if( !pCache->Initialize( name, platform, *tocFileName, *cacheFileName ) )
{
HELIUM_TRACE( TraceLevels::Error, "CacheManager: Failed to initialize cache \"%s\".\n", *name );
return NULL;
}
if( !m_cacheMaps[ platform ].Insert( cacheAccessor, KeyValue< Name, Cache* >( name, pCache ) ) )
{
// Cache instance was added while we were trying to create a new one, so release the one we allocated and
// use the existing instance.
pCache->Shutdown();
m_cachePool.Release( pCache );
pCache = cacheAccessor->Second();
HELIUM_ASSERT( pCache );
}
return pCache;
}
/// Get the cache data directory for the specified platform.
///
/// @param[in] platform Target platform, or Cache::PLATFORM_INVALID name to use the current platform.
///
/// @return Cache data directory path.
const String& CacheManager::GetPlatformDataDirectory( Cache::EPlatform platform )
{
HELIUM_ASSERT(
platform == Cache::PLATFORM_INVALID ||
static_cast< size_t >( platform ) < static_cast< size_t >( Cache::PLATFORM_MAX ) );
if( platform == Cache::PLATFORM_INVALID )
{
platform = GetCurrentPlatform();
}
return m_platformDataDirectories[ platform ];
}
/// Get the singleton CacheManager instance.
///
/// @return Pointer to the CacheManager instance.
///
/// @see Startup(), Shutdown()
CacheManager* CacheManager::GetInstance()
{
return sm_pInstance;
}
/// Initialize the singleton CacheManager instance.
///
/// @see Shutdown(), GetInstance()
void CacheManager::Startup()
{
if ( ++g_InitCount == 1 )
{
FilePath baseDirectory;
HELIUM_VERIFY( FileLocations::GetBaseDirectory( baseDirectory ) );
HELIUM_ASSERT( sm_pInstance == NULL );
sm_pInstance = new CacheManager( baseDirectory );
HELIUM_ASSERT( sm_pInstance );
}
}
/// Destroy the singleton CacheManager instance.
///
/// @see Startup(), GetInstance()
void CacheManager::Shutdown()
{
if ( --g_InitCount == 0 )
{
HELIUM_ASSERT( sm_pInstance );
delete sm_pInstance;
sm_pInstance = NULL;
}
}
/// Get the identifier for the current cache platform.
///
/// @return Current cache platform.
Cache::EPlatform CacheManager::GetCurrentPlatform()
{
#if HELIUM_OS_WIN || HELIUM_OS_MAC || HELIUM_OS_LINUX
return Cache::PLATFORM_PC;
#else
#error CacheManager: Unsupported platform.
#endif
}