diff --git a/server/tests/LocationGameStateTests.cpp b/server/tests/LocationGameStateTests.cpp index 373ebe1..89d6f26 100644 --- a/server/tests/LocationGameStateTests.cpp +++ b/server/tests/LocationGameStateTests.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include namespace { @@ -116,6 +117,34 @@ int main() FRG::makeNetworkCompatibilityFingerprint(reorderedContent)) { 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; definition.config.id = "test_location"; diff --git a/src/Campaign.cpp b/src/Campaign.cpp index 9ccc91e..d7b9ac4 100644 --- a/src/Campaign.cpp +++ b/src/Campaign.cpp @@ -29,18 +29,23 @@ namespace FRG { void hashString(std::uint64_t& hash, const std::string& value) { const std::uint64_t size = static_cast(value.size()); - hashBytes(hash, &size, sizeof(size)); + for (unsigned int shift = 0; shift < 64; shift += 8) { + const unsigned char byte = + static_cast((size >> shift) & 0xffu); + hashBytes(hash, &byte, sizeof(byte)); + } 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; - std::string extension = value.substr(value.size() - 5); + if (value.size() < expected.size()) return false; + std::string extension = + value.substr(value.size() - expected.size()); std::transform( extension.begin(), extension.end(), extension.begin(), [](unsigned char c) { return static_cast(std::tolower(c)); }); - return extension == ".anim"; + return extension == expected; } void collectAnimationPaths( @@ -48,7 +53,7 @@ namespace FRG { { if (value.is_string()) { const std::string path = value.get(); - if (hasAnimationExtension(path)) animationPaths.insert(path); + if (hasExtension(path, ".anim")) animationPaths.insert(path); return; } if (value.is_array()) { @@ -244,17 +249,19 @@ namespace FRG { const std::vector bytes = readCompatibilityResource(path, zipFile); hashString(hash, path); - hashBytes(hash, bytes.data(), bytes.size()); - if (path.size() >= 5 && - path.substr(path.size() - 5) == ".json") { + if (hasExtension(path, ".json")) { try { - collectAnimationPaths( - json::parse(bytes.begin(), bytes.end()), animationPaths); + const json parsed = json::parse(bytes.begin(), bytes.end()); + const std::string canonical = parsed.dump(); + hashString(hash, canonical); + collectAnimationPaths(parsed, animationPaths); } catch (const std::exception& error) { throw std::runtime_error( "Cannot fingerprint invalid JSON resource '" + path + "': " + error.what()); } + } else { + hashBytes(hash, bytes.data(), bytes.size()); } } for (const std::string& path : animationPaths) { diff --git a/src/Game.cpp b/src/Game.cpp index 21058a3..81cd50a 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -559,6 +559,7 @@ namespace FRG << "' selected; creating WebSocket client" << std::endl; // 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 = "ws://127.0.0.1:8080/"; diff --git a/src/game_server/WebsocketGameClient.cpp b/src/game_server/WebsocketGameClient.cpp index 2af3dbc..493f413 100644 --- a/src/game_server/WebsocketGameClient.cpp +++ b/src/game_server/WebsocketGameClient.cpp @@ -1,5 +1,6 @@ #include "WebsocketGameClient.h" #include "NetworkProtocol.h" +#include "../utils/Utils.h" #ifdef __EMSCRIPTEN__ #include #else @@ -525,10 +526,11 @@ void WebsocketGameClient::update(std::int64_t) std::string error; InitialStateMessage initial; if (parseInitialStateMessage(text, initial, error)) { + logger() << text << std::endl; if (!expectedCompatibilityFingerprint_.empty() && initial.compatibilityFingerprint != expectedCompatibilityFingerprint_) { - std::cerr << "[network] Incompatible server content: expected " + logger() << "[network] Incompatible server content: expected " << expectedCompatibilityFingerprint_ << ", received " << initial.compatibilityFingerprint << '\n'; continue; @@ -541,12 +543,12 @@ void WebsocketGameClient::update(std::int64_t) std::move(initial.compatibilityFingerprint); pendingTicks_.clear(); initialized_ = true; - std::cout << "[network] Applied initial state at tick " + logger() << "[network] Applied initial state at tick " << initial.snapshotTick << " as player '" << initial.assignedPlayerId << "' (delay " << initial.inputDelayTicks << " ticks)" << std::endl; } catch (const std::exception& exception) { - std::cerr << "[network] Invalid initial state: " + logger() << "[network] Invalid initial state: " << exception.what() << '\n'; } continue; @@ -555,7 +557,7 @@ void WebsocketGameClient::update(std::int64_t) if (parseTickMessage(text, tick, error)) { if (initialized_ && tick.tickId >= nextExpectedTick_) { if (!loggedFirstTick_) { - std::cout << "[network] Received first lockstep tick " + logger() << "[network] Received first lockstep tick " << tick.tickId << std::endl; loggedFirstTick_ = true; } @@ -566,14 +568,14 @@ void WebsocketGameClient::update(std::int64_t) std::string errorCode; std::string errorDetail; if (parseErrorMessage(text, errorCode, errorDetail)) { - std::cerr << "[network] Server rejected request (" << errorCode + logger() << "[network] Server rejected request (" << errorCode << "): " << errorDetail << '\n'; if (errorCode == "server_full" || errorCode == "incompatible") rejected_ = true; continue; } - std::cerr << "[network] Ignoring WebSocket message: " + logger() << "[network] Ignoring WebSocket message: " << error << '\n'; } @@ -619,7 +621,7 @@ void WebsocketGameClient::update(std::int64_t) replica_.save(state); const std::string localHash = canonicalStateHash(std::move(state)); if (localHash != tick.stateHash) { - std::cerr << "[network] Lockstep divergence at tick " + logger() << "[network] Lockstep divergence at tick " << tick.tickId << ": server=" << tick.stateHash << ", client=" << localHash << '\n'; }