From 2d5491e4a9ceaa7bf91a1a59c15b69a976312956 Mon Sep 17 00:00:00 2001 From: nmlgc Date: Sat, 25 Dec 2021 19:35:50 +0100 Subject: [PATCH] [Decompilation] [th03] YUME.NEM: Loading "I don't need the return value in MAINL.EXE? Let's copy-paste the function and remove all of its `return` statements then!" :zunpet: Completes P0172, funded by [Anonymous] and Blue Bolt. --- Makefile.mak | 2 +- th03/formats/score_ld.cpp | 32 +++++++++++++++++++ th03/formats/scoredat.cpp | 1 + th03/formats/scoredat.hpp | 9 ++++++ th03/hiscore/regist.cpp | 1 + th03/op_03.cpp | 12 +++++++ th03_mainl.asm | 63 +++---------------------------------- th03_op.asm | 66 +++------------------------------------ 8 files changed, 64 insertions(+), 122 deletions(-) create mode 100644 th03/formats/score_ld.cpp create mode 100644 th03/op_03.cpp diff --git a/Makefile.mak b/Makefile.mak index 1edec070..50f17245 100644 --- a/Makefile.mak +++ b/Makefile.mak @@ -93,7 +93,7 @@ bin\th03\res_yume.com: th03\res_yume.cpp $** | masters.lib -bin\th03\op.exe: th03\op_01.cpp th03\op_02.cpp bin\th03\scoredat.obj bin\th03\op.obj bin\th02\exit_dos.obj bin\th01\vplanset.obj bin\th02\snd_mode.obj bin\th02\snd_pmdr.obj bin\th02\snd_load.obj bin\th03\exit.obj bin\th03\vector1.obj bin\th03\cdg_put.obj bin\th02\frmdely1.obj bin\th03\input_s.obj bin\th03\pi_put.obj bin\th03\snd_kaja.obj bin\th03\initop.obj bin\th03\cdg_load.obj bin\th03\grppsafx.obj bin\th03\pi_load.obj bin\th03\inp_m_w.obj bin\th03\cdg_p_na.obj bin\th03\hfliplut.obj bin\th02\frmdely2.obj +bin\th03\op.exe: th03\op_01.cpp th03\op_02.cpp bin\th03\scoredat.obj th03\op_03.cpp bin\th03\op.obj bin\th02\exit_dos.obj bin\th01\vplanset.obj bin\th02\snd_mode.obj bin\th02\snd_pmdr.obj bin\th02\snd_load.obj bin\th03\exit.obj bin\th03\vector1.obj bin\th03\cdg_put.obj bin\th02\frmdely1.obj bin\th03\input_s.obj bin\th03\pi_put.obj bin\th03\snd_kaja.obj bin\th03\initop.obj bin\th03\cdg_load.obj bin\th03\grppsafx.obj bin\th03\pi_load.obj bin\th03\inp_m_w.obj bin\th03\cdg_p_na.obj bin\th03\hfliplut.obj bin\th02\frmdely2.obj $(CC) $(CFLAGS) $(LARGE_LFLAGS) -3 -Z -DGAME=3 -DBINARY='O' -nbin\th03\ -eOP.EXE @&&| $** | diff --git a/th03/formats/score_ld.cpp b/th03/formats/score_ld.cpp new file mode 100644 index 00000000..3a1e6ae4 --- /dev/null +++ b/th03/formats/score_ld.cpp @@ -0,0 +1,32 @@ +#if (BINARY == 'L') + #define recreated + #define loaded + void pascal near scoredat_load_and_decode(rank_t rank) +#else + #define recreated true + #define loaded false + bool16 pascal near scoredat_load_and_decode(rank_t rank) +#endif +{ + if(!file_exist(SCOREDAT_FN)) { + // Unnecessary, since scoredat_recreate() would also create the file? + file_create(SCOREDAT_FN); + file_close(); + + scoredat_recreate(); + return recreated; + } + file_ropen(SCOREDAT_FN); + file_seek((rank * sizeof(scoredat_section_t)), SEEK_SET); + file_read(&hi, sizeof(scoredat_section_t)); + file_close(); + scoredat_decode(); + if(scoredat_sum_invalid()) { + scoredat_recreate(); + return recreated; + } + return loaded; + + #undef recreated + #undef loaded +} diff --git a/th03/formats/scoredat.cpp b/th03/formats/scoredat.cpp index cde45dfc..6255d4aa 100644 --- a/th03/formats/scoredat.cpp +++ b/th03/formats/scoredat.cpp @@ -2,6 +2,7 @@ #include #include "platform.h" +#include "master.hpp" #include "th01/rank.h" #include "th03/common.h" #include "th03/score.h" diff --git a/th03/formats/scoredat.hpp b/th03/formats/scoredat.hpp index c2794c94..cf4f6509 100644 --- a/th03/formats/scoredat.hpp +++ b/th03/formats/scoredat.hpp @@ -51,6 +51,15 @@ extern scoredat_section_t hi; void near scoredat_decode(void); #ifdef RANK_H + // Loads the score data for the given [rank] into [hi]. The OP.EXE version + // returns `true` if that data was recreated due to corruption or a + // nonexistent file. + #if (BINARY == 'O') + bool16 pascal near scoredat_load_and_decode(rank_t rank); + #elif (BINARY == 'L') + void pascal near scoredat_load_and_decode(rank_t rank); + #endif + // Calculates the checksum and encrypts [hi] in-place, then saves it to // the score file under the given [rank]. The MAINL version also sets // [cleared] based on resident data. diff --git a/th03/hiscore/regist.cpp b/th03/hiscore/regist.cpp index 901dc68f..6cc40e44 100644 --- a/th03/hiscore/regist.cpp +++ b/th03/hiscore/regist.cpp @@ -10,4 +10,5 @@ #include "th03/sprites/regi.h" #include "th03/formats/scoredat.hpp" +#include "th03/formats/score_ld.cpp" #include "th03/formats/score_es.cpp" diff --git a/th03/op_03.cpp b/th03/op_03.cpp new file mode 100644 index 00000000..3d6fca81 --- /dev/null +++ b/th03/op_03.cpp @@ -0,0 +1,12 @@ +#pragma option -zPgroup_01 + +#include "platform.h" +#include "master.hpp" +#include "th01/rank.h" +#include "th03/common.h" +#include "th03/score.h" +#include "th03/playchar.hpp" +#include "th03/sprites/regi.h" +#include "th03/formats/scoredat.hpp" + +#include "th03/formats/score_ld.cpp" diff --git a/th03_mainl.asm b/th03_mainl.asm index 51dbf4cb..a17ab31e 100644 --- a/th03_mainl.asm +++ b/th03_mainl.asm @@ -27,7 +27,7 @@ include th03/formats/scoredat.inc extern _execl:proc extern _tolower:proc -group_01 group CFG_LRES_TEXT, mainl_01_TEXT, SCOREDAT_TEXT, mainl_02_TEXT, REGIST_TEXT, mainl_03_TEXT +group_01 group CFG_LRES_TEXT, mainl_01_TEXT, SCOREDAT_TEXT, REGIST_TEXT, mainl_03_TEXT ; =========================================================================== @@ -2600,64 +2600,10 @@ sub_AC6E endp mainl_01_TEXT ends SCOREDAT_TEXT segment byte public 'CODE' use16 - @scoredat_decode$qv procdesc near - @scoredat_recreate$qv procdesc near - @scoredat_sum_invalid$qv procdesc near + @SCOREDAT_LOAD_AND_DECODE$Q6RANK_T procdesc pascal near \ + rank:word SCOREDAT_TEXT ends -mainl_02_TEXT segment byte public 'CODE' use16 - -; =============== S U B R O U T I N E ======================================= - -; Attributes: bp-based frame - -sub_AE8E proc near - -arg_0 = word ptr 4 - - push bp - mov bp, sp - push ds - push _SCOREDAT_FN - call file_exist - or ax, ax - jnz short loc_AEB0 - push ds - push _SCOREDAT_FN - call file_create - call file_close - jmp short loc_AEE9 -; --------------------------------------------------------------------------- - -loc_AEB0: - push ds - push _SCOREDAT_FN - call file_ropen - mov ax, [bp+arg_0] - imul ax, size scoredat_section_t - movzx eax, ax - push eax - push 0 - call file_seek - push ds - push offset _hi - push size scoredat_section_t - call file_read - call file_close - call @scoredat_decode$qv - call @scoredat_sum_invalid$qv - or al, al - jz short loc_AEEC - -loc_AEE9: - call @scoredat_recreate$qv - -loc_AEEC: - pop bp - retn 2 -sub_AE8E endp -mainl_02_TEXT ends - REGIST_TEXT segment byte public 'CODE' use16 @SCOREDAT_ENCODE_AND_SAVE$Q6RANK_T procdesc pascal near \ rank:word @@ -3787,8 +3733,7 @@ sub_B7D2 proc near les bx, _resident mov al, es:[bx+resident_t.rank] mov ah, 0 - push ax - call sub_AE8E + call @scoredat_load_and_decode$q6rank_t pascal, ax les bx, _resident cmp es:[bx+resident_t.story_stage], STAGE_NONE jnz short loc_B819 diff --git a/th03_op.asm b/th03_op.asm index c8e1acd0..73a281fd 100644 --- a/th03_op.asm +++ b/th03_op.asm @@ -1973,68 +1973,11 @@ op_02_TEXT segment byte public 'CODE' use16 op_02_TEXT ends SCOREDAT_TEXT segment byte public 'CODE' use16 - @scoredat_decode$qv procdesc near - @scoredat_recreate$qv procdesc near - @scoredat_sum_invalid$qv procdesc near SCOREDAT_TEXT ends op_03_TEXT segment byte public 'CODE' use16 - -; =============== S U B R O U T I N E ======================================= - -; Attributes: bp-based frame - -sub_B2F2 proc near - -arg_0 = word ptr 4 - - push bp - mov bp, sp - push ds - push _SCOREDAT_FN - call file_exist - or ax, ax - jnz short loc_B314 - push ds - push _SCOREDAT_FN - call file_create - call file_close - jmp short loc_B34D -; --------------------------------------------------------------------------- - -loc_B314: - push ds - push _SCOREDAT_FN - call file_ropen - mov ax, [bp+arg_0] - imul ax, size scoredat_section_t - movzx eax, ax - push eax - push 0 - call file_seek - push ds - push offset _hi - push size scoredat_section_t - call file_read - call file_close - call @scoredat_decode$qv - call @scoredat_sum_invalid$qv - or al, al - jz short loc_B357 - -loc_B34D: - call @scoredat_recreate$qv - mov ax, 1 - pop bp - retn 2 -; --------------------------------------------------------------------------- - -loc_B357: - xor ax, ax - pop bp - retn 2 -sub_B2F2 endp - + @SCOREDAT_LOAD_AND_DECODE$Q6RANK_T procdesc pascal near \ + rank:word ; =============== S U B R O U T I N E ======================================= @@ -2052,8 +1995,7 @@ var_1 = byte ptr -1 ; --------------------------------------------------------------------------- loc_B36A: - push si - call sub_B2F2 + call @scoredat_load_and_decode$q6rank_t pascal, si or ax, ax jz short loc_B376 mov al, 7 @@ -2069,7 +2011,7 @@ loc_B381: inc si loc_B382: - cmp si, 4 + cmp si, (RANK_LUNATIC + 1) jl short loc_B36A mov al, [bp+var_1]