Blame view

d2char.c 4.96 KB
1
2
#include <stdio.h>
Imanol-Mikel Barba Sabariego authored
3
#include "d2char.h"
4
#include "d2mercs.h"
5
#include "d2skills.h"
Imanol-Mikel Barba Sabariego authored
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

uint32_t calcChecksum(D2CharHeader* c, void* charData) {
    uint32_t origChecksum = c->checksum;
    c->checksum = 0;
    uint32_t sum = 0;
    void* data = malloc(c->fileSize);
    memcpy(data, (void*)c, D2S_HEADER_LENGTH);
    memcpy(data + D2S_HEADER_LENGTH, charData, c->fileSize - D2S_HEADER_LENGTH);
    for(int i = 0; i < c->fileSize; ++i) {
        sum = (sum << 1) + ((uint8_t*)data)[i];
    }
    free(data);
    c->checksum = origChecksum;
    return sum;
}

int checkChecksum(D2CharHeader* c, void* charData) {
    uint32_t checksum = calcChecksum(c, charData);
    return c->checksum == checksum;
}

int isHardcore(D2CharHeader* c) {
    return c->charStatus & D2S_CHARSTATUS_HARDCORE;
}
31
32
33
34
void setHardcore(D2CharHeader* c) {

}
Imanol-Mikel Barba Sabariego authored
35
36
37
38
int hasDied(D2CharHeader* c) {
    return c->charStatus & D2S_CHARSTATUS_DIED;
}
39
40
41
42
void setDied(D2CharHeader* c) {

}
Imanol-Mikel Barba Sabariego authored
43
44
45
46
int isExpansion(D2CharHeader* c) {
    return c->charStatus & D2S_CHARSTATUS_EXPANSION;
}
47
48
49
50
void setExpansion(D2CharHeader* c) {

}
Imanol-Mikel Barba Sabariego authored
51
52
int isLadder(D2CharHeader* c) {
    return c->charStatus & D2S_CHARSTATUS_LADDER;
Imanol-Mikel Barba Sabariego authored
53
54
}
55
56
57
58
59
void setLadder(D2CharHeader* c) {

}

D2S_ACT getCurrentAct(D2CharHeader* c) {
Imanol-Mikel Barba Sabariego authored
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
    if(isExpansion(c)) {
        return c->charProgress % 5;
    } else {
        return c->charProgress % 4;
    }
}

int isFemale(D2CharHeader* c) {
    return (c->charClass == D2S_CHARCLASS_AMAZON || 
            c->charClass == D2S_CHARCLASS_ASSASSIN ||
            c->charClass == D2S_CHARCLASS_SORCERESS);
}

const char* getCharacterTitle(D2CharHeader* c) {
    int tier;
    if(isExpansion(c)) {
        // Expansion
        tier = c->charProgress / 5;
        if(isHardcore(c)) {
            // Expansion Hardcore
            switch(tier) {
                case 1:
                return D2S_CHARPROGRESS_EXPANSION_TIER1_NAME_HARDCORE;
                break;
                case 2:
                return D2S_CHARPROGRESS_EXPANSION_TIER2_NAME_HARDCORE;
                break;
                case 3:
                return D2S_CHARPROGRESS_EXPANSION_TIER3_NAME_HARDCORE;
                break;
            }
        } else {
            // Expansion Softcore
            switch(tier) {
                case 1:
                return D2S_CHARPROGRESS_EXPANSION_TIER1_NAME;
                break;
                case 2:
                return D2S_CHARPROGRESS_EXPANSION_TIER2_NAME;
                break;
                case 3:
                return isFemale(c) ? D2S_CHARPROGRESS_EXPANSION_TIER3_NAME_F : D2S_CHARPROGRESS_EXPANSION_TIER3_NAME_M;
                break;
            }
        }
    } else {
        // Classic
        if(isHardcore(c)) {
            // Classic Hardcore
            switch(tier) {
                case 1:
                return isFemale(c) ? D2S_CHARPROGRESS_CLASSIC_TIER1_NAME_HARDCORE_F : D2S_CHARPROGRESS_CLASSIC_TIER1_NAME_HARDCORE_M;
                break;
                case 2:
                return isFemale(c) ? D2S_CHARPROGRESS_CLASSIC_TIER2_NAME_HARDCORE_F : D2S_CHARPROGRESS_CLASSIC_TIER2_NAME_HARDCORE_M;
                break;
                case 3:
                return isFemale(c) ? D2S_CHARPROGRESS_CLASSIC_TIER3_NAME_HARDCORE_F : D2S_CHARPROGRESS_CLASSIC_TIER3_NAME_HARDCORE_M;
                break;
            }
        } else {
            // Classic Softcore
            switch(tier) {
                case 1:
                return isFemale(c) ? D2S_CHARPROGRESS_CLASSIC_TIER1_NAME_F : D2S_CHARPROGRESS_CLASSIC_TIER1_NAME_M;
                break;
                case 2:
                return isFemale(c) ? D2S_CHARPROGRESS_CLASSIC_TIER2_NAME_F : D2S_CHARPROGRESS_CLASSIC_TIER2_NAME_M;
                break;
                case 3:
                return isFemale(c) ? D2S_CHARPROGRESS_CLASSIC_TIER3_NAME_F : D2S_CHARPROGRESS_CLASSIC_TIER3_NAME_M;
                break;
            }
        }
    }
135
    return D2S_CHARPROGRESS_TIER0_NAME;
136
137
138
}

size_t getLastPlayed(D2CharHeader* c, char* buf, size_t bufLen) {
139
140
141
142
143
144
145
146
    // In amd64, since long is 64 bits long, time_t is also 64 bits long
    // thus needing conversion from the save file type, which is a uint32_t
    #ifdef __x86_64__
    uint64_t convTimestamp = c->lastPlayed;
    #else
    uint32_t convTimestamp = c->lastPlayed;
    #endif
    struct tm* time = localtime((time_t*)&convTimestamp);
147
148
149
150
151
152
153
    size_t ret = strftime(buf, bufLen, "%c", time);
    if(!ret) {
        fprintf(stderr,"libd2char error: Provided buffer for time string was too small\n");
    }
    return ret;
}
154
D2S_DIFFICULTY getCurrentDifficulty(D2CharHeader* c) {
155
156
157
158
159
160
161
162
    for(int i = D2S_DIFFICULTY_NORMAL; i <= D2S_DIFFICULTY_HELL; ++i) {
        if(c->difficulty[i] & D2S_DIFFICULTY_ACTIVE) {
            return i;
        }
    }
    return D2S_DIFFICULTY_UNKNOWN;
}
163
164
165
166
167
168
169
170
171
int setProgress(D2S_ACT act, D2S_DIFFICULTY difficulty) {
    // TODO
    // set c->difficulty and c->progress?
    return 0;
}

int getAttribute(D2S_STAT attr) {
    // TODO
    return 0;
172
173
}
174
175
176
int setAttribute(D2S_STAT attr, unsigned int value) {
    // TODO
    return 0;
Imanol-Mikel Barba Sabariego authored
177
}