dedit
a tool for Teeworlds demo file shenanigens
git clone https://jhhm.no/repos/dedit.git
dedit/include/snap.h
get raw file: snap.h
1/* This file is part of dedit - a tool for editing Teeworlds/DDNet demo files 2 Copyright (C) 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 SNAP_H 18#define SNAP_H 19 20#include "demo.h" 21 22#define SNAP_MAX_ITEMS 1024 23#define SNAP_MAX_SIZE (64 * 1024) 24#define SNAP_KEY(type, id) (((type) << 16) | (id)) 25 26/* demostate struct, the full game state of a demo at a tick */ 27typedef struct 28{ 29 demo *demo; 30 int chunkidx; 31 int firstchunk; 32 int tick; 33 int firsttick; 34 int lasttick; 35 char hassnap; 36 demosnap snap; 37} demostate; 38 39/* Initializes empty snapshot */ 40void snapinit(demosnap *snap); 41 42/* Frees the items of snap */ 43/* Sets snap to an empty snapshot */ 44void snapfree(demosnap *snap); 45 46/* Copies src into dst, dst must not hold a snapshot */ 47/* Returns a positive number on success, negative on error */ 48int snapcopy(demosnap *dst, const demosnap *src); 49 50/* Returns the index of the item with type and id in snap, -1 if not found */ 51int snapfinditem(const demosnap *snap, int type, int id); 52 53/* Appends a new item to snap, copying numdata ints from data */ 54/* Offsets of snap must be updated with snapupdateoffsets afterwards */ 55/* Returns a positive number on success, negative on error */ 56int snapadditem(demosnap *snap, int type, int id, int numdata, const int *data); 57 58/* Recalculates the offsets and datasize of snap from its items */ 59/* Returns a positive number on success, negative on error */ 60int snapupdateoffsets(demosnap *snap); 61 62/* Returns the size of snap in bytes, as it is stored in a demo */ 63int snapsize(const demosnap *snap); 64 65/* Applies delta to from, and stores the resulting snapshot in to */ 66/* to must not hold a snapshot */ 67/* Returns a positive number on success, negative on error */ 68int snapapplydelta(const demosnap *from, const demodelta *delta, demosnap *to); 69 70/* Creates the delta from snapshot from to snapshot to */ 71/* Returns 0 if the snapshots are equal, positive number if delta is not empty, negative on error */ 72/* Returns -2 if an item changed size, which can not be stored in a delta */ 73/* Allocates memory needed in delta, unless an error occurred */ 74int snapcreatedelta(const demosnap *from, const demosnap *to, demodelta *delta); 75 76/* Frees the item data of delta */ 77/* Sets delta to an empty delta */ 78void deltafree(demodelta *delta); 79 80/* Initializes state at the start of demo, before the first tick */ 81void demostateinit(demostate *state, demo *demo); 82 83/* Returns the next tick of state, -1 if there are no more ticks */ 84int demostatepeektick(const demostate *state); 85 86/* Advances state to next tick, and calculates the full snapshot */ 87/* Returns a positive number on success, 0 on end of demo, and negative number on error */ 88int demostatenext(demostate *state); 89 90/* Frees everything for state */ 91void demostatefree(demostate *state); 92 93#endif // SNAP_H 94