#include "d2stat.h" #include #include // Internal function, gives the offset ***IN BITS, NOT BYTES*** of the specified stat unsigned long long int _searchStat(D2S_STAT stat, void* charData, size_t dataLen) { if(!strncmp(charData, D2S_STAT_HEADER, D2S_STAT_HEADER_LENGTH)) { fprintf(stderr,"libd2char error: Stat header not present in charData\n"); return 0; } unsigned long long int byteOffset = D2S_STAT_HEADER_LENGTH; unsigned long long int bitOffset = 0; while(byteOffset < dataLen) { uint16_t statID = 0; memcpy(&statID, charData + byteOffset, sizeof(statID)); statID >>= bitOffset; statID &= 0x1FF; if(statID == D2S_STAT_FOOTER) { break; } else if(statID == stat) { return (byteOffset * 8) + bitOffset; } byteOffset += statCost[statID] / 8; bitOffset += statCost[statID] % 8; } return 0; } int getStat(D2S_STAT stat, unsigned int* value, void* charData, size_t dataLen) { unsigned long long int statOffset = _searchStat(stat,charData,dataLen); if(!statOffset) { fprintf(stderr,"libd2char error: Unable to find attribute: %d\n", stat); return -1; } uint64_t statValue = 0; memcpy(&statValue, charData + (statOffset / 8), sizeof(statValue)); statValue >>= (statOffset % 8); statValue &= statCostMask[stat]; *value = (unsigned int)statValue; return 0; } int setStat(D2S_STAT stat, unsigned int value, void* charData, size_t dataLen) { unsigned long long int statOffset = _searchStat(stat,charData,dataLen); if(!statOffset) { fprintf(stderr,"libd2char error: Unable to find attribute: %d\n", stat); return -1; } uint64_t statValue = 0; memcpy(&statValue, charData + (statOffset / 8), sizeof(statValue)); statValue &= (~statCostMask[stat] << (statOffset % 8)); statValue |= (value << (statOffset % 8)); memcpy(charData + (statOffset / 8), &statValue, sizeof(statValue)); return 0; }