DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
dataread.cpp
1#pragma helper_mask_gpr 0x30
2#pragma helper_mask_fpr 0x1000
3#pragma name_counter 234
4
5#include "common.h"
6#include "sce/libcdvd.h"
7#include "sce/sifdev.h"
8#include "sce/sifrpc.h"
9
10#include <cassert>
11#include <cstdio>
12#include <cstring>
13
14#include "btsysscript.hpp"
15#include "cloth.hpp"
16#include "clothread.hpp"
17#include "dataalloc.hpp"
18#include "dataread.hpp"
19#include "dataset.hpp"
20#include "framevu1.hpp"
21#include "gameutil.hpp"
22#include "mathutil.hpp"
23#include "mglib.hpp"
24#include "savedata.hpp"
25#include "snd.hpp"
26#include "sound.hpp"
27#include "sysmes.hpp"
28
29/* One record of the archive's index file. The four numbers a read needs sit behind twelve bytes
30 the index does not use, and the first word is where the entry's name begins in the same file. */
32 int name;
33 int unk_04[3];
34 int offset;
35 int size;
36 int sector;
37 int sectors;
38};
39
40/* One file inside a pack. The name is the entry's own first bytes, everything it locates is a byte
41 offset from the entry rather than from the pack, and a first byte of zero ends the table — so a
42 pack can be walked without being told how many files it holds. */
43struct PACK_ENTRY {
44 char name[64];
45 int offset;
46 int size;
47 int next;
48};
49
50/* The index is turned into a tree of path components rather than a list of names, so a lookup
51 costs one search per component instead of one comparison per entry. A directory carries no
52 entry of its own and leaves the header null. */
59
60static char CurrentDir[256] = "y:/ps2/dc_data/";
61
62static int header_num;
63static u_int *packfile_buff;
64#ifndef PAL
65static NAME_TREE *tree;
66#endif
67static int data_sector;
68static int old_vsync;
69static int start_vsync;
70
71#ifdef PAL
72static u_char header_buff[0x50000];
73#else
74static u_char header_buff[0x40000];
75#endif
76static BG_READ_INFO bg_read_info[32];
77
78static NAME_TREE *search_tree(NAME_TREE *node, char *name);
79static int CDRead(char *path, u_int *buffer, int *out_size);
80
81#ifndef PAL
82static void copy_data_head(DATA_HEADER *dest, DATA_HEADER_READ *record) {
83 dest->name = record->name;
84 dest->offset = record->offset;
85 dest->size = record->size;
86 dest->sector = record->sector;
87 dest->sectors = record->sectors;
88}
89#endif
90
91#ifdef PAL
92/* The index is searched as the list it is on the disc, one name comparison per entry. */
93static DATA_HEADER *SearchFile(char *path) {
94 DATA_HEADER_READ *record;
95 int i;
96
97 record = (DATA_HEADER_READ *) header_buff;
98
99 for (i = 0; i < header_num; i++, record++) {
100 if (strcasecmp((char *) record->name, path) == 0) {
101 return (DATA_HEADER *) record;
102 }
103 }
104
105 return 0;
106}
107#else
108static DATA_HEADER *SearchFile(char *path) {
109 NAME_TREE *node;
110 char *word_end;
111 char ch;
112 char word[256];
113
114 word_end = word;
115 node = tree;
116
117 while ((ch = *path) != 0) {
118 if (ch == '/') {
119 *word_end = 0;
120 node = search_tree(node, word);
121
122 if (!node) {
123 return 0;
124 }
125
126 path++;
127 word_end = word;
128 } else {
129 *word_end = ch;
130 path++;
131 word_end++;
132 }
133 }
134
135 *word_end = 0;
136 node = search_tree(node, word);
137
138 if (!node) {
139 return 0;
140 }
141
142 return node->data;
143}
144#endif
145
146void InitReadBG() {
147 int i;
148
149 for (i = 0; i < 32; i++) {
150 bg_read_info[i].busy = false;
151 }
152
153 old_vsync = -1;
154 start_vsync = 0;
155}
156
157/* The buffer goes to the drive rather than through the processor, so an address the drive cannot
158 reach is fatal rather than slow, and one that is not on a 64-byte boundary is only reported. */
159int LoadFileBG(char *name, u_long128 *buffer, int *out_size) {
160 BG_READ_INFO *info;
161#ifdef PAL
162 DATA_HEADER_READ *header;
163#else
164 DATA_HEADER *header;
165#endif
166 int i;
167
168 if (out_size) {
169 *out_size = 0;
170 }
171
172 if (!name) {
173 return 0;
174 }
175
176 if (*name == 0) {
177 return 0;
178 }
179
180 info = bg_read_info;
181
182 for (i = 0; i < 32; i++, info++) {
183 if (info->busy == 0) {
184 break;
185 }
186 }
187
188 if (i == 32) {
189 return 0;
190 }
191
192 if ((int) buffer > 0x2000000) {
193 printf("address error\n");
194
195 for (;;)
196 ;
197 }
198
199 if ((int) buffer % 64) {
200 printf("/*/*/*/*/not 64byte align at %x %s\n", buffer, name);
201 }
202
203#ifdef PAL
204 header = (DATA_HEADER_READ *) SearchFile(name);
205
206#else
207 header = SearchFile(name);
208
209#endif
210 if (!header) {
211 return 0;
212 }
213
214 strcpy(info->name, name);
215 info->busy = true;
216 info->id = 0;
217 info->done = 0;
218 info->buffer = buffer;
219 info->size = header->size;
220
221 if (out_size) {
222 *out_size = header->size;
223 }
224
225 info->sector = header->sector + data_sector;
226 info->sectors = header->sectors;
227 return 1;
228}
229
230BG_READ_INFO *GetReadBGFile(int index) {
231 if (index < 0 || index >= 32) {
232 return 0;
233 }
234
235 return bg_read_info[index].busy ? &bg_read_info[index] : 0;
236}
237
238void StartReadBG() {
239 InitReadBG();
240}
241
242/* One read is issued per vertical sync and the same call collects it, so a queued file costs two
243 frames at best and the queue is walked from the front every time. */
244void ReadBG() {
245 BG_READ_INFO *info;
246 sceCdRMode mode;
247 int vsync;
248 int i;
249
250 vsync = MGGetVSyncCount();
251
252 if (old_vsync == vsync) {
253 return;
254 }
255
256 old_vsync = vsync;
257 start_vsync++;
258 mode.trycount = 0;
259 mode.spindlctrl = 1;
260 mode.datapattern = 0;
261 info = bg_read_info;
262
263 for (i = 0; i < 32; i++, info++) {
264 if (info->busy) {
265 if (info->id != 0 && info->done == 0) {
266 break;
267 }
268
269 if (info->id == 0 && info->done == 0) {
270 break;
271 }
272 }
273 }
274
275 if (i == 32) {
276 return;
277 }
278
279 if (info->id == 0) {
280 start_vsync = 0;
281 info->id = sceCdRead(info->sector, info->sectors, info->buffer, &mode);
282 } else if (info->id != 0) {
283 if (sceCdSync(1)) {
284 return;
285 }
286
287 if (sceCdGetError()) {
288 printf("error at %s\n", info->name);
289 info->id = 0;
290 return;
291 }
292
293 info->done = 1;
294 }
295}
296
297int ReadBGSync() {
298 int i;
299 BG_READ_INFO *info;
300
301 ReadBG();
302 info = bg_read_info;
303
304 for (i = 0; i < 32; i++, info++) {
305 if (info->busy) {
306 if (info->id == 0) {
307 break;
308 }
309
310 if (info->done == 0) {
311 break;
312 }
313 }
314 }
315
316 if (i == 32) {
317 return 0;
318 }
319
320 return 1;
321}
322
323void BreakReadBG() {
324 sceCdBreak();
325 InitReadBG();
326}
327
328/* A component is looked for in the whole subtree rather than among the node's own children, so a
329 path whose middle components are spelled wrong still finds its entry. */
330#ifndef PAL
331static NAME_TREE *search_tree(NAME_TREE *node, char *name) {
332 NAME_TREE *found;
333
334 if (strcasecmp(node->name, name) == 0) {
335 return node;
336 }
337
338 node = node->child;
339
340 while (node) {
341 found = search_tree(node, name);
342
343 if (found) {
344 return found;
345 }
346
347 node = node->next;
348 }
349
350 return 0;
351}
352#endif
353
354#ifndef PAL
355static void add_tree(NAME_TREE *parent, NAME_TREE *child_node) {
356 NAME_TREE *sibling;
357
358 sibling = parent->child;
359
360 if (sibling == 0) {
361 parent->child = child_node;
362 return;
363 }
364
365 for (;;) {
366 if (sibling->next == 0) {
367 sibling->next = child_node;
368 return;
369 }
370
371 sibling = sibling->next;
372 }
373}
374#endif
375
376/* The whole tree is built inside the one buffer it is handed: the nodes and their headers grow up
377 from the bottom and the names down from the top, so nothing is ever freed and the two meeting is
378 what the size report at the end is for. */
379#ifndef PAL
380static char *create_word_tree(char *index_image, int size, char *tree_buffer) {
381 DATA_HEADER_READ *record;
382 NAME_TREE *parent;
383 char *bottom;
384 char *top;
385 int i;
386 char *cursor;
387 NAME_TREE *node;
388 char *path;
389 char *word_end;
390 char ch;
391 char word[256];
392
393 bottom = tree_buffer;
394 top = tree_buffer + size - 1;
395 tree = (NAME_TREE *) tree_buffer;
396 tree->next = 0;
397 tree->child = 0;
398 tree->data = 0;
399 tree->name = tree_buffer + 16;
400 tree_buffer[16] = 0;
401 bottom += 32;
402 header_num = *(u_int *) index_image >> 5;
403
404 for (i = 0; i < header_num; i++) {
405 record = (DATA_HEADER_READ *) (index_image + i * 32);
406 path = (char *) (record->name + (int) index_image);
407 cursor = path;
408
409 while ((ch = *cursor) != 0) {
410 if (ch == '\\') {
411 *cursor = '/';
412 }
413
414 cursor++;
415 }
416
417 cursor = path;
418 word_end = word;
419 parent = tree;
420
421 for (;;) {
422 ch = *cursor;
423
424 if (ch == '/' || ch == 0) {
425 *word_end = 0;
426
427 if (word[0] == 0) {
428 break;
429 }
430
431 node = search_tree(parent, word);
432
433 if (!node) {
434 node = (NAME_TREE *) bottom;
435 memset(bottom, 0, 16);
436 bottom += 16;
437 top -= strlen(word) + 1;
438 strcpy(top, word);
439 node->name = top;
440 add_tree(parent, node);
441 }
442
443 parent = node;
444 word_end = word;
445
446 if (*cursor == 0) {
447 node->data = (DATA_HEADER *) bottom;
448 copy_data_head(node->data, record);
449 bottom += 20;
450 break;
451 }
452
453 cursor++;
454 } else {
455 *word_end = ch;
456 cursor++;
457 word_end++;
458 }
459 }
460 }
461
462 printf("file header size = %d\n", size - (top - bottom));
463 return tree_buffer;
464}
465#endif
466
467/* The drive is asked for the data file itself only to learn where it starts; everything after this
468 is read by sector from that base, which is why no path but the index's is ever opened. */
469#ifdef PAL
470/* The index is read straight into the header buffer and kept as it is, with each entry's name
471 offset turned into a pointer to the name and its path separators made forward slashes. */
472void InitCDFile() {
473 sceCdlFILE file;
474 int fd;
475 int size;
476 DATA_HEADER_READ *records;
477 int i;
478
479 packfile_buff = 0;
480
481 while (1) {
482 if (sceCdSearchFile(&file, "\\DATA.DAT;1")) {
483 sceCdSync(0);
484
485 if (sceCdGetError() == 0) {
486 break;
487 }
488 }
489 }
490
491 data_sector = file.lsn;
492 fd = sceOpen("cdrom0:\\DATA.HD2;1", SCE_RDONLY);
493
494 if (fd < 0) {
495 printf("File open error \"\"\n \n \n");
496 __assert("etc.cpp", 565, "FALSE");
497 }
498
499 size = sceLseek(fd, 0, SCE_SEEK_END);
500 sceLseek(fd, 0, SCE_SEEK_SET);
501 sceRead(fd, header_buff, size);
502 sceClose(fd);
503 records = (DATA_HEADER_READ *) header_buff;
504 // The names follow the last record, so the first name's offset counts the records.
505 header_num = (u_int) records->name >> 5;
506
507 for (i = 0; i < header_num; i++) {
508 char *cursor;
509 char ch;
510
511 records[i].name += (int) header_buff;
512
513 for (cursor = (char *) records[i].name; (ch = *cursor) != 0; cursor++) {
514 if (ch == '\\') {
515 *cursor = '/';
516 }
517 }
518 }
519}
520#else
521void InitCDFile() {
522 char index_image[307200];
523 sceCdlFILE file;
524 int fd;
525 int size;
526
527 packfile_buff = 0;
528
529 while (1) {
530 if (sceCdSearchFile(&file, "\\DATA.DAT;1")) {
531 sceCdSync(0);
532
533 if (sceCdGetError() == 0) {
534 break;
535 }
536 }
537 }
538
539 data_sector = file.lsn;
540 fd = sceOpen("cdrom0:\\DATA.HD2;1", SCE_RDONLY);
541
542 if (fd < 0) {
543 printf("File open error \"\"\n \n \n");
544 /* The file and line the original's assertion carries are spelled out: a reconstruction
545 whose lines fall elsewhere cannot reach them through __FILE__ and __LINE__. */
546 __assert("etc.cpp", 556, "FALSE");
547 }
548
549 size = sceLseek(fd, 0, SCE_SEEK_END);
550 sceLseek(fd, 0, SCE_SEEK_SET);
551 sceRead(fd, index_image, size);
552 sceClose(fd);
553 create_word_tree(index_image, sizeof header_buff, (char *) header_buff);
554}
555#endif
556
557void InitMemoryFile() {
558}
559
560int LoadFile(char *path, void *buffer, int *out_size) {
561 if (!LoadFile2(path, buffer, out_size, 0)) {
562 printf("File open error \"%s\"\n \n \n", path);
563#ifdef PAL
564 __assert("etc.cpp", 753, "FALSE");
565#else
566 __assert("etc.cpp", 740, "FALSE");
567#endif
568 }
569
570 return 1;
571}
572
573/* A name may carry a device in front of a colon, which is dropped: everything the game ships with
574 is on the disc, and the development tree the other devices reached is what CurrentDir names. */
575int LoadFile2(char *path, void *buffer, int *out_size, int mode) {
576 char *cursor;
577 char *device_end;
578 char *file_name;
579 char ch;
580 int host_file;
581 char device[256];
582
583 if (out_size) {
584 *out_size = 0;
585 }
586
587 /* The two devices the development build could read through instead of the disc. Nothing left
588 here reads either one; they are what the test below was written against. */
589 char sim[16] = "sim:";
590 char host[16] = "host:";
591
592 cursor = path;
593 device_end = device;
594
595 while ((ch = *cursor) != 0) {
596 if (ch == ':') {
597 break;
598 }
599
600 *device_end = ch;
601 device_end++;
602 cursor++;
603 }
604
605 if (ch) {
606 file_name = cursor + 1;
607 } else {
608 file_name = path;
609 }
610
611 /* Reading through the host machine is what the development tree was for, and the retail build
612 keeps the question and does nothing with the answer. */
613 if (memcmp(device, "host", 4) != 0 && memcmp(CurrentDir, "host:", 4) == 0) {
614 host_file = 1;
615 }
616
617 if ((int) buffer > 0x2000000) {
618 printf("address error\n");
619
620 for (;;)
621 ;
622 }
623
624 char full_path[256] = "";
625
626 strcat(full_path, file_name);
627 return CDRead(full_path, (u_int *) buffer, out_size);
628}
629
630static int CDRead(char *path, u_int *buffer, int *out_size) {
631#ifdef PAL
632 DATA_HEADER_READ *header;
633#else
634 DATA_HEADER *header;
635#endif
636 sceCdRMode mode;
637
638#ifdef PAL
639 header = (DATA_HEADER_READ *) SearchFile(path);
640
641#else
642 header = SearchFile(path);
643
644#endif
645 if (!header) {
646 return 0;
647 }
648
649 mode.trycount = 0;
650 mode.spindlctrl = 1;
651 mode.datapattern = 0;
652
653 while (1) {
654 if (sceCdRead(header->sector + data_sector, header->sectors, buffer, &mode)) {
655 sceCdSync(0);
656
657 if (sceCdGetError() == 0) {
658 break;
659 }
660 }
661 }
662
663 if (out_size) {
664 *out_size = header->size;
665 }
666
667 return 1;
668}
669
670int WriteFile(char *path, void *buffer, int size) {
671 int fd;
672
673 fd = sceOpen(path, SCE_WRONLY | SCE_CREAT | SCE_TRUNC);
674
675 if (fd < 0) {
676 return 0;
677 }
678
679 sceWrite(fd, buffer, size);
680 sceClose(fd);
681 return 1;
682}
683
684int LoadPackFile(char *path, u_int *buffer, int *out_size) {
685 packfile_buff = buffer;
686
687 if (!LoadFile2(path, buffer, out_size, 0)) {
688 packfile_buff = 0;
689 return 0;
690 }
691
692 return 1;
693}
694
695u_int *GetPackFile(char *name, int *out_size) {
696 return GetPackFile(packfile_buff, name, out_size);
697}
698
699/* A pack is looked up by the last component of a path, so a caller may name a file the way the
700 archive spells it and still find it inside the pack it was loaded from. */
701u_int *GetPackFile(u_int *pack, char *name, int *out_size) {
702 char *base_name;
703 PACK_ENTRY *entry;
704 u_int *data;
705 char ch;
706
707 if (!pack) {
708 return 0;
709 }
710
711 base_name = name;
712
713 while ((ch = *name) != 0) {
714 if (ch == '/') {
715 base_name = name + 1;
716 }
717
718 name++;
719 }
720
721 entry = (PACK_ENTRY *) pack;
722
723 while (entry->name[0]) {
724 if (strcasecmp(entry->name, base_name) == 0) {
725 data = (u_int *) ((char *) entry + entry->offset);
726
727 if (out_size) {
728 *out_size = entry->size;
729 }
730
731 return data;
732 }
733
734 entry = (PACK_ENTRY *) ((char *) entry + entry->next);
735 }
736
737 return 0;
738}
739
740u_int *GetPackFile(u_int *pack, int index, char **out_name, int *out_size) {
741 PACK_ENTRY *entry;
742 u_int *data;
743 int i;
744
745 if (!pack) {
746 return 0;
747 }
748
749 entry = (PACK_ENTRY *) pack;
750 i = 0;
751
752 while (entry->name[0]) {
753 if (index == i) {
754 data = (u_int *) ((char *) entry + entry->offset);
755
756 if (out_size) {
757 *out_size = entry->size;
758 }
759
760 *out_name = entry->name;
761 return data;
762 }
763
764 entry = (PACK_ENTRY *) ((char *) entry + entry->next);
765 i++;
766 }
767
768 return 0;
769}
770
771/* Every file of one extension at once, which is how a pack of animations or textures is taken
772 whole. The caller says how many it has room for and gets back how many it was given. */
773int GetPackFileExt(u_int *pack, char *extension, u_int **files, int max_files, int *sizes, char **names) {
774 int found_count;
775 int i;
776 u_int *data;
777 char *ext_start;
778 char ch;
779 int size;
780 char *name;
781
782 found_count = 0;
783 i = 0;
784
785 for (;;) {
786 data = GetPackFile(pack, i, &name, &size);
787
788 if (!data) {
789 break;
790 }
791
792 ext_start = name;
793
794 while ((ch = *ext_start) != 0) {
795 if (ch == '.') {
796 ext_start++;
797 break;
798 }
799
800 ext_start++;
801 }
802
803 if (strcasecmp(extension, ext_start) == 0) {
804 files[found_count] = data;
805
806 if (names) {
807 names[found_count] = name;
808 }
809
810 if (sizes) {
811 sizes[found_count] = size;
812 }
813
814 found_count++;
815
816 if (found_count >= max_files) {
817 break;
818 }
819 }
820
821 i++;
822 }
823
824 return found_count;
825}
char name[128]
Definition dataread.hpp:28
u_long128 * buffer
Definition dataread.hpp:29
NAME_TREE * next
Definition dataread.cpp:57
NAME_TREE * child
Definition dataread.cpp:56
char * name
Definition dataread.cpp:54
DATA_HEADER * data
Definition dataread.cpp:55
Definition dataread.cpp:43
int next
Definition dataread.cpp:47
int size
Definition dataread.cpp:46
char name[64]
Definition dataread.cpp:44
int offset
Definition dataread.cpp:45
u_int lsn
Definition libcdvd.h:9