From bbef9b0bdb2f3e0d19f48c918b991abd1eef6eb9 Mon Sep 17 00:00:00 2001 From: nmlgc Date: Mon, 18 May 2020 21:29:12 +0200 Subject: [PATCH] [Decompilation] [th01] REYHI*.DAT saving Now with POSIX file I/O in both executables. And the confirmation that the name array is indeed exclusively accessed per-byte. Part of P0093, funded by Ember2528. --- th01/hiscore/regist.cpp | 45 ++++++++ th01/hiscore/regist[data].asm | 6 + th01/hiscore/regist_name[data].asm | 3 +- th01/hiscore/score_nm.cpp | 2 +- th01/hiscore/scoredat.hpp | 9 +- th01/hiscore/scorelod.cpp | 9 +- th01/hiscore/scorelod[data].asm | 12 +- th01/main_19.cpp | 2 + th01_fuuin.asm | 169 +--------------------------- th01_reiiden.asm | 170 +---------------------------- 10 files changed, 75 insertions(+), 352 deletions(-) diff --git a/th01/hiscore/regist.cpp b/th01/hiscore/regist.cpp index 60879296..e2f646e5 100644 --- a/th01/hiscore/regist.cpp +++ b/th01/hiscore/regist.cpp @@ -530,3 +530,48 @@ regist_input_ret_t regist_on_input( } return RI_REGULAR; } + +#if (BINARY == 'E') + inline void scoredat_free(void) +#else + void scoredat_free(void) +#endif +{ + delete[] scoredat_names; + delete[] scoredat_stages; + delete[] scoredat_routes; + delete[] scoredat_points; +} + +void scoredat_save(void) +{ + extern const char SCOREDAT_FN_EASY_2[]; + extern const char SCOREDAT_FN_NORMAL_2[]; + extern const char SCOREDAT_FN_HARD_2[]; + extern const char SCOREDAT_FN_LUNATIC_2[]; + extern const char SCOREDAT_FOPEN_WB[]; + + struct hack { + char x[sizeof(SCOREDAT_MAGIC)]; + }; + #undef SCOREDAT_MAGIC + extern const hack SCOREDAT_MAGIC; + + FILE* fp; + const hack magic = SCOREDAT_MAGIC; + char fn[16]; + scoredat_fn(fn, 2); + + if( (fp = fopen(fn, SCOREDAT_FOPEN_WB)) == NULL) { + return; + } + write(fileno(fp), magic.x, sizeof(SCOREDAT_MAGIC) - 1); + for(int i = 0; i < SCOREDAT_NAMES_SIZE; i++) { + scoredat_names[i] = scoredat_name_byte_encode(scoredat_names[i]); + } + write(fileno(fp), scoredat_names, SCOREDAT_NAMES_SIZE); + write(fileno(fp), scoredat_points, sizeof(uint32_t) * SCOREDAT_PLACES); + write(fileno(fp), scoredat_stages, sizeof(int16_t) * SCOREDAT_PLACES); + write(fileno(fp), scoredat_routes, sizeof(twobyte_t) * SCOREDAT_PLACES); + fclose(fp); +} diff --git a/th01/hiscore/regist[data].asm b/th01/hiscore/regist[data].asm index 117cfbc0..a334bed2 100644 --- a/th01/hiscore/regist[data].asm +++ b/th01/hiscore/regist[data].asm @@ -54,4 +54,10 @@ if BINARY eq 'E' _ALPHABET_KANJI_FMT_4 db '%c%c',0 _REGIST_STRING_FMT_0 db '%s',0 _REGIST_STRING_FMT_1 db '%s',0 + _SCOREDAT_FN_EASY_2 db 'REYHIES.DAT',0 + _SCOREDAT_FN_NORMAL_2 db 'REYHINO.DAT',0 + _SCOREDAT_FN_HARD_2 db 'REYHIHA.DAT',0 + _SCOREDAT_FN_LUNATIC_2 db 'REYHILU.DAT',0 endif +public _SCOREDAT_FOPEN_WB +_SCOREDAT_FOPEN_WB db 'wb',0 diff --git a/th01/hiscore/regist_name[data].asm b/th01/hiscore/regist_name[data].asm index bad576ea..ba0c6f49 100644 --- a/th01/hiscore/regist_name[data].asm +++ b/th01/hiscore/regist_name[data].asm @@ -1,7 +1,8 @@ -public _REGIST_NAME_BLANK, _REGIST_NAME_SPACES +public _REGIST_NAME_BLANK, _REGIST_NAME_SPACES, _SCOREDAT_MAGIC _REGIST_NAME_BLANK db '________',0 if BINARY eq 'M' public _regist_jump_to_enter _regist_jump_to_enter db 0 endif _REGIST_NAME_SPACES db '        ',0 +_SCOREDAT_MAGIC db 'HISCORE',0 diff --git a/th01/hiscore/score_nm.cpp b/th01/hiscore/score_nm.cpp index 458a8a59..b908f608 100644 --- a/th01/hiscore/score_nm.cpp +++ b/th01/hiscore/score_nm.cpp @@ -1,7 +1,7 @@ void scoredat_name_get(int place, char str[SCOREDAT_NAME_BYTES + 1]) { for(int i = 0; i < SCOREDAT_NAME_BYTES; i++) { - str[i] = scoredat_name_byte((place * SCOREDAT_NAME_BYTES) + i); + str[i] = scoredat_names[(place * SCOREDAT_NAME_BYTES) + i]; } str[SCOREDAT_NAME_BYTES] = '\0'; } diff --git a/th01/hiscore/scoredat.hpp b/th01/hiscore/scoredat.hpp index 428a73f1..6ff10b1f 100644 --- a/th01/hiscore/scoredat.hpp +++ b/th01/hiscore/scoredat.hpp @@ -4,6 +4,7 @@ // Actually creates slightly different assembly compared to sizeof() on a // int8_t array! #define SCOREDAT_NAME_BYTES (SCOREDAT_NAME_KANJI * 2) +#define SCOREDAT_NAMES_SIZE (SCOREDAT_PLACES * SCOREDAT_NAME_BYTES) #define SCOREDAT_ROUTE_LEN 2 #define SCOREDAT_CLEARED 40 @@ -34,17 +35,11 @@ struct scoredat_t { twobyte_t route[SCOREDAT_PLACES]; }; -extern scoredat_name_t* scoredat_names; +extern int8_t* scoredat_names; // Yeah, technically a scoredat_name_t. extern int16_t* scoredat_stages; extern uint32_t* scoredat_points; extern twobyte_t* scoredat_routes; -// Byte-wise access to [scoredat_names]. -inline int8_t& scoredat_name_byte(size_t byte) -{ - return reinterpret_cast(scoredat_names)[byte]; -} - // Byte-wise access to [scoredat_routes]. inline int8_t& scoredat_route_byte(int place, int byte) { diff --git a/th01/hiscore/scorelod.cpp b/th01/hiscore/scorelod.cpp index fc5235f9..cfa9b7ca 100644 --- a/th01/hiscore/scorelod.cpp +++ b/th01/hiscore/scorelod.cpp @@ -112,20 +112,19 @@ int scoredat_load() return 1; } - scoredat_names = new scoredat_name_t[SCOREDAT_PLACES]; + scoredat_names = new int8_t[SCOREDAT_NAMES_SIZE]; scoredat_stages = new int16_t[SCOREDAT_PLACES]; scoredat_routes = new twobyte_t[SCOREDAT_PLACES]; scoredat_points = new uint32_t[SCOREDAT_PLACES]; - scoredat_read(scoredat_names, sizeof(scoredat_name_t) * SCOREDAT_PLACES); + scoredat_read(scoredat_names, SCOREDAT_NAMES_SIZE); scoredat_read(scoredat_points, sizeof(uint32_t) * SCOREDAT_PLACES); scoredat_read(scoredat_stages, sizeof(int16_t) * SCOREDAT_PLACES); scoredat_read(scoredat_routes, sizeof(twobyte_t) * SCOREDAT_PLACES); scoredat_close(); - for(int i = 0; i < sizeof(scoredat_name_t) * SCOREDAT_PLACES; i++) { - scoredat_name_byte(i) = - scoredat_name_byte_decode(scoredat_name_byte(i)); + for(int i = 0; i < SCOREDAT_NAMES_SIZE; i++) { + scoredat_names[i] = scoredat_name_byte_decode(scoredat_names[i]); } scoredat_sti(); return 0; diff --git a/th01/hiscore/scorelod[data].asm b/th01/hiscore/scorelod[data].asm index 5a525512..ed97b942 100644 --- a/th01/hiscore/scorelod[data].asm +++ b/th01/hiscore/scorelod[data].asm @@ -1,9 +1,9 @@ public _SCOREDAT_ROUTE_NONE, _scoredat_name_default public _SCOREDAT_MAGIC_0, _SCOREDAT_MAGIC_1 -public _SCOREDAT_FN_EASY_0, _SCOREDAT_FN_EASY_1 -public _SCOREDAT_FN_NORMAL_0, _SCOREDAT_FN_NORMAL_1 -public _SCOREDAT_FN_HARD_0, _SCOREDAT_FN_HARD_1 -public _SCOREDAT_FN_LUNATIC_0, _SCOREDAT_FN_LUNATIC_1 +public _SCOREDAT_FN_EASY_0, _SCOREDAT_FN_EASY_1, _SCOREDAT_FN_EASY_2 +public _SCOREDAT_FN_NORMAL_0, _SCOREDAT_FN_NORMAL_1, _SCOREDAT_FN_NORMAL_2 +public _SCOREDAT_FN_HARD_0, _SCOREDAT_FN_HARD_1, _SCOREDAT_FN_HARD_2 +public _SCOREDAT_FN_LUNATIC_0, _SCOREDAT_FN_LUNATIC_1, _SCOREDAT_FN_LUNATIC_2 _SCOREDAT_ROUTE_NONE db '無',0 _scoredat_name_default db '東方★靈異伝  ',0 @@ -28,12 +28,16 @@ if BINARY eq 'E' else _SCOREDAT_MAGIC_1 label byte _SCOREDAT_MAGIC_0 db 'HISCORE',0 + _SCOREDAT_FN_EASY_2 label byte _SCOREDAT_FN_EASY_1 label byte _SCOREDAT_FN_EASY_0 db 'REYHIES.DAT',0 + _SCOREDAT_FN_NORMAL_2 label byte _SCOREDAT_FN_NORMAL_1 label byte _SCOREDAT_FN_NORMAL_0 db 'REYHINO.DAT',0 + _SCOREDAT_FN_HARD_2 label byte _SCOREDAT_FN_HARD_1 label byte _SCOREDAT_FN_HARD_0 db 'REYHIHA.DAT',0 + _SCOREDAT_FN_LUNATIC_2 label byte _SCOREDAT_FN_LUNATIC_1 label byte _SCOREDAT_FN_LUNATIC_0 db 'REYHILU.DAT',0 endif diff --git a/th01/main_19.cpp b/th01/main_19.cpp index 0df30b44..63d5cc1f 100644 --- a/th01/main_19.cpp +++ b/th01/main_19.cpp @@ -6,6 +6,8 @@ #pragma option -3 -Z -d extern "C" { +#include +#include #include "ReC98.h" #include "th01/ranks.h" #include "th01/hardware/graph.h" diff --git a/th01_fuuin.asm b/th01_fuuin.asm index 99e0c590..29008725 100644 --- a/th01_fuuin.asm +++ b/th01_fuuin.asm @@ -42,9 +42,7 @@ include th01/th01.inc extern _exit:proc extern _farfree:proc extern _farmalloc:proc - extern _fclose:proc extern _filelength:proc - extern _fopen:proc extern _int86:proc extern _intdosx:proc extern _memcmp:proc @@ -57,7 +55,6 @@ include th01/th01.inc extern _strcmp:proc extern _strcpy:proc extern _vsprintf:proc - extern _write:proc fuuin_02 group fuuin_02_TEXT, fuuin_02__TEXT @@ -373,12 +370,12 @@ fuuin_01_TEXT ends fuuin_02_TEXT segment byte public 'CODE' use16 extern _input_sense:proc extern _input_reset_sense:proc - extern _scoredat_name_byte_encode:proc extern _scoredat_load:proc extern _scoredat_name_get:proc extern _alphabet_put_initial:proc extern _regist_put_initial:proc extern _regist_on_input:proc + extern _scoredat_save:proc fuuin_02_TEXT ends fuuin_02__TEXT segment byte public 'CODE' use16 @@ -388,160 +385,6 @@ fuuin_02__TEXT segment byte public 'CODE' use16 ; Attributes: bp-based frame -sub_B40E proc far - -dest = byte ptr -1Ch -var_C = byte ptr -0Ch -stream = dword ptr -4 - - enter 1Ch, 0 - push si - lea ax, [bp+var_C] - push ss - push ax - push ds - push offset aHiscore_0 ; "HISCORE" - mov cx, 8 - call SCOPY@ - mov al, _rank - mov ah, 0 - mov bx, ax - cmp bx, RANK_LUNATIC - ja short loc_B45C - add bx, bx - jmp cs:off_B533[bx] - -loc_B437: - push ds - push offset aReyhies_dat_1 ; "REYHIES.DAT" - jmp short loc_B44D -; --------------------------------------------------------------------------- - -loc_B43D: - push ds - push offset aReyhino_dat_1 ; "REYHINO.DAT" - jmp short loc_B44D -; --------------------------------------------------------------------------- - -loc_B443: - push ds - push offset aReyhiha_dat_1 ; "REYHIHA.DAT" - jmp short loc_B44D -; --------------------------------------------------------------------------- - -loc_B449: - push ds - push offset aReyhilu_dat_1 ; "REYHILU.DAT" - -loc_B44D: - push ss - lea ax, [bp+dest] - push ax ; dest - call _strcpy - add sp, 8 - jmp short $+2 - -loc_B45C: - push ds - push offset aWb_0 ; "wb" - push ss - lea ax, [bp+dest] - push ax ; path - call _fopen - add sp, 8 - mov word ptr [bp+stream+2], dx - mov word ptr [bp+stream], ax - or ax, dx - jnz short loc_B47A - jmp loc_B530 -; --------------------------------------------------------------------------- - -loc_B47A: - push 7 - push ss - lea ax, [bp+var_C] - push ax - les bx, [bp+stream] - mov al, es:[bx+4] - cbw - push ax - call _write - add sp, 8 - xor si, si - jmp short loc_B4AF -; --------------------------------------------------------------------------- - -loc_B496: - les bx, _scoredat_names - add bx, si - mov al, es:[bx] - call fuuin_02:_scoredat_name_byte_encode pascal, ax - pop cx - les bx, _scoredat_names - add bx, si - mov es:[bx], al - inc si - -loc_B4AF: - cmp si, size scoredat_names_t - jl short loc_B496 - push size scoredat_names_t - push word ptr _scoredat_names+2 - push word ptr _scoredat_names - les bx, [bp+stream] - mov al, es:[bx+4] - cbw - push ax - call _write - add sp, 8 - push size scoredat_points_t - push word ptr _scoredat_points+2 - push word ptr _scoredat_points - les bx, [bp+stream] - mov al, es:[bx+4] - cbw - push ax - call _write - add sp, 8 - push size scoredat_stages_t - push word ptr _scoredat_stages+2 - push word ptr _scoredat_stages - les bx, [bp+stream] - mov al, es:[bx+4] - cbw - push ax - call _write - add sp, 8 - push size scoredat_routes_t - push word ptr _scoredat_routes+2 - push word ptr _scoredat_routes - les bx, [bp+stream] - mov al, es:[bx+4] - cbw - push ax - call _write - add sp, 8 - push word ptr [bp+stream+2] - push word ptr [bp+stream] ; stream - call _fclose - add sp, 4 - -loc_B530: - pop si - leave - retf -sub_B40E endp - -; --------------------------------------------------------------------------- -off_B533 dw offset loc_B437 - dw offset loc_B43D - dw offset loc_B443 - dw offset loc_B449 - -; =============== S U B R O U T I N E ======================================= - -; Attributes: bp-based frame - sub_B53B proc far @@entered_name = byte ptr -18h @@ -633,7 +476,7 @@ loc_B5B4: loc_B5CB: cmp si, SCOREDAT_NAME_BYTES jl short loc_B5B4 - call sub_B40E + call _scoredat_save pop di pop si leave @@ -2698,7 +2541,6 @@ include th01/hardware/input_main_end[data].asm include th01/hiscore/alphabet_syms[data].asm include th01/hardware/grppfnfx_ptrs[data].asm include th01/hiscore/regist_name[data].asm -aHiscore_0 db 'HISCORE',0 off_12C1E dd aB@gcbGwbB@ ; " イージー " dd aB@gmbGGlb@ ; " ノーマル " @@ -2707,13 +2549,6 @@ off_12C1E dd aB@gcbGwbB@ include th01/hardware/grppfnfx[data].asm include th01/hiscore/scorelod[data].asm include th01/hiscore/regist[data].asm -aReyhies_dat_1 db 'REYHIES.DAT',0 -aReyhino_dat_1 db 'REYHINO.DAT',0 -aReyhiha_dat_1 db 'REYHIHA.DAT',0 -; char aReyhilu_dat_1[] -aReyhilu_dat_1 db 'REYHILU.DAT',0 -; char aWb_0[] -aWb_0 db 'wb',0 aB@gcbGwbB@ db ' イージー ',0 aB@gmbGGlb@ db ' ノーマル ',0 aB@gnbGhb@b@ db ' ハード  ',0 diff --git a/th01_reiiden.asm b/th01_reiiden.asm index 378b69eb..d842f3a7 100644 --- a/th01_reiiden.asm +++ b/th01_reiiden.asm @@ -49,9 +49,7 @@ include th01/th01.inc extern _farheapcheck:proc extern _farheapchecknode:proc extern _farmalloc:proc - extern _fclose:proc extern _filelength:proc - extern _fopen:proc extern _int86:proc extern _intdosx:proc extern _kbhit:proc @@ -65,7 +63,6 @@ include th01/th01.inc extern _strcpy:proc extern _toupper:proc extern _vsprintf:proc - extern _write:proc main_01 group main_01_TEXT, main_01__TEXT main_19 group main_19_TEXT, main_19__TEXT @@ -8414,7 +8411,6 @@ main_18_TEXT ends ; Segment type: Pure code main_19_TEXT segment byte public 'CODE' use16 - extern _scoredat_name_byte_encode:proc extern _scoredat_load:proc extern _scoredat_hiscore_get:proc extern _scoredat_name_get:proc @@ -8425,166 +8421,8 @@ main_19__TEXT segment byte public 'CODE' use16 extern _alphabet_put_initial:proc extern _regist_put_initial:proc extern _regist_on_input:proc - -; =============== S U B R O U T I N E ======================================= - -; Attributes: bp-based frame -public _scoredat_free -_scoredat_free proc far - push bp - mov bp, sp - call @$bdla$qnv c, large [_scoredat_names] - call @$bdla$qnv c, large [_scoredat_stages] - call @$bdla$qnv c, large [_scoredat_routes] - call @$bdla$qnv c, large [_scoredat_points] - pop bp - retf -_scoredat_free endp - - - ; =============== S U B R O U T I N E ======================================= - -; Attributes: bp-based frame - -sub_146F3 proc far - -dest = byte ptr -1Ch -var_C = byte ptr -0Ch -stream = dword ptr -4 - - enter 1Ch, 0 - push si - lea ax, [bp+var_C] - push ss - push ax - push ds - push offset aHiscore_0 ; "HISCORE" - mov cx, 8 - call SCOPY@ - mov al, _rank - cbw - mov bx, ax - cmp bx, RANK_LUNATIC - ja short loc_1473E - add bx, bx - jmp cs:off_14806[bx] - -loc_1471B: - push ds - push offset _SCOREDAT_FN_EASY_0 ; "REYHIES.DAT" - jmp short loc_14731 -; --------------------------------------------------------------------------- - -loc_14721: - push ds - push offset _SCOREDAT_FN_NORMAL_0 ; "REYHINO.DAT" - jmp short loc_14731 -; --------------------------------------------------------------------------- - -loc_14727: - push ds - push offset _SCOREDAT_FN_HARD_0 ; "REYHIHA.DAT" - jmp short loc_14731 -; --------------------------------------------------------------------------- - -loc_1472D: - push ds - push offset _SCOREDAT_FN_LUNATIC_0 ; "REYHILU.DAT" - -loc_14731: - push ss - lea ax, [bp+dest] - push ax ; dest - call _strcpy - add sp, 8 - -loc_1473E: - push ds - push offset aWB ; "wb" - push ss - lea ax, [bp+dest] - push ax ; path - call _fopen - add sp, 8 - mov word ptr [bp+stream+2], dx - mov word ptr [bp+stream], ax - or ax, dx - jz loc_14803 - push 7 - push ss - lea ax, [bp+var_C] - push ax - les bx, [bp+stream] - mov al, es:[bx+4] - cbw - push ax - call _write - add sp, 8 - xor si, si - jmp short loc_14790 -; --------------------------------------------------------------------------- - -loc_14777: - les bx, _scoredat_names - add bx, si - mov al, es:[bx] - call main_19:_scoredat_name_byte_encode stdcall, ax - pop cx - les bx, _scoredat_names - add bx, si - mov es:[bx], al - inc si - -loc_14790: - cmp si, size scoredat_names_t - jl short loc_14777 - push size scoredat_names_t - pushd [_scoredat_names] - les bx, [bp+stream] - mov al, es:[bx+4] - cbw - push ax - call _write - add sp, 8 - push size scoredat_points_t - pushd [_scoredat_points] - les bx, [bp+stream] - mov al, es:[bx+4] - cbw - push ax - call _write - add sp, 8 - push size scoredat_stages_t - pushd [_scoredat_stages] - les bx, [bp+stream] - mov al, es:[bx+4] - cbw - push ax - call _write - add sp, 8 - push size scoredat_routes_t - pushd [_scoredat_routes] - les bx, [bp+stream] - mov al, es:[bx+4] - cbw - push ax - call _write - add sp, 8 - pushd [bp+stream] ; stream - call _fclose - add sp, 4 - -loc_14803: - pop si - leave - retf -sub_146F3 endp - -; --------------------------------------------------------------------------- -off_14806 dw offset loc_1471B - dw offset loc_14721 - dw offset loc_14727 - dw offset loc_1472D + extern _scoredat_free:proc + extern _scoredat_save:proc ; =============== S U B R O U T I N E ======================================= @@ -8673,7 +8511,7 @@ loc_1488F: loc_148A6: cmp si, SCOREDAT_NAME_BYTES jl short loc_1488F - call sub_146F3 + call _scoredat_save pop di pop si leave @@ -59726,7 +59564,6 @@ aVgvivfb@vyb@vj db ' db 0 include th01/hiscore/alphabet_syms[data].asm include th01/hiscore/regist_name[data].asm -aHiscore_0 db 'HISCORE',0 off_358A3 dd aB@gcbGwbB@ ; " イージー " dd aB@gmbGGlb@ ; " ノーマル " @@ -59734,7 +59571,6 @@ off_358A3 dd aB@gcbGwbB@ dd aGlgigegbgbgn ; "ルナティック" include th01/hiscore/scorelod[data].asm include th01/hiscore/regist[data].asm -aWB db 'wb',0 aB@gcbGwbB@ db ' イージー ',0 aB@gmbGGlb@ db ' ノーマル ',0 aB@gnbGhb@b@ db ' ハード  ',0