fixing hash function
This commit is contained in:
1 parent
90669ff333
commit
a784da9fb4
4 files changed
+57
-18
No files matched your search
@@ -3,6 +3,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
@@ -116,6 +117,34 @@ int main()
|
|||||||
FRG::makeNetworkCompatibilityFingerprint(reorderedContent)) {
|
FRG::makeNetworkCompatibilityFingerprint(reorderedContent)) {
|
||||||
return fail("Compatibility fingerprint ignored navigation-map order");
|
return fail("Compatibility fingerprint ignored navigation-map order");
|
||||||
}
|
}
|
||||||
|
const std::filesystem::path lineEndingResource =
|
||||||
|
std::filesystem::temp_directory_path() /
|
||||||
|
"frg_fingerprint_line_endings.json";
|
||||||
|
FRG::CampaignConfig lineEndingCampaign;
|
||||||
|
lineEndingCampaign.id = "line_endings";
|
||||||
|
lineEndingCampaign.networkCompatibility = "line-endings-test";
|
||||||
|
lineEndingCampaign.locationConfigPaths = {
|
||||||
|
lineEndingResource.string()};
|
||||||
|
FRG::LocationConfig lineEndingLocation;
|
||||||
|
lineEndingLocation.id = "test";
|
||||||
|
lineEndingCampaign.locations.push_back(lineEndingLocation);
|
||||||
|
{
|
||||||
|
std::ofstream output(lineEndingResource, std::ios::binary);
|
||||||
|
output << "{\r\n \"id\": \"test\"\r\n}\r\n";
|
||||||
|
}
|
||||||
|
const std::string crlfFingerprint =
|
||||||
|
FRG::makeNetworkCompatibilityFingerprint(lineEndingCampaign);
|
||||||
|
{
|
||||||
|
std::ofstream output(
|
||||||
|
lineEndingResource, std::ios::binary | std::ios::trunc);
|
||||||
|
output << "{\n \"id\": \"test\"\n}\n";
|
||||||
|
}
|
||||||
|
const std::string lfFingerprint =
|
||||||
|
FRG::makeNetworkCompatibilityFingerprint(lineEndingCampaign);
|
||||||
|
std::filesystem::remove(lineEndingResource);
|
||||||
|
if (crlfFingerprint != lfFingerprint) {
|
||||||
|
return fail("Compatibility fingerprint depends on line endings");
|
||||||
|
}
|
||||||
|
|
||||||
FRG::LocationDefinition definition;
|
FRG::LocationDefinition definition;
|
||||||
definition.config.id = "test_location";
|
definition.config.id = "test_location";
|
||||||
|
|||||||
+18
-11
@@ -29,18 +29,23 @@ namespace FRG {
|
|||||||
void hashString(std::uint64_t& hash, const std::string& value)
|
void hashString(std::uint64_t& hash, const std::string& value)
|
||||||
{
|
{
|
||||||
const std::uint64_t size = static_cast<std::uint64_t>(value.size());
|
const std::uint64_t size = static_cast<std::uint64_t>(value.size());
|
||||||
hashBytes(hash, &size, sizeof(size));
|
for (unsigned int shift = 0; shift < 64; shift += 8) {
|
||||||
|
const unsigned char byte =
|
||||||
|
static_cast<unsigned char>((size >> shift) & 0xffu);
|
||||||
|
hashBytes(hash, &byte, sizeof(byte));
|
||||||
|
}
|
||||||
hashBytes(hash, value.data(), value.size());
|
hashBytes(hash, value.data(), value.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasAnimationExtension(const std::string& value)
|
bool hasExtension(const std::string& value, const std::string& expected)
|
||||||
{
|
{
|
||||||
if (value.size() < 5) return false;
|
if (value.size() < expected.size()) return false;
|
||||||
std::string extension = value.substr(value.size() - 5);
|
std::string extension =
|
||||||
|
value.substr(value.size() - expected.size());
|
||||||
std::transform(
|
std::transform(
|
||||||
extension.begin(), extension.end(), extension.begin(),
|
extension.begin(), extension.end(), extension.begin(),
|
||||||
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
||||||
return extension == ".anim";
|
return extension == expected;
|
||||||
}
|
}
|
||||||
|
|
||||||
void collectAnimationPaths(
|
void collectAnimationPaths(
|
||||||
@@ -48,7 +53,7 @@ namespace FRG {
|
|||||||
{
|
{
|
||||||
if (value.is_string()) {
|
if (value.is_string()) {
|
||||||
const std::string path = value.get<std::string>();
|
const std::string path = value.get<std::string>();
|
||||||
if (hasAnimationExtension(path)) animationPaths.insert(path);
|
if (hasExtension(path, ".anim")) animationPaths.insert(path);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (value.is_array()) {
|
if (value.is_array()) {
|
||||||
@@ -244,17 +249,19 @@ namespace FRG {
|
|||||||
const std::vector<char> bytes =
|
const std::vector<char> bytes =
|
||||||
readCompatibilityResource(path, zipFile);
|
readCompatibilityResource(path, zipFile);
|
||||||
hashString(hash, path);
|
hashString(hash, path);
|
||||||
hashBytes(hash, bytes.data(), bytes.size());
|
if (hasExtension(path, ".json")) {
|
||||||
if (path.size() >= 5 &&
|
|
||||||
path.substr(path.size() - 5) == ".json") {
|
|
||||||
try {
|
try {
|
||||||
collectAnimationPaths(
|
const json parsed = json::parse(bytes.begin(), bytes.end());
|
||||||
json::parse(bytes.begin(), bytes.end()), animationPaths);
|
const std::string canonical = parsed.dump();
|
||||||
|
hashString(hash, canonical);
|
||||||
|
collectAnimationPaths(parsed, animationPaths);
|
||||||
} catch (const std::exception& error) {
|
} catch (const std::exception& error) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"Cannot fingerprint invalid JSON resource '" + path +
|
"Cannot fingerprint invalid JSON resource '" + path +
|
||||||
"': " + error.what());
|
"': " + error.what());
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
hashBytes(hash, bytes.data(), bytes.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const std::string& path : animationPaths) {
|
for (const std::string& path : animationPaths) {
|
||||||
|
|||||||
@@ -559,6 +559,7 @@ namespace FRG
|
|||||||
<< "' selected; creating WebSocket client" << std::endl;
|
<< "' selected; creating WebSocket client" << std::endl;
|
||||||
// Use "ws://127.0.0.1:8080/" for a local development server.
|
// Use "ws://127.0.0.1:8080/" for a local development server.
|
||||||
|
|
||||||
|
|
||||||
static const char* const kMultiplayerServerUrl = "wss://api.shadowoverbishkek.com/server001/";
|
static const char* const kMultiplayerServerUrl = "wss://api.shadowoverbishkek.com/server001/";
|
||||||
//static const char* const kMultiplayerServerUrl = "ws://127.0.0.1:8080/";
|
//static const char* const kMultiplayerServerUrl = "ws://127.0.0.1:8080/";
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "WebsocketGameClient.h"
|
#include "WebsocketGameClient.h"
|
||||||
#include "NetworkProtocol.h"
|
#include "NetworkProtocol.h"
|
||||||
|
#include "../utils/Utils.h"
|
||||||
#ifdef __EMSCRIPTEN__
|
#ifdef __EMSCRIPTEN__
|
||||||
#include <emscripten/websocket.h>
|
#include <emscripten/websocket.h>
|
||||||
#else
|
#else
|
||||||
@@ -525,10 +526,11 @@ void WebsocketGameClient::update(std::int64_t)
|
|||||||
std::string error;
|
std::string error;
|
||||||
InitialStateMessage initial;
|
InitialStateMessage initial;
|
||||||
if (parseInitialStateMessage(text, initial, error)) {
|
if (parseInitialStateMessage(text, initial, error)) {
|
||||||
|
logger() << text << std::endl;
|
||||||
if (!expectedCompatibilityFingerprint_.empty() &&
|
if (!expectedCompatibilityFingerprint_.empty() &&
|
||||||
initial.compatibilityFingerprint !=
|
initial.compatibilityFingerprint !=
|
||||||
expectedCompatibilityFingerprint_) {
|
expectedCompatibilityFingerprint_) {
|
||||||
std::cerr << "[network] Incompatible server content: expected "
|
logger() << "[network] Incompatible server content: expected "
|
||||||
<< expectedCompatibilityFingerprint_ << ", received "
|
<< expectedCompatibilityFingerprint_ << ", received "
|
||||||
<< initial.compatibilityFingerprint << '\n';
|
<< initial.compatibilityFingerprint << '\n';
|
||||||
continue;
|
continue;
|
||||||
@@ -541,12 +543,12 @@ void WebsocketGameClient::update(std::int64_t)
|
|||||||
std::move(initial.compatibilityFingerprint);
|
std::move(initial.compatibilityFingerprint);
|
||||||
pendingTicks_.clear();
|
pendingTicks_.clear();
|
||||||
initialized_ = true;
|
initialized_ = true;
|
||||||
std::cout << "[network] Applied initial state at tick "
|
logger() << "[network] Applied initial state at tick "
|
||||||
<< initial.snapshotTick << " as player '"
|
<< initial.snapshotTick << " as player '"
|
||||||
<< initial.assignedPlayerId << "' (delay "
|
<< initial.assignedPlayerId << "' (delay "
|
||||||
<< initial.inputDelayTicks << " ticks)" << std::endl;
|
<< initial.inputDelayTicks << " ticks)" << std::endl;
|
||||||
} catch (const std::exception& exception) {
|
} catch (const std::exception& exception) {
|
||||||
std::cerr << "[network] Invalid initial state: "
|
logger() << "[network] Invalid initial state: "
|
||||||
<< exception.what() << '\n';
|
<< exception.what() << '\n';
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
@@ -555,7 +557,7 @@ void WebsocketGameClient::update(std::int64_t)
|
|||||||
if (parseTickMessage(text, tick, error)) {
|
if (parseTickMessage(text, tick, error)) {
|
||||||
if (initialized_ && tick.tickId >= nextExpectedTick_) {
|
if (initialized_ && tick.tickId >= nextExpectedTick_) {
|
||||||
if (!loggedFirstTick_) {
|
if (!loggedFirstTick_) {
|
||||||
std::cout << "[network] Received first lockstep tick "
|
logger() << "[network] Received first lockstep tick "
|
||||||
<< tick.tickId << std::endl;
|
<< tick.tickId << std::endl;
|
||||||
loggedFirstTick_ = true;
|
loggedFirstTick_ = true;
|
||||||
}
|
}
|
||||||
@@ -566,14 +568,14 @@ void WebsocketGameClient::update(std::int64_t)
|
|||||||
std::string errorCode;
|
std::string errorCode;
|
||||||
std::string errorDetail;
|
std::string errorDetail;
|
||||||
if (parseErrorMessage(text, errorCode, errorDetail)) {
|
if (parseErrorMessage(text, errorCode, errorDetail)) {
|
||||||
std::cerr << "[network] Server rejected request (" << errorCode
|
logger() << "[network] Server rejected request (" << errorCode
|
||||||
<< "): " << errorDetail << '\n';
|
<< "): " << errorDetail << '\n';
|
||||||
if (errorCode == "server_full" ||
|
if (errorCode == "server_full" ||
|
||||||
errorCode == "incompatible")
|
errorCode == "incompatible")
|
||||||
rejected_ = true;
|
rejected_ = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::cerr << "[network] Ignoring WebSocket message: "
|
logger() << "[network] Ignoring WebSocket message: "
|
||||||
<< error << '\n';
|
<< error << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -619,7 +621,7 @@ void WebsocketGameClient::update(std::int64_t)
|
|||||||
replica_.save(state);
|
replica_.save(state);
|
||||||
const std::string localHash = canonicalStateHash(std::move(state));
|
const std::string localHash = canonicalStateHash(std::move(state));
|
||||||
if (localHash != tick.stateHash) {
|
if (localHash != tick.stateHash) {
|
||||||
std::cerr << "[network] Lockstep divergence at tick "
|
logger() << "[network] Lockstep divergence at tick "
|
||||||
<< tick.tickId << ": server=" << tick.stateHash
|
<< tick.tickId << ": server=" << tick.stateHash
|
||||||
<< ", client=" << localHash << '\n';
|
<< ", client=" << localHash << '\n';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user