summaryrefslogtreecommitdiff
path: root/src/demo.c
blob: fd1f31068ff775615d643e8c344b948576f5e2ca (plain)
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
213
214
215
216
217
218
219
220
221
#include "../inc/demo.h"
#include "../inc/huffman.h"
#include "../inc/pack.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CHUNK_TICK_MASK 0b10000000
#define CHUNK_TICK_KEYFRAME_MASK 0b01000000
#define CHUNK_TICK_INLINE_MASK 0b00100000
#define CHUNK_NORMAL_TYPE_MASK 0b01100000
#define CHUNK_NORMAL_SIZE_MASK 0b00011111

const unsigned char mapmagic[] = {0x6b, 0xe6, 0xda, 0x4a, 0xce, 0xbd, 0x38, 0x0c,
                                  0x9b, 0x5b, 0x12, 0x89, 0xc8, 0x42, 0xd7, 0x80};

// TODO make version sensitive
int readdemoheader(FILE *fp, demoheader *dh)
{
    if (fscanf(fp, "TWDEMO%*c%c", &dh->version) != 1)
        return -1;
    if (fscanf(fp, "%64c", dh->netversion) != 1)
        return -2;
    if (fscanf(fp, "%64c", dh->mapname) != 1)
        return -3;
    if (fread(dh->mapsize, 1, 4, fp) != 4)
        return -4;
    if (fread(dh->mapcrc, 1, 4, fp) != 4)
        return -5;
    if (fscanf(fp, "%8c", dh->type) != 1)
        return -6;
    if (fread(dh->length, 1, 4, fp) != 4)
        return -7;
    if (fscanf(fp, "%20c", dh->timestamp) != 1)
        return -8;
    printf("version: %hhu\n", dh->version);
    printf("net_version: %s\n", dh->netversion);
    printf("mapname: %s\n", dh->mapname);
    printf("mapsize: %i\n", reverseint(dh->mapsize));
    printf("mapcrc: %i\n", reverseint(dh->mapcrc));
    printf("type: %s\n", dh->type);
    printf("length: %i\n", reverseint(dh->length));
    printf("timestamp: %s\n", dh->timestamp);
    return 1;
}

int readdemotimeline(FILE *fp, demotimeline *dt)
{
    if (fscanf(fp, "%260c", dt->data) != 1)
        return -1;
    return 1;
}

// TODO make version sensitive
int readdemomap(FILE *fp, demomap *dm, int mapsize)
{
    char magicbuf[sizeof(mapmagic)];
    fread(magicbuf, 1, sizeof(mapmagic), fp);

    if (memcmp(magicbuf, mapmagic, sizeof(mapmagic)) != 0)
        return -1;

    if (fread(dm->sha256, 1, 32, fp) != 32)
        return -2;

    if (fread(dm->data, 1, mapsize, fp) != mapsize)
        return -3;

    return 1;
}

int readdemotick(FILE *fp, char chunkhead, demotick *tick, unsigned char ver)
{
    tick->keyframe = (chunkhead & CHUNK_TICK_KEYFRAME_MASK);

    if (ver >= 5)
    {
        if (chunkhead & CHUNK_TICK_INLINE_MASK)
        {
            tick->innline = 1;
            tick->delta = (chunkhead & 0x1f); // TODO name this mask?
        }
        else
        {
            tick->innline = 0;
            unsigned char tickdelta[4];
            fread(tickdelta, 1, 4, fp);
            tick->delta = reverseint(tickdelta);
        }
    }
    else
    {
        if ((chunkhead & 0x3f) != 0) // TODO name this mask?
            tick->delta = (chunkhead & 0x3f);
        else
        {
            unsigned char tickdelta[4];
            fread(tickdelta, 1, 4, fp);
        }
    }
    return 1;
}

int readdemosnap(FILE *fp, demosnap *snap, int size)
{
    unsigned char data[size];
    unsigned char unpacked[1024 * 8];

    fread(data, sizeof(unsigned char), size, fp);

    int r = decompresshuff((char *)data, size, (char *)unpacked, 1034 * 8);

    unsigned char *cp = unpacked;

    snap->datasize = readint(&cp);
    snap->numitems = readint(&cp);

    snap->offsets = (int *)malloc(snap->numitems * sizeof(int));
    snap->items = (demosnapitem *)malloc(snap->numitems * sizeof(int));

    for (int i = 0; i < snap->numitems; i++)
        snap->offsets[i] = readint(&cp);

    for (int i = 0; i < snap->numitems; i++)
    {
        unsigned int item_key = (unsigned int)readint(&cp);

        snap->items[i].type = (item_key >> 16) & 0xffff;
        snap->items[i].id = (item_key & 0xffff);

        if (i == (snap->numitems - 1))
            snap->items[i].numdata = (snap->datasize - snap->offsets[i]) / 4 - 1;
        else
            snap->items[i].numdata = (snap->offsets[i + 1] - snap->offsets[i]) / 4 - 1;

        snap->items[i].data = (int *)malloc(size * sizeof(int));

        for (int y = 0; y < snap->items[i].numdata; y++)
            snap->items[i].data[y] = readint(&cp);
    }

    return 1;
}

int readdemochunk(FILE *fp, demochunk *chunk, unsigned char ver)
{
    char chunkhead;
    chunkhead = (char)fgetc(fp);

    if (chunkhead == EOF)
        return 0;

    if (chunkhead & CHUNK_TICK_MASK)
    {
        demotick *tick = (demotick *)malloc(sizeof(demotick));
        if (readdemotick(fp, chunkhead, tick, ver))
        {
            chunk->type = 0;
            chunk->data.tick = tick;
            printf("TICK={keyframe: %d, innline: %d, delta: %d}\n", tick->keyframe, tick->innline, tick->delta);
        }
        else
        {
            printf("[ ERROR ] error while reading tick!\n");
            return -1;
        }
    }
    else
    {
        unsigned char type = (chunkhead & CHUNK_NORMAL_TYPE_MASK) >> 5;
        short size = (chunkhead & CHUNK_NORMAL_SIZE_MASK);

        if (size == 30)
            size = (short)fgetc(fp);
        else if (size == 31)
            fread(&size, sizeof(short), 1, fp);

        if (type == 1)
        {
            demosnap *snap = (demosnap *)malloc(sizeof(demosnap));
            if (readdemosnap(fp, snap, size))
            {
                chunk->type = 1; // TODO make a chunk-type enum
                chunk->data.snap = snap;

                printf("SNAPSHOT={datasize: %d, numitems: %d, offsets: [ ", snap->datasize, snap->numitems);
                for (int i = 0; i < snap->numitems; i++)
                    printf("%d, ", snap->offsets[i]);

                printf("], items: [ ");
                for (int i = 0; i < snap->numitems; i++)
                {
                    printf("(type: %d, id: %d) { ", snap->items[i].type, snap->items[i].id);
                    for (int y = 0; y < snap->items[i].numdata; y++)
                        printf("%d, ", snap->items[i].data[y]);
                    printf("} ");
                }
                printf("]\n");
            }
            else
            {
                printf("[ ERROR ] error while reading snapshot!\n");
                return -1;
            }
        }
        else if (type == 2)
        {
            printf("MESSAGE={");
            fseek(fp, size, SEEK_CUR);
        }
        else if (type == 3)
        {
            printf("SNAPSHOT_DELTA={");
            fseek(fp, size, SEEK_CUR);
        }

        printf("size: %d}\n", size);
    }
    return 1;
}