forked from stuerp/foo_sid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSidTuneMod.cpp
219 lines (176 loc) · 5.43 KB
/
SidTuneMod.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sidendian.h>
#include "SidTuneMod.h"
#include <map>
#include <mutex>
#include <string>
#include <vector>
#include <sidtune/SidTuneBase.h>
#include <foobar2000.h>
#pragma warning(disable: 26415 26418 26434 26446 26481 26482 26485 26493)
const char ERR_CANT_OPEN_FILE[] = "SIDTUNE ERROR: Could not open file for binary input";
const char ERR_EMPTY[] = "SIDTUNE ERROR: No data to load";
const char ERR_CANT_LOAD_FILE[] = "SIDTUNE ERROR: Could not load input file";
struct CacheItem
{
size_t _ReferenceCount;
std::vector<uint8_t> _Data;
CacheItem() noexcept : _ReferenceCount(0)
{
}
};
static class FileCache
{
public:
std::string Add(const std::string& filePath, const file::ptr file)
{
std::lock_guard<std::mutex> Guard(_Lock);
CacheItem & Item = _Items[filePath];
if (Item._ReferenceCount == 0)
{
try
{
abort_callback_dummy AbortHandler;
const t_filesize FileSize = file->get_size_ex(AbortHandler);
file->seek(0, AbortHandler);
Item._Data.resize((size_t)FileSize);
file->read_object(&Item._Data[0], (t_size)FileSize, AbortHandler);
Item._ReferenceCount++;
}
catch (...)
{
return filePath;
}
}
else
{
Item._ReferenceCount++;
}
return filePath;
}
void Remove(const std::string& filePath)
{
std::lock_guard<std::mutex> Guard(_Lock);
CacheItem& Item = _Items[filePath];
if (Item._ReferenceCount <= 1)
{
for (auto it = _Items.begin(); it != _Items.end();)
{
if (it->first == filePath)
{
it = _Items.erase(it);
}
else
{
++it;
}
}
}
else
{
--Item._ReferenceCount;
}
}
bool TryGet(const std::string& filePath, std::vector<uint8_t>& data)
{
std::lock_guard<std::mutex> Guard(_Lock);
const CacheItem& Item = _Items[filePath];
if (Item._ReferenceCount == 0)
return false;
data = Item._Data;
return true;
}
private:
std::mutex _Lock;
std::map<std::string, CacheItem> _Items;
} _FileCache;
void SidTuneMod::MyLoaderFunc(const char * filePath, std::vector<uint8_t>& bufferRef)
{
std::string FilePath = filePath;
if (!_FileCache.TryGet(FilePath, bufferRef))
{
try
{
service_ptr_t<file> File;
abort_callback_dummy AbortHandler;
filesystem::g_open(File, filePath, filesystem::open_mode_read, AbortHandler);
const t_filesize FileSize = File->get_size_ex(AbortHandler);
bufferRef.resize((size_t)FileSize);
File->read_object(&bufferRef[0], (t_size)FileSize, AbortHandler);
}
catch (...)
{
throw libsidplayfp::loadError(ERR_CANT_OPEN_FILE);
}
}
else
{
if (!bufferRef.size())
throw libsidplayfp::loadError(ERR_CANT_OPEN_FILE);
}
}
SidTuneMod::SidTuneMod(file::ptr file, std::string fileName, const char ** fileNameExt, const bool separatorIsSlash)
: _fileName(fileName), SidTune(MyLoaderFunc, _FileCache.Add(fileName, file).c_str(), fileNameExt, separatorIsSlash)
{
}
SidTuneMod::~SidTuneMod()
{
try
{
_FileCache.Remove(_fileName);
}
catch (...)
{
}
}
static unsigned char htoi(const char * src) noexcept
{
if (src == nullptr)
return 0;
unsigned char byte;
if (src[0] >= '0' && src[0] <= '9')
byte = (src[0] - '0') << 4;
else
if (src[0] >= 'A' && src[0] <= 'F')
byte = (src[0] - 'A' + 10) << 4;
else
if (src[0] >= 'a' && src[0] <= 'f')
byte = (src[0] - 'a' + 10) << 4;
else
return 0;
if (src[1] >= '0' && src[1] <= '9')
byte += src[1] - '0';
else
if (src[1] >= 'A' && src[1] <= 'F')
byte += src[1] - 'A' + (unsigned char)10;
else
if (src[1] >= 'a' && src[1] <= 'f')
byte += src[1] - 'a' + (unsigned char)10;
else
return 0;
return byte;
}
void SidTuneMod::createMD5(hasher_md5_result& digest)
{
char Hash[33];
const char * HashPtr = SidTune::createMD5(Hash);
if (HashPtr)
{
for (unsigned int i = 0; i < 16; ++i, HashPtr += 2)
digest.m_data[i] = htoi(HashPtr);
}
}