324 lines
11 KiB
C++
324 lines
11 KiB
C++
// This file is part of c-evo-evo.
|
|
//
|
|
// This software is governed by the CeCILL license under French law and
|
|
// abiding by the rules of distribution of free software. You can use,
|
|
// modify and/or redistribute the software under the terms of the CeCILL
|
|
// license as circulated by CEA, CNRS and INRIA at the following URL
|
|
// "http://www.cecill.info".
|
|
//
|
|
// As a counterpart to the access to the source code and rights to copy,
|
|
// modify and redistribute granted by the license, users are provided only
|
|
// with a limited warranty and the software's author, the holder of the
|
|
// economic rights, and the successive licensors have only limited
|
|
// liability.
|
|
//
|
|
// In this respect, the user's attention is drawn to the risks associated
|
|
// with loading, using, modifying and/or developing or reproducing the
|
|
// software by the user in light of its specific status of free software,
|
|
// that may mean that it is complicated to manipulate, and that also
|
|
// therefore means that it is reserved for developers and experienced
|
|
// professionals having in-depth computer knowledge. Users are therefore
|
|
// encouraged to load and test the software's suitability as regards their
|
|
// requirements in conditions enabling the security of their systems and/or
|
|
// data to be ensured and, more generally, to use and operate it in the
|
|
// same conditions as regards security.
|
|
//
|
|
// The fact that you are presently reading this means that you have had
|
|
// knowledge of the CeCILL license and that you accept its terms.
|
|
|
|
#include "Cevo/CmdList.h"
|
|
#include "Cevo/Protocol.h"
|
|
|
|
#include <QIODevice>
|
|
#include <QString>
|
|
|
|
#include <cstring>
|
|
|
|
namespace Cevo {
|
|
|
|
namespace {
|
|
// Unaligned 32-bit little-endian access, replacing the original's direct
|
|
// `Cardinal((@LogData[Pos])^)` pointer-cast reads/writes - this format is
|
|
// little-endian-only (same assumption the original already made, since a
|
|
// raw pointer cast only behaves this way on the little-endian hosts C-evo
|
|
// actually ships for).
|
|
std::uint32_t readU32(const QByteArray &buffer, int pos)
|
|
{
|
|
std::uint32_t value;
|
|
std::memcpy(&value, buffer.constData() + pos, sizeof(value));
|
|
return value;
|
|
}
|
|
|
|
void writeU32(QByteArray &buffer, int pos, std::uint32_t value)
|
|
{
|
|
std::memcpy(buffer.data() + pos, &value, sizeof(value));
|
|
}
|
|
} // namespace
|
|
|
|
int commandWithData(int command, int dataSize)
|
|
{
|
|
if (dataSize > CommandDataMaxSize) {
|
|
throw std::out_of_range(
|
|
QString("Command data size %1 out of range (0-%2).").arg(dataSize).arg(CommandDataMaxSize).toStdString());
|
|
}
|
|
const int dataElementCount = (dataSize + CommandDataElementSize - 1) / CommandDataElementSize; // Ceil
|
|
return command | (dataElementCount & static_cast<int>(CommandDataElementCountMask));
|
|
}
|
|
|
|
void CmdList::ensurePadded(int requiredSize)
|
|
{
|
|
// Get() always reads a full 32-bit word even for record formats that use
|
|
// only 1-3 of its bytes near the end of the log, so the backing buffer
|
|
// must always stay at least 4 bytes larger than any position it writes
|
|
// to or reads from (the original relied on ReallocMem's large, chunk-
|
|
// rounded over-allocation for this same safety margin).
|
|
const int padded = requiredSize + 4;
|
|
if (padded > m_log.size()) {
|
|
const int oldSize = m_log.size();
|
|
m_log.resize(padded);
|
|
std::memset(m_log.data() + oldSize, 0, padded - oldSize);
|
|
}
|
|
}
|
|
|
|
void CmdList::get(int &command, int &player, int &subject, const void *&data)
|
|
{
|
|
if (m_state.loadMoveCode > 0) {
|
|
player = -1;
|
|
std::uint32_t dirCode;
|
|
if (m_state.loadMoveCode & 1) { // FM
|
|
dirCode = (m_state.loadMoveCode >> 1) & 7;
|
|
subject = m_state.lastMovingUnit;
|
|
m_state.loadMoveCode >>= 4;
|
|
} else { // M
|
|
dirCode = (m_state.loadMoveCode >> 3) & 7;
|
|
subject = static_cast<int>((m_state.loadMoveCode >> 6) & 0xFFF);
|
|
m_state.loadMoveCode >>= 18;
|
|
m_state.lastMovingUnit = subject;
|
|
}
|
|
switch (dirCode) {
|
|
case 0: command = sMoveUnit + 0x090; break;
|
|
case 1: command = sMoveUnit + 0x0F0; break;
|
|
case 2: command = sMoveUnit + 0x390; break;
|
|
case 3: command = sMoveUnit + 0x3F0; break;
|
|
case 4: command = sMoveUnit + 0x020; break;
|
|
case 5: command = sMoveUnit + 0x060; break;
|
|
case 6: command = sMoveUnit + 0x100; break;
|
|
case 7: command = sMoveUnit + 0x300; break;
|
|
}
|
|
data = nullptr;
|
|
return;
|
|
}
|
|
|
|
ensurePadded(m_state.loadPos);
|
|
const std::uint32_t code = readU32(m_log, m_state.loadPos);
|
|
if ((code & 3) == 0) { // non-clientex command
|
|
command = static_cast<int>(((code >> 2) & 0x3FFF) + sExecute);
|
|
player = static_cast<int>((code >> 16) & 0xF);
|
|
subject = static_cast<int>((code >> 20) & 0xFFF);
|
|
m_state.loadPos += 4;
|
|
} else if ((code & 7) == 2) { // clientex command
|
|
command = static_cast<int>((code >> 3) & 0xFFFF);
|
|
player = static_cast<int>((code >> 19) & 0xF);
|
|
subject = 0;
|
|
m_state.loadPos += 3;
|
|
} else { // move command shortcut
|
|
if ((code & 1) && (code & (7u << 4)) != (6u << 4)) {
|
|
m_state.loadMoveCode = code & 0xFF;
|
|
m_state.loadPos += 1;
|
|
} else {
|
|
m_state.loadMoveCode = code & 0xFFFFFF;
|
|
m_state.loadPos += 3;
|
|
}
|
|
get(command, player, subject, data);
|
|
return;
|
|
}
|
|
|
|
if ((command & static_cast<int>(CommandDataElementCountMask)) == 0) {
|
|
data = nullptr;
|
|
} else {
|
|
ensurePadded(m_state.loadPos);
|
|
data = m_log.constData() + m_state.loadPos;
|
|
m_state.loadPos += (command & static_cast<int>(CommandDataElementCountMask)) * CommandDataElementSize;
|
|
}
|
|
}
|
|
|
|
void CmdList::getDataChanges(std::uint32_t *data, int dataSize)
|
|
{
|
|
ensurePadded(m_state.loadPos);
|
|
std::uint32_t map0 = readU32(m_log, m_state.loadPos);
|
|
m_state.loadPos += 4;
|
|
int b0 = 0;
|
|
while (map0 > 0) {
|
|
if (map0 & 1) {
|
|
ensurePadded(m_state.loadPos);
|
|
std::uint32_t map1 = readU32(m_log, m_state.loadPos);
|
|
m_state.loadPos += 4;
|
|
for (int b1 = 0; b1 < 32; ++b1) {
|
|
if ((1u << b1) & map1) {
|
|
ensurePadded(m_state.loadPos);
|
|
if (b0 * 32 + b1 < dataSize)
|
|
data[b0 * 32 + b1] = readU32(m_log, m_state.loadPos);
|
|
m_state.loadPos += 4;
|
|
}
|
|
}
|
|
}
|
|
++b0;
|
|
map0 >>= 1;
|
|
}
|
|
}
|
|
|
|
void CmdList::put(int command, int player, int subject, const void *data)
|
|
{
|
|
if ((command & 0xFC00) == sMoveUnit) { // move command shortcut
|
|
std::uint32_t dirCode = 0;
|
|
switch (command) {
|
|
case sMoveUnit + 0x090: dirCode = 0; break;
|
|
case sMoveUnit + 0x0F0: dirCode = 1; break;
|
|
case sMoveUnit + 0x390: dirCode = 2; break;
|
|
case sMoveUnit + 0x3F0: dirCode = 3; break;
|
|
case sMoveUnit + 0x020: dirCode = 4; break;
|
|
case sMoveUnit + 0x060: dirCode = 5; break;
|
|
case sMoveUnit + 0x100: dirCode = 6; break;
|
|
case sMoveUnit + 0x300: dirCode = 7; break;
|
|
}
|
|
std::uint32_t code;
|
|
if (subject == m_state.lastMovingUnit)
|
|
code = 1 + (dirCode << 1);
|
|
else
|
|
code = 6 + (dirCode << 3) + (static_cast<std::uint32_t>(subject) << 6);
|
|
|
|
if (m_state.moveCode == 0) {
|
|
m_state.moveCode = code;
|
|
} else if (m_state.moveCode & 1) { // FM + this
|
|
m_state.moveCode += code << 4;
|
|
putData(&m_state.moveCode, (code & 1) ? 1 : 3); // FM+FM : FM+M
|
|
m_state.moveCode = 0;
|
|
} else if (code & 1) { // M + FM
|
|
m_state.moveCode += code << 18;
|
|
putData(&m_state.moveCode, 3);
|
|
m_state.moveCode = 0;
|
|
} else { // M + M
|
|
putData(&m_state.moveCode, 3);
|
|
m_state.moveCode = code;
|
|
}
|
|
m_state.lastMovingUnit = subject;
|
|
} else {
|
|
completeMoveCode();
|
|
std::uint32_t code;
|
|
if (command >= cClientEx) {
|
|
code = 2 + (static_cast<std::uint32_t>(command) << 3) + (static_cast<std::uint32_t>(player) << 19);
|
|
putData(&code, 3);
|
|
} else {
|
|
code = (static_cast<std::uint32_t>(command - sExecute) << 2) +
|
|
(static_cast<std::uint32_t>(player) << 16) + (static_cast<std::uint32_t>(subject) << 20);
|
|
putData(&code, 4);
|
|
}
|
|
}
|
|
if (command & static_cast<int>(CommandDataElementCountMask))
|
|
putData(data, (command & static_cast<int>(CommandDataElementCountMask)) * CommandDataElementSize);
|
|
}
|
|
|
|
void CmdList::putDataChanges(int command, int player, const std::uint32_t *oldData, const std::uint32_t *newData,
|
|
int dataSize)
|
|
{
|
|
if (dataSize <= 0)
|
|
return;
|
|
if (dataSize > MaxDataSize)
|
|
dataSize = MaxDataSize;
|
|
completeMoveCode();
|
|
|
|
int mapPos = m_state.nLog + 8;
|
|
int logPos = mapPos + 4;
|
|
std::uint32_t map0 = 0;
|
|
for (int b0 = 0; b0 <= (dataSize - 1) / 32; ++b0) {
|
|
ensurePadded(logPos + 4 * 32);
|
|
map0 >>= 1;
|
|
std::uint32_t map1 = 0;
|
|
int rowEnd = dataSize - 1;
|
|
if (rowEnd > b0 * 32 + 31)
|
|
rowEnd = b0 * 32 + 31;
|
|
for (int b1 = b0 * 32; b1 <= rowEnd; ++b1) {
|
|
map1 >>= 1;
|
|
if (newData[b1] != oldData[b1]) {
|
|
writeU32(m_log, logPos, newData[b1]);
|
|
logPos += 4;
|
|
map1 += 0x80000000u;
|
|
}
|
|
}
|
|
if (map1 > 0) {
|
|
map1 >>= (b0 * 32 + 31 - rowEnd);
|
|
writeU32(m_log, mapPos, map1);
|
|
mapPos = logPos;
|
|
logPos += 4;
|
|
map0 += 0x80000000u;
|
|
}
|
|
}
|
|
if (map0 == 0)
|
|
return; // no changes
|
|
|
|
map0 >>= (31 - (dataSize - 1) / 32);
|
|
writeU32(m_log, m_state.nLog + 4, map0);
|
|
const std::uint32_t code =
|
|
(static_cast<std::uint32_t>(command - sExecute) << 2) + (static_cast<std::uint32_t>(player) << 16);
|
|
writeU32(m_log, m_state.nLog, code);
|
|
m_state.nLog = mapPos;
|
|
}
|
|
|
|
void CmdList::putData(const void *data, int length)
|
|
{
|
|
ensurePadded(m_state.nLog + length);
|
|
std::memcpy(m_log.data() + m_state.nLog, data, length);
|
|
m_state.nLog += length;
|
|
}
|
|
|
|
void CmdList::completeMoveCode()
|
|
{
|
|
if (m_state.moveCode > 0) {
|
|
putData(&m_state.moveCode, (m_state.moveCode & 1) ? 1 : 3); // Single FM : Single M
|
|
m_state.moveCode = 0;
|
|
}
|
|
}
|
|
|
|
void CmdList::loadFromFile(QIODevice &file)
|
|
{
|
|
std::int32_t nLog = 0;
|
|
file.read(reinterpret_cast<char *>(&nLog), 4);
|
|
m_state.nLog = nLog;
|
|
m_log = QByteArray(nLog + 4, '\0'); // zero-filled trailing slack, see ensurePadded()
|
|
file.read(m_log.data(), nLog);
|
|
m_state.loadPos = 0;
|
|
}
|
|
|
|
void CmdList::saveToFile(QIODevice &file)
|
|
{
|
|
completeMoveCode();
|
|
const std::int32_t nLog = m_state.nLog;
|
|
file.write(reinterpret_cast<const char *>(&nLog), 4);
|
|
file.write(m_log.constData(), nLog);
|
|
}
|
|
|
|
void CmdList::appendToFile(QIODevice &file, const CmdListState &oldState)
|
|
{
|
|
completeMoveCode();
|
|
const std::int32_t nLog = m_state.nLog;
|
|
file.write(reinterpret_cast<const char *>(&nLog), 4);
|
|
file.seek(file.pos() + oldState.nLog);
|
|
file.write(m_log.constData() + oldState.nLog, m_state.nLog - oldState.nLog);
|
|
}
|
|
|
|
void CmdList::cut()
|
|
{
|
|
m_state.nLog = m_state.loadPos;
|
|
}
|
|
|
|
int CmdList::progress() const
|
|
{
|
|
if (m_state.loadPos == m_state.nLog && m_state.loadMoveCode == 0)
|
|
return 1000; // loading complete
|
|
if (m_state.nLog > (1 << 20))
|
|
return (m_state.loadPos >> 8) * 999 / (m_state.nLog >> 8);
|
|
return m_state.loadPos * 999 / m_state.nLog;
|
|
}
|
|
|
|
} // namespace Cevo
|