forked from xomniversex/foo_gep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric.cpp
More file actions
253 lines (204 loc) · 7.62 KB
/
generic.cpp
File metadata and controls
253 lines (204 loc) · 7.62 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "config.h"
#include "base.h"
#include "reader.h"
#include "coleco.h"
#include <gme/blargg_endian.h>
#include <gme/Gym_Emu.h>
#include <gme/Sgc_Emu.h>
#undef HEADER_STRING
#define HEADER_STRING(i,n,f) meta_add((i), (n), (f), sizeof(f))
static gme_type_t const gme_type_list_ [] = { gme_ay_type, gme_gbs_type, gme_gym_type, gme_hes_type, gme_kss_type, gme_sap_type, gme_sgc_type };
static unsigned identify_header( void const* header )
{
switch ( get_be32( header ) )
{
case BLARGG_4CHAR('Z','X','A','Y'): return 1;
case BLARGG_4CHAR('G','B','S',0x01): return 2;
case BLARGG_4CHAR('G','Y','M','X'): return 3;
case BLARGG_4CHAR('H','E','S','M'): return 4;
case BLARGG_4CHAR('K','S','C','C'):
case BLARGG_4CHAR('K','S','S','X'): return 5;
case BLARGG_4CHAR('S','A','P',0x0D): return 6;
case BLARGG_4CHAR('S','G','C',0x1A): return 7;
}
return 0;
}
static struct coleco_setup
{
coleco_setup()
{
for ( unsigned i = 0; i < 0x2000; i++ )
{
image [i] *= 820109;
}
Sgc_Emu::set_coleco_bios( image );
}
} setup_stuff;
class input_generic : public input_gep
{
unsigned type;
public:
static bool g_is_our_path( const char * p_path, const char * p_extension )
{
for ( unsigned i = 0, j = tabsize( gme_type_list_ ); i < j; ++i )
{
if ( ! ( cfg_format_enable & ( 1 << ( i + 3 ) ) ) ) continue;
if ( ! stricmp( p_extension, gme_type_list_[ i ]->extension_ ) )
return 1;
}
return 0;
}
void open( service_ptr_t<file> p_filehint, const char * p_path, t_input_open_reason p_reason, abort_callback & p_abort )
{
if ( p_reason == input_open_info_write ) p_reason = input_open_info_read;
input_gep::open( p_filehint, p_path, p_reason, p_abort );
char header [4];
m_file->read_object_t( header, p_abort );
type = identify_header( header );
if ( !type ) throw exception_io_data();
gme_type_t const gtype = gme_type_list_[ type - 1 ];
// Disallow type / extension overlap
if ( stricmp( gtype->extension_, pfc::string_extension( p_path ) ) ) throw exception_io_data();
m_file->seek( 0, p_abort );
foobar_Data_Reader rdr( m_file, p_abort );
if ( p_reason == input_open_info_read )
emu = gtype->new_info();
else if ( p_reason == input_open_decode )
emu = gtype->new_emu();
if ( !emu ) throw std::bad_alloc();
if ( p_reason == input_open_decode )
{
if ( gtype == gme_gym_type )
{
static_cast<Gym_Emu *>( emu )->disable_oversampling();
setup_effects( false );
}
else
setup_effects();
}
ERRCHK( emu->set_sample_rate( sample_rate ) );
ERRCHK( emu->load( rdr ) );
handle_warning();
if ( p_reason == input_open_decode && gtype != gme_gym_type )
setup_effects_2();
static_api_ptr_t<hasher_md5> p_hasher;
hasher_md5_state p_hash_state;
p_hasher->initialize( p_hash_state );
emu->hash_( info_hasher_md5( p_hasher, p_hash_state ) );
m_file_hash = p_hasher->get_result( p_hash_state );
m_file.release();
if ( gtype != gme_gym_type )
{
pfc::string_replace_extension list( p_path, "m3u" );
if ( filesystem::g_exists( list, p_abort ) )
{
filesystem::g_open( p_filehint, list, filesystem::open_mode_read, p_abort );
foobar_Data_Reader rdr( p_filehint, p_abort );
ERRCHK( emu->load_m3u( rdr ) );
handle_warning();
}
}
}
unsigned get_subsong_count()
{
return emu->track_count();
}
t_uint32 get_subsong( unsigned p_subsong )
{
return p_subsong;
}
void get_info( t_uint32 p_subsong, file_info & p_info, abort_callback & p_abort )
{
p_info.info_set("codec", gme_type_list_[ type - 1 ]->extension_ );
p_info.info_set( "encoding", "synthesized" );
//p_info.info_set_int("samplerate", sample_rate);
p_info.info_set_int("channels", 2);
p_info.info_set_int("bitspersample", 16);
track_info_t i;
ERRCHK( emu->track_info( &i, p_subsong ) );
int length = 0;
double dlength = double(tag_song_ms + tag_fade_ms) * .001;
if ( i.intro_length != -1 && i.loop_length != -1 )
length = i.intro_length + i.loop_length * ( i.repeat_count > 0 ? i.repeat_count : 2 );
else if ( i.length != -1 )
length = i.length;
if ( length )
{
tag_song_ms = length;
tag_fade_ms = i.fade_length;
dlength = double(tag_song_ms + tag_fade_ms) * .001;
}
HEADER_STRING( p_info, "system", i.system );
HEADER_STRING( p_info, "album", i.game );
HEADER_STRING( p_info, "title", i.song );
HEADER_STRING( p_info, "artist", i.author );
HEADER_STRING( p_info, "date", i.date );
HEADER_STRING( p_info, "engineer", i.engineer );
HEADER_STRING( p_info, "composer", i.composer );
HEADER_STRING( p_info, "sequencer", i.sequencer );
HEADER_STRING( p_info, "copyright", i.copyright );
HEADER_STRING( p_info, "comment", i.comment );
HEADER_STRING( p_info, "dumper", i.dumper );
HEADER_STRING( p_info, "tagger", i.tagger );
p_info.set_length(dlength);
pfc::string8 hash_string;
for ( unsigned i = 0; i < 16; i++ ) hash_string += pfc::format_uint( (t_uint8)m_file_hash.m_data[i], 2, 16 );
p_info.info_set( "gme_hash", hash_string );
service_ptr_t<metadb_index_client> index_client = new service_impl_t<metadb_index_client_gep>;
m_index_hash = index_client->transform( p_info, playable_location_impl( m_path, p_subsong ) );
pfc::array_t<t_uint8> tag;
static_api_ptr_t<metadb_index_manager>()->get_user_data_t( guid_gep_index, m_index_hash, tag );
if ( tag.get_count() )
{
file::ptr tag_file;
filesystem::g_open_tempmem( tag_file, p_abort );
tag_file->write_object( tag.get_ptr(), tag.get_count(), p_abort );
p_info.meta_remove_all();
tag_processor::read_trailing( tag_file, p_info, p_abort );
p_info.info_set( "tagtype", "apev2 db" );
}
}
void retag_set_info( t_uint32 p_subsong, const file_info & p_info, abort_callback & p_abort )
{
file::ptr tag_file;
filesystem::g_open_tempmem( tag_file, p_abort );
tag_processor::write_apev2( tag_file, p_info, p_abort );
pfc::array_t<t_uint8> tag;
tag_file->seek( 0, p_abort );
tag.set_count( tag_file->get_size_ex( p_abort ) );
tag_file->read_object( tag.get_ptr(), tag.get_count(), p_abort );
static_api_ptr_t<metadb_index_manager>()->set_user_data( guid_gep_index, m_index_hash, tag.get_ptr(), tag.get_count() );
}
void retag_commit( abort_callback & p_abort )
{
}
void decode_initialize( t_uint32 p_subsong, unsigned p_flags, abort_callback & p_abort )
{
track_info_t i;
ERRCHK( emu->track_info( &i, p_subsong ) );
int length = 0;
double dlength = double(tag_song_ms + tag_fade_ms) * .001;
if ( i.intro_length != -1 && i.loop_length != -1 )
length = i.intro_length + i.loop_length * ( i.repeat_count > 0 ? i.repeat_count : 2 );
else if ( i.length != -1 )
length = i.length;
if ( length )
{
tag_song_ms = length;
tag_fade_ms = i.fade_length;
dlength = double(tag_song_ms + tag_fade_ms) * .001;
}
input_gep::decode_initialize( p_subsong, p_flags, p_abort );
}
};
#define DECLARE_FILE_TYPE_n(NS,NAME,MASK) \
namespace NS { static input_file_type_impl g_filetype_instance(NAME,MASK,true); \
static service_factory_single_ref_t<input_file_type_impl> g_filetype_service(g_filetype_instance); }
DECLARE_FILE_TYPE_n( a, "AY files", "*.AY" );
DECLARE_FILE_TYPE_n( b, "GBS files", "*.GBS" );
DECLARE_FILE_TYPE_n( c, "GYM files", "*.GYM" );
DECLARE_FILE_TYPE_n( d, "HES files", "*.HES" );
DECLARE_FILE_TYPE_n( e, "KSS files", "*.KSS" );
DECLARE_FILE_TYPE_n( f, "SAP files", "*.SAP" );
DECLARE_FILE_TYPE_n( g, "SGC files", "*.SGC" );
static input_factory_t <input_generic> g_input_factory_generic;