dedit
a tool for Teeworlds demo file shenanigens
git clone https://jhhm.no/repos/dedit.git
dedit/src/commands.c
get raw file: commands.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 "commands.h" 20#include "demo.h" 21#include "pack.h" 22 23#include <stdio.h> 24#include <stdlib.h> 25#include <string.h> 26 27// TODO set by id and name has much repeating, fix that 28int setnamebyid(int id, char *new, demo *demo) 29{ 30 int newlen = strlen(new); 31 if (newlen >= 15) 32 return -1; 33 34 int intname[4] = {0x80808080, 0x80808080, 0x80808080, 0x80808000}; 35 strtoint(new, newlen + 1, intname); 36 int found = 0; 37 38 for (int i = 0; i < demo->data.numchunks; i++) 39 { 40 if (demo->data.chunks[i].type != DEMOSNAP) 41 continue; 42 demosnap *snap = demo->data.chunks[i].data.snap; 43 44 for (int y = 0; y < snap->numitems; y++) 45 { 46 demosnapitem *item = &snap->items[y]; 47 if (item->type == 11 && item->id == id) 48 { 49 memcpy(item->data, intname, 4 * sizeof(int)); 50 found++; 51 } 52 } 53 } 54 return found; 55} 56 57int setnamebyname(char *old, char *new, demo *demo) 58{ 59 int newlen = strlen(new); 60 if (newlen >= 15) 61 return -1; 62 63 int intname[4] = {0x80808080, 0x80808080, 0x80808080, 0x80808000}; 64 strtoint(new, newlen + 1, intname); 65 char found = 0; 66 67 for (int i = 0; i < demo->data.numchunks; i++) 68 { 69 if (demo->data.chunks[i].type != DEMOSNAP) 70 continue; 71 demosnap *snap = demo->data.chunks[i].data.snap; 72 73 for (int y = 0; y < snap->numitems; y++) 74 { 75 demosnapitem *item = &snap->items[y]; 76 if (item->type != 11) 77 continue; 78 79 char name[16]; 80 intstostr(item->data, 4, name); 81 if (strcmp(old, name) == 0) 82 { 83 memcpy(item->data, intname, 4 * sizeof(int)); 84 found++; 85 } 86 } 87 } 88 return found; 89} 90 91int setskinbyid(int id, char *skin, demo *demo) 92{ 93 int skinlen = strlen(skin); 94 if (skinlen >= 23) 95 return -1; 96 97 int intskin[5] = {0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808000}; 98 strtoint(skin, skinlen + 1, intskin); 99 char found = 0; 100 101 for (int i = 0; i < demo->data.numchunks; i++) 102 { 103 if (demo->data.chunks[i].type != DEMOSNAP) 104 continue; 105 demosnap *snap = demo->data.chunks[i].data.snap; 106 107 for (int y = 0; y < snap->numitems; y++) 108 { 109 demosnapitem *item = &snap->items[y]; 110 if (item->type == 11 && item->id == id) 111 { 112 memcpy(&item->data[8], intskin, 5 * sizeof(int)); 113 found++; 114 } 115 } 116 } 117 return found; 118} 119 120int setskinbyname(char *name, char *skin, demo *demo) 121{ 122 int skinlen = strlen(skin); 123 if (skinlen >= 23) 124 return -1; 125 126 int intskin[5] = {0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808000}; 127 strtoint(skin, skinlen + 1, intskin); 128 char found = 0; 129 130 for (int i = 0; i < demo->data.numchunks; i++) 131 { 132 if (demo->data.chunks[i].type != DEMOSNAP) 133 continue; 134 demosnap *snap = demo->data.chunks[i].data.snap; 135 136 for (int y = 0; y < snap->numitems; y++) 137 { 138 demosnapitem *item = &snap->items[y]; 139 if (item->type != 11) 140 continue; 141 142 char iname[16]; 143 intstostr(item->data, 4, iname); 144 if (strcmp(name, iname) == 0) 145 { 146 memcpy(&item->data[8], intskin, 5 * sizeof(int)); 147 found++; 148 } 149 } 150 } 151 return found; 152} 153 154int changemap(FILE *map, char *mapname, demo *demo) 155{ 156 int namelen = strlen(mapname); 157 if (namelen >= 31) 158 return -1; 159 160 fseek(map, 0, SEEK_END); 161 long mapsize = ftell(map); 162 fseek(map, 0, SEEK_SET); 163 164 if (mapsize < 4) 165 return -1; 166 167 char *mapbuff = (char *)malloc(mapsize); 168 if (mapbuff == NULL) 169 return -1; 170 171 fread(mapbuff, 1, mapsize, map); 172 173 if (memcmp(mapbuff, "DATA", 4) != 0) 174 { 175 free(mapbuff); 176 return -1; 177 } 178 179 free(demo->map.data); 180 demo->map.data = mapbuff; 181 182 if (demo->header.version >= 6) 183 demo->header.version = 5; 184 185 demo->header.mapsize = mapsize; 186 demo->header.mapcrc = mapsize; 187 memcpy(demo->header.mapname, mapname, namelen + 1); 188 189 return 1; 190} 191 192int exportmap(FILE *out, demo *demo) 193{ 194 if (fwrite(demo->map.data, 1, demo->header.mapsize, out) < (unsigned int)demo->header.mapsize) 195 return -1; 196 197 return 1; 198}