DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
memorycardaccess.cpp
1#include "memorycardaccess.hpp"
2
3#include <cstdio>
4#include <cstring>
5
6#include "menu_draw.hpp"
7#include "savedata.hpp"
8
9MC_DIR_ENTRY SaveFileInfo[MC_DIR_ENTRY_MAX];
10char mcdmybuf[0x40];
11SV_CONFIG_SYS sys_config;
12
13void InitSaveFileInfoTbl() {
14 for (int i = 0; i < MC_DIR_ENTRY_MAX; i++) {
15 memset(&SaveFileInfo[i], 0, sizeof(SaveFileInfo[i]));
16 SaveFileInfo[i].name[0] = 0;
17 }
18}
19
20int GetOpenAttribute(char *name) {
21 for (int i = 0; i < MC_DIR_ENTRY_MAX; i++) {
22 char *entry = SaveFileInfo[i].name;
23
24 if (strcmp(name, entry) == 0) {
25 printf("same file existed!!!!!!\n");
26 return 1;
27 }
28 }
29
30 return 0;
31}
32
34#ifdef PAL
35 strcpy(this->dir_name, "BESCES-50295dkcloud");
36#else
37 switch (GetMenuLangFlag()) {
38 case LANG_JAPANESE:
39 strcpy(this->dir_name, "BISCPS-15004dkcloud");
40 break;
41 case LANG_ENGLISH_US:
42 default:
43 strcpy(this->dir_name, "BASCUS-97111dkcloud");
44 break;
45 }
46
47#endif
48 strcpy(this->file_name, "darkcloud");
49
50 for (int i = 0; i < 0x40; i++) {
51 this->current_dir[i] = 0;
52 }
53
54 this->port = 0;
55 this->file_no = 0;
56 this->fd = -1;
57 memset(&this->error, 0, sizeof(this->error));
58 this->SetVersion("darkcloudVer1.9");
59 this->func_no = MC_OPERATION_IDLE;
60 this->idle_code = 0x3D;
61 this->step = 0;
62 this->save_buffer = NULL;
63 this->load_buffer = NULL;
64 this->read_buffer = NULL;
65 this->dir_table = SaveFileInfo;
66 this->transferred = 0;
67 this->transfer_size = 0;
68 memset(this->card, 0, sizeof(this->card));
69 memset(this->file_info, 0, sizeof(this->file_info));
70 memset(&this->icon, 0, sizeof(this->icon));
71 this->card[0].present = 1;
72 this->card[1].present = 1;
73 printf("SaveData size = %d\n", sizeof(CSaveData));
74}
75
77 int status = sceMcInit();
78 this->Initialize();
79 InitSaveFileInfoTbl();
80
81 int result;
82
83 switch (status) {
84 case sceMcIniSucceed:
85 printf("Memory Card Initialized Successed!!\n\n\n\n");
86 result = 0;
87 break;
88 case sceMcIniErrKernel:
89 result = 1;
90 printf("Initialized Failed!!\n");
91 break;
92 case sceMcIniOldMcserv:
93 result = 1;
94 printf("mcserv.irx is old file\n");
95 break;
96 case sceMcIniOldMcman:
97 result = 1;
98 printf("mcman.irx is old file \n");
99 break;
100 }
101
102 return result;
103}
104
105void CMemoryCardAccess::SetBuff(char *buffer) {
106 char *data;
107 char *sum;
108 u32 i;
109 char total;
110
111 buffer = (char *) ((((int) buffer >> 6) + 1) << 6);
112 this->save_buffer = (CSaveData *) buffer;
113 memcpy(this->save_buffer, SaveData, 0x131C0);
114 this->save_buffer->ConvertConfig(&sys_config);
115 char *version = (char *) this->save_buffer + 0x131C0;
116 strcpy(version, this->version);
117 this->check_sum = version + 0x20;
118 data = (char *) this->save_buffer;
119 sum = this->check_sum;
120 memset(sum, 0, 0x4C7);
121 total = 0;
122
123 for (i = 0; i < 0x131C0; i++) {
124 total += *data++;
125
126 if ((int) i % 64 == 63) {
127 *sum++ = total;
128 total = 0;
129 }
130 }
131
132 this->read_buffer = (char *) ((((int) sum >> 6) + 1) << 6);
133 this->load_buffer = this->read_buffer;
134}
135
137 memcpy(&this->icon.view, &icon->view, sizeof(MC_ICON_FILE));
138 memcpy(&this->icon.copy, &icon->copy, sizeof(MC_ICON_FILE));
139 memcpy(&this->icon.del, &icon->del, sizeof(MC_ICON_FILE));
140}
141
143 int bg_color[4][4] = {
144 {0x80, 0, 0x40, 0},
145 {0, 0x80, 0, 0},
146 {0, 0, 0x80, 0},
147 {0x80, 0x80, 0x80, 0},
148 };
149 float light_dir[3][4] = {
150 {0.5f, 0.5f, 0.5f, 0.0f},
151 {0.0f, -0.4f, -0.1f, 0.0f},
152 {-0.5f, -0.5f, 0.5f, 0.0f},
153 };
154 float light_color[3][4] = {
155 {0.48f, 0.48f, 0.03f, 0.0f},
156 {0.5f, 0.33f, 0.2f, 0.0f},
157 {0.14f, 0.14f, 0.38f, 0.0f},
158 };
159 float ambient[4] = {0.5f, 0.5f, 0.5f, 0.0f};
160
161 memset(&this->icon_sys, 0, sizeof(sceMcIconSys));
162 strcpy(this->icon_sys.head, "PS2D");
163 // "Dark Cloud Data" in full-width Shift-JIS.
164 strcpy((char *) this->icon_sys.title_name, "\x82\x63\x82\x81\x82\x92\x82\x8B\x81\x40\x82\x62\x82\x8C\x82\x8F"
165 "\x82\x95\x82\x84\x81\x40\x82\x63\x82\x81\x82\x94\x82\x81");
166 this->icon_sys.nl_offset = 20;
167 this->icon_sys.trans_rate = 0x60;
168 // Only the first entry of each table is copied; the rest of the image stays zero.
169 memcpy(this->icon_sys.bg_color, bg_color, 16);
170 memcpy(this->icon_sys.light_dir, light_dir, 16);
171 memcpy(this->icon_sys.light_color, light_color, 16);
172 memcpy(this->icon_sys.ambient, ambient, 16);
173 strcpy(this->icon_sys.fname_view, this->icon.view.name);
174 strcpy(this->icon_sys.fname_copy, this->icon.copy.name);
175 strcpy(this->icon_sys.fname_del, this->icon.del.name);
176}
177
179 this->func_no = func_no;
180 this->step = 0;
181
182 if (func_no == MC_OPERATION_IDLE) {
183 this->idle_code = 0x3D;
184 }
185
186 sceMcSync(MC_NOWAIT, NULL, NULL);
187}
188
190 return this->func_no;
191}
192
194 int result;
195 int target_file;
196
197 result = 0;
198
199 switch (func_no) {
200 case MC_OPERATION_SEARCH_TYPE:
201 result = SearchMcType();
202 break;
203 case MC_OPERATION_GET_DIR:
204 result = GetDir();
205 break;
206 case MC_OPERATION_MAKE_DIR:
207 result = MakeDir();
208 break;
209 case MC_OPERATION_GET_ALL_SAVE_FILE_INFO:
210 result = GetAllSaveFileInfo();
211 break;
212 case MC_OPERATION_SAVE:
213 result = SaveToMc(file_no);
214 break;
215 case MC_OPERATION_LOAD:
216 result = LoadFromMc(file_no);
217 break;
218 case MC_OPERATION_FORMAT:
219 result = FormatForMc();
220 break;
221 case MC_OPERATION_UNFORMAT:
222 result = McUnFormatForDebug();
223 break;
224 case MC_OPERATION_DELETE:
225 if (error.code == MC_ERROR_NONE) {
226 target_file = file_no;
227 } else {
228 target_file = error.file_no;
229 }
230
231 result = DeleteFile(target_file);
232 break;
233 case MC_OPERATION_LOAD_CONFIG:
234 result = LoadSysConfig();
235 break;
236 case MC_OPERATION_SAVE_CONFIG:
237 result = SaveSysConfig();
238 break;
239 case MC_OPERATION_WRITE_TEST:
240 result = Write();
241 break;
242 case MC_OPERATION_CONVERT:
243 result = Convert();
244 break;
245 }
246
247 if (result == 1) {
248 step = 0;
249 SetFuncNo(MC_OPERATION_IDLE);
250 } else {
251 McError(result);
252 }
253
254 return result;
255}
256
257void CMemoryCardAccess::SetVersion(char *version) {
258 strcpy(this->version, version);
259}
260
262 return this->version;
263}
264
266 static int old_format;
268 int cmd;
269 int status;
270
271 card = &this->card[this->port];
272
273 if (this->step == 0) {
274 old_format = card->formatted;
275 }
276
277 switch (this->step % 2) {
278 case 0:
279 memset(&card->present, 0, sizeof(card->present));
280
281 if (sceMcGetInfo(this->port, 1, &card->type, &card->free_size, &card->formatted) == 0) {
282 this->step++;
283 } else {
284 this->step += 2;
285 }
286
287 break;
288 case 1:
289 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
290 break;
291 }
292
293 if (cmd != 1) {
294 sceMcSync(MC_NOWAIT, NULL, NULL);
295 break;
296 }
297
298 card->present = 1;
299 card->result = status;
300
301 switch (status) {
302 case sceMcResSucceed:
303 break;
304 case sceMcResChangedCard:
305 card->formatted = 1;
306 break;
307 case sceMcResNoFormat:
308 card->formatted = 0;
309 break;
310 default:
311 if (status < -10) {
312 card->present = 0;
313 }
314
315 break;
316 }
317
318 this->step++;
319
320 if (this->step >= 2 && card->present && card->formatted) {
321 if (old_format && card->formatted) {
322 card->format_change = 0;
323 }
324
325 if (!old_format && card->formatted) {
326 card->format_change = 1;
327 }
328
329 return 1;
330 }
331
332 if (this->step >= 16) {
333 if (old_format && card->formatted) {
334 card->format_change = 0;
335 }
336
337 if (!old_format && !card->formatted) {
338 card->format_change = 0;
339 }
340
341 if (!old_format && card->formatted) {
342 card->format_change = 1;
343 }
344
345 if (old_format && !card->formatted) {
346 card->format_change = -1;
347 }
348
349 return 1;
350 }
351
352 break;
353 }
354
355 return 0;
356}
357
360 char name[0x40];
361 char path[0x40];
362 char dir[0x80];
363 char pattern[0x48];
364 int cmd;
365 int result;
366
367 card = &this->card[this->port];
368
369 switch (this->step) {
370 case 0:
371 strcpy(name, this->dir_name);
372 strcpy(path, "/");
373 strcat(path, this->dir_name);
374 InitSaveFileInfoTbl();
375 card->dir_exists = 0;
376 this->step++;
377 break;
378 case 1:
379 strcpy(dir, "/");
380 strcat(dir, this->dir_name);
381
382 if (sceMcChdir(this->port, 1, dir, this->current_dir) == 0) {
383 this->step++;
384 } else {
385 return -1;
386 }
387
388 break;
389 case 2:
390 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
391 break;
392 }
393
394 if (cmd != 0xC) {
395 return -1;
396 }
397
398 if (result < 0) {
399 if (result == sceMcResNoEntry || result == sceMcResNoFormat) {
400 return 1;
401 }
402
403 if (result < -9) {
404 return -1;
405 }
406 }
407
408 card->dir_exists = 1;
409 result = 0;
410 strcpy(pattern, this->file_name);
411 strcat(pattern, "*");
412
413 if (sceMcGetDir(this->port, 1, pattern, 0, 15, SaveFileInfo) == 0) {
414 this->step++;
415 } else {
416 return -1;
417 }
418
419 break;
420 case 3:
421 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
422 break;
423 }
424
425 if (cmd != 0xD || (cmd == 0xD && result < 0)) {
426 return -1;
427 }
428
429 card->dir_entries = result;
430 return 1;
431 }
432
433 return 0;
434}
435
437 int port_no;
438 int cmd;
439 int result;
440 int i;
441
442 port_no = this->port;
443
444 switch (this->step) {
445 case 0:
446 if (sceMcOpen(port_no, 1, this->dir_name, 1) == 0) {
447 this->step++;
448 } else {
449 return -1;
450 }
451
452 break;
453 case 1:
454 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
455 break;
456 }
457
458 if (cmd != 2 || (cmd == 2 && result < 0)) {
459 return -1;
460 }
461
462 this->fd = result;
463 this->transfer_size = 0x40;
464 memset(&sys_config, 0, this->transfer_size + 0x40);
465 sys_config.values[17] = 1;
466 this->transferred = 0;
467
468 if (sceMcRead(this->fd, &sys_config, this->transfer_size) == 0) {
469 this->step++;
470 } else {
471 return -1;
472 }
473
474 break;
475 case 2:
476 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
477 break;
478 }
479
480 if (cmd != 5 || (cmd == 5 && result < 0)) {
481 return -1;
482 }
483
484 this->transferred += result;
485
486 if (this->transferred < this->transfer_size) {
487 break;
488 }
489
490 if (sceMcClose(this->fd) == 0) {
491 this->step++;
492 } else {
493 return -1;
494 }
495
496 break;
497 case 3:
498 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
499 break;
500 }
501
502 if (cmd != 3 || (cmd == 3 && result < 0)) {
503 return -1;
504 }
505
506 if (SaveData->InvertConfig(&sys_config) != 0) {
507 for (i = 0; i < 12; i++) {
508 }
509 }
510
511 return 1;
512 }
513
514 return 0;
515}
516
518 int port_no;
519 int i;
520 int cmd;
521 int result;
522
523 port_no = this->port;
524
525 switch (this->step) {
526 case 0:
527 if (sceMcOpen(port_no, 1, this->dir_name, 2) == 0) {
528 this->step++;
529 } else {
530 return -1;
531 }
532
533 break;
534 case 1:
535 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
536 break;
537 }
538
539 if (cmd != 2 || (cmd == 2 && result < 0)) {
540 return -1;
541 }
542
543 this->fd = result;
544 this->fd = result;
545 this->transferred = 0;
546 this->transfer_size = 0x40;
547
548 for (i = 0; i < 18; i++) {
549 printf("config%d = %d\n", i, sys_config.values[i]);
550 }
551
552 if (sceMcWrite(this->fd, &sys_config, this->transfer_size) == 0) {
553 this->step++;
554 } else {
555 return -1;
556 }
557
558 break;
559 case 2:
560 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
561 break;
562 }
563
564 if (cmd != 6 || (cmd == 6 && result < 0)) {
565 return -1;
566 }
567
568 this->transferred += result;
569
570 if (this->transferred < this->transfer_size) {
571 break;
572 }
573
574 if (sceMcFlush(this->fd) == 0) {
575 this->step++;
576 } else {
577 return -1;
578 }
579
580 break;
581 case 3:
582 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
583 break;
584 }
585
586 if (cmd != 0xA || (cmd == 0xA && result < 0)) {
587 return -1;
588 }
589
590 if (sceMcClose(this->fd) == 0) {
591 this->step++;
592 } else {
593 return -1;
594 }
595
596 break;
597 case 4:
598 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
599 break;
600 }
601
602 if (cmd != 3 || (cmd == 3 && result < 0)) {
603 return -1;
604 }
605
606 return 1;
607 }
608
609 return 0;
610}
611
613 char buffer[0x198];
614 int result;
615 int cmd;
616
617 result = 0;
618 sceMcChdir(this->port, 1, "/", NULL);
619 sceMcSync(MC_WAIT, &cmd, &result);
620 sceMcOpen(this->port, 1, "test", 0x202);
621 sceMcSync(MC_WAIT, &cmd, &result);
622 this->fd = result;
623 sceMcWrite(this->fd, buffer, 0x76F800);
624 sceMcSync(MC_WAIT, &cmd, &result);
625 sceMcFlush(this->fd);
626 sceMcSync(MC_WAIT, &cmd, &result);
627 sceMcClose(this->fd);
628 sceMcSync(MC_WAIT, &cmd, &result);
629 return 1;
630}
631
633 int result;
634 int cmd;
635 char src_name[0x80];
636 char dst_name[0x80];
637 char buffer[0x19000];
638 int i;
639 int fd;
640
641 sceMcChdir(this->port, 1, "/", "");
642 sceMcSync(MC_WAIT, &cmd, &result);
643 printf("move to dir\n");
644
645 while (this->MakeDir() == 0) {
646 }
647
648 printf("create new dir\n");
649
650 for (i = 0; i < MC_SAVE_FILE_MAX; i++) {
651 strcpy(src_name, "BASCUS-97112dkcloud/");
652 strcat(src_name, this->file_name);
653 strcat(src_name, "%d");
654 sprintf(src_name, src_name, i);
655 strcpy(dst_name, "BASCUS-97111dkcloud/");
656 strcat(dst_name, this->file_name);
657 strcat(dst_name, "%d");
658 sprintf(dst_name, dst_name, i);
659
660 if (sceMcOpen(this->port, 1, src_name, 1) != 0) {
661 continue;
662 }
663
664 sceMcSync(MC_WAIT, &cmd, &result);
665
666 if (cmd != 2 || (cmd == 2 && result < 0)) {
667 continue;
668 }
669
670 fd = result;
671 printf("fname = %s\n", src_name);
672 memset(this->load_buffer, 0, 0x136E7);
673 sceMcRead(fd, this->load_buffer, 0x136A7);
674 sceMcSync(MC_WAIT, &cmd, &result);
675 sceMcClose(fd);
676 sceMcSync(MC_WAIT, &cmd, &result);
677
678 if (sceMcOpen(this->port, 1, dst_name, 0x202) != 0) {
679 continue;
680 }
681
682 sceMcSync(MC_WAIT, &cmd, &result);
683
684 if (cmd != 2 || (cmd == 2 && result < 0)) {
685 continue;
686 }
687
688 fd = result;
689 printf("fname = %s\n", dst_name);
690 memset(buffer, 0, 0x136E7);
691 sceMcWrite(fd, this->load_buffer, 0x136A7);
692 sceMcSync(MC_WAIT, &cmd, &result);
693 sceMcFlush(fd);
694 sceMcSync(MC_WAIT, &cmd, &result);
695 printf("save %d\n", i);
696 sceMcClose(fd);
697 sceMcSync(MC_WAIT, &cmd, &result);
698 }
699
700 return 1;
701}
702
704 int status = 0;
705 static int iconNo = -1;
706 char path[128];
707 int cmd;
708
709 switch (this->step) {
710 case 0:
711 if (sceMcMkdir(this->port, 1, this->dir_name) == 0) {
712 this->step++;
713 } else {
714 return -1;
715 }
716
717 break;
718 case 1:
719 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
720 break;
721 }
722
723 if (cmd != 0xB) {
724 break;
725 }
726
727 if (cmd == 0xB && status < 0) {
728 return -1;
729 }
730
731 strcpy(path, this->dir_name);
732 strcat(path, "/");
733
734 if (sceMcChdir(this->port, 1, path, this->current_dir) == 0) {
735 this->step++;
736 } else {
737 return -1;
738 }
739
740 break;
741 case 2:
742 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
743 break;
744 }
745
746 if (cmd != 0xC || (cmd == 0xC && status < 0)) {
747 return -1;
748 }
749
750 cmd = sceMcOpen(this->port, 1, this->dir_name, 0x202);
751
752 if (cmd == 0) {
753 this->step++;
754 } else {
755 return -1;
756 }
757
758 break;
759 case 3:
760 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
761 break;
762 }
763
764 if (cmd != 2 || (cmd == 2 && status < 0)) {
765 return -1;
766 }
767
768 this->fd = status;
769 this->transferred = 0;
770 this->transfer_size = sizeof(mcdmybuf);
771 memset(mcdmybuf, 0, sizeof(mcdmybuf));
772 mcdmybuf[0x11] = 1;
773
774 if (sceMcWrite(this->fd, mcdmybuf, this->transfer_size) == 0) {
775 this->step++;
776 } else {
777 return -1;
778 }
779
780 break;
781 case 4:
782 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
783 break;
784 }
785
786 if (cmd != 6 || (cmd == 6 && status < 0)) {
787 return -1;
788 }
789
790 this->transferred += status;
791 printf("read size = %d\n", this->transfer_size);
792
793 if (this->transferred < this->transfer_size) {
794 break;
795 }
796
797 if (sceMcClose(this->fd) == 0) {
798 this->step++;
799 } else {
800 return -1;
801 }
802
803 break;
804 case 5:
805 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
806 break;
807 }
808
809 if (cmd != 3 || (cmd == 3 && status < 0)) {
810 return -1;
811 }
812
813 if (sceMcOpen(this->port, 1, "icon.sys", 0x202) == 0) {
814 iconNo = -1;
815 this->step++;
816 } else {
817 return -1;
818 }
819
820 break;
821 case 6:
822 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
823 break;
824 }
825
826 if (cmd != 2 || (cmd == 2 && status < 0)) {
827 printf("open error = %d\n", status);
828 return -1;
829 }
830
831 this->fd = status;
832 this->transferred = 0;
833 this->transfer_size = sizeof(sceMcIconSys);
834
835 if (sceMcWrite(this->fd, &this->icon_sys, this->transfer_size) == 0) {
836 this->step++;
837 } else {
838 return -1;
839 }
840
841 break;
842 case 7:
843 case 11:
844 case 15:
845 case 19:
846 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
847 break;
848 }
849
850 if (cmd != 6 || (cmd == 6 && status < 0)) {
851 return -1;
852 }
853
854 this->transferred += status;
855
856 if (this->transferred < this->transfer_size) {
857 break;
858 }
859
860 if (sceMcFlush(this->fd) == 0) {
861 this->step++;
862 } else {
863 return -1;
864 }
865
866 break;
867 case 8:
868 case 12:
869 case 16:
870 case 20:
871 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
872 break;
873 }
874
875 if (cmd != 0xA || (cmd == 0xA && status < 0)) {
876 return -1;
877 }
878
879 cmd = sceMcClose(this->fd);
880
881 if (cmd == 0) {
882 this->step++;
883 } else {
884 return -1;
885 }
886
887 break;
888 case 9:
889 case 13:
890 case 17:
891 case 21:
892 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
893 break;
894 }
895
896 if (cmd != 3 || (cmd == 3 && status < 0)) {
897 return -1;
898 }
899
900 iconNo++;
901
902 if (iconNo < 3) {
903 cmd = sceMcOpen(this->port, 1, (&this->icon.view)[iconNo].name, 0x203);
904
905 if (cmd == 0) {
906 this->step++;
907 } else {
908 return -1;
909 }
910 } else {
911 return 1;
912 }
913
914 break;
915 case 10:
916 case 14:
917 case 18:
918 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
919 break;
920 }
921
922 if (cmd != 2 || (cmd == 2 && status < 0)) {
923 return -1;
924 }
925
926 this->fd = status;
927 this->transferred = 0;
928 this->transfer_size = (&this->icon.view)[iconNo].size;
929 cmd = sceMcWrite(this->fd, (&this->icon.view)[iconNo].data, this->transfer_size);
930
931 if (cmd == 0) {
932 this->step++;
933 } else {
934 return -1;
935 }
936
937 break;
938 }
939
940 return 0;
941}
942
944 char name[0x28];
945 int cmd;
946 int result;
947 char *data;
948 SAVEDATA_INFO *info;
949 int i;
951 int dungeon;
952
953 info = &this->file_info[file_no];
954
955 switch ((this->step - 1) % 4) {
956 case 0:
957 strcpy(name, this->file_name);
958 strcat(name, "%d");
959 sprintf(name, name, file_no);
960
961 if (sceMcOpen(this->port, 1, name, 1) == 0) {
962 this->step++;
963 } else {
964 return -1;
965 }
966
967 break;
968 case 1:
969 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
970 break;
971 }
972
973 if (cmd != 2) {
974 return -1;
975 }
976
977 if (result < 0) {
978 if (result == sceMcResNoEntry || result == sceMcResNoFormat) {
979 printf("not found\n");
980 info->state = 0;
981 this->step += 3;
982 return 1;
983 }
984
985 return -1;
986 }
987
988 this->fd = result;
989 this->transferred = 0;
990 this->transfer_size = 0x136A7;
991 this->error.retry_count = 0;
992 memset((void *) this->read_buffer, 0, this->transfer_size);
993
994 if (sceMcRead(this->fd, this->read_buffer, this->transfer_size) == 0) {
995 this->step++;
996 } else {
997 return -1;
998 }
999
1000 break;
1001 case 2:
1002 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1003 break;
1004 }
1005
1006 if (cmd != 5 || (cmd == 5 && result < 0)) {
1007 return -1;
1008 }
1009
1010 if (this->transferred < this->transfer_size) {
1011 this->error.retry_count++;
1012
1013 if (this->error.retry_count > 100) {
1014 printf("getinfo read error \n");
1015
1016 if (sceMcClose(this->fd) == 0) {
1017 this->step++;
1018 } else {
1019 return -1;
1020 }
1021
1022 break;
1023 }
1024 }
1025
1026 this->transferred += result;
1027
1028 if (this->transferred < this->transfer_size) {
1029 break;
1030 }
1031
1032 if (sceMcClose(this->fd) == 0) {
1033 this->step++;
1034 } else {
1035 return -1;
1036 }
1037
1038 break;
1039 case 3:
1040 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1041 break;
1042 }
1043
1044 if (cmd != 3 || (cmd == 3 && result < 0)) {
1045 return -1;
1046 }
1047
1048 i = 0;
1049 data = this->read_buffer;
1050
1051 while (i < this->transfer_size) {
1052 if (memcmp(data, "darkcloud", 9) == 0) {
1053 i = this->transfer_size;
1054 break;
1055 }
1056
1057 data++;
1058 i++;
1059 }
1060
1061 error = &this->error;
1062
1063 if (*data == 'd' && strcmp(this->GetVersion(), data) != 0) {
1064 this->step++;
1065 error->step = this->step;
1066 error->file_no = file_no;
1067 error->code = MC_ERROR_VERSION;
1068 return 1;
1069 }
1070
1071 if (this->transferred < this->transfer_size) {
1072 error->code = MC_ERROR_SHORT_READ;
1073 error->file_no = this->file_no;
1074 this->step++;
1075 printf("not enough size\n");
1076 return 1;
1077 }
1078
1079 info->state = 1;
1080 info->file_no = file_no + 1;
1081 memcpy(info->name, ((CSaveData *) this->read_buffer)->GetCharaName(0), sizeof(info->name));
1082 info->map_no = ((CSaveData *) this->read_buffer)->map_no;
1083 info->play_time = ((CSaveData *) this->read_buffer)->GetPlayTime();
1084 info->party_size = ((CSaveData *) this->read_buffer)->GetDngStatus()->GetPartySize();
1085 info->quest_total = 0;
1086
1087 for (dungeon = 0; dungeon < 6; dungeon++) {
1088 info->quest_total += ((CSaveData *) this->read_buffer)->QuestDungeon(dungeon, 0);
1089 }
1090
1091 info->quest_total += ((CSaveData *) this->read_buffer)->QuestDungeon(6, 0);
1092
1093 if (info->quest_total > 9999) {
1094 info->quest_total = 9999;
1095 }
1096
1097 this->step++;
1098 return 1;
1099 }
1100
1101 return 0;
1102}
1103
1105 int result;
1106 int file_no;
1107 int status;
1108
1109 switch (this->step) {
1110 case 0:
1111 if (sceMcSync(MC_NOWAIT, NULL, &result) == 0) {
1112 break;
1113 }
1114
1115 this->step++;
1116 memset(this->file_info, 0, sizeof(this->file_info));
1117 default:
1118 file_no = (this->step - 1) >> 2;
1119 status = this->GetSaveFileInfoFromMc(file_no);
1120
1121 if (status == 1 && file_no + 1 >= MC_SAVE_FILE_MAX) {
1122 return 1;
1123 }
1124
1125 if (status < 0) {
1126 return -1;
1127 }
1128
1129 break;
1130 }
1131
1132 return 0;
1133}
1134
1136 char name[0x20];
1137
1138 if (file_no < 0) {
1139 return 0;
1140 }
1141
1142 strcpy(name, this->file_name);
1143 strcat(name, "%d");
1144 sprintf(name, name, file_no);
1145 return GetOpenAttribute(name);
1146}
1147
1149 char name[0x20];
1150 char pattern[0x48];
1151 int result;
1152 int cmd;
1153 int status;
1154 int i;
1156
1157 strcpy(name, this->file_name);
1158 strcat(name, "%d");
1159 sprintf(name, name, file_no);
1160
1161 switch (this->step) {
1162 case 0:
1163 sys_config.values[17] = file_no;
1164 sys_config.values_copy1[17] = file_no;
1165 sys_config.values_copy2[17] = file_no;
1166 cmd = sceMcOpen(this->port, 1, name, GetOpenAttribute(name) ? 2 : 0x202);
1167
1168 if (cmd == 0) {
1169 this->step++;
1170 } else {
1171 printf("cmd = %d\n", cmd);
1172 return -1;
1173 }
1174
1175 break;
1176 case 1:
1177 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1178 break;
1179 }
1180
1181 if (cmd != 2) {
1182 printf("fd = %d\n", result);
1183 break;
1184 }
1185
1186 if (result < 0) {
1187 return -1;
1188 }
1189
1190 this->fd = result;
1191 this->transferred = 0;
1192 this->transfer_size = 0x136A7;
1193
1194 if (sceMcWrite(this->fd, this->save_buffer, this->transfer_size) == 0) {
1195 this->step++;
1196 } else {
1197 return -1;
1198 }
1199
1200 break;
1201 case 2:
1202 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1203 break;
1204 }
1205
1206 if (cmd != 6 || (cmd == 6 && result < 0)) {
1207 return -1;
1208 }
1209
1210 this->transferred += result;
1211
1212 if (this->transferred < this->transfer_size) {
1213 break;
1214 }
1215
1216 if (sceMcFlush(this->fd) == 0) {
1217 this->step++;
1218 } else {
1219 return -1;
1220 }
1221
1222 break;
1223 case 3:
1224 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1225 break;
1226 }
1227
1228 if (cmd != 0xA || (cmd == 0xA && result < 0)) {
1229 return -1;
1230 }
1231
1232 if (sceMcClose(this->fd) == 0) {
1233 this->step++;
1234 } else {
1235 return -1;
1236 }
1237
1238 break;
1239 case 4:
1240 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1241 break;
1242 }
1243
1244 if (cmd != 3 || (cmd == 3 && result < 0)) {
1245 return -1;
1246 }
1247
1248 strcpy(pattern, this->file_name);
1249 strcat(pattern, "*");
1250
1251 if (sceMcGetDir(this->port, 1, pattern, 0, 15, SaveFileInfo) == 0) {
1252 this->step++;
1253 } else {
1254 return -1;
1255 }
1256
1257 break;
1258 case 5:
1259 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1260 break;
1261 }
1262
1263 if (cmd != 0xD || (cmd == 0xD && result < 0)) {
1264 return -1;
1265 }
1266
1267 card = &this->card[this->port];
1268 card->dir_exists = 1;
1269 this->step++;
1270 break;
1271 case 6:
1272 cmd = sceMcOpen(this->port, 1, this->dir_name, 2);
1273
1274 if (cmd == 0) {
1275 this->step++;
1276 } else {
1277 return -1;
1278 }
1279
1280 break;
1281 case 7:
1282 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1283 break;
1284 }
1285
1286 if (cmd != 2) {
1287 return -1;
1288 }
1289
1290 if (result < 0) {
1291 return -1;
1292 }
1293
1294 this->fd = result;
1295 this->transferred = 0;
1296 this->transfer_size = 0x40;
1297
1298 for (i = 0; i < 12; i++) {
1299 }
1300
1301 if (sceMcWrite(this->fd, &sys_config, this->transfer_size) == 0) {
1302 this->step++;
1303 } else {
1304 return -1;
1305 }
1306
1307 break;
1308 case 8:
1309 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1310 break;
1311 }
1312
1313 if (cmd != 6 || (cmd == 6 && result < 0)) {
1314 return -1;
1315 }
1316
1317 this->transferred += result;
1318
1319 if (this->transferred < this->transfer_size) {
1320 break;
1321 }
1322
1323 if (sceMcFlush(this->fd) == 0) {
1324 this->step++;
1325 } else {
1326 return -1;
1327 }
1328
1329 break;
1330 case 9:
1331 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1332 break;
1333 }
1334
1335 if (cmd != 0xA || (cmd == 0xA && result < 0)) {
1336 return -1;
1337 }
1338
1339 if (sceMcClose(this->fd) == 0) {
1340 this->step++;
1341 } else {
1342 return -1;
1343 }
1344
1345 break;
1346 case 10:
1347 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1348 break;
1349 }
1350
1351 if (cmd != 3 || (cmd == 3 && result < 0)) {
1352 return -1;
1353 }
1354
1355 return 1;
1356 }
1357
1358 return status;
1359}
1360
1362 char name[0x80];
1363 char version[0x20];
1364 char saved_version[0x20];
1365 int result;
1366 int cmd;
1367 int ok;
1368 char *data;
1369 char *sum;
1370 char total;
1371 int i;
1372
1373 switch (this->step) {
1374 case 0:
1375 strcpy(name, this->file_name);
1376 strcat(name, "%d");
1377 sprintf(name, name, file_no);
1378
1379 if (sceMcOpen(this->port, 1, name, 1) == 0) {
1380 this->step++;
1381 } else {
1382 return -1;
1383 }
1384
1385 break;
1386 case 1:
1387 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1388 break;
1389 }
1390
1391 if (cmd != 2 || (cmd == 2 && result < 0)) {
1392 return -1;
1393 }
1394
1395 this->fd = result;
1396 this->transfer_size = 0x136A7;
1397 memset(this->load_buffer, 0, this->transfer_size + 0x40);
1398 this->transferred = 0;
1399
1400 if (sceMcRead(this->fd, this->load_buffer, this->transfer_size) == 0) {
1401 this->step++;
1402 } else {
1403 return -1;
1404 }
1405
1406 break;
1407 case 2:
1408 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1409 break;
1410 }
1411
1412 if (cmd != 5 || (cmd == 5 && result < 0)) {
1413 return -1;
1414 }
1415
1416 this->transferred += result;
1417
1418 if (this->transferred < this->transfer_size) {
1419 break;
1420 }
1421
1422 if (sceMcClose(this->fd) == 0) {
1423 this->step++;
1424 } else {
1425 return -1;
1426 }
1427
1428 break;
1429 case 3:
1430 if (sceMcSync(MC_NOWAIT, &cmd, &result) == 0) {
1431 break;
1432 }
1433
1434 if (cmd != 3 || (cmd == 3 && result < 0)) {
1435 return -1;
1436 }
1437
1438 ok = 1;
1439 data = this->load_buffer + sizeof(CSaveData);
1440 strcpy(version, this->GetVersion());
1441 memcpy(saved_version, data, sizeof(saved_version));
1442
1443 if (saved_version[0] == version[0]) {
1444 if (strcmp(version, saved_version) == 0) {
1445 sum = data + 0x20;
1446 data = this->load_buffer;
1447 total = 0;
1448
1449 for (i = 0; i < sizeof(CSaveData); i++) {
1450 total += *data++;
1451
1452 if (i % 64 == 63) {
1453 if (total != *sum) {
1454 printf("save data break!!!\n");
1455 ok = 0;
1456 break;
1457 }
1458
1459 sum++;
1460 total = 0;
1461 }
1462 }
1463 }
1464 } else {
1465 ok = 0;
1466 printf("not load\n");
1467 }
1468
1469 if (ok) {
1470 memcpy(SaveData, this->load_buffer, sizeof(CSaveData));
1471 ((s32 *) SaveData->GetConfigData())[17] = file_no;
1472 } else {
1473 return -1;
1474 }
1475
1476 return 1;
1477 }
1478
1479 return 0;
1480}
1481
1483 int result;
1485 int cmd;
1486 int status;
1487
1488 result = 0;
1489 card = &this->card[this->port];
1490
1491 switch (this->step) {
1492 case 0:
1493 if (sceMcFormat(this->port, 1) == 0) {
1494 this->step++;
1495 } else {
1496 return -1;
1497 }
1498
1499 break;
1500 case 1:
1501 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
1502 break;
1503 }
1504
1505 if (cmd != 0x10) {
1506 break;
1507 }
1508
1509 if (status < 0) {
1510 printf("format failed\n");
1511 return -1;
1512 }
1513
1514 printf("format finished\n");
1515 this->step++;
1516 break;
1517 case 2:
1518 if (sceMcGetInfo(this->port, 1, &card->type, &card->free_size, &card->formatted) == 0) {
1519 this->step++;
1520 } else {
1521 return -1;
1522 }
1523
1524 break;
1525 case 3:
1526 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
1527 break;
1528 }
1529
1530 if (cmd != 1) {
1531 return -1;
1532 }
1533
1534 card->present = 1;
1535
1536 if (status < -9) {
1537 card->present = 0;
1538 }
1539
1540 card->result = status;
1541 result = 1;
1542 break;
1543 }
1544
1545 return result;
1546}
1547
1549 int status;
1550 int cmd;
1551
1552 switch (this->step) {
1553 case 0: {
1554 char name[64] = "darkcloud%d";
1555
1556 sprintf(name, name, file_no);
1557 printf("delete:%s\n", name);
1558
1559 if (sceMcDelete(this->port, 1, name) == 0) {
1560 this->step++;
1561 } else {
1562 sceMcSync(MC_NOWAIT, NULL, NULL);
1563 }
1564
1565 break;
1566 }
1567 case 1:
1568 if (sceMcSync(MC_NOWAIT, &cmd, &status) == 0) {
1569 break;
1570 }
1571
1572 if (cmd != 0xF) {
1573 break;
1574 }
1575
1576 if (status < 0) {
1577 this->error.retry_count++;
1578
1579 if (this->error.retry_count > 120) {
1580 printf("delete error \n");
1581 return 1;
1582 }
1583
1584 break;
1585 }
1586
1587 return 1;
1588 }
1589
1590 return 0;
1591}
1592
1594 int result = 0;
1595
1596 switch (this->GetFuncNo()) {
1597 case MC_OPERATION_GET_DIR:
1598 result = msg_no + 5;
1599 break;
1600 case MC_OPERATION_MAKE_DIR:
1601 result = msg_no + 11;
1602 break;
1603 case MC_OPERATION_GET_ALL_SAVE_FILE_INFO:
1604 result = msg_no + 5;
1605 break;
1606 case MC_OPERATION_SAVE:
1607 case MC_OPERATION_SAVE_CONFIG:
1608 result = msg_no + 11;
1609 break;
1610 case MC_OPERATION_LOAD:
1611 case MC_OPERATION_LOAD_CONFIG:
1612 result = msg_no + 21;
1613 break;
1614 case MC_OPERATION_FORMAT:
1615 result = msg_no + 35;
1616 break;
1617 case MC_OPERATION_UNFORMAT:
1618 result = msg_no + 39;
1619 break;
1620 case MC_OPERATION_DELETE:
1621 result = msg_no + 41;
1622 break;
1623 case MC_OPERATION_UNK_8:
1624 result = msg_no + 43;
1625 break;
1626 }
1627
1628 return result;
1629}
1630
1632 MC_ERROR_INFO *info = &this->error;
1633
1634 switch (result) {
1635 case sceMcResNoFormat:
1636 case sceMcResFailDetect:
1637 printf("mc is unformat\n");
1638 info->code = MC_ERROR_UNFORMATTED;
1639 break;
1640 case sceMcResFullDevice:
1641 printf("memory is over or noting, break!\n");
1642 info->code = MC_ERROR_FULL;
1643 break;
1644 case sceMcResNoEntry:
1645 printf("file not open or not exist\n");
1646 break;
1647 case sceMcResDeniedPermit:
1648 printf("not open by write mode \n");
1649 break;
1650 case sceMcResFailReplace:
1651 printf("write failed\n");
1652 break;
1653 }
1654
1655 if (result < -10) {
1656 info->code = MC_ERROR_NOT_CARD;
1657 printf("not memory card or (read write)error = %d\n", result);
1658 }
1659
1660 if (result < 0) {
1661 printf("result = %d\n", result);
1662 info->func_no = this->GetFuncNo();
1663 info->step = this->step;
1664 printf("func = %d\n", info->func_no);
1665 printf("phase = %d\n", info->step);
1666 info->file_no = this->file_no;
1667 }
1668
1669 return 0;
1670}
1671
1673 int cmd;
1674 int result;
1675 int running;
1676
1677 running = 0;
1678
1679 while (running == 0) {
1680 running = sceMcSync(MC_WAIT, &cmd, &result);
1681 }
1682}
1683
1685 int result;
1686 int cmd;
1687 int status;
1688
1689 switch (this->step) {
1690 case 0:
1691 result = sceMcUnformat(this->port, 1);
1692
1693 if (result == 0) {
1694 this->step++;
1695 } else {
1696 sceMcSync(MC_NOWAIT, &result, &status);
1697 printf("dmy sync\n");
1698 }
1699
1700 break;
1701 case 1:
1702 result = sceMcSync(MC_NOWAIT, &cmd, &status);
1703
1704 if (result == 0) {
1705 break;
1706 }
1707
1708 if (cmd != 0x11) {
1709 break;
1710 }
1711
1712 if (cmd == 0x11 && status < 0) {
1713 break;
1714 }
1715
1716 this->card[this->port].formatted = 0;
1717 return 1;
1718 }
1719
1720 return 0;
1721}
1722
1723char AtoraVibeTextureName[] __attribute__((section(".rodata"))) = "stayframe";
int LoadFromMc(int file_no)
void SetVersion(char *version)
void SetFuncNo(int func_no)
SAVEDATA_INFO file_info[MC_SAVE_FILE_MAX]
int GetMsgNo(int msg_no)
int GetSaveFileInfoFromMc(int file_no)
MC_DIR_ENTRY * dir_table
int DeleteFile(int file_no)
int CheckFileNo(int file_no)
void SetIconData(MC_ICON_DATA *icon)
int McError(int result)
int SaveToMc(int file_no)
void SetBuff(char *buffer)
MC_CARD_INFO card[MC_CARD_MAX]
void * GetConfigData()
Definition savedata.cpp:33
void ConvertConfig(SV_CONFIG_SYS *out)
Definition savedata.cpp:559
int InvertConfig(SV_CONFIG_SYS *in)
Definition savedata.cpp:580
char name[0x20]
MC_ICON_FILE view
MC_ICON_FILE copy
MC_ICON_FILE del
char values[18]
Definition savedata.hpp:44
char values_copy1[18]
Definition savedata.hpp:45
char values_copy2[18]
Definition savedata.hpp:46
sceMcColor bg_color[4]
Definition libmc.h:58
sceMcColorF ambient
Definition libmc.h:61
char head[4]
Definition libmc.h:53
u_char title_name[68]
Definition libmc.h:62
sceMcColorF light_color[3]
Definition libmc.h:60
char fname_del[64]
Definition libmc.h:65
char fname_copy[64]
Definition libmc.h:64
u_int trans_rate
Definition libmc.h:57
sceMcVu0FVECTOR light_dir[3]
Definition libmc.h:59
u_short nl_offset
Definition libmc.h:55
char fname_view[64]
Definition libmc.h:63