Skip to content

Commit

Permalink
[Decompilation] [th01] REIIDEN.CFG loading and saving
Browse files Browse the repository at this point in the history
That's where the backwards `goto` for .CFG file error handling
originated!

Part of P0090, funded by Yanga.
  • Loading branch information
nmlgc committed May 12, 2020
1 parent 05a0e9b commit 7ad14db
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 213 deletions.
21 changes: 21 additions & 0 deletions th01/formats/cfg.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#define CFG_FN "reiiden.cfg"
#define CFG_ID "REIIDEN"

struct cfg_options_t {
int8_t rank;
int8_t bgm_mode;
int8_t bombs;
int8_t lives_extra; // Add 2 for the actual number of lives
};

#define CFG_RANK_DEFAULT (RANK_NORMAL)
#define CFG_BGM_MODE_DEFAULT (true)
#define CFG_BOMBS_DEFAULT (1)
#define CFG_LIVES_EXTRA_DEFAULT (2)

#define CFG_LIVES_EXTRA_MAX (4)

struct cfg_t {
char id[sizeof(CFG_ID) - 1];
cfg_options_t opts;
};
13 changes: 13 additions & 0 deletions th01/formats/cfg.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cfg_options_t struc
O_rank db ?
O_bgm_mode db ?
O_bombs db ?
O_lives_extra db ?
cfg_options_t ends

CFG_RANK_DEFAULT = RANK_NORMAL
CFG_BGM_MODE_DEFAULT = 1
CFG_BOMBS_DEFAULT = 1
CFG_LIVES_EXTRA_DEFAULT = 2

CFG_LIVES_EXTRA_MAX = 4
6 changes: 6 additions & 0 deletions th01/formats/cfg[data].asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public _CFG_FN, _CFG_ID, _FOPEN_RB, _FOPEN_WB
_CFG_FN db 'reiiden.cfg',0
_FOPEN_RB db 'rb',0
_REIIDEN label byte
_CFG_ID db 'REIIDEN',0
_FOPEN_WB db 'wb',0
61 changes: 61 additions & 0 deletions th01/op_01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
* Code segment #1 of TH01's OP.EXE
*/

#pragma option -Z

extern "C" {
#include <stdio.h>
#include "ReC98.h"
#include "th01/ranks.h"
#include "th01/hardware/graph.h"
#include "th01/formats/cfg.hpp"

// Unused. The only thing on the main menu with this color is the "1996 ZUN"
// text at the bottom... probably part of an effect that we never got to see.
Expand Down Expand Up @@ -36,4 +41,60 @@ void snap_col_4(void)
graph_accesspage_func(0);
}

/// REIIDEN.CFG loading and saving
/// ------------------------------
extern cfg_options_t opts;
// These will be removed once the strings can be defined here
#undef CFG_FN
#undef CFG_ID
extern const char CFG_FN[], CFG_ID[], FOPEN_RB[], FOPEN_WB[];

void cfg_load(void)
{
cfg_t cfg_in;
bool read_failure = false;
FILE* fp;

if(( fp = fopen(CFG_FN, FOPEN_RB) ) == NULL) {
use_defaults:
read_failure = true;
}
if(!read_failure) {
fread(&cfg_in, 1, sizeof(cfg_in), fp);
if(memcmp(cfg_in.id, CFG_ID, sizeof(cfg_in.id))) {
fclose(fp);
goto use_defaults;
}
opts.rank = cfg_in.opts.rank;
opts.bgm_mode = cfg_in.opts.bgm_mode;
opts.bombs = cfg_in.opts.bombs;
opts.lives_extra = cfg_in.opts.lives_extra;
fclose(fp);
} else {
opts.rank = CFG_RANK_DEFAULT;
opts.bgm_mode = CFG_BGM_MODE_DEFAULT;
opts.bombs = CFG_BOMBS_DEFAULT;
opts.lives_extra = CFG_LIVES_EXTRA_DEFAULT;
}
}

void cfg_save(void)
{
bool write_failure = false;
FILE* fp;

if(( fp = fopen(CFG_FN, FOPEN_WB) ) == NULL) {
write_failure = true;
}
if(!write_failure) {
fputs(CFG_ID, fp);
fputc(opts.rank, fp);
fputc(opts.bgm_mode, fp);
fputc(opts.bombs, fp);
fputc(opts.lives_extra, fp);
fclose(fp);
}
}
/// ------------------------------

}
Loading

0 comments on commit 7ad14db

Please sign in to comment.