Skip to content

Commit

Permalink
[Decompilation] [th04/th05] MIKO.CFG loading and saving
Browse files Browse the repository at this point in the history
Part of P0261, funded by [Anonymous] and Yanga.
  • Loading branch information
nmlgc committed Nov 1, 2023
1 parent 412df0c commit 0ded9d4
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 177 deletions.
4 changes: 2 additions & 2 deletions Makefile.mak
Expand Up @@ -147,7 +147,7 @@ bin\th04\res_huma.com: th04\res_huma.cpp
$**
| masters.lib

bin\th04\op.exe: bin\th04\op.obj th04\m_char.cpp bin\th01\vplanset.obj bin\th02\frmdely1.obj bin\th03\pi_put.obj bin\th03\pi_load.obj bin\th03\hfliplut.obj bin\th04\input_w.obj bin\th04\vector.obj bin\th04\snd_pmdr.obj bin\th04\snd_mmdr.obj bin\th04\snd_kaja.obj bin\th04\cdg_p_nc.obj bin\th04\snd_mode.obj bin\th04\snd_dlym.obj bin\th02\exit_dos.obj bin\th04\snd_load.obj bin\th04\cdg_put.obj bin\th04\exit.obj bin\th04\initop.obj bin\th04\cdg_p_na.obj bin\th04\input_s.obj bin\th04\snd_se_r.obj bin\th04\snd_se.obj bin\th04\egcrect.obj bin\th04\bgimage.obj bin\th04\bgimager.obj bin\th04\cdg_load.obj th04\frmdely2.c
bin\th04\op.exe: th04\cfg.cpp bin\th04\op.obj th04\m_char.cpp bin\th01\vplanset.obj bin\th02\frmdely1.obj bin\th03\pi_put.obj bin\th03\pi_load.obj bin\th03\hfliplut.obj bin\th04\input_w.obj bin\th04\vector.obj bin\th04\snd_pmdr.obj bin\th04\snd_mmdr.obj bin\th04\snd_kaja.obj bin\th04\cdg_p_nc.obj bin\th04\snd_mode.obj bin\th04\snd_dlym.obj bin\th02\exit_dos.obj bin\th04\snd_load.obj bin\th04\cdg_put.obj bin\th04\exit.obj bin\th04\initop.obj bin\th04\cdg_p_na.obj bin\th04\input_s.obj bin\th04\snd_se_r.obj bin\th04\snd_se.obj bin\th04\egcrect.obj bin\th04\bgimage.obj bin\th04\bgimager.obj bin\th04\cdg_load.obj th04\frmdely2.c
$(CC) $(CFLAGS) $(LARGE_LFLAGS) -DGAME=4 -DBINARY='O' -3 -Z -d -nbin\th04\ -eOP.EXE @&&|
$**
|
Expand All @@ -170,7 +170,7 @@ bin\th05\res_kso.com: th05\res_kso.cpp
$**
| masters.lib

bin\th05\op.exe: th05\op_start.cpp bin\th05\op.obj th05\hi_view.cpp th05\m_char.cpp bin\th04\cdg_p_na.obj bin\th04\snd_se_r.obj bin\th04\snd_se.obj bin\th04\bgimage.obj bin\th05\cdg_put.obj bin\th04\exit.obj bin\th05\vector.obj bin\th05\musicp_c.obj bin\th05\musicp_a.obj bin\th05\bgimager.obj bin\th05\snd_load.obj bin\th05\snd_kaja.obj bin\th05\pi_cpp_1.obj bin\th05\pi_asm_1.obj bin\th05\pi_cpp_2.obj bin\th05\pi_asm_2.obj bin\th05\initop.obj bin\th05\input_s.obj bin\th05\inp_h_w.obj bin\th05\snd_dlym.obj th05\cdg_p_nc.cpp bin\th05\frmdelay.obj bin\th04\cdg_load.obj bin\th05\egcrect.obj bin\th03\hfliplut.obj bin\th04\snd_pmdr.obj bin\th04\snd_mmdr.obj bin\th04\snd_mode.obj bin\th02\exit_dos.obj
bin\th05\op.exe: th05\op_start.cpp bin\th05\op.obj th05\cfg.cpp th05\hi_view.cpp th05\m_char.cpp bin\th04\cdg_p_na.obj bin\th04\snd_se_r.obj bin\th04\snd_se.obj bin\th04\bgimage.obj bin\th05\cdg_put.obj bin\th04\exit.obj bin\th05\vector.obj bin\th05\musicp_c.obj bin\th05\musicp_a.obj bin\th05\bgimager.obj bin\th05\snd_load.obj bin\th05\snd_kaja.obj bin\th05\pi_cpp_1.obj bin\th05\pi_asm_1.obj bin\th05\pi_cpp_2.obj bin\th05\pi_asm_2.obj bin\th05\initop.obj bin\th05\input_s.obj bin\th05\inp_h_w.obj bin\th05\snd_dlym.obj th05\cdg_p_nc.cpp bin\th05\frmdelay.obj bin\th04\cdg_load.obj bin\th05\egcrect.obj bin\th03\hfliplut.obj bin\th04\snd_pmdr.obj bin\th04\snd_mmdr.obj bin\th04\snd_mode.obj bin\th02\exit_dos.obj
$(CC) $(CFLAGS) $(LARGE_LFLAGS) -DGAME=5 -DBINARY='O' -3 -Z -nbin\th05\ -eOP.EXE @&&|
$**
|
Expand Down
3 changes: 2 additions & 1 deletion th03/formats/cfg.hpp
Expand Up @@ -31,7 +31,8 @@ static inline resident_t __seg* cfg_load_and_set_resident(
return resident_seg;
}

// Saves the .CFG file, without changing its resident segment pointer.
// Saves the current configuration values to the .CFG file, without changing
// its resident segment pointer.
void near cfg_save(void);

// Like cfg_save(), but resets the resident segment pointer to zero.
Expand Down
3 changes: 0 additions & 3 deletions th03/formats/cfg[data].asm

This file was deleted.

1 change: 1 addition & 0 deletions th04/cfg.cpp
@@ -0,0 +1 @@
#include "th04/formats/cfg.cpp"
157 changes: 0 additions & 157 deletions th04/formats/cfg.asm

This file was deleted.

101 changes: 101 additions & 0 deletions th04/formats/cfg.cpp
@@ -0,0 +1,101 @@
#include <stddef.h>
#include "platform.h"
#include "master.hpp"
#include "th04/score.h"
#if (GAME == 5)
#include "th04/common.h"
#include "th05/resident.hpp"
#else
#include "th04/resident.hpp"
#endif
#include "th04/formats/cfg.hpp"
#include "th04/snd/snd.h"

#undef CFG_FN
extern const char CFG_FN[];

#if (GAME == 4)
bool snd_sel_disabled = false; // ZUN bloat: Unused in this game.
#endif

inline void cfg_options_update_from_resident(cfg_options_t &opts) {
opts.rank = resident->rank;
opts.lives = resident->cfg_lives;
opts.bombs = resident->cfg_bombs;
opts.bgm_mode = resident->bgm_mode;
opts.se_mode = resident->se_mode;
opts.turbo_mode = resident->turbo_mode;
}

void near cfg_load(void)
{
cfg_t cfg;

cfg_load_and_set_resident(cfg, CFG_FN);

// TH05 does this in RES_KSO.COM.
#if (GAME == 4)
resident->rank = cfg.opts.rank;
resident->cfg_lives = cfg.opts.lives;
resident->cfg_bombs = cfg.opts.bombs;
resident->bgm_mode = cfg.opts.bgm_mode;
resident->se_mode = cfg.opts.se_mode;
resident->turbo_mode = cfg.opts.turbo_mode;
#endif

if((resident->cfg_lives > CFG_LIVES_MAX) || (resident->cfg_lives == 0)) {
resident->cfg_lives = CFG_LIVES_DEFAULT;
}
if(resident->cfg_bombs > CFG_BOMBS_MAX) {
resident->cfg_bombs = CFG_BOMBS_MAX;
}
if(resident->bgm_mode >= SND_BGM_MODE_COUNT) {
resident->bgm_mode = SND_BGM_OFF;
}
if(resident->se_mode >= SND_SE_MODE_COUNT) {
resident->se_mode = SND_SE_OFF;
}
}

void near cfg_save(void)
{
union {
int8_t sum;
int16_t space; // ZUN bloat
} u1;
cfg_options_t opts;

file_append(CFG_FN); // We don't write the full file here!
file_seek(0, SEEK_SET);

cfg_options_update_from_resident(opts);
file_write(&opts, sizeof(cfg_options_t));

// Skip over the resident pointer
file_seek(offsetof(cfg_t, opts_sum), SEEK_SET);

u1.sum = opts.sum();
file_write(&u1.sum, sizeof(u1.sum));
file_close();
}

void near cfg_save_exit(void)
{
#if (GAME == 5)
extern const cfg_t cfg_empty;
cfg_t cfg = cfg_empty;
#else
cfg_t cfg = { 0 };
#endif

// ZUN landmine: In contrast to the function above, we do write the entire
// structure here. Since file_append() will fail if the file doesn't exist,
// file_create() would have been safer here.
file_append(CFG_FN);
file_seek(0, SEEK_SET);

cfg_options_update_from_resident(cfg.opts);
cfg.opts_sum = cfg.opts.sum();
file_write(&cfg, sizeof(cfg));
file_close();
}
4 changes: 4 additions & 0 deletions th04/formats/cfg.hpp
Expand Up @@ -6,6 +6,10 @@ typedef struct {
int8_t bgm_mode;
int8_t se_mode;
int8_t turbo_mode;

int8_t sum(void) const {
return (rank + lives + bombs + bgm_mode + se_mode + turbo_mode);
}
} cfg_options_t;

#include "th03/formats/cfg.hpp"
Expand Down
20 changes: 12 additions & 8 deletions th04_op.asm
Expand Up @@ -23,11 +23,11 @@ include th04/th04.inc
include th04/hardware/grppsafx.inc
include th04/op/music.inc

extern SCOPY@:proc
extern _execl:proc
extern _getch:proc
extern _strlen:proc

op_01 group CFG_TEXT, OP_01_TEXT
g_SHARED group SHARED, SHARED_

; ===========================================================================
Expand Down Expand Up @@ -153,14 +153,18 @@ _TEXT ends

; ===========================================================================

CFG_TEXT segment byte public 'CODE' use16
@cfg_load$qv procdesc near
@cfg_save$qv procdesc near
@cfg_save_exit$qv procdesc near
CFG_TEXT ends

; Segment type: Pure code
op_01_TEXT segment byte public 'CODE' use16
assume cs:op_01_TEXT
assume cs:op_01
;org 0Ch
assume es:nothing, ss:nothing, ds:_DATA, fs:nothing, gs:nothing

include th04/formats/cfg.asm

; =============== S U B R O U T I N E =======================================

; Attributes: bp-based frame
Expand Down Expand Up @@ -2672,8 +2676,8 @@ SHARED_ ends

.data

include th03/formats/cfg[data].asm
_menu_sel db 0
_menu_sel = byte ptr $-1 ; place in padding area of previous segment

_quit db 0
_main_menu_unused_1 db 1
public _MENU_DESC
Expand Down Expand Up @@ -2706,8 +2710,8 @@ _MENU_DESC label dword
dd aGqbGav_3 ; "ゲームを開始します(ルナティック)"
_main_menu_initialized db 0
_option_initialized db 0
public _cfg_fn
_cfg_fn db 'MIKO.CFG',0
public _CFG_FN
_CFG_FN db 'MIKO.CFG',0
; char aMain[]
aMain db 'main',0
; char path[]
Expand Down
1 change: 1 addition & 0 deletions th05/cfg.cpp
@@ -0,0 +1 @@
#include "th04/formats/cfg.cpp"
4 changes: 2 additions & 2 deletions th05/formats/cfg[data].asm
@@ -1,3 +1,3 @@
public _cfg_empty, _cfg_fn
public _cfg_empty, _CFG_FN
_cfg_empty cfg_t <0>
_cfg_fn db 'MIKO.CFG',0
_CFG_FN db 'MIKO.CFG',0

0 comments on commit 0ded9d4

Please sign in to comment.