JHHM

dedit

a tool for Teeworlds demo file shenanigens
git clone https://jhhm.no/repos/dedit.git

dedit/src/main.c

get raw file: main.c

1/* This file is part of dedit - a tool for editing Teeworlds/DDNet demo files
2   Copyright (C) 2024, 2026 JHHM
3
4   Dedit is free software: you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation, either version 3 of the License, or
7   (at your option) any later version.
8
9   Dedit is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with dedit.  If not, see <https://www.gnu.org/licenses/>. */
16
17#define _POSIX_C_SOURCE 200112L
18
19#include "args.h"
20#include "chat.h"
21#include "commands.h"
22#include "demo.h"
23#include "huffman.h"
24#include "merge.h"
25
26#include <stdio.h>
27#include <stdlib.h>
28
29int mapname(char *mappath, char *out)
30{
31    int start = 0;
32    int end;
33    int i = 0;
34    while (1)
35    {
36        if (mappath[i] == '/')
37            start = i + 1;
38        if (mappath[i] == '.' || mappath[i] == '\0')
39        {
40            end = i;
41            break;
42        }
43        i++;
44    }
45
46    if ((end - start) > 31)
47        return 0;
48
49    i = 0;
50    while (start != end)
51    {
52        out[i] = mappath[start];
53        start++;
54        i++;
55    }
56    out[i] = '\0';
57    return 1;
58}
59
60FILE *demofile = NULL, *outfile = NULL, *mapfile = NULL, *extmapfile = NULL, *mergefile = NULL;
61demo DEMO;
62
63void exitperror(char *str)
64{
65    perror(str);
66    exit(EXIT_FAILURE);
67}
68
69void setdemo(arg *argument)
70{
71    demofile = fopen(argument->opts[0], "r");
72    if (!demofile)
73    {
74        fprintf(stderr, "Error: failed to open demofile '%s', ", argument->opts[0]);
75        exitperror("reason");
76    }
77    if (readdemo(demofile, &DEMO) < 0)
78    {
79        fprintf(stderr, "Error: failed to parse demo\n");
80        exit(EXIT_FAILURE);
81    }
82    fclose(demofile);
83}
84
85void setmap(arg *argument)
86{
87    mapfile = fopen(argument->opts[0], "r");
88    if (!mapfile)
89        exitperror("Error: failed to open mapfile, reason");
90
91    char mapnamebuff[32];
92    if (mapname(argument->opts[0], mapnamebuff))
93    {
94        if (changemap(mapfile, mapnamebuff, &DEMO) < 1)
95        {
96            fprintf(stderr, "Error: failed to set map '%s'\n", argument->opts[0]);
97            exit(EXIT_FAILURE);
98        }
99    }
100    else
101    {
102        fprintf(stderr, "Error: map name is too long (> 32)\n");
103        exit(EXIT_FAILURE);
104    }
105    fclose(mapfile);
106}
107
108void runmerge(arg *mergearg)
109{
110    demo other;
111    int ret;
112
113    mergefile = fopen(mergearg->opts[0], "r");
114    if (!mergefile)
115    {
116        fprintf(stderr, "Error: failed to open merge demofile '%s', ", mergearg->opts[0]);
117        exitperror("reason");
118    }
119    if (readdemo(mergefile, &other) < 0)
120    {
121        fprintf(stderr, "Error: failed to parse merge demo '%s'\n", mergearg->opts[0]);
122        exit(EXIT_FAILURE);
123    }
124    fclose(mergefile);
125
126    ret = mergedemo(&DEMO, &other);
127    if (ret < 0)
128    {
129        fprintf(stderr, "Error: failed to merge demo '%s'\n", mergearg->opts[0]);
130        exit(EXIT_FAILURE);
131    }
132
133    freedemo(&other);
134}
135
136void runrename(arg *renamearg)
137{
138    int intid;
139    int ret;
140
141    if (sscanf(renamearg->opts[0], "%d", &intid) == 1)
142        ret = setnamebyid(intid, renamearg->opts[1], &DEMO);
143    else
144        ret = setnamebyname(renamearg->opts[0], renamearg->opts[1], &DEMO);
145
146    if (ret < 0)
147    {
148        printf("Error: failed to rename player '%s', reason: name too long\n", renamearg->opts[0]);
149        exit(EXIT_FAILURE);
150    }
151
152    if (ret == 0)
153    {
154        printf("Info: found no player with id of '%s'", renamearg->opts[0]);
155    }
156}
157
158void runsetskin(arg *skinarg)
159{
160    int intid;
161    int ret;
162    if (sscanf(skinarg->opts[0], "%d", &intid) == 1)
163        ret = setskinbyid(intid, skinarg->opts[1], &DEMO);
164    else
165        ret = setskinbyname(skinarg->opts[0], skinarg->opts[1], &DEMO);
166
167    if (ret < 0)
168    {
169        printf("Error: failed to set skin of player '%s', reason: skin name too long\n", skinarg->opts[0]);
170        exit(EXIT_FAILURE);
171    }
172
173    if (ret == 0)
174    {
175        printf("Info: found no player with id of '%s'", skinarg->opts[0]);
176    }
177}
178
179void runextractmap(arg *extarg)
180{
181    extmapfile = fopen(extarg->opts[0], "w");
182    if (!extmapfile)
183        exitperror("Error: failed to write to extract map file, reason");
184
185    if (exportmap(extmapfile, &DEMO) < 0)
186    {
187        fprintf(stderr, "Error: failed to write demo map to extract map file\n");
188        exit(EXIT_FAILURE);
189    }
190    fclose(extmapfile);
191}
192
193void runoutput(arg *outarg)
194{
195    outfile = fopen(outarg->opts[0], "w");
196    if (!outfile)
197        exitperror("Error: failed to write to outputfile, reason");
198
199    if (writedemo(outfile, &DEMO) < 0)
200    {
201        fprintf(stderr, "Error: failed to write demo file to outputfile\n");
202        exit(EXIT_FAILURE);
203    }
204    fclose(outfile);
205}
206
207int main(int argc, char *argv[])
208{
209    inithuff(NULL);
210
211    setinfo("dedit - a tool for editing Teeworlds/DDNet demo files");
212    setusage("dedit <demo> [OPTIONS]");
213
214    addarg("<demo>", "Sets input demo", setdemo);
215
216    addopt("-r", "--rename", 2, "<id/name> <name>", "Renames player with id/name to name", runrename);
217    addopt("-s", "--skin", 2, "<id/name> <skin>", "Set skin of player with id/name to skin", runsetskin);
218    addopt("-m", "--map", 1, "<map>", "Changes the map of demo to map", setmap);
219    addopt("-M", "--merge", 1, "<demo>", "Merges the game data of demo into input demo", runmerge);
220    addopt("-e", "--extract-map", 1, "<file>", "Saves the map of demo to file", NULL);
221    addopt("-o", "--output", 1, "<file>", "Saves the output demo to file", NULL);
222    addopt("-i", "--info", 0, NULL, "Prints info of demo", NULL);
223    addopt("-I", "--extended-info", 0, NULL, "Prints extended info of demo", NULL);
224    addopt("-c", "--chat", 0, NULL, "Prints the demo chat log", NULL);
225    addopt("-h", "--help", 0, NULL, "Prints this help info", NULL);
226
227    if (!parseargs(argc, argv))
228    {
229        paerror("Error:");
230        printf("For more help run 'dedit --help'\n");
231    }
232
233    if (argc == 1 || getopt("-h", 0))
234    {
235        printhelp();
236        exit(EXIT_SUCCESS);
237    }
238
239    arg *demoarg = getarg(0);
240    if (demoarg == NULL)
241    {
242        printf("Error: Missing required argument <demo>\n");
243        exit(EXIT_FAILURE);
244    }
245
246    demoarg->runarg(demoarg);
247
248    int i = 0;
249    arg *opt;
250    while (1)
251    {
252        opt = getopt("-M", i);
253        if (opt == NULL)
254            break;
255        opt->runarg(opt);
256        i++;
257    }
258
259    arg *outarg = getopt("-o", 0);
260    arg *maparg = getopt("-m", 0);
261    arg *exmarg = getopt("-e", 0);
262    arg *infarg = getopt("-i", 0);
263    arg *Infarg = getopt("-I", 0);
264    arg *chtarg = getopt("-c", 0);
265
266    if (exmarg)
267        runextractmap(exmarg);
268
269    if (maparg)
270        setmap(maparg);
271
272    if (Infarg)
273        printdemo(&DEMO, 1);
274    else if (infarg)
275        printdemo(&DEMO, 0);
276
277    if (chtarg)
278        printchat(&DEMO);
279
280    while (1)
281    {
282        opt = getopt("-s", i);
283        if (opt == NULL)
284            break;
285        opt->runarg(opt);
286        i++;
287    }
288
289    i = 0;
290    while (1)
291    {
292        opt = getopt("-r", i);
293        if (opt == NULL)
294            break;
295        opt->runarg(opt);
296        i++;
297    }
298
299    if (outarg)
300        runoutput(outarg);
301
302    freeargs();
303
304    exit(EXIT_SUCCESS);
305}