forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_content_disc.c
executable file
·376 lines (301 loc) · 12.2 KB
/
task_content_disc.c
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2021 - Daniel De Matteis
* Copyright (C) 2016-2019 - Brad Parker
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <file/file_path.h>
#include <queues/task_queue.h>
#include <string/stdstring.h>
#ifdef _WIN32
#ifdef _XBOX
#include <xtl.h>
#define setmode _setmode
#define INVALID_FILE_ATTRIBUTES -1
#else
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#endif
#endif
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include <boolean.h>
#include <compat/strl.h>
#include <compat/posix_string.h>
#include <string/stdstring.h>
#include <vfs/vfs_implementation.h>
#ifdef HAVE_CDROM
#include <vfs/vfs_implementation_cdrom.h>
#endif
#include <retro_miscellaneous.h>
#include <streams/file_stream.h>
#include <string/stdstring.h>
#include "task_content.h"
#include "tasks_internal.h"
#include "../configuration.h"
#include "../msg_hash.h"
#include "../retroarch.h"
#include "../verbosity.h"
#ifdef HAVE_CDROM
enum cdrom_dump_state
{
DUMP_STATE_TOC_PENDING = 0,
DUMP_STATE_WRITE_CUE,
DUMP_STATE_NEXT_TRACK,
DUMP_STATE_READ_TRACK
};
typedef struct
{
int64_t cur_track_bytes;
int64_t track_written_bytes;
int64_t disc_total_bytes;
int64_t disc_read_bytes;
RFILE *file;
RFILE *output_file;
libretro_vfs_implementation_file *stream;
const cdrom_toc_t *toc;
enum cdrom_dump_state state;
unsigned char cur_track;
char drive_letter[2];
char cdrom_path[64];
char title[512];
bool next;
} task_cdrom_dump_state_t;
static void task_cdrom_dump_handler(retro_task_t *task)
{
task_cdrom_dump_state_t *state = (task_cdrom_dump_state_t*)task->state;
settings_t *settings = config_get_ptr();
const char *directory_core_assets = settings
? settings->paths.directory_core_assets : NULL;
if (task_get_progress(task) == 100)
{
if (state->file)
filestream_close(state->file);
if (state->output_file)
filestream_close(state->output_file);
state->file = NULL;
state->output_file = NULL;
task_set_finished(task, true);
RARCH_LOG("[CDROM]: Dump finished.\n");
return;
}
switch (state->state)
{
case DUMP_STATE_TOC_PENDING:
{
/* open cuesheet file from drive */
char cue_path[PATH_MAX_LENGTH] = {0};
cdrom_device_fillpath(cue_path, sizeof(cue_path), state->drive_letter[0], 0, true);
state->file = filestream_open(cue_path, RETRO_VFS_FILE_ACCESS_READ, 0);
if (!state->file)
{
RARCH_ERR("[CDROM]: Error opening file for reading: %s\n", cue_path);
task_set_progress(task, 100);
task_free_title(task);
task_set_title(task, strdup(msg_hash_to_str(MENU_ENUM_LABEL_VALUE_QT_FILE_READ_OPEN_FAILED)));
return;
}
state->state = DUMP_STATE_WRITE_CUE;
break;
}
case DUMP_STATE_WRITE_CUE:
{
char output_file[PATH_MAX_LENGTH];
char cue_filename[PATH_MAX_LENGTH];
/* write cuesheet to a file */
int64_t cue_size = filestream_get_size(state->file);
char *cue_data = (char*)calloc(1, cue_size);
output_file[0] = cue_filename[0] = '\0';
filestream_read(state->file, cue_data, cue_size);
state->stream = filestream_get_vfs_handle(state->file);
state->toc = retro_vfs_file_get_cdrom_toc();
if (cdrom_has_atip(state->stream))
RARCH_LOG("[CDROM]: This disc is not genuine.\n");
filestream_close(state->file);
snprintf(cue_filename, sizeof(cue_filename), "%s.cue", state->title);
fill_pathname_join(output_file,
directory_core_assets, cue_filename, sizeof(output_file));
{
RFILE *file = filestream_open(output_file, RETRO_VFS_FILE_ACCESS_WRITE, 0);
unsigned char point = 0;
if (!file)
{
RARCH_ERR("[CDROM]: Error opening file for writing: %s\n", output_file);
task_set_progress(task, 100);
task_free_title(task);
task_set_title(task, strdup(msg_hash_to_str(MENU_ENUM_LABEL_VALUE_QT_FILE_WRITE_OPEN_FAILED)));
return;
}
for (point = 1; point <= state->toc->num_tracks; point++)
{
const char *track_type = "MODE1/2352";
char track_filename[PATH_MAX_LENGTH];
state->disc_total_bytes += state->toc->track[point - 1].track_bytes;
track_filename[0] = '\0';
if (state->toc->track[point - 1].audio)
track_type = "AUDIO";
else if (state->toc->track[point - 1].mode == 1)
track_type = "MODE1/2352";
else if (state->toc->track[point - 1].mode == 2)
track_type = "MODE2/2352";
snprintf(track_filename, sizeof(track_filename), "%s (Track %02d).bin", state->title, point);
filestream_printf(file, "FILE \"%s\" BINARY\n", track_filename);
filestream_printf(file, " TRACK %02d %s\n", point, track_type);
{
unsigned pregap_lba_len = state->toc->track[point - 1].lba - state->toc->track[point - 1].lba_start;
if (state->toc->track[point - 1].audio && pregap_lba_len > 0)
{
unsigned char min = 0;
unsigned char sec = 0;
unsigned char frame = 0;
cdrom_lba_to_msf(pregap_lba_len, &min, &sec, &frame);
filestream_printf(file, " INDEX 00 00:00:00\n");
filestream_printf(file, " INDEX 01 %02u:%02u:%02u\n", (unsigned)min, (unsigned)sec, (unsigned)frame);
}
else
filestream_printf(file, " INDEX 01 00:00:00\n");
}
}
filestream_close(file);
}
state->file = NULL;
state->state = DUMP_STATE_NEXT_TRACK;
free(cue_data);
}
break;
case DUMP_STATE_NEXT_TRACK:
{
/* no file is open as we either just started or just finished a track, need to start dumping the next track */
state->cur_track++;
/* no more tracks to dump, we're done */
if (state->toc && state->cur_track > state->toc->num_tracks)
{
task_set_progress(task, 100);
return;
}
RARCH_LOG("[CDROM]: Dumping track %d...\n", state->cur_track);
memset(state->cdrom_path, 0, sizeof(state->cdrom_path));
cdrom_device_fillpath(state->cdrom_path, sizeof(state->cdrom_path), state->drive_letter[0], state->cur_track, false);
state->track_written_bytes = 0;
state->file = filestream_open(state->cdrom_path, RETRO_VFS_FILE_ACCESS_READ, 0);
/* open a new file for writing for this next track */
if (state->file)
{
char output_path[PATH_MAX_LENGTH];
char track_filename[PATH_MAX_LENGTH];
output_path[0] = track_filename[0] = '\0';
snprintf(track_filename, sizeof(track_filename), "%s (Track %02d).bin", state->title, state->cur_track);
state->cur_track_bytes = filestream_get_size(state->file);
fill_pathname_join(output_path,
directory_core_assets, track_filename, sizeof(output_path));
state->output_file = filestream_open(output_path, RETRO_VFS_FILE_ACCESS_WRITE, 0);
if (!state->output_file)
{
RARCH_ERR("[CDROM]: Error opening file for writing: %s\n", output_path);
task_set_progress(task, 100);
task_free_title(task);
task_set_title(task, strdup(msg_hash_to_str(MENU_ENUM_LABEL_VALUE_QT_FILE_WRITE_OPEN_FAILED)));
return;
}
}
else
{
RARCH_ERR("[CDROM]: Error opening file for writing: %s\n", state->cdrom_path);
task_set_progress(task, 100);
task_free_title(task);
task_set_title(task, strdup(msg_hash_to_str(MENU_ENUM_LABEL_VALUE_QT_FILE_WRITE_OPEN_FAILED)));
return;
}
state->state = DUMP_STATE_READ_TRACK;
}
break;
case DUMP_STATE_READ_TRACK:
/* read data from track and write it to a file in chunks */
if (state->cur_track_bytes > state->track_written_bytes)
{
char data[2352 * 2] = {0};
int64_t read_bytes = filestream_read(state->file, data, sizeof(data));
int progress = 0;
if (read_bytes <= 0)
{
task_set_progress(task, 100);
task_free_title(task);
task_set_title(task, strdup(msg_hash_to_str(MSG_DISC_DUMP_FAILED_TO_READ_FROM_DRIVE)));
return;
}
state->track_written_bytes += read_bytes;
state->disc_read_bytes += read_bytes;
progress = (state->disc_read_bytes / (double)state->disc_total_bytes) * 100.0;
#ifdef CDROM_DEBUG
RARCH_LOG("[CDROM]: Read %" PRId64 " bytes, totalling %" PRId64 " of %" PRId64 " bytes. Progress: %d%%\n", read_bytes, state->track_written_bytes, state->cur_track_bytes, progress);
#endif
if (filestream_write(state->output_file, data, read_bytes) <= 0)
{
task_set_progress(task, 100);
task_free_title(task);
task_set_title(task, strdup(msg_hash_to_str(MSG_DISC_DUMP_FAILED_TO_WRITE_TO_DISK)));
return;
}
task_set_progress(task, progress);
}
else if (state->cur_track_bytes == state->track_written_bytes)
{
/* TODO: FIXME: this stops after only the first track */
if (state->file)
{
filestream_close(state->file);
state->file = NULL;
}
if (state->output_file)
{
filestream_close(state->output_file);
state->file = NULL;
}
state->state = DUMP_STATE_NEXT_TRACK;
}
break;
}
}
static void task_cdrom_dump_callback(retro_task_t *task,
void *task_data,
void *user_data, const char *error)
{
task_cdrom_dump_state_t *state = (task_cdrom_dump_state_t*)task->state;
if (state)
free(state);
}
void task_push_cdrom_dump(const char *drive)
{
retro_task_t *task = task_init();
task_cdrom_dump_state_t *state = (task_cdrom_dump_state_t*)calloc(1, sizeof(*state));
state->drive_letter[0] = drive[0];
state->next = true;
state->cur_track = 0;
state->state = DUMP_STATE_TOC_PENDING;
fill_str_dated_filename(state->title, "cdrom", NULL, sizeof(state->title));
task->state = state;
task->handler = task_cdrom_dump_handler;
task->callback = task_cdrom_dump_callback;
task->title = strdup(msg_hash_to_str(MSG_DUMPING_DISC));
RARCH_LOG("[CDROM]: Starting disc dump...\n");
task_queue_push(task);
}
#endif