[Maintenance] [th02] Move .CFG/resident initialization to separate files

All future RES_*.COM binaries reuse this code.

Completes P0076, funded by [Anonymous] and -Tom-.
This commit is contained in:
nmlgc 2020-02-21 22:56:18 +01:00
parent f8d83eefa3
commit 9ae9754fb3
7 changed files with 102 additions and 97 deletions

19
th02/formats/cfg.h Normal file
View File

@ -0,0 +1,19 @@
#pragma option -a1
#if GAME == 2
# define CFG_FN "huuma.cfg"
typedef struct {
int8_t rank;
int8_t bgm_mode;
int8_t bombs;
int8_t lives;
int8_t perf;
} cfg_options_t;
#endif
typedef struct {
cfg_options_t opts;
void __seg *resident_sgm;
int8_t debug;
} cfg_t;
#pragma option -a.

15
th02/formats/cfg_init.c Normal file
View File

@ -0,0 +1,15 @@
void cfg_init(seg_t resident_sgm)
{
const char *fn = CFG_FN;
cfg_options_t opts = OPTS_DEFAULT;
int handle = dos_axdx(0x3D02, fn);
if(handle > 0) {
dos_seek(handle, sizeof(opts), SEEK_SET);
} else {
handle = dos_create(fn, _A_ARCH);
dos_write(handle, &opts, sizeof(opts));
}
dos_write(handle, &resident_sgm, sizeof(resident_sgm));
dos_write(handle, &debug, sizeof(debug));
dos_close(handle);
}

View File

@ -4,6 +4,7 @@
*/
#include "th02\th02.h"
#include "th02/formats/cfg.h"
char rank = RANK_NORMAL;
char unused_1 = 0;
@ -23,7 +24,7 @@ int pascal cfg_load(void)
seg_t resident_sgm;
file_ropen(CFG_FN);
file_seek(offsetof(huuma_cfg_t, resident_sgm), 0);
file_seek(offsetof(cfg_t, resident_sgm), 0);
file_read(&resident_sgm, sizeof(resident_sgm));
file_close();
if(!resident_sgm) {

View File

@ -7,6 +7,7 @@
#include "libs/kaja/kaja.h"
#include "th02/th02.h"
#include "th02/initexit.h"
#include "th02/formats/cfg.h"
#include "th02/formats/pi.h"
#include "th02/snd/snd.h"
@ -40,7 +41,7 @@ void pascal musicroom(void);
int cfg_load(void)
{
huuma_cfg_t cfg;
cfg_t cfg;
const char *cfg_fn = CFG_FN;
if(file_exist(cfg_fn)) {
@ -77,7 +78,7 @@ int cfg_load(void)
void cfg_save(void)
{
const char *cfg_fn = CFG_FN;
huuma_cfg_t cfg;
cfg_t cfg;
cfg.debug = 0;
cfg.opts.rank = rank;
@ -87,7 +88,7 @@ void cfg_save(void)
cfg.opts.perf = resident->perf;
file_create(cfg_fn);
file_write(&cfg, offsetof(huuma_cfg_t, resident_sgm));
file_write(&cfg, offsetof(cfg_t, resident_sgm));
file_write(&resident_sgm, sizeof(resident_sgm));
file_write(&cfg.debug, sizeof(cfg.debug));
file_close();

51
th02/res_init.c Normal file
View File

@ -0,0 +1,51 @@
int main(int argc, const char **argv)
{
seg_t sgm;
const char *res_id = RES_ID;
int i;
char far *resident_bytes;
sgm = resdata_exist(res_id, RES_ID_STRLEN, RES_PARASIZE);
dos_puts2("\n\n" LOGO "\n");
graph_clear();
#ifdef RES_INIT_TOP
RES_INIT_TOP;
#endif
if(argc == 2) {
#define arg1_is(capital, small) \
(argv[1][0] == '-' || argv[1][0] == '/') \
&& (argv[1][1] == (capital) || argv[1][1] == (small))
if(arg1_is('R', 'r')) {
if(!sgm) {
dos_puts2("わたし、まだいませんよぉ\n\n");
return 1;
}
dos_free(sgm);
dos_puts2("さよなら、また会えたらいいな\n\n");
return 0;
} else if(arg1_is('D', 'd')) {
debug = 1;
} else {
dos_puts2("そんなオプション付けられても、困るんですけど\n\n");
sgm = sgm; /* optimization barrier #1 */
return 1;
}
}
if(sgm) {
dos_puts2("わたし、すでにいますよぉ\n\n");
argv = argv; /* optimization barrier #2 */
return 1;
}
sgm = resdata_create(res_id, RES_ID_STRLEN, RES_PARASIZE);
if(!sgm) {
dos_puts2("作れません、わたしの居場所がないの!\n\n");
return 1;
}
resident_bytes = MK_FP(sgm, 0);
dos_puts2("それでは、よろしくお願いします\n\n");
for(i = (RES_ID_STRLEN + 1); i < sizeof(resident_t); i++) {
resident_bytes[i] = 0;
}
cfg_init(sgm);
return 0;
}

View File

@ -151,26 +151,6 @@ void key_delay(void);
#define MUSIC_CMT_LINE_LEN 42
#define MUSIC_CMT_LINE_COUNT 20
// Configuration file
// ------------------
#define CFG_FN "huuma.cfg"
#pragma option -a1
typedef struct {
int8_t rank;
int8_t bgm_mode;
int8_t bombs;
int8_t lives;
int8_t perf;
} huuma_options_t;
typedef struct {
huuma_options_t opts;
void __seg *resident_sgm;
int8_t debug;
} huuma_cfg_t;
#pragma option -a.
// ------------------
// Resident structure
// ------------------
#define RES_ID "MIKOConfig"

View File

@ -7,88 +7,26 @@
#include <stddef.h>
#include "th02/th02.h"
#include "th02/snd/snd.h"
#include "th02/formats/cfg.h"
#pragma option -a1
int pascal scoredat_verify(void);
char debug = 0;
const cfg_options_t OPTS_DEFAULT = { RANK_NORMAL, SND_BGM_FM, 3, 2, 0 };
void cfg_write(seg_t resident_sgm)
{
static const huuma_options_t opts_default = {
RANK_NORMAL, SND_BGM_FM, 3, 2, 0
};
static const char HUUMA_CFG[] = CFG_FN;
const char *fn = HUUMA_CFG;
huuma_options_t opts = opts_default;
int handle = dos_axdx(0x3D02, fn);
if(handle > 0) {
dos_seek(handle, sizeof(opts), SEEK_SET);
} else {
handle = dos_create(fn, _A_ARCH);
dos_write(handle, &opts, sizeof(opts));
}
dos_write(handle, &resident_sgm, sizeof(resident_sgm));
dos_write(handle, &debug, sizeof(debug));
dos_close(handle);
}
#include "th02/formats/cfg_init.c"
#define LOGO \
"東方封魔録用  常駐プログラム ZUN_RES.com Version1.01 (c)zun 1997"
int main(int argc, const char **argv)
{
int pascal scoredat_verify(void);
#define RES_INIT_TOP \
if(scoredat_verify() == 1) { \
dos_puts2("ハイスコアファイルがおかしいの、もう一度実行してね。\n"); \
return 1; \
}
seg_t sgm;
const char *res_id = RES_ID;
int i;
char far *resident;
sgm = resdata_exist(res_id, RES_ID_STRLEN, RES_PARASIZE);
dos_puts2("\n\n" LOGO "\n");
graph_clear();
if(scoredat_verify() == 1) {
dos_puts2("ハイスコアファイルがおかしいの、もう一度実行してね。\n");
return 1;
}
if(argc == 2) {
#define arg1_is(capital, small) \
(argv[1][0] == '-' || argv[1][0] == '/') \
&& (argv[1][1] == (capital) || argv[1][1] == (small))
if(arg1_is('R', 'r')) {
if(!sgm) {
dos_puts2("わたし、まだいませんよぉ\n\n");
return 1;
}
dos_free(sgm);
dos_puts2("さよなら、また会えたらいいな\n\n");
return 0;
} else if(arg1_is('D', 'd')) {
debug = 1;
} else {
dos_puts2("そんなオプション付けられても、困るんですけど\n\n");
sgm = sgm; /* optimization barrier #1 */
return 1;
}
}
if(sgm) {
dos_puts2("わたし、すでにいますよぉ\n\n");
argv = argv; /* optimization barrier #2 */
return 1;
}
sgm = resdata_create(res_id, RES_ID_STRLEN, RES_PARASIZE);
if(!sgm) {
dos_puts2("作れません、わたしの居場所がないの!\n\n");
return 1;
}
resident = MK_FP(sgm, 0);
dos_puts2("それでは、よろしくお願いします\n\n");
for(i = offsetof(resident_t, stage); i < sizeof(resident_t); i++) {
resident[i] = 0;
}
cfg_write(sgm);
return 0;
}
#include "th02/res_init.c"
#pragma codestring "\x00"