From 8de23299d96c642ec39ec73d12322cc78865136c Mon Sep 17 00:00:00 2001 From: johannes Date: Sat, 6 Apr 2024 13:09:04 +0200 Subject: implemented extract map --- inc/commands.h | 3 +++ makefile | 2 +- src/commands.c | 8 ++++++++ src/main.c | 31 +++++++++++++++++++++++-------- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/inc/commands.h b/inc/commands.h index 9761151..b48b28c 100644 --- a/inc/commands.h +++ b/inc/commands.h @@ -23,6 +23,9 @@ int setskinbyname(char *name, char *skin, demo *demo); /* Will fail if mapname >= 31, or map is not a teeworlds datafile */ int changemap(FILE *map, char *mapname, demo *demo); +/* Writes the map in demo to out */ +int exportmap(FILE *out, demo *demo); + void runcommand(cmdinput *cmd, demo *demo); #endif // COMMANDS_H diff --git a/makefile b/makefile index 0c26517..63d2cb3 100644 --- a/makefile +++ b/makefile @@ -6,7 +6,7 @@ EXE := $(BIN_DIR)/dedit SRC := $(wildcard $(SRC_DIR)/*.c) OBJ := $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) -CFLAGS := -Wall -Wextra +CFLAGS := -Wall -Wextra .PHONY: all clean run format diff --git a/src/commands.c b/src/commands.c index 752b451..337b0d3 100644 --- a/src/commands.c +++ b/src/commands.c @@ -171,6 +171,14 @@ int changemap(FILE *map, char *mapname, demo *demo) return 1; } +int exportmap(FILE *out, demo *demo) +{ + if (fwrite(demo->map.data, 1, demo->header.mapsize, out) < (unsigned int) demo->header.mapsize) + return -1; + + return 1; +} + void runcommand(cmdinput *cmd, demo *demo) { switch (cmd->cmdtype) diff --git a/src/main.c b/src/main.c index 2889601..7b75a30 100644 --- a/src/main.c +++ b/src/main.c @@ -37,7 +37,7 @@ int mapname(char *mappath, char *out) return 1; } -FILE *demofile = NULL, *outfile = NULL, *mapfile = NULL, *exmapfile = NULL; +FILE *demofile = NULL, *outfile = NULL, *mapfile = NULL, *extmapfile = NULL; demo DEMO; void exitperror(char *str) @@ -117,11 +117,26 @@ void runsetskin(arg *skinarg) } } +void runextractmap(arg *extarg) +{ + extmapfile = fopen(extarg->opts[0], "w"); + if (!extmapfile) + exitperror("Error: failed to write to extract map file, reason"); + + if (exportmap(extmapfile, &DEMO) < 0) + { + fprintf(stderr, "Error: failed to write demo map to extract map file\n"); + exit(EXIT_FAILURE); + } + fclose(extmapfile); +} + void runoutput(arg *outarg) { outfile = fopen(outarg->opts[0], "w"); if (!outfile) exitperror("Error: failed to write to outputfile, reason"); + if (writedemo(outfile, &DEMO) < 0) { fprintf(stderr, "Error: failed to write demo file to outputfile\n"); @@ -139,8 +154,8 @@ int main(int argc, char *argv[]) addarg("", "Sets input demo", setdemo); - addopt("-r", "--rename", 2, " ", "Renames player with id/name to name", NULL); - addopt("-s", "--skin", 2, " ", "Set skin of player with id/name to skin", NULL); + addopt("-r", "--rename", 2, " ", "Renames player with id/name to name", runrename); + addopt("-s", "--skin", 2, " ", "Set skin of player with id/name to skin", runsetskin); addopt("-m", "--map", 1, "", "Changes the map of demo to map", setmap); addopt("-e", "--extract-map", 1, "", "Saves the map of demo to file", NULL); addopt("-o", "--output", 1, "", "Saves the output demo to file", NULL); @@ -175,6 +190,9 @@ int main(int argc, char *argv[]) arg *infarg = getopt("-i", 0); arg *Infarg = getopt("-I", 0); + if (exmarg) + runextractmap(exmarg); + if (maparg) setmap(maparg); @@ -183,9 +201,6 @@ int main(int argc, char *argv[]) else if (infarg) printdemo(&DEMO, 0); - if (exmarg) - printf("Info: not implemented extract map\n"); - int i = 0; arg *opt; while (1) @@ -193,7 +208,7 @@ int main(int argc, char *argv[]) opt = getopt("-s", i); if (opt == NULL) break; - runsetskin(opt); + opt->runarg(opt); i++; } @@ -203,7 +218,7 @@ int main(int argc, char *argv[]) opt = getopt("-r", i); if (opt == NULL) break; - runrename(opt); + opt->runarg(opt); i++; } -- cgit v1.2.3