1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-11-27 14:37:16 +00:00

cleanup: arguments

This commit is contained in:
Zeno Rogue 2018-02-26 13:14:20 +01:00
parent bf108b671b
commit 283c6bba39
5 changed files with 91 additions and 67 deletions

View File

@ -9,31 +9,32 @@ string picfile = "hyperrogue.pic";
const char *musicfile = "";
const char *loadlevel = NULL;
eLand readland(const char *s) {
string ss = s;
bool appears(const string& haystack, const string& needle) {
return haystack.find(needle) != string::npos;
}
eLand readland(const string& ss) {
if(ss == "II") return laCrossroads2;
if(ss == "III") return laCrossroads3;
if(ss == "IV") return laCrossroads4;
if(ss == "V") return laCrossroads5;
for(int l=0; l<landtypes; l++) if(strstr(linf[l].name, s) != NULL) {
for(int l=0; l<landtypes; l++) if(appears(linf[l].name, ss)) {
return eLand(l);
break;
}
return laNone;
}
eItem readItem(const char *s) {
string ss = s;
for(int i=0; i<ittypes; i++) if(strstr(iinf[i].name, s) != NULL) {
eItem readItem(const string& ss) {
for(int i=0; i<ittypes; i++) if(appears(iinf[i].name, ss)) {
return eItem(i);
break;
}
return itNone;
}
eMonster readMonster(const char *s) {
string ss = s;
for(int i=0; i<motypes; i++) if(strstr(minf[i].name, s) != NULL) {
eMonster readMonster(const string& ss) {
for(int i=0; i<motypes; i++) if(appears(minf[i].name, ss)) {
return eMonster(i);
break;
}
@ -59,10 +60,36 @@ void initializeCLI() {
#endif
}
namespace arg {
vector<string> argument;
int pos;
void lshift() { pos++; }
void shift() {
lshift(); if(pos >= size(argument)) { printf("Missing parameter\n"); exit(1); }
}
const string& args() { return argument[pos]; }
const char* argcs() { return args().c_str(); }
int argi() { return atoi(argcs()); }
int arghex() { return strtol(argcs(), NULL, 16); }
ld argf() { return atof(argcs()); }
bool argis(const string& s) { return args() == s; }
void init(int argc, char **argv) { for(int i=0; i<argc; i++) argument.push_back(argv[i]); shift(); }
void phaseerror(int x) {
printf("Command line error: cannot read command '%s' from phase %d in phase %d\n", args().c_str(), x, curphase);
exit(1);
}
}
int arg::readCommon() {
if(argis("-c")) { PHASE(1); shift(); conffile = args(); }
else if(argis("-s")) { PHASE(1); shift(); scorefile = args(); }
else if(argis("-m")) { PHASE(1); shift(); musicfile = args(); }
if(argis("-c")) { PHASE(1); shift(); conffile = argcs(); }
else if(argis("-s")) { PHASE(1); shift(); scorefile = argcs(); }
else if(argis("-m")) { PHASE(1); shift(); musicfile = argcs(); }
#if CAP_SDLAUDIO
else if(argis("-se")) { PHASE(1); shift(); wheresounds = args(); }
#endif
@ -82,15 +109,15 @@ int arg::readCommon() {
PHASE(2);
firstland = specialland = laCanvas;
shift();
if(args()[1] == 0) patterns::whichCanvas = args()[0];
else patterns::canvasback = strtol(args(), NULL, 16);
if(args().size() == 1) patterns::whichCanvas = args()[0];
else patterns::canvasback = arghex();
}
else if(argis("-noplayer"))
mapeditor::drawplayer = !mapeditor::drawplayer;
else if(argis("-pattern")) {
PHASE(3);
shift();
const char *c = args();
const char *c = argcs();
using namespace patterns;
subpattern_flags = 0;
whichPattern = 0;
@ -118,13 +145,13 @@ int arg::readCommon() {
showstartmenu = false;
}
else if(argis("-back")) {
shift(); backcolor = strtol(args(), NULL, 16);
shift(); backcolor = arghex();
}
else if(argis("-borders")) {
shift(); bordcolor = strtol(args(), NULL, 16);
shift(); bordcolor = arghex();
}
else if(argis("-fore")) {
shift(); forecolor = strtol(args(), NULL, 16);
shift(); forecolor = arghex();
}
else if(argis("-W2")) {
shift(); cheatdest = readland(args()); autocheat = true;
@ -280,7 +307,7 @@ else if(args()[0] == '-' && args()[1] == x && args()[2] == '0') { showstartmenu
}
else if(argis("-qpar")) {
int p;
shift(); sscanf(args(), "%d,%d,%d",
shift(); sscanf(argcs(), "%d,%d,%d",
&p, &quotientspace::rvadd, &quotientspace::rvdir
);
autocheat = true;
@ -297,7 +324,7 @@ else if(args()[0] == '-' && args()[1] == x && args()[2] == '0') { showstartmenu
}
if(geometry == gQuotient2) restartGame('g');
int a, b;
shift(); sscanf(args(), "%d,%d", &a, &b);
shift(); sscanf(argcs(), "%d,%d", &a, &b);
using namespace fieldpattern;
current_extra = a;
fgeomextras[current_extra].current_prime_id = b;
@ -310,14 +337,14 @@ else if(args()[0] == '-' && args()[1] == x && args()[2] == '0') { showstartmenu
}
else if(argis("-tpar")) {
torusconfig::torus_mode = torusconfig::tmSingle;
shift(); sscanf(args(), "%d,%d,%d",
shift(); sscanf(argcs(), "%d,%d,%d",
&torusconfig::qty,
&torusconfig::dx,
&torusconfig::dy
);
}
else if(argis("-tparx")) {
shift(); sscanf(args(), "%d,%d,%d",
shift(); sscanf(argcs(), "%d,%d,%d",
(int*) &torusconfig::torus_mode,
&torusconfig::sdx,
&torusconfig::sdy
@ -383,7 +410,7 @@ else if(args()[0] == '-' && args()[1] == x && args()[2] == '0') { showstartmenu
PHASEFROM(2);
shift();
int clWidth=0, clHeight=0, clFont=0;
sscanf(args(), "%dx%dx%d", &clWidth, &clHeight, &clFont);
sscanf(argcs(), "%dx%dx%d", &clWidth, &clHeight, &clFont);
if(clWidth) vid.xres = clWidth;
if(clHeight) vid.yres = clHeight;
if(clFont) vid.fsize = clFont;
@ -452,12 +479,12 @@ else if(args()[0] == '-' && args()[1] == x && args()[2] == '0') { showstartmenu
#if CAP_SDL
else if(argis("-pngshot")) {
PHASE(3); shift();
printf("saving PNG screenshot to %s\n", args());
saveHighQualityShot(args());
printf("saving PNG screenshot to %s\n", argcs());
saveHighQualityShot(argcs());
}
#endif
else if(argis("-svgsize")) {
shift(); sscanf(args(), "%d/%d", &svg::svgsize, &svg::divby);
shift(); sscanf(argcs(), "%d/%d", &svg::svgsize, &svg::divby);
}
else if(argis("-svgfont")) {
shift(); svg::font = args();
@ -465,18 +492,18 @@ else if(args()[0] == '-' && args()[1] == x && args()[2] == '0') { showstartmenu
// (this is helpful with Inkscape's PDF+TeX output feature; define \myfont yourself)
}
else if(argis("-pngsize")) {
shift(); sscanf(args(), "%d", &pngres);
shift(); pngres = argi();
}
else if(argis("-pngformat")) {
shift(); sscanf(args(), "%d", &pngformat);
shift(); pngformat = argi();
}
else if(argis("-svggamma")) {
shift(); svg::gamma = argf();
}
else if(argis("-svgshot")) {
PHASE(3); shift();
printf("saving SVG screenshot to %s\n", args());
svg::render(args());
printf("saving SVG screenshot to %s\n", argcs());
svg::render(argcs());
}
else if(argis("--help") || argis("-h")) {
printf("Press F1 while playing to get ingame options.\n\n");
@ -532,7 +559,6 @@ purehookset hooks_config;
hookset<int()> *hooks_args;
namespace arg {
int argc; const char **argv;
int curphase;
auto ah = addHook(hooks_args, 0, readCommon);
@ -540,11 +566,11 @@ namespace arg {
void read(int phase) {
curphase = phase;
callhooks(hooks_config);
while(argc) {
while(pos < size(argument)) {
for(auto& h: *hooks_args) {
int r = h.second(); if(r == 2) return; if(r == 0) { lshift(); goto cont; }
}
printf("Unknown option: %s\n", args());
printf("Unknown option: %s\n", argcs());
exit(3);
cont: ;
}

View File

@ -40,12 +40,14 @@ void moreStack() {
}
#endif
hookset<bool(int argc, const char** argv)> *hooks_main;
hookset<bool(int argc, char** argv)> *hooks_main;
#ifndef NOMAIN
int main(int argc, const char **argv) {
int main(int argc, char **argv) {
#if ISWEB
emscripten_get_commandline(argc, argv);
emscripten_get_commandline();
#else
arg::init(argc, argv);
#endif
if(callhandlers(false, hooks_main, argc, argv)) return 0;
#if !ISWEB
@ -54,7 +56,6 @@ int main(int argc, const char **argv) {
#endif
#endif
#if CAP_COMMANDLINE
arg::init(argc, argv);
initializeCLI();
#endif
initAll();

27
hyper.h
View File

@ -1503,29 +1503,22 @@ extern int cshpos;
namespace arg {
#if CAP_COMMANDLINE
extern int argc; extern const char **argv;
inline void lshift() {
argc--; argv++;
}
void lshift();
inline void shift() {
lshift(); if(!argc) { printf("Missing parameter\n"); exit(1); }
}
void shift();
inline const char* args() { return *argv; }
inline int argi() { return atoi(*argv); }
inline ld argf() { return atof(*argv); }
inline bool argis(const char *s) { return strcmp(*argv, s) == 0; }
const string& args();
int argi();
ld argf();
bool argis(const string& s);
int arghex();
inline void init(int _argc, const char **_argv) { argc=_argc-1; argv=_argv+1; }
void init(int _argc, char **_argv);
extern int curphase;
inline void phaseerror(int x) {
printf("Command line error: cannot read command '%s' from phase %d in phase %d\n", args(), x, curphase);
exit(1);
}
void phaseerror(int x);
// returned values: 0 = ok, 1 = not recognized, 2 = shift phase
int readCommon();
@ -1758,7 +1751,7 @@ template<class T, class V, class... U> V callhandlers(V zero, hookset<T> *h, U&.
extern hookset<bool(int sym, int uni)> *hooks_handleKey;
extern hookset<void(cell *c, const transmatrix& V)> *hooks_drawcell;
extern hookset<bool(int argc, const char** argv)> *hooks_main;
extern hookset<bool(int argc, char** argv)> *hooks_main;
extern hookset<int()> *hooks_args;
extern hookset<eLand(eLand)> *hooks_nextland;

View File

@ -24,7 +24,7 @@ template<class A, class B, class C> void emscripten_set_main_loop(A a, B b, C c)
#endif
void initweb();
void emscripten_get_commandline(int& argc, const char **& argv);
void emscripten_get_commandline();
void loadCompressedChar(int &otwidth, int &otheight, int *tpix);
@ -151,10 +151,10 @@ transmatrix getOrientation() {
rotmatrix(0, 2, gamma * M_PI / 180);
}
vector<string> emscripten_args;
vector<const char*> emscripten_args_c;
void emscripten_get_commandline(int& argc, const char **& argv) {
void emscripten_get_commandline() {
#ifdef EMSCRIPTEN_FIXED_ARG
string s = EMSCRIPTEN_FIXED_ARG;
#else
char *str = (char*)EM_ASM_INT({
var jsString = document.location.href;
var lengthBytes = lengthBytesUTF8(jsString)+1;
@ -163,14 +163,17 @@ void emscripten_get_commandline(int& argc, const char **& argv) {
return stringOnWasmHeap;
});
string s = str;
#endif
s += "+xxxxxx";
for(int i=0; i<size(s); i++) if(s[i] == '?') {
#ifndef EMSCRIPTEN_FIXED_ARG
printf("HREF: %s\n", str);
emscripten_args.push_back("hyperweb");
#endif
arg::argument.push_back("hyperweb"); arg::lshift();
string next = ""; i += 3;
for(; i<size(s); i++) {
if(s[i] == '+') {
emscripten_args.push_back(next);
arg::argument.push_back(next);
next = "";
}
else if(s[i] == '%') {
@ -182,11 +185,12 @@ void emscripten_get_commandline(int& argc, const char **& argv) {
}
else next += s[i];
}
argc = size(emscripten_args);
for(string& s: emscripten_args) emscripten_args_c.push_back(s.c_str());
argv = &(emscripten_args_c[0]);
printf("Arguments:"); for(int i=0; i<argc; i++) printf(" %s", argv[i]); printf("\n");
printf("Arguments:"); for(string s: arg::argument) printf(" %s", s.c_str()); printf("\n");
break;
}
#ifndef EMSCRIPTEN_FIXED_ARG
free(str);
#endif
}

View File

@ -196,8 +196,8 @@ namespace mapstream {
return dir;
}
bool loadMap(const char *fname) {
f = fopen(fname, "rb");
bool loadMap(const string& fname) {
f = fopen(fname.c_str(), "rb");
if(!f) return false;
clearMemory();
int vernum = loadInt();