JHHM

dedit

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

dedit/include/demo.h

get raw file: demo.h

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#ifndef DEMO_H
18#define DEMO_H
19
20#include <stdio.h>
21
22#define DEMO_TIMELINE_LENGTH 260
23
24typedef enum
25{
26    DEMOTICK = 0,
27    DEMOSNAP = 1,
28    DEMOMESSAGE = 2,
29    DEMODELTA = 3
30} demochunktype;
31
32/* demoheader struct */
33typedef struct
34{
35    unsigned char version;
36    char netversion[64];
37    char mapname[64];
38    int mapsize;
39    int mapcrc;
40    char type[8];
41    int length;
42    char timestamp[20];
43} demoheader;
44
45/* demotimeline struct, only contains the raw data */
46typedef struct
47{
48    char data[DEMO_TIMELINE_LENGTH];
49} demotimeline;
50
51/* demomap struct */
52typedef struct
53{
54    unsigned char sha256[32];
55    char *data;
56} demomap;
57
58typedef struct
59{
60    unsigned short type;
61    unsigned short id;
62    int numdata;
63    int *data;
64} demosnapitem;
65
66/* Chunk types */
67typedef struct
68{
69    int datasize;
70    int numitems;
71    int *offsets;
72    demosnapitem *items;
73} demosnap;
74
75typedef struct
76{
77    char keyframe;
78    char innline;
79    int delta;
80} demotick;
81
82typedef struct
83{
84    unsigned short type;
85    unsigned short id;
86    int size;
87    int *data;
88} demodeltaitem;
89
90typedef struct
91{
92    int numremoveditems;
93    int numitemdeltas;
94    int *removeditemkeys;
95    demodeltaitem *itemdeltas;
96} demodelta;
97
98typedef struct
99{
100    int datasize;
101    char *data;
102} demomessage;
103
104/* union for different chunks */
105typedef union {
106    demosnap *snap;
107    demotick *tick;
108    demodelta *delta;
109    demomessage *message;
110} chunkdata;
111
112/* demochunk struct */
113typedef struct
114{
115    demochunktype type;
116    chunkdata data;
117} demochunk;
118
119/* demodata struct, stores all chunks */
120typedef struct
121{
122    int numchunks;
123    demochunk *chunks;
124} demodata;
125
126typedef struct
127{
128    demoheader header;
129    demotimeline timeline;
130    demomap map;
131    demodata data;
132} demo;
133
134/* Read demo header from demofile into given demoheader */
135/* Returns a positive number on success, negative on fail */
136int readdemoheader(FILE *demofile, demoheader *header);
137
138/* Read demo timeline from demofile into given demoheader */
139/* Returns a positive number on success, negative on fail */
140/* Must have read the header from demofile beforehand */
141int readdemotimeline(FILE *demofile, demotimeline *timeline);
142
143/* Read demo map from demofile into given demomap */
144/* Returns a positive number on success, negative on fail */
145/* Must have read the demoheader and demotimeline beforehand */
146int readdemomap(FILE *demofile, demomap *map, int mapsize, unsigned char version);
147
148/* Reads a chunk from demofile into given chunk */
149/* Returns a positive number on success, 0 on EOF, and negative number on error */
150/* Will allocate memory if chunk is snap, delta or message */
151int readdemochunk(FILE *demofile, demochunk *chunk, unsigned char version);
152
153/* Reads all chunks from demofile */
154/* Returns a positive number on success, negative on error */
155/* Will allocate the needed memory for data */
156int readdemochunks(FILE *demofile, demodata *data, unsigned char version);
157
158/* Reads a demo file from demofile */
159/* Returns a positive number on success, negative on error */
160/* Allocates memory needed in demo map and data */
161int readdemo(FILE *demofile, demo *demo);
162
163/* Writes given demo file to outfile */
164/* Returns a positive number on success, negative on error */
165int writedemo(FILE *outfile, demo *demo);
166
167/* Prints given demo file */
168/* if printchunks then print all chunks */
169void printdemo(demo *demo, char printchunks);
170
171/* Frees everything for demo */
172/* Sets demo to NULL */
173void freedemo(demo *demo);
174
175/* Frees all chunks in data */
176/* Sets data to empty */
177void freedemodata(demodata *data);
178
179#endif // DEMO_H