1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
#include "../inc/commands.h"
#include "../inc/demo.h"
#include "../inc/pack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// TODO set by id and name has much repeating, fix that
int setnamebyid(int id, char *new, demo *demo)
{
int newlen = strlen(new);
if (newlen >= 15)
return -1;
int intname[4] = {0x80808080, 0x80808080, 0x80808080, 0x80808000};
strtoint(new, newlen + 1, intname);
int found = 0;
for (int i = 0; i < demo->data.numchunks; i++)
{
if (demo->data.chunks[i].type != DEMOSNAP)
continue;
demosnap *snap = demo->data.chunks[i].data.snap;
for (int y = 0; y < snap->numitems; y++)
{
demosnapitem *item = &snap->items[y];
if (item->type == 11 && item->id == id)
{
memcpy(item->data, intname, 4 * sizeof(int));
found++;
}
}
}
return found;
}
int setnamebyname(char *old, char *new, demo *demo)
{
int newlen = strlen(new);
if (newlen >= 15)
return -1;
int intname[4] = {0x80808080, 0x80808080, 0x80808080, 0x80808000};
strtoint(new, newlen + 1, intname);
char found = 0;
for (int i = 0; i < demo->data.numchunks; i++)
{
if (demo->data.chunks[i].type != DEMOSNAP)
continue;
demosnap *snap = demo->data.chunks[i].data.snap;
for (int y = 0; y < snap->numitems; y++)
{
demosnapitem *item = &snap->items[y];
if (item->type != 11)
continue;
char name[16];
intstostr(item->data, 4, name);
if (strcmp(old, name) == 0)
{
memcpy(item->data, intname, 4 * sizeof(int));
found++;
}
}
}
return found;
}
int setskinbyid(int id, char *skin, demo *demo)
{
int skinlen = strlen(skin);
if (skinlen >= 23)
return -1;
int intskin[5] = {0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808000};
strtoint(skin, skinlen + 1, intskin);
char found = 0;
for (int i = 0; i < demo->data.numchunks; i++)
{
if (demo->data.chunks[i].type != DEMOSNAP)
continue;
demosnap *snap = demo->data.chunks[i].data.snap;
for (int y = 0; y < snap->numitems; y++)
{
demosnapitem *item = &snap->items[y];
if (item->type == 11 && item->id == id)
{
memcpy(&item->data[8], intskin, 5 * sizeof(int));
found++;
}
}
}
return found;
}
int setskinbyname(char *name, char *skin, demo *demo)
{
int skinlen = strlen(skin);
if (skinlen >= 23)
return -1;
int intskin[5] = {0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808000};
strtoint(skin, skinlen + 1, intskin);
char found = 0;
for (int i = 0; i < demo->data.numchunks; i++)
{
if (demo->data.chunks[i].type != DEMOSNAP)
continue;
demosnap *snap = demo->data.chunks[i].data.snap;
for (int y = 0; y < snap->numitems; y++)
{
demosnapitem *item = &snap->items[y];
if (item->type != 11)
continue;
char iname[16];
intstostr(item->data, 4, iname);
if (strcmp(name, iname) == 0)
{
memcpy(&item->data[8], intskin, 5 * sizeof(int));
found++;
}
}
}
return found;
}
int changemap(FILE *map, char *mapname, demo *demo)
{
int namelen = strlen(mapname);
if (namelen >= 31)
return -1;
fseek(map, 0, SEEK_END);
long mapsize = ftell(map);
fseek(map, 0, SEEK_SET);
if (mapsize < 4)
return -1;
char *mapbuff = (char *)malloc(mapsize);
if (mapbuff == NULL)
return -1;
fread(mapbuff, 1, mapsize, map);
if (memcmp(mapbuff, "DATA", 4) != 0)
{
free(mapbuff);
return -1;
}
free(demo->map.data);
demo->map.data = mapbuff;
if (demo->header.version >= 6)
demo->header.version = 5;
demo->header.mapsize = mapsize;
demo->header.mapcrc = mapsize;
memcpy(demo->header.mapname, mapname, namelen + 1);
return 1;
}
void runcommand(cmdinput *cmd, demo *demo)
{
switch (cmd->cmdtype)
{
case 'n':
switch (cmd->settype)
{
case 'i':
if (setnamebyid(cmd->id, cmd->to, demo) < 0)
printf("WARNING: Could not set name '%s' because it exeeds max limit of 15 characters\n", cmd->to);
break;
case 'n':
if (setnamebyname(cmd->from, cmd->to, demo) < 0)
printf("WARNING: Could not set name '%s' because it exeeds max limit of 15 characters\n", cmd->to);
break;
default:
printf("WARNING: Unknown modifier for name\n");
}
break;
case 's':
switch (cmd->settype)
{
case 'i':
if (setskinbyid(cmd->id, cmd->to, demo) < 0)
printf("WARNING: Could not set skin '%s' because it exeeds max limit of 23 characters\n", cmd->to);
break;
case 'n':
if (setskinbyname(cmd->from, cmd->to, demo) < 0)
printf("WARNING: Could not set skin '%s' because it exeeds max limit of 23 characters\n", cmd->to);
break;
default:
printf("WARNING: Unknown modifier for skin\n");
}
break;
default:
printf("WARNING: Unknown command type\n");
break;
}
}
|