forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSongCacheIndex.cpp
171 lines (149 loc) · 5.74 KB
/
SongCacheIndex.cpp
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
166
167
168
169
170
171
#include "global.h"
#include "SongCacheIndex.h"
#include "RageLog.h"
#include "RageUtil.h"
#include "RageFileManager.h"
#include "Song.h"
#include "SpecialFiles.h"
using std::vector;
/*
* A quick explanation of song cache hashes: Each song has two hashes; a hash of the
* song path, and a hash of the song directory. The former is Song::GetCacheFilePath;
* it stays the same if the contents of the directory change. The latter is
* GetHashForDirectory(m_sSongDir), and changes on each modification.
*
* The file hash is used as the cache filename. We don't want to use the directory
* hash: if we do that, then we'll write a new cache file every time the song changes,
* and they'll accumulate or we'll have to be careful to delete them.
*
* The directory hash is stored in here, indexed by the song path, and used to determine
* if a song has changed.
*
* Another advantage of this system is that we can load songs from cache given only their
* path; we don't have to actually look in the directory (to find out the directory hash)
* in order to find the cache file.
*/
#define CACHE_INDEX SpecialFiles::CACHE_DIR + "index.cache"
SongCacheIndex *SONGINDEX; // global and accessible from anywhere in our program
std::string SongCacheIndex::GetCacheFilePath( const std::string &sGroup, const std::string &sPath )
{
/* Don't use GetHashForFile, since we don't want to spend time
* checking the file size and date. */
std::string s;
if( sPath.size() > 2 && sPath[0] == '/' && sPath[sPath.size()-1] == '/' )
s.assign( sPath, 1, sPath.size() - 2 );
else if( sPath.size() > 1 && sPath[0] == '/' )
s.assign( sPath, 1, sPath.size() - 1 );
else
s = sPath;
/* Change slashes and invalid utf-8 characters to _.
* http://en.wikipedia.org/wiki/UTF-8
* Mac OS X doesn't support precomposed unicode characters in files names and
* so we should probably replace them with combining diacritics.
* XXX How do we do this and is it even worth it? */
const char *invalid = "/\xc0\xc1\xfe\xff\xf8\xf9\xfa\xfb\xfc\xfd\xf5\xf6\xf7";
for( size_t pos = s.find_first_of(invalid); pos != std::string::npos; pos = s.find_first_of(invalid, pos) )
s[pos] = '_';
// CACHE_DIR ends with a /.
return fmt::sprintf( "%s%s/%s", SpecialFiles::CACHE_DIR.c_str(), sGroup.c_str(), s.c_str() );
}
SongCacheIndex::SongCacheIndex()
{
ReadCacheIndex();
}
SongCacheIndex::~SongCacheIndex()
{
}
void SongCacheIndex::ReadFromDisk()
{
ReadCacheIndex();
}
static void EmptyDir( std::string dir )
{
ASSERT(dir[dir.size()-1] == '/');
vector<std::string> asCacheFileNames;
GetDirListing( dir, asCacheFileNames );
for (auto const &name: asCacheFileNames)
{
if( !IsADirectory(dir + name) )
{
FILEMAN->Remove( dir + name );
}
}
}
void SongCacheIndex::ReadCacheIndex()
{
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 );
EmptyDir( SpecialFiles::CACHE_DIR+"Banners/" );
//EmptyDir( SpecialFiles::CACHE_DIR+"Backgrounds/" );
EmptyDir( SpecialFiles::CACHE_DIR+"Songs/" );
EmptyDir( SpecialFiles::CACHE_DIR+"Courses/" );
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();
}
void SongCacheIndex::SaveCacheIndex()
{
CacheIndex.WriteFile(CACHE_INDEX);
}
void SongCacheIndex::AddCacheIndex(const std::string &path, unsigned hash)
{
if( hash == 0 )
++hash; /* no 0 hash values */
CacheIndex.SetValue( "Cache", "CacheVersion", FILE_CACHE_VERSION );
CacheIndex.SetValue( "Cache", MangleName(path), hash );
if(!delay_save_cache)
{
CacheIndex.WriteFile(CACHE_INDEX);
}
}
unsigned SongCacheIndex::GetCacheHash( const std::string &path ) const
{
unsigned iDirHash = 0;
if( !CacheIndex.GetValue( "Cache", MangleName(path), iDirHash ) )
return 0;
if( iDirHash == 0 )
++iDirHash; /* no 0 hash values */
return iDirHash;
}
std::string SongCacheIndex::MangleName( const std::string &Name )
{
/* We store paths in an INI. We can't store '='. */
std::string ret = Name;
Rage::replace(ret, "=", "");
return ret;
}
/*
* (c) 2002-2003 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/