Merge pull request #113 from OpenStarbound/wip/net-n-btree

PR for WIP net branch
This commit is contained in:
Kae 2024-09-15 15:17:38 +10:00 committed by GitHub
commit 304d32d5c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
109 changed files with 1361 additions and 904 deletions

235
.github/workflows/build.yml vendored Normal file
View File

@ -0,0 +1,235 @@
name: Build
on:
workflow_dispatch:
push:
branches:
- "*"
pull_request:
branches:
- "*"
jobs:
build_windows:
name: Build OpenStarbound Windows x64
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: Install CMake & Ninja
uses: lukka/get-cmake@latest
with:
cmakeVersion: 3.29.2
- name: sccache
uses: hendrikmuhs/ccache-action@v1.2
with:
variant: sccache
key: ${{ github.job }}-${{ runner.os }}
max-size: 1000M
- uses: ilammy/msvc-dev-cmd@v1
- name: vcpkg
uses: lukka/run-vcpkg@v11
id: runvcpkg
with:
vcpkgJsonGlob: '**/source/vcpkg.json'
vcpkgConfigurationJsonGlob: '**/source/vcpkg-configuration.json'
- name: Run CMake
uses: lukka/run-cmake@v10
with:
cmakeListsTxtPath: '${{ github.workspace }}/source/CMakeLists.txt'
configurePreset: 'windows-release'
buildPreset: 'windows-release'
testPreset: 'windows-release'
- name: Run Post-Build Task
working-directory: ${{ github.workspace }}
run: scripts\ci\windows\post_build.bat
- name: Assemble Files
working-directory: ${{ github.workspace }}
run: scripts\ci\windows\assemble.bat
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Windows-All-DevOnly
path: dist/*
- name: Upload Client
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Windows-Client
path: client_distribution/*
- name: Upload Server
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Windows-Server
path: server_distribution/*
- name: Create Installer
working-directory: ${{ github.workspace }}
run: |
& "C:\Program Files (x86)\Inno Setup 6\iscc.exe" /Oinstaller scripts\inno\setup.iss
- name: Upload Installer
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Windows-Installer
path: installer/*
build_linux:
name: Build OpenStarbound Linux x86_64
runs-on: ubuntu-20.04
steps:
- name: Install Packages
run: |
sudo apt-get update
sudo apt-get install -y pkg-config libxmu-dev libxi-dev libgl-dev libglu1-mesa-dev libsdl2-dev
- name: Install CMake & Ninja
uses: lukka/get-cmake@latest
with:
cmakeVersion: 3.29.2
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: sccache
uses: hendrikmuhs/ccache-action@v1.2
with:
variant: sccache
key: ${{ github.job }}-${{ runner.os }}
max-size: 250M
- name: vcpkg
uses: lukka/run-vcpkg@v11
id: runvcpkg
with:
vcpkgJsonGlob: '**/source/vcpkg.json'
vcpkgConfigurationJsonGlob: '**/source/vcpkg-configuration.json'
- name: Run CMake
uses: lukka/run-cmake@v10
with:
cmakeListsTxtPath: '${{ github.workspace }}/source/CMakeLists.txt'
configurePreset: 'linux-release'
buildPreset: 'linux-release'
testPreset: 'linux-release'
- name: Assemble Files
working-directory: ${{ github.workspace }}
run: scripts/ci/linux/assemble.sh
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Linux
path: dist.tar
- name: Upload Client Files
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Linux-Client
path: client.tar
- name: Upload Server Files
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Linux-Server
path: server.tar
build-mac-intel:
name: Build OpenStarbound macOS x86_64
runs-on: macos-13
steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: Install CMake & Ninja
uses: lukka/get-cmake@latest
with:
cmakeVersion: 3.29.0
- name: sccache
uses: hendrikmuhs/ccache-action@v1.2
with:
variant: sccache
key: ${{ github.job }}-Intel-${{ runner.os }}
max-size: 250M
- name: vcpkg
uses: lukka/run-vcpkg@v11
id: runvcpkg
with:
vcpkgJsonGlob: '**/source/vcpkg.json'
vcpkgConfigurationJsonGlob: '**/source/vcpkg-configuration.json'
- name: Run CMake
uses: lukka/run-cmake@v10
with:
cmakeListsTxtPath: '${{ github.workspace }}/source/CMakeLists.txt'
configurePreset: 'macos-release'
buildPreset: 'macos-release'
testPreset: 'macos-release'
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-macOS-Intel
path: dist/*
build-mac-arm:
name: Build OpenStarbound macOS arm64
runs-on: macos-14
steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: Install CMake & Ninja
uses: lukka/get-cmake@latest
with:
cmakeVersion: 3.29.2
- name: sccache
uses: hendrikmuhs/ccache-action@v1.2
with:
variant: sccache
key: ${{ github.job }}-ARM-${{ runner.os }}
max-size: 250M
- name: vcpkg
uses: lukka/run-vcpkg@v11
id: runvcpkg
with:
vcpkgJsonGlob: '**/source/vcpkg.json'
vcpkgConfigurationJsonGlob: '**/source/vcpkg-configuration.json'
- name: Run CMake
uses: lukka/run-cmake@v10
with:
cmakeListsTxtPath: '${{ github.workspace }}/source/CMakeLists.txt'
configurePreset: 'macos-arm-release'
buildPreset: 'macos-arm-release'
testPreset: 'macos-arm-release'
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-macOS-Silicon
path: dist/*

View File

@ -1,76 +0,0 @@
name: Ubuntu Linux
on:
push:
branches:
- "*"
tags:
- "*"
pull_request:
branches:
- "*"
jobs:
build:
name: OpenStarbound Linux x86_64
runs-on: ubuntu-20.04
steps:
- name: Install Packages
run: |
sudo apt-get update
sudo apt-get install -y pkg-config libxmu-dev libxi-dev libgl-dev libglu1-mesa-dev libsdl2-dev
- name: Install CMake & Ninja
uses: lukka/get-cmake@latest
with:
cmakeVersion: 3.29.2
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: sccache
uses: hendrikmuhs/ccache-action@v1.2
with:
variant: sccache
key: ${{ github.job }}-${{ runner.os }}
max-size: 250M
- name: vcpkg
uses: lukka/run-vcpkg@v11
id: runvcpkg
with:
vcpkgJsonGlob: '**/source/vcpkg.json'
vcpkgConfigurationJsonGlob: '**/source/vcpkg-configuration.json'
- name: Run CMake
uses: lukka/run-cmake@v10
with:
cmakeListsTxtPath: '${{ github.workspace }}/source/CMakeLists.txt'
configurePreset: 'linux-release'
buildPreset: 'linux-release'
testPreset: 'linux-release'
- name: Assemble Files
working-directory: ${{ github.workspace }}
run: scripts/ci/linux/assemble.sh
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Linux
path: dist.tar
- name: Upload Client Files
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Linux-Client
path: client.tar
- name: Upload Server Files
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Linux-Server
path: server.tar

View File

@ -1,97 +0,0 @@
name: macOS
on:
push:
branches:
- "*"
tags:
- "*"
pull_request:
branches:
- "*"
jobs:
build-intel:
name: OpenStarbound macOS x86_64
runs-on: macos-13
steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: Install CMake & Ninja
uses: lukka/get-cmake@latest
with:
cmakeVersion: 3.29.0
- name: sccache
uses: hendrikmuhs/ccache-action@v1.2
with:
variant: sccache
key: ${{ github.job }}-Intel-${{ runner.os }}
max-size: 250M
- name: vcpkg
uses: lukka/run-vcpkg@v11
id: runvcpkg
with:
vcpkgJsonGlob: '**/source/vcpkg.json'
vcpkgConfigurationJsonGlob: '**/source/vcpkg-configuration.json'
- name: Run CMake
uses: lukka/run-cmake@v10
with:
cmakeListsTxtPath: '${{ github.workspace }}/source/CMakeLists.txt'
configurePreset: 'macos-release'
buildPreset: 'macos-release'
testPreset: 'macos-release'
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-macOS-Intel
path: dist/*
build-arm:
name: OpenStarbound macOS arm64
runs-on: macos-14
steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: Install CMake & Ninja
uses: lukka/get-cmake@latest
with:
cmakeVersion: 3.29.2
- name: sccache
uses: hendrikmuhs/ccache-action@v1.2
with:
variant: sccache
key: ${{ github.job }}-ARM-${{ runner.os }}
max-size: 250M
- name: vcpkg
uses: lukka/run-vcpkg@v11
id: runvcpkg
with:
vcpkgJsonGlob: '**/source/vcpkg.json'
vcpkgConfigurationJsonGlob: '**/source/vcpkg-configuration.json'
- name: Run CMake
uses: lukka/run-cmake@v10
with:
cmakeListsTxtPath: '${{ github.workspace }}/source/CMakeLists.txt'
configurePreset: 'macos-arm-release'
buildPreset: 'macos-arm-release'
testPreset: 'macos-arm-release'
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-macOS-Silicon
path: dist/*

View File

@ -1,89 +0,0 @@
name: Windows
on:
push:
branches:
- "*"
tags:
- "*"
pull_request:
branches:
- "*"
jobs:
build:
name: Build OpenStarbound Windows x64
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: Install CMake & Ninja
uses: lukka/get-cmake@latest
with:
cmakeVersion: 3.29.2
- name: sccache
uses: hendrikmuhs/ccache-action@v1.2
with:
variant: sccache
key: ${{ github.job }}-${{ runner.os }}
max-size: 1000M
- uses: ilammy/msvc-dev-cmd@v1
- name: vcpkg
uses: lukka/run-vcpkg@v11
id: runvcpkg
with:
vcpkgJsonGlob: '**/source/vcpkg.json'
vcpkgConfigurationJsonGlob: '**/source/vcpkg-configuration.json'
- name: Run CMake
uses: lukka/run-cmake@v10
with:
cmakeListsTxtPath: '${{ github.workspace }}/source/CMakeLists.txt'
configurePreset: 'windows-release'
buildPreset: 'windows-release'
testPreset: 'windows-release'
- name: Run Post-Build Task
working-directory: ${{ github.workspace }}
run: scripts\ci\windows\post_build.bat
- name: Assemble Files
working-directory: ${{ github.workspace }}
run: scripts\ci\windows\assemble.bat
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Windows-All-DevOnly
path: dist/*
- name: Upload Client
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Windows-Client
path: client_distribution/*
- name: Upload Server
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Windows-Server
path: server_distribution/*
- name: Create Installer
working-directory: ${{ github.workspace }}
run: |
& "C:\Program Files (x86)\Inno Setup 6\iscc.exe" /Oinstaller scripts\inno\setup.iss
- name: Upload Installer
uses: actions/upload-artifact@v4
with:
name: OpenStarbound-Windows-Installer
path: installer/*

View File

@ -18,7 +18,6 @@ SET (star_base_HEADERS
StarMixer.hpp
StarPackedAssetSource.hpp
StarRootBase.hpp
StarVersion.hpp
StarVersionOptionParser.hpp
StarWorldGeometry.hpp
scripting/StarImageLuaBindings.hpp
@ -40,8 +39,7 @@ SET (star_base_SOURCES
scripting/StarImageLuaBindings.cpp
)
CONFIGURE_FILE (StarVersion.cpp.in ${CMAKE_CURRENT_BINARY_DIR}/StarVersion.cpp)
ADD_LIBRARY (star_base OBJECT ${star_base_SOURCES} ${star_base_HEADERS} ${CMAKE_CURRENT_BINARY_DIR}/StarVersion.cpp)
ADD_LIBRARY (star_base OBJECT ${star_base_SOURCES} ${star_base_HEADERS})
IF(STAR_PRECOMPILED_HEADERS)
TARGET_PRECOMPILE_HEADERS (star_base REUSE_FROM star_core)

View File

@ -68,7 +68,7 @@ static void validatePath(AssetPath const& components, bool canContainSubPath, bo
throw AssetException::format("Path '{}' cannot contain directives", components);
}
static void validatePath(StringView const& path, bool canContainSubPath, bool canContainDirectives) {
static void validatePath(StringView path, bool canContainSubPath, bool canContainDirectives) {
std::string_view const& str = path.utf8();
size_t end = str.find_first_of(":?");

View File

@ -70,10 +70,12 @@ SET (star_core_HEADERS
StarMultiArray.hpp
StarMultiArrayInterpolator.hpp
StarMultiTable.hpp
StarNetCompatibility.hpp
StarNetElement.hpp
StarNetElementBasicFields.hpp
StarNetElementContainers.hpp
StarNetElementDynamicGroup.hpp
StarNetElementExt.hpp
StarNetElementFloatFields.hpp
StarNetElementGroup.hpp
StarNetElementSignal.hpp
@ -122,6 +124,7 @@ SET (star_core_HEADERS
StarUnicode.hpp
StarUuid.hpp
StarVector.hpp
StarVersion.hpp
StarVlqEncoding.hpp
StarWeightedPool.hpp
StarWorkerPool.hpp
@ -162,6 +165,7 @@ SET (star_core_SOURCES
StarLua.cpp
StarLuaConverters.cpp
StarMemory.cpp
StarNetCompatibility.cpp
StarNetElement.cpp
StarNetElementBasicFields.cpp
StarNetElementGroup.cpp
@ -217,7 +221,8 @@ ELSEIF (STAR_SYSTEM_FAMILY_WINDOWS)
ENDIF ()
ADD_LIBRARY (star_core OBJECT ${star_core_SOURCES} ${star_core_HEADERS})
CONFIGURE_FILE (StarVersion.cpp.in ${CMAKE_CURRENT_BINARY_DIR}/StarVersion.cpp)
ADD_LIBRARY (star_core OBJECT ${star_core_SOURCES} ${star_core_HEADERS} ${CMAKE_CURRENT_BINARY_DIR}/StarVersion.cpp)
IF(STAR_PRECOMPILED_HEADERS)
TARGET_PRECOMPILE_HEADERS (star_core PUBLIC StarPch.hpp)

View File

@ -1,6 +1,7 @@
#include "StarBTreeDatabase.hpp"
#include "StarSha256.hpp"
#include "StarVlqEncoding.hpp"
#include "StarLogging.hpp"
namespace Star {
@ -243,7 +244,7 @@ uint32_t BTreeDatabase::freeBlockCount() {
indexBlockIndex = indexBlock.nextFreeBlock;
}
count += m_availableBlocks.size() + m_pendingFree.size();
count += m_availableBlocks.size();
// Include untracked blocks at the end of the file in the free count.
count += (m_device->size() - m_deviceSize) / m_blockSize;
@ -272,7 +273,7 @@ uint32_t BTreeDatabase::leafBlockCount() {
return true;
}
BTreeDatabase* parent;
BTreeDatabase* parent = nullptr;
BlockIndex leafBlockCount = 0;
};
@ -293,8 +294,8 @@ void BTreeDatabase::rollback() {
m_availableBlocks.clear();
m_indexCache.clear();
m_uncommittedWrites.clear();
m_uncommitted.clear();
m_pendingFree.clear();
readRoot();
@ -305,7 +306,8 @@ void BTreeDatabase::rollback() {
void BTreeDatabase::close(bool closeDevice) {
WriteLocker writeLocker(m_lock);
if (m_open) {
doCommit();
if (!tryFlatten())
doCommit();
m_indexCache.clear();
@ -536,7 +538,7 @@ auto BTreeDatabase::BTreeImpl::loadIndex(Pointer pointer) -> Index {
index->pointers.resize(s);
for (uint32_t i = 0; i < s; ++i) {
auto& e = index->pointers[i];
e.key =buffer.readBytes(parent->m_keySize);
e.key = buffer.readBytes(parent->m_keySize);
e.pointer = buffer.read<BlockIndex>();
}
@ -896,17 +898,25 @@ void BTreeDatabase::rawReadBlock(BlockIndex blockIndex, size_t blockOffset, char
if (size <= 0)
return;
m_device->readFullAbsolute(HeaderSize + blockIndex * (StreamOffset)m_blockSize + blockOffset, block, size);
if (auto buffer = m_uncommittedWrites.ptr(blockIndex))
buffer->copyTo(block, blockOffset, size);
else
m_device->readFullAbsolute(HeaderSize + blockIndex * (StreamOffset)m_blockSize + blockOffset, block, size);
}
void BTreeDatabase::rawWriteBlock(BlockIndex blockIndex, size_t blockOffset, char const* block, size_t size) const {
void BTreeDatabase::rawWriteBlock(BlockIndex blockIndex, size_t blockOffset, char const* block, size_t size) {
if (blockOffset > m_blockSize || size > m_blockSize - blockOffset)
throw DBException::format("Write past end of block, offset: {} size {}", blockOffset, size);
if (size <= 0)
return;
m_device->writeFullAbsolute(HeaderSize + blockIndex * (StreamOffset)m_blockSize + blockOffset, block, size);
StreamOffset blockStart = HeaderSize + blockIndex * (StreamOffset)m_blockSize;
auto buffer = m_uncommittedWrites.find(blockIndex);
if (buffer == m_uncommittedWrites.end())
buffer = m_uncommittedWrites.emplace(blockIndex, m_device->readBytesAbsolute(blockStart, m_blockSize)).first;
buffer->second.writeFrom(block, blockOffset, size);
}
auto BTreeDatabase::readFreeIndexBlock(BlockIndex blockIndex) -> FreeIndexBlock {
@ -991,12 +1001,12 @@ auto BTreeDatabase::leafTailBlocks(BlockIndex leafPointer) -> List<BlockIndex> {
}
void BTreeDatabase::freeBlock(BlockIndex b) {
if (m_uncommitted.contains(b)) {
if (m_uncommitted.contains(b))
m_uncommitted.remove(b);
m_availableBlocks.add(b);
} else {
m_pendingFree.append(b);
}
if (m_uncommittedWrites.contains(b))
m_uncommittedWrites.remove(b);
m_availableBlocks.add(b);
}
auto BTreeDatabase::reserveBlock() -> BlockIndex {
@ -1007,10 +1017,7 @@ auto BTreeDatabase::reserveBlock() -> BlockIndex {
FreeIndexBlock indexBlock = readFreeIndexBlock(m_headFreeIndexBlock);
for (auto const& b : indexBlock.freeBlocks)
m_availableBlocks.add(b);
// We cannot make available the block itself, because we must maintain
// atomic consistency. We will need to free this block later and commit
// the new free index block chain.
m_pendingFree.append(m_headFreeIndexBlock);
m_availableBlocks.add(m_headFreeIndexBlock);
m_headFreeIndexBlock = indexBlock.nextFreeBlock;
}
@ -1068,65 +1075,168 @@ void BTreeDatabase::readRoot() {
}
void BTreeDatabase::doCommit() {
if (m_availableBlocks.empty() && m_pendingFree.empty() && m_uncommitted.empty())
if (m_availableBlocks.empty() && m_uncommitted.empty())
return;
if (!m_availableBlocks.empty() || !m_pendingFree.empty()) {
if (!m_availableBlocks.empty()) {
// First, read the existing head FreeIndexBlock, if it exists
FreeIndexBlock indexBlock = FreeIndexBlock{InvalidBlockIndex, {}};
if (m_headFreeIndexBlock != InvalidBlockIndex) {
auto newBlock = [&]() -> BlockIndex {
if (!m_availableBlocks.empty())
return m_availableBlocks.takeFirst();
else
return makeEndBlock();
};
if (m_headFreeIndexBlock != InvalidBlockIndex)
indexBlock = readFreeIndexBlock(m_headFreeIndexBlock);
if (indexBlock.freeBlocks.size() >= maxFreeIndexLength()) {
// If the existing head free index block is full, then we should start a
// new one and leave it alone
indexBlock.nextFreeBlock = m_headFreeIndexBlock;
indexBlock.freeBlocks.clear();
} else {
// If we are copying an existing free index block, the old free index
// block will be a newly freed block
indexBlock.freeBlocks.append(m_headFreeIndexBlock);
}
}
else
m_headFreeIndexBlock = newBlock();
// Then, we need to write all the available blocks, which are safe to write
// to, and the pending free blocks, which are NOT safe to write to, to the
// FreeIndexBlock chain.
// Then, we need to write all the available blocks to the FreeIndexBlock chain.
while (true) {
if (indexBlock.freeBlocks.size() < maxFreeIndexLength() && (!m_availableBlocks.empty() || !m_pendingFree.empty())) {
// If we have room on our current FreeIndexblock, just add a block to
// it. Prioritize the pending free blocks, because we cannot use those
// to write to.
BlockIndex toAdd;
if (m_pendingFree.empty())
toAdd = m_availableBlocks.takeFirst();
else
toAdd = m_pendingFree.takeFirst();
// If we have room on our current FreeIndexBlock, just add a block to it.
if (!m_availableBlocks.empty() && indexBlock.freeBlocks.size() < maxFreeIndexLength()) {
BlockIndex toAdd = m_availableBlocks.takeFirst();
indexBlock.freeBlocks.append(toAdd);
} else {
// If our index block is full OR we are out of blocks to free, then
// need to write a new head free index block.
if (m_availableBlocks.empty())
m_headFreeIndexBlock = makeEndBlock();
else
m_headFreeIndexBlock = m_availableBlocks.takeFirst();
// Update the current head free index block.
writeFreeIndexBlock(m_headFreeIndexBlock, indexBlock);
// If we're out of blocks to free, then we're done
if (m_availableBlocks.empty() && m_pendingFree.empty())
if (m_availableBlocks.empty())
break;
indexBlock.nextFreeBlock = m_headFreeIndexBlock;
indexBlock.freeBlocks.clear();
// If our head free index block is full, then
// need to write a new head free index block.
if (indexBlock.freeBlocks.size() >= maxFreeIndexLength()) {
indexBlock.nextFreeBlock = m_headFreeIndexBlock;
indexBlock.freeBlocks.clear();
m_headFreeIndexBlock = newBlock();
writeFreeIndexBlock(m_headFreeIndexBlock, indexBlock);
}
}
}
}
writeRoot();
commitWrites();
writeRoot();
m_uncommitted.clear();
}
void BTreeDatabase::commitWrites() {
for (auto& write : m_uncommittedWrites)
m_device->writeFullAbsolute(HeaderSize + write.first * (StreamOffset)m_blockSize, write.second.ptr(), m_blockSize);
m_device->sync();
m_uncommittedWrites.clear();
}
bool BTreeDatabase::tryFlatten() {
if (m_headFreeIndexBlock == InvalidBlockIndex || m_rootIsLeaf || !m_device->isWritable())
return false;
BlockIndex freeBlockCount = 0;
BlockIndex indexBlockIndex = m_headFreeIndexBlock;
while (indexBlockIndex != InvalidBlockIndex) {
FreeIndexBlock indexBlock = readFreeIndexBlock(indexBlockIndex);
freeBlockCount += 1 + indexBlock.freeBlocks.size();
indexBlockIndex = indexBlock.nextFreeBlock;
}
BlockIndex expectedBlockCount = (m_deviceSize - HeaderSize) / m_blockSize;
float free = float(freeBlockCount) / float(expectedBlockCount);
if (free < 0.05f)
return false;
Logger::info("[BTreeDatabase] File '{}' is {:.2f}% free space, flattening", m_device->deviceName(), free * 100.f);
indexBlockIndex = m_headFreeIndexBlock;
{
List<BlockIndex> availableBlocksList;
do {
FreeIndexBlock indexBlock = readFreeIndexBlock(indexBlockIndex);
availableBlocksList.appendAll(indexBlock.freeBlocks);
availableBlocksList.append(indexBlockIndex);
indexBlockIndex = indexBlock.nextFreeBlock;
} while (indexBlockIndex != InvalidBlockIndex);
m_headFreeIndexBlock = InvalidBlockIndex;
sort(availableBlocksList);
for (auto& availableBlock : availableBlocksList)
m_availableBlocks.insert(m_availableBlocks.end(), availableBlock);
}
BlockIndex count = 1; // 1 to include root index
double start = Time::monotonicTime();
auto index = m_impl.loadIndex(m_impl.rootPointer());
if (flattenVisitor(index, count)) {
m_impl.deleteIndex(index);
index->self = InvalidBlockIndex;
m_root = m_impl.storeIndex(index);
}
m_availableBlocks.clear();
m_device->resize(m_deviceSize = HeaderSize + (StreamOffset)m_blockSize * count);
m_indexCache.clear();
commitWrites();
writeRoot();
m_uncommitted.clear();
Logger::info("[BTreeDatabase] Finished flattening '{}' in {:.2f} milliseconds", m_device->deviceName(), (Time::monotonicTime() - start) * 1000.f);
return true;
}
bool BTreeDatabase::flattenVisitor(BTreeImpl::Index& index, BlockIndex& count) {
auto pointerCount = index->pointerCount();
count += pointerCount;
bool canStore = !m_availableBlocks.empty();
bool needsStore = false;
if (m_impl.indexLevel(index) == 0) {
for (size_t i = 0; i != pointerCount; ++i) {
auto indexPointer = index->pointer(i);
auto tailBlocks = leafTailBlocks(indexPointer);
if (canStore) {
bool leafNeedsStore = m_availableBlocks.first() < indexPointer;
if (!leafNeedsStore)
for (size_t i = 0; !leafNeedsStore && i != tailBlocks.size(); ++i)
if (m_availableBlocks.first() < tailBlocks[i])
leafNeedsStore = true;
if (leafNeedsStore) {
auto leaf = m_impl.loadLeaf(indexPointer);
m_impl.deleteLeaf(leaf);
leaf->self = InvalidBlockIndex;
index->updatePointer(i, m_impl.storeLeaf(leaf));
tailBlocks = leafTailBlocks(leaf->self);
needsStore = true;
}
canStore = !m_availableBlocks.empty();
}
count += tailBlocks.size();
}
} else {
for (size_t i = 0; i != pointerCount; ++i) {
auto childIndex = m_impl.loadIndex(index->pointer(i));
if (canStore && flattenVisitor(childIndex, count)) {
m_impl.deleteIndex(childIndex);
childIndex->self = InvalidBlockIndex;
index->updatePointer(i, m_impl.storeIndex(childIndex));
canStore = !m_availableBlocks.empty();
needsStore = true;
}
}
}
return needsStore || (canStore && m_availableBlocks.first() < index->self);
}
void BTreeDatabase::checkIfOpen(char const* methodName, bool shouldBeOpen) const {
if (shouldBeOpen && !m_open)
throw DBException::format("BTreeDatabase method '{}' called when not open, must be open.", methodName);
@ -1146,7 +1256,7 @@ void BTreeDatabase::checkKeySize(ByteArray const& k) const {
}
uint32_t BTreeDatabase::maxFreeIndexLength() const {
return (m_blockSize - 2 - sizeof(BlockIndex) - 4) / sizeof(BlockIndex);
return (m_blockSize / sizeof(BlockIndex)) - 2 - sizeof(BlockIndex) - 4;
}
BTreeSha256Database::BTreeSha256Database() {

View File

@ -230,7 +230,7 @@ private:
void updateBlock(BlockIndex blockIndex, ByteArray const& block);
void rawReadBlock(BlockIndex blockIndex, size_t blockOffset, char* block, size_t size) const;
void rawWriteBlock(BlockIndex blockIndex, size_t blockOffset, char const* block, size_t size) const;
void rawWriteBlock(BlockIndex blockIndex, size_t blockOffset, char const* block, size_t size);
void updateHeadFreeIndexBlock(BlockIndex newHead);
@ -251,6 +251,9 @@ private:
void writeRoot();
void readRoot();
void doCommit();
void commitWrites();
bool tryFlatten();
bool flattenVisitor(BTreeImpl::Index& index, BlockIndex& count);
void checkIfOpen(char const* methodName, bool shouldBeOpen) const;
void checkBlockIndex(size_t blockIndex) const;
@ -285,14 +288,14 @@ private:
bool m_dirty;
// Blocks that can be freely allocated and written to without violating
// atomic consistency
// atomic consistency.
Set<BlockIndex> m_availableBlocks;
// Blocks to be freed on next commit.
Deque<BlockIndex> m_pendingFree;
// Blocks that have been written in uncommitted portions of the tree.
Set<BlockIndex> m_uncommitted;
// Temporarily holds written data so that it can be rolled back.
mutable Map<BlockIndex, ByteArray> m_uncommittedWrites;
};
// Version of BTreeDatabase that hashes keys with SHA-256 to produce a unique

View File

@ -35,6 +35,10 @@ void DataStream::setStreamCompatibilityVersion(unsigned streamCompatibilityVersi
m_streamCompatibilityVersion = streamCompatibilityVersion;
}
void DataStream::setStreamCompatibilityVersion(NetCompatibilityRules const& rules) {
m_streamCompatibilityVersion = rules.version();
}
ByteArray DataStream::readBytes(size_t len) {
ByteArray ba;
ba.resize(len);

View File

@ -1,6 +1,7 @@
#pragma once
#include "StarString.hpp"
#include "StarNetCompatibility.hpp"
namespace Star {
@ -12,7 +13,7 @@ public:
DataStream();
virtual ~DataStream() = default;
static unsigned const CurrentStreamVersion = 1;
static unsigned const CurrentStreamVersion = 2;
// DataStream defaults to big-endian order for all primitive types
ByteOrder byteOrder() const;
@ -27,7 +28,7 @@ public:
// changed for compatibility with older versions of DataStream serialization.
unsigned streamCompatibilityVersion() const;
void setStreamCompatibilityVersion(unsigned streamCompatibilityVersion);
void setStreamCompatibilityVersion(NetCompatibilityRules const& rules);
// Do direct reads and writes
virtual void readData(char* data, size_t len) = 0;
virtual void writeData(char const* data, size_t len) = 0;

View File

@ -126,8 +126,8 @@ private:
class DataStreamExternalBuffer : public DataStream {
public:
DataStreamExternalBuffer();
explicit DataStreamExternalBuffer(ByteArray const& byteArray);
explicit DataStreamExternalBuffer(DataStreamBuffer const& buffer);
DataStreamExternalBuffer(ByteArray const& byteArray);
DataStreamExternalBuffer(DataStreamBuffer const& buffer);
DataStreamExternalBuffer(DataStreamExternalBuffer const& buffer) = default;
DataStreamExternalBuffer(char const* externalData, size_t len);

View File

@ -30,8 +30,6 @@ FTContext ftContext;
struct FontImpl {
FT_Face face;
unsigned loadedPixelSize = 0;
String::Char loadedChar = 0;
};
FontPtr Font::loadFont(String const& fileName, unsigned pixelSize) {
@ -97,19 +95,11 @@ tuple<Image, Vec2I, bool> Font::render(String::Char c) {
throw FontException("Font::render called on uninitialized font.");
FT_Face face = m_fontImpl->face;
if (FT_Load_Glyph(face, FT_Get_Char_Index(face, c), FontLoadFlags) != 0)
return {};
if (m_fontImpl->loadedPixelSize != m_pixelSize || m_fontImpl->loadedChar != c) {
FT_UInt glyph_index = FT_Get_Char_Index(face, c);
if (FT_Load_Glyph(face, glyph_index, FontLoadFlags) != 0)
return {};
// convert to an anti-aliased bitmap
if (FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL) != 0)
return {};
}
m_fontImpl->loadedPixelSize = m_pixelSize;
m_fontImpl->loadedChar = c;
if (FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL) != 0)
return {};
FT_GlyphSlot slot = face->glyph;
if (!slot->bitmap.buffer)
@ -134,7 +124,7 @@ tuple<Image, Vec2I, bool> Font::render(String::Char c) {
}
}
}
} else if (colored = slot->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) {
} else if (colored = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA)) {
unsigned bpp = image.bytesPerPixel();
uint8_t* data = image.data() + bpp + ((image.width() * (image.height() - 2)) * bpp); // offset by 1 pixel as it's padded
for (size_t y = 0; y != height; ++y) {

View File

@ -214,7 +214,7 @@ ImageOperation imageOperationFromString(StringView string) {
else // we're in A of A=B. In vanilla only A=B pairs are evaluated, so only throw an error if B is also there.
return operation;
if (which = !which)
if ((which = !which))
operation.colorReplaceMap[*(Vec4B*)&a] = *(Vec4B*)&b;
hexLen = 0;

View File

@ -211,32 +211,32 @@ Json jsonFromList(List<Value> const& list, Converter&& valueConvert) {
return res;
}
template <typename Value>
Set<Value> jsonToSet(Json const& v) {
return jsonToSet<Value>(v, construct<Value>());
template <typename SetType>
SetType jsonToSet(Json const& v) {
return jsonToSet<SetType>(v, construct<typename SetType::value_type>());
}
template <typename Value, typename Converter>
Set<Value> jsonToSet(Json const& v, Converter&& valueConvert) {
template <typename SetType, typename Converter>
SetType jsonToSet(Json const& v, Converter&& valueConvert) {
if (v.type() != Json::Type::Array)
throw JsonException("Json type is not an array in jsonToSet");
Set<Value> res;
SetType res;
for (auto const& entry : v.iterateArray())
res.add(valueConvert(entry));
return res;
}
template <typename Value>
Json jsonFromSet(Set<Value> const& Set) {
return jsonFromSet<Value>(Set, construct<Json>());
template <typename SetType>
Json jsonFromSet(SetType const& Set) {
return jsonFromSet<SetType>(Set, construct<Json>());
}
template <typename Value, typename Converter>
Json jsonFromSet(Set<Value> const& Set, Converter&& valueConvert) {
template <typename SetType, typename Converter>
Json jsonFromSet(SetType const& Set, Converter&& valueConvert) {
JsonArray res;
for (auto entry : Set)
for (auto& entry : Set)
res.push_back(valueConvert(entry));
return res;

View File

@ -0,0 +1,7 @@
#include "StarNetCompatibility.hpp"
namespace Star {
VersionNumber const OpenProtocolVersion = 2;
}

View File

@ -0,0 +1,55 @@
#pragma once
#include "StarVersion.hpp"
#include "StarHash.hpp"
namespace Star {
extern VersionNumber const OpenProtocolVersion;
constexpr VersionNumber AnyVersion = 0xFFFFFFFF;
constexpr VersionNumber LegacyVersion = 0;
class NetCompatibilityRules {
public:
NetCompatibilityRules();
NetCompatibilityRules(uint64_t) = delete;
NetCompatibilityRules(VersionNumber version);
VersionNumber version() const;
void setVersion(VersionNumber version);
bool isLegacy() const;
bool operator==(NetCompatibilityRules const& a) const;
private:
VersionNumber m_version = OpenProtocolVersion;
};
inline NetCompatibilityRules::NetCompatibilityRules() : m_version(OpenProtocolVersion) {}
inline NetCompatibilityRules::NetCompatibilityRules(VersionNumber v) : m_version(v) {}
inline VersionNumber NetCompatibilityRules::version() const {
return m_version;
}
inline void NetCompatibilityRules::setVersion(VersionNumber version) {
m_version = version;
}
inline bool NetCompatibilityRules::isLegacy() const {
return m_version == LegacyVersion;
}
inline bool NetCompatibilityRules::operator==(NetCompatibilityRules const& a) const {
return m_version == a.m_version;
}
template <>
struct hash<NetCompatibilityRules> {
size_t operator()(NetCompatibilityRules const& s) const {
return s.version();
}
};
}

View File

@ -7,8 +7,8 @@ uint64_t NetElementVersion::current() const {
return m_version;
}
void NetElementVersion::increment() {
++m_version;
uint64_t NetElementVersion::increment() {
return ++m_version;
}
void NetElement::enableNetInterpolation(float) {}

View File

@ -9,7 +9,7 @@ namespace Star {
class NetElementVersion {
public:
uint64_t current() const;
void increment();
uint64_t increment();
private:
uint64_t m_version = 0;
@ -20,15 +20,15 @@ class NetElement {
public:
virtual ~NetElement() = default;
// A network of NetElements will have a shared monotinically increasing
// A network of NetElements will have a shared monotonically increasing
// NetElementVersion. When elements are updated, they will mark the version
// number at the time they are updated so that a delta can be constructed
// that contains only changes since any past version.
virtual void initNetVersion(NetElementVersion const* version = nullptr) = 0;
// Full store / load of the entire element.
virtual void netStore(DataStream& ds) const = 0;
virtual void netLoad(DataStream& ds) = 0;
virtual void netStore(DataStream& ds, NetCompatibilityRules rules) const = 0;
virtual void netLoad(DataStream& ds, NetCompatibilityRules rules) = 0;
// Enables interpolation mode. If interpolation mode is enabled, then
// NetElements will delay presenting incoming delta data for the
@ -46,14 +46,34 @@ public:
// the version at the time of the *last* call to writeDelta, + 1. If
// fromVersion is 0, this will always write the full state. Should return
// true if a delta was needed and was written to DataStream, false otherwise.
virtual bool writeNetDelta(DataStream& ds, uint64_t fromVersion) const = 0;
virtual bool writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const = 0;
// Read a delta written by writeNetDelta. 'interpolationTime' is the time in
// the future that data from this delta should be delayed and smoothed into,
// if interpolation is enabled.
virtual void readNetDelta(DataStream& ds, float interpolationTime = 0.0) = 0;
virtual void readNetDelta(DataStream& ds, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) = 0;
// When extrapolating, it is important to notify when a delta WOULD have been
// received even if no deltas are produced, so no extrapolation takes place.
virtual void blankNetDelta(float interpolationTime);
VersionNumber compatibilityVersion() const;
void setCompatibilityVersion(VersionNumber version);
bool checkWithRules(NetCompatibilityRules const& rules) const;
private:
VersionNumber m_netCompatibilityVersion = AnyVersion;
};
inline VersionNumber NetElement::compatibilityVersion() const {
return m_netCompatibilityVersion;
}
inline void NetElement::setCompatibilityVersion(VersionNumber version) {
m_netCompatibilityVersion = version;
}
inline bool NetElement::checkWithRules(NetCompatibilityRules const& rules) const {
if (m_netCompatibilityVersion != AnyVersion)
return rules.version() >= m_netCompatibilityVersion;
return true;
}
}

View File

@ -49,8 +49,8 @@ void NetElementEvent::setIgnoreOccurrencesOnNetLoad(bool ignoreOccurrencesOnNetL
m_ignoreOccurrencesOnNetLoad = ignoreOccurrencesOnNetLoad;
}
void NetElementEvent::netLoad(DataStream& ds) {
NetElementUInt::netLoad(ds);
void NetElementEvent::netLoad(DataStream& ds, NetCompatibilityRules rules) {
NetElementUInt::netLoad(ds, rules);
if (m_ignoreOccurrencesOnNetLoad)
ignoreOccurrences();
}

View File

@ -38,11 +38,11 @@ public:
void disableNetInterpolation() override;
void tickNetInterpolation(float dt) override;
void netStore(DataStream& ds) const override;
void netLoad(DataStream& ds) override;
void netStore(DataStream& ds, NetCompatibilityRules rules = {}) const override;
void netLoad(DataStream& ds, NetCompatibilityRules rules) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules = {}) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
protected:
virtual void readData(DataStream& ds, T& t) const = 0;
@ -107,7 +107,7 @@ public:
void ignoreOccurrences();
void setIgnoreOccurrencesOnNetLoad(bool ignoreOccurrencesOnNetLoad);
void netLoad(DataStream& ds) override;
void netLoad(DataStream& ds, NetCompatibilityRules rules) override;
protected:
void updated() override;
@ -211,7 +211,8 @@ void NetElementBasicField<T>::tickNetInterpolation(float dt) {
}
template <typename T>
void NetElementBasicField<T>::netStore(DataStream& ds) const {
void NetElementBasicField<T>::netStore(DataStream& ds, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return;
if (m_pendingInterpolatedValues && !m_pendingInterpolatedValues->empty())
writeData(ds, m_pendingInterpolatedValues->last().second);
else
@ -219,7 +220,8 @@ void NetElementBasicField<T>::netStore(DataStream& ds) const {
}
template <typename T>
void NetElementBasicField<T>::netLoad(DataStream& ds) {
void NetElementBasicField<T>::netLoad(DataStream& ds, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
readData(ds, m_value);
m_latestUpdateVersion = m_netVersion ? m_netVersion->current() : 0;
updated();
@ -228,7 +230,8 @@ void NetElementBasicField<T>::netLoad(DataStream& ds) {
}
template <typename T>
bool NetElementBasicField<T>::writeNetDelta(DataStream& ds, uint64_t fromVersion) const {
bool NetElementBasicField<T>::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return false;
if (m_latestUpdateVersion < fromVersion)
return false;
@ -236,11 +239,13 @@ bool NetElementBasicField<T>::writeNetDelta(DataStream& ds, uint64_t fromVersion
writeData(ds, m_pendingInterpolatedValues->last().second);
else
writeData(ds, m_value);
return true;
}
template <typename T>
void NetElementBasicField<T>::readNetDelta(DataStream& ds, float interpolationTime) {
void NetElementBasicField<T>::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
T t;
readData(ds, t);
m_latestUpdateVersion = m_netVersion ? m_netVersion->current() : 0;

View File

@ -26,11 +26,12 @@ public:
void disableNetInterpolation() override;
void tickNetInterpolation(float dt) override;
void netStore(DataStream& ds) const override;
void netLoad(DataStream& ds) override;
void netStore(DataStream& ds, NetCompatibilityRules rules = {}) const override;
void netLoad(DataStream& ds, NetCompatibilityRules rules) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f) override;
bool shouldWriteNetDelta(uint64_t fromVersion, NetCompatibilityRules rules = {}) const;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules = {}) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
mapped_type const& get(key_type const& key) const;
mapped_type const* ptr(key_type const& key) const;
@ -77,6 +78,8 @@ public:
template <typename MapType>
void setContents(MapType const& values);
uint64_t changeDataLastVersion() const;
private:
// If a delta is written from further back than this many steps, the delta
// will fall back to a full serialization of the entire state.
@ -152,7 +155,8 @@ void NetElementMapWrapper<BaseMap>::tickNetInterpolation(float dt) {
}
template <typename BaseMap>
void NetElementMapWrapper<BaseMap>::netStore(DataStream& ds) const {
void NetElementMapWrapper<BaseMap>::netStore(DataStream& ds, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return;
ds.writeVlqU(BaseMap::size() + m_pendingChangeData.size());
for (auto const& pair : *this)
writeChange(ds, SetChange{pair.first, pair.second});
@ -162,7 +166,8 @@ void NetElementMapWrapper<BaseMap>::netStore(DataStream& ds) const {
}
template <typename BaseMap>
void NetElementMapWrapper<BaseMap>::netLoad(DataStream& ds) {
void NetElementMapWrapper<BaseMap>::netLoad(DataStream& ds, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
m_changeData.clear();
m_changeDataLastVersion = m_netVersion ? m_netVersion->current() : 0;
m_pendingChangeData.clear();
@ -181,13 +186,27 @@ void NetElementMapWrapper<BaseMap>::netLoad(DataStream& ds) {
}
template <typename BaseMap>
bool NetElementMapWrapper<BaseMap>::writeNetDelta(DataStream& ds, uint64_t fromVersion) const {
bool NetElementMapWrapper<BaseMap>::shouldWriteNetDelta(uint64_t fromVersion, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return false;
if (fromVersion < m_changeDataLastVersion)
return true;
for (auto const& p : m_changeData)
if (p.first >= fromVersion)
return true;
return false;
}
template <typename BaseMap>
bool NetElementMapWrapper<BaseMap>::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return false;
bool deltaWritten = false;
if (fromVersion < m_changeDataLastVersion) {
deltaWritten = true;
ds.writeVlqU(1);
netStore(ds);
netStore(ds, rules);
} else {
for (auto const& p : m_changeData) {
@ -206,13 +225,14 @@ bool NetElementMapWrapper<BaseMap>::writeNetDelta(DataStream& ds, uint64_t fromV
}
template <typename BaseMap>
void NetElementMapWrapper<BaseMap>::readNetDelta(DataStream& ds, float interpolationTime) {
void NetElementMapWrapper<BaseMap>::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
while (true) {
uint64_t code = ds.readVlqU();
if (code == 0) {
break;
} else if (code == 1) {
netLoad(ds);
netLoad(ds, rules);
} else if (code == 2) {
auto change = readChange(ds);
addChangeData(change);
@ -381,6 +401,11 @@ void NetElementMapWrapper<BaseMap>::setContents(MapType const& values) {
reset(BaseMap::from(values));
}
template <typename BaseMap>
uint64_t NetElementMapWrapper<BaseMap>::changeDataLastVersion() const {
return m_changeDataLastVersion;
}
template <typename BaseMap>
void NetElementMapWrapper<BaseMap>::writeChange(DataStream& ds, ElementChange const& change) {
if (auto sc = change.template ptr<SetChange>()) {

View File

@ -45,11 +45,11 @@ public:
void disableNetInterpolation() override;
void tickNetInterpolation(float dt) override;
void netStore(DataStream& ds) const override;
void netLoad(DataStream& ds) override;
void netStore(DataStream& ds, NetCompatibilityRules rules = {}) const override;
void netLoad(DataStream& ds, NetCompatibilityRules rules) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules = {}) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void blankNetDelta(float interpolationTime = 0.0f) override;
private:
@ -89,7 +89,7 @@ template <typename Element>
auto NetElementDynamicGroup<Element>::addNetElement(ElementPtr element) -> ElementId {
readyElement(element);
DataStreamBuffer storeBuffer;
element->netStore(storeBuffer);
element->netStore(storeBuffer, {});
auto id = m_idMap.add(std::move(element));
addChangeData(ElementAddition(id, storeBuffer.takeData()));
@ -134,7 +134,7 @@ void NetElementDynamicGroup<Element>::initNetVersion(NetElementVersion const* ve
for (auto& pair : m_idMap) {
pair.second->initNetVersion(m_netVersion);
DataStreamBuffer storeBuffer;
pair.second->netStore(storeBuffer);
pair.second->netStore(storeBuffer, {});
addChangeData(ElementAddition(pair.first, storeBuffer.takeData()));
}
}
@ -162,19 +162,22 @@ void NetElementDynamicGroup<Element>::tickNetInterpolation(float dt) {
}
template <typename Element>
void NetElementDynamicGroup<Element>::netStore(DataStream& ds) const {
void NetElementDynamicGroup<Element>::netStore(DataStream& ds, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return;
ds.writeVlqU(m_idMap.size());
m_buffer.setStreamCompatibilityVersion(rules);
for (auto& pair : m_idMap) {
ds.writeVlqU(pair.first);
pair.second->netStore(m_buffer);
pair.second->netStore(m_buffer, rules);
ds.write(m_buffer.data());
m_buffer.clear();
}
}
template <typename Element>
void NetElementDynamicGroup<Element>::netLoad(DataStream& ds) {
void NetElementDynamicGroup<Element>::netLoad(DataStream& ds, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
m_changeData.clear();
m_changeDataLastVersion = m_netVersion ? m_netVersion->current() : 0;
m_idMap.clear();
@ -188,7 +191,7 @@ void NetElementDynamicGroup<Element>::netLoad(DataStream& ds) {
DataStreamBuffer storeBuffer(ds.read<ByteArray>());
ElementPtr element = make_shared<Element>();
element->netLoad(storeBuffer);
element->netLoad(storeBuffer, rules);
readyElement(element);
m_idMap.add(id, std::move(element));
@ -197,10 +200,11 @@ void NetElementDynamicGroup<Element>::netLoad(DataStream& ds) {
}
template <typename Element>
bool NetElementDynamicGroup<Element>::writeNetDelta(DataStream& ds, uint64_t fromVersion) const {
bool NetElementDynamicGroup<Element>::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return false;
if (fromVersion < m_changeDataLastVersion) {
ds.write<bool>(true);
netStore(ds);
netStore(ds, rules);
return true;
} else {
@ -220,8 +224,9 @@ bool NetElementDynamicGroup<Element>::writeNetDelta(DataStream& ds, uint64_t fro
}
}
m_buffer.setStreamCompatibilityVersion(rules);
for (auto& p : m_idMap) {
if (p.second->writeNetDelta(m_buffer, fromVersion)) {
if (p.second->writeNetDelta(m_buffer, fromVersion, rules)) {
willWrite();
ds.writeVlqU(p.first + 1);
ds.writeBytes(m_buffer.data());
@ -237,10 +242,11 @@ bool NetElementDynamicGroup<Element>::writeNetDelta(DataStream& ds, uint64_t fro
}
template <typename Element>
void NetElementDynamicGroup<Element>::readNetDelta(DataStream& ds, float interpolationTime) {
void NetElementDynamicGroup<Element>::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
bool isFull = ds.read<bool>();
if (isFull) {
netLoad(ds);
netLoad(ds, rules);
} else {
while (true) {
uint64_t code = ds.readVlqU();
@ -256,7 +262,7 @@ void NetElementDynamicGroup<Element>::readNetDelta(DataStream& ds, float interpo
} else if (auto addition = changeUpdate.template ptr<ElementAddition>()) {
ElementPtr element = make_shared<Element>();
DataStreamBuffer storeBuffer(std::move(get<1>(*addition)));
element->netLoad(storeBuffer);
element->netLoad(storeBuffer, rules);
readyElement(element);
m_idMap.add(get<0>(*addition), std::move(element));
} else if (auto removal = changeUpdate.template ptr<ElementRemoval>()) {
@ -265,7 +271,7 @@ void NetElementDynamicGroup<Element>::readNetDelta(DataStream& ds, float interpo
} else {
ElementId elementId = code - 1;
auto const& element = m_idMap.get(elementId);
element->readNetDelta(ds, interpolationTime);
element->readNetDelta(ds, interpolationTime, rules);
if (m_interpolationEnabled)
m_receivedDeltaIds.add(elementId);
}

View File

@ -0,0 +1,85 @@
#pragma once
#include "StarNetElement.hpp"
namespace Star {
template <typename BaseNetElement>
class NetElementOverride : public BaseNetElement {
public:
void netStore(DataStream& ds, NetCompatibilityRules rules = {}) const override;
void netLoad(DataStream& ds, NetCompatibilityRules rules) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules = {}) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
typedef std::function<void(DataStream&, NetCompatibilityRules)> NetStorer;
typedef std::function<void(DataStream&, NetCompatibilityRules)> NetLoader;
typedef std::function<bool(DataStream&, uint64_t, NetCompatibilityRules)> NetDeltaWriter;
typedef std::function<void(DataStream&, float, NetCompatibilityRules)> NetDeltaReader;
void setNetStorer(NetStorer);
void setNetLoader(NetLoader);
void setNetDeltaWriter(NetDeltaWriter);
void setNetDeltaReader(NetDeltaReader);
void setOverrides(NetStorer netStorer, NetLoader netLoader,
NetDeltaWriter netDeltaWriter, NetDeltaReader netDeltaReader);
private:
NetStorer m_netStorer;
NetLoader m_netLoader;
NetDeltaWriter m_netDeltaWriter;
NetDeltaReader m_netDeltaReader;
};
template <typename BaseNetElement>
void NetElementOverride<BaseNetElement>::netStore(DataStream& ds, NetCompatibilityRules rules) const {
if (m_netStorer)
m_netStorer(ds, rules);
else
BaseNetElement::netStore(ds, rules);
}
template <typename BaseNetElement>
void NetElementOverride<BaseNetElement>::netLoad(DataStream& ds, NetCompatibilityRules rules) {
if (m_netLoader)
m_netLoader(ds, rules);
else
BaseNetElement::netLoad(ds, rules);
}
template <typename BaseNetElement>
bool NetElementOverride<BaseNetElement>::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
if (m_netDeltaWriter)
return m_netDeltaWriter(ds, fromVersion, rules);
else
return BaseNetElement::writeNetDelta(ds, fromVersion, rules);
}
template <typename BaseNetElement>
void NetElementOverride<BaseNetElement>::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
if (m_netDeltaReader)
m_netDeltaReader(ds, interpolationTime, rules);
else
BaseNetElement::readNetDelta(ds, interpolationTime, rules);
}
template <typename BaseNetElement>
inline void NetElementOverride<BaseNetElement>::setNetStorer(NetStorer f) { m_netStorer = std::move(f); }
template <typename BaseNetElement>
inline void NetElementOverride<BaseNetElement>::setNetLoader(NetLoader f) { m_netLoader = std::move(f); }
template <typename BaseNetElement>
inline void NetElementOverride<BaseNetElement>::setNetDeltaWriter(NetDeltaWriter f) { m_netDeltaWriter = std::move(f); }
template <typename BaseNetElement>
inline void NetElementOverride<BaseNetElement>::setNetDeltaReader(NetDeltaReader f) { m_netDeltaReader = std::move(f); }
template <typename BaseNetElement>
inline void NetElementOverride<BaseNetElement>::setOverrides(NetStorer netStorer, NetLoader netLoader, NetDeltaWriter netDeltaWriter, NetDeltaReader netDeltaReader) {
m_netStorer = std::move(netStorer);
m_netLoader = std::move(netLoader);
m_netDeltaWriter = std::move(netDeltaWriter);
m_netDeltaReader = std::move(netDeltaReader);
}
}

View File

@ -36,11 +36,11 @@ public:
void disableNetInterpolation() override;
void tickNetInterpolation(float dt) override;
void netStore(DataStream& ds) const override;
void netLoad(DataStream& ds) override;
void netStore(DataStream& ds, NetCompatibilityRules rules = {}) const override;
void netLoad(DataStream& ds, NetCompatibilityRules rules) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules = {}) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void blankNetDelta(float interpolationTime = 0.0f) override;
private:
@ -131,7 +131,8 @@ void NetElementFloating<T>::tickNetInterpolation(float dt) {
}
template <typename T>
void NetElementFloating<T>::netStore(DataStream& ds) const {
void NetElementFloating<T>::netStore(DataStream& ds, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return;
if (m_interpolationDataPoints)
writeValue(ds, m_interpolationDataPoints->last().second);
else
@ -139,7 +140,8 @@ void NetElementFloating<T>::netStore(DataStream& ds) const {
}
template <typename T>
void NetElementFloating<T>::netLoad(DataStream& ds) {
void NetElementFloating<T>::netLoad(DataStream& ds, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
m_value = readValue(ds);
m_latestUpdateVersion = m_netVersion ? m_netVersion->current() : 0;
if (m_interpolationDataPoints) {
@ -149,7 +151,8 @@ void NetElementFloating<T>::netLoad(DataStream& ds) {
}
template <typename T>
bool NetElementFloating<T>::writeNetDelta(DataStream& ds, uint64_t fromVersion) const {
bool NetElementFloating<T>::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return false;
if (m_latestUpdateVersion < fromVersion)
return false;
@ -162,7 +165,7 @@ bool NetElementFloating<T>::writeNetDelta(DataStream& ds, uint64_t fromVersion)
}
template <typename T>
void NetElementFloating<T>::readNetDelta(DataStream& ds, float interpolationTime) {
void NetElementFloating<T>::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
T t = readValue(ds);
m_latestUpdateVersion = m_netVersion ? m_netVersion->current() : 0;

View File

@ -21,14 +21,18 @@ void NetElementGroup::initNetVersion(NetElementVersion const* version) {
p.first->initNetVersion(m_version);
}
void NetElementGroup::netStore(DataStream& ds) const {
void NetElementGroup::netStore(DataStream& ds, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return;
for (auto& p : m_elements)
p.first->netStore(ds);
if (p.first->checkWithRules(rules))
p.first->netStore(ds, rules);
}
void NetElementGroup::netLoad(DataStream& ds) {
void NetElementGroup::netLoad(DataStream& ds, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
for (auto& p : m_elements)
p.first->netLoad(ds);
if (p.first->checkWithRules(rules))
p.first->netLoad(ds, rules);
}
void NetElementGroup::enableNetInterpolation(float extrapolationHint) {
@ -56,17 +60,23 @@ void NetElementGroup::tickNetInterpolation(float dt) {
}
}
bool NetElementGroup::writeNetDelta(DataStream& ds, uint64_t fromStep) const {
bool NetElementGroup::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return false;
if (m_elements.size() == 0) {
return false;
} else if (m_elements.size() == 1) {
return m_elements[0].first->writeNetDelta(ds, fromStep);
return m_elements[0].first->writeNetDelta(ds, fromVersion, rules);
} else {
bool deltaWritten = false;
for (uint64_t i = 0; i < m_elements.size(); ++i) {
if (m_elements[i].first->writeNetDelta(m_buffer, fromStep)) {
uint64_t i = 0;
m_buffer.setStreamCompatibilityVersion(rules);
for (auto& element : m_elements) {
if (!element.first->checkWithRules(rules))
continue;
++i;
if (element.first->writeNetDelta(m_buffer, fromVersion, rules)) {
deltaWritten = true;
ds.writeVlqU(i + 1);
ds.writeVlqU(i);
ds.writeBytes(m_buffer.data());
m_buffer.clear();
}
@ -77,23 +87,28 @@ bool NetElementGroup::writeNetDelta(DataStream& ds, uint64_t fromStep) const {
}
}
void NetElementGroup::readNetDelta(DataStream& ds, float interpolationTime) {
void NetElementGroup::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
if (m_elements.size() == 0) {
throw IOException("readNetDelta called on empty NetElementGroup");
} else if (m_elements.size() == 1) {
m_elements[0].first->readNetDelta(ds, interpolationTime);
m_elements[0].first->readNetDelta(ds, interpolationTime, rules);
} else {
uint64_t readIndex = ds.readVlqU();
for (uint64_t i = 0; i < m_elements.size(); ++i) {
uint64_t i = 0;
for (auto& element : m_elements) {
if (!element.first->checkWithRules(rules))
continue;
if (readIndex == 0 || readIndex - 1 > i) {
if (m_interpolationEnabled)
m_elements[i].first->blankNetDelta(interpolationTime);
} else if (readIndex - 1 == i) {
m_elements[i].first->readNetDelta(ds, interpolationTime);
m_elements[i].first->readNetDelta(ds, interpolationTime, rules);
readIndex = ds.readVlqU();
} else {
throw IOException("group indexes out of order in NetElementGroup::readNetDelta");
}
++i;
}
}
}

View File

@ -24,15 +24,15 @@ public:
void initNetVersion(NetElementVersion const* version = nullptr) override;
void netStore(DataStream& ds) const override;
void netLoad(DataStream& ds) override;
void netStore(DataStream& ds, NetCompatibilityRules rules = {}) const override;
void netLoad(DataStream& ds, NetCompatibilityRules rules) override;
void enableNetInterpolation(float extrapolationHint = 0.0f) override;
void disableNetInterpolation() override;
void tickNetInterpolation(float dt) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules = {}) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void blankNetDelta(float interpolationTime) override;
NetElementVersion const* netVersion() const;

View File

@ -16,15 +16,15 @@ public:
void initNetVersion(NetElementVersion const* version = nullptr) override;
void netStore(DataStream& ds) const override;
void netLoad(DataStream& ds) override;
void netStore(DataStream& ds, NetCompatibilityRules rules = {}) const override;
void netLoad(DataStream& ds, NetCompatibilityRules rules) override;
void enableNetInterpolation(float extrapolationHint = 0.0f) override;
void disableNetInterpolation() override;
void tickNetInterpolation(float dt) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules = {}) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void send(Signal signal);
List<Signal> receive();
@ -55,10 +55,10 @@ void NetElementSignal<Signal>::initNetVersion(NetElementVersion const* version)
}
template <typename Signal>
void NetElementSignal<Signal>::netStore(DataStream&) const {}
void NetElementSignal<Signal>::netStore(DataStream&, NetCompatibilityRules) const {}
template <typename Signal>
void NetElementSignal<Signal>::netLoad(DataStream&) {
void NetElementSignal<Signal>::netLoad(DataStream&, NetCompatibilityRules) {
}
template <typename Signal>
@ -83,7 +83,8 @@ void NetElementSignal<Signal>::tickNetInterpolation(float dt) {
}
template <typename Signal>
bool NetElementSignal<Signal>::writeNetDelta(DataStream& ds, uint64_t fromVersion) const {
bool NetElementSignal<Signal>::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return false;
size_t numToWrite = 0;
for (auto const& p : m_signals) {
if (p.version >= fromVersion)
@ -103,7 +104,8 @@ bool NetElementSignal<Signal>::writeNetDelta(DataStream& ds, uint64_t fromVersio
}
template <typename Signal>
void NetElementSignal<Signal>::readNetDelta(DataStream& ds, float interpolationTime) {
void NetElementSignal<Signal>::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
size_t numToRead = ds.readVlqU();
for (size_t i = 0; i < numToRead; ++i) {
Signal s;

View File

@ -26,23 +26,27 @@ void NetElementSyncGroup::tickNetInterpolation(float dt) {
}
}
void NetElementSyncGroup::netStore(DataStream& ds) const {
void NetElementSyncGroup::netStore(DataStream& ds, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return;
const_cast<NetElementSyncGroup*>(this)->netElementsNeedStore();
return NetElementGroup::netStore(ds);
return NetElementGroup::netStore(ds, rules);
}
void NetElementSyncGroup::netLoad(DataStream& ds) {
NetElementGroup::netLoad(ds);
void NetElementSyncGroup::netLoad(DataStream& ds, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
NetElementGroup::netLoad(ds, rules);
netElementsNeedLoad(true);
}
bool NetElementSyncGroup::writeNetDelta(DataStream& ds, uint64_t fromVersion) const {
bool NetElementSyncGroup::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return false;
const_cast<NetElementSyncGroup*>(this)->netElementsNeedStore();
return NetElementGroup::writeNetDelta(ds, fromVersion);
return NetElementGroup::writeNetDelta(ds, fromVersion, rules);
}
void NetElementSyncGroup::readNetDelta(DataStream& ds, float interpolationTime) {
NetElementGroup::readNetDelta(ds, interpolationTime);
void NetElementSyncGroup::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
NetElementGroup::readNetDelta(ds, interpolationTime, rules);
m_hasRecentChanges = true;
m_recentDeltaTime = interpolationTime;

View File

@ -13,11 +13,11 @@ public:
void disableNetInterpolation() override;
void tickNetInterpolation(float dt) override;
void netStore(DataStream& ds) const override;
void netLoad(DataStream& ds) override;
void netStore(DataStream& ds, NetCompatibilityRules rules = {}) const override;
void netLoad(DataStream& ds, NetCompatibilityRules rules) override;
bool writeNetDelta(DataStream& ds, uint64_t fromStep) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules = {}) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void blankNetDelta(float interpolationTime = 0.0f) override;
protected:

View File

@ -1,5 +1,4 @@
#ifndef STAR_NET_ELEMENT_TOP_HPP
#define STAR_NET_ELEMENT_TOP_HPP
#pragma once
#include "StarNetElement.hpp"
@ -12,10 +11,11 @@ class NetElementTop : public BaseNetElement {
public:
NetElementTop();
// Returns the state update, combined with the version code that should be
// passed to the next call to writeState. If 'fromVersion' is 0, then this
// is a full write for an initial read of a slave NetElementTop.
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0);
// Writes the state update to the given DataStream then returns the version
// code that should be passed to the next call to writeState. If
// 'fromVersion' is 0, then this is a full write for an initial read of a
// slave NetElementTop.
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0, NetCompatibilityRules rules = {});
// Reads a state produced by a call to writeState, optionally with the
// interpolation delay time for the data contained in this state update. If
// the state is a full update rather than a delta, the interoplation delay
@ -23,7 +23,7 @@ public:
// readState, *unless* extrapolation is enabled. If extrapolation is
// enabled, reading a blank update calls 'blankNetDelta' which is necessary
// to not improperly extrapolate past the end of incoming deltas.
void readNetState(ByteArray data, float interpolationTime = 0.0);
void readNetState(ByteArray data, float interpolationTime = 0.0f, NetCompatibilityRules rules = {});
private:
using BaseNetElement::initNetVersion;
@ -32,6 +32,7 @@ private:
using BaseNetElement::writeNetDelta;
using BaseNetElement::readNetDelta;
using BaseNetElement::blankNetDelta;
using BaseNetElement::checkWithRules;
NetElementVersion m_netVersion;
};
@ -42,41 +43,34 @@ NetElementTop<BaseNetElement>::NetElementTop() {
}
template <typename BaseNetElement>
pair<ByteArray, uint64_t> NetElementTop<BaseNetElement>::writeNetState(uint64_t fromVersion) {
pair<ByteArray, uint64_t> NetElementTop<BaseNetElement>::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
DataStreamBuffer ds;
ds.setStreamCompatibilityVersion(rules);
if (fromVersion == 0) {
DataStreamBuffer ds;
ds.write<bool>(true);
BaseNetElement::netStore(ds);
m_netVersion.increment();
return {ds.takeData(), m_netVersion.current()};
BaseNetElement::netStore(ds, rules);
return {ds.takeData(), m_netVersion.increment()};
} else {
DataStreamBuffer ds;
ds.write<bool>(false);
if (!BaseNetElement::writeNetDelta(ds, fromVersion)) {
if (!BaseNetElement::writeNetDelta(ds, fromVersion, rules))
return {ByteArray(), m_netVersion.current()};
} else {
m_netVersion.increment();
return {ds.takeData(), m_netVersion.current()};
}
else
return {ds.takeData(), m_netVersion.increment()};
}
}
template <typename BaseNetElement>
void NetElementTop<BaseNetElement>::readNetState(ByteArray data, float interpolationTime) {
void NetElementTop<BaseNetElement>::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
if (data.empty()) {
BaseNetElement::blankNetDelta(interpolationTime);
} else {
DataStreamBuffer ds(std::move(data));
ds.setStreamCompatibilityVersion(rules);
if (ds.read<bool>())
BaseNetElement::netLoad(ds);
BaseNetElement::netLoad(ds, rules);
else
BaseNetElement::readNetDelta(ds, interpolationTime);
BaseNetElement::readNetDelta(ds, interpolationTime, rules);
}
}
}
#endif

View File

@ -1,5 +1,4 @@
#ifndef STAR_TEAMBAR_HPP
#define STAR_TEAMBAR_HPP
#pragma once
#include "StarPane.hpp"
#include "StarUuid.hpp"
@ -114,5 +113,3 @@ private:
};
}
#endif

View File

@ -350,13 +350,13 @@ void ArmorWearer::netElementsNeedLoad(bool) {
m_backNeedsSync |= itemDatabase->loadItem(m_backCosmeticItemDataNetState.get(), m_backCosmeticItem);
if (m_headItemDataNetState.pullUpdated())
m_headNeedsSync |= !m_headCosmeticItem && itemDatabase->loadItem(m_headItemDataNetState.get(), m_headItem);
m_headNeedsSync |= !itemDatabase->loadItem(m_headItemDataNetState.get(), m_headItem);
if (m_chestItemDataNetState.pullUpdated())
m_chestNeedsSync |= !m_chestCosmeticItem && itemDatabase->loadItem(m_chestItemDataNetState.get(), m_chestItem);
m_chestNeedsSync |= itemDatabase->loadItem(m_chestItemDataNetState.get(), m_chestItem);
if (m_legsItemDataNetState.pullUpdated())
m_legsNeedsSync |= !m_legsCosmeticItem && itemDatabase->loadItem(m_legsItemDataNetState.get(), m_legsItem);
m_legsNeedsSync |= !itemDatabase->loadItem(m_legsItemDataNetState.get(), m_legsItem);
if (m_backItemDataNetState.pullUpdated())
m_backNeedsSync |= !m_backCosmeticItem && itemDatabase->loadItem(m_backItemDataNetState.get(), m_backItem);
m_backNeedsSync |= itemDatabase->loadItem(m_backItemDataNetState.get(), m_backItem);
}
void ArmorWearer::netElementsNeedStore() {

View File

@ -77,11 +77,12 @@ ShipUpgrades ClientContext::shipUpgrades() const {
return m_shipUpgrades.get();
}
void ClientContext::readUpdate(ByteArray data) {
void ClientContext::readUpdate(ByteArray data, NetCompatibilityRules rules) {
if (data.empty())
return;
DataStreamBuffer ds(std::move(data));
ds.setStreamCompatibilityVersion(rules);
m_rpc->receive(ds.read<ByteArray>());
@ -89,10 +90,10 @@ void ClientContext::readUpdate(ByteArray data) {
if (!shipUpdates.empty())
m_newShipUpdates.merge(DataStreamBuffer::deserialize<WorldChunks>(std::move(shipUpdates)), true);
m_netGroup.readNetState(ds.read<ByteArray>());
m_netGroup.readNetState(ds.read<ByteArray>(), 0.0f, rules);
}
ByteArray ClientContext::writeUpdate() {
ByteArray ClientContext::writeUpdate(NetCompatibilityRules rules) {
return m_rpc->send();
}
@ -104,4 +105,12 @@ ConnectionId ClientContext::connectionId() const {
return m_connectionId;
}
void ClientContext::setNetCompatibilityRules(NetCompatibilityRules netCompatibilityRules) {
m_netCompatibilityRules = netCompatibilityRules;
}
NetCompatibilityRules ClientContext::netCompatibilityRules() const {
return m_netCompatibilityRules;
}
}

View File

@ -40,16 +40,20 @@ public:
WorldChunks newShipUpdates();
ShipUpgrades shipUpgrades() const;
void readUpdate(ByteArray data);
ByteArray writeUpdate();
void readUpdate(ByteArray data, NetCompatibilityRules rules);
ByteArray writeUpdate(NetCompatibilityRules rules);
void setConnectionId(ConnectionId connectionId);
ConnectionId connectionId() const;
void setNetCompatibilityRules(NetCompatibilityRules netCompatibilityRules);
NetCompatibilityRules netCompatibilityRules() const;
private:
Uuid m_serverUuid;
Uuid m_playerUuid;
ConnectionId m_connectionId = 0;
NetCompatibilityRules m_netCompatibilityRules;
JsonRpcPtr m_rpc;

View File

@ -262,16 +262,33 @@ String CommandProcessor::setTileProtection(ConnectionId connectionId, String con
return "Not enough arguments to /settileprotection. Use /settileprotection <dungeonId> <protected>";
try {
DungeonId dungeonId = lexicalCast<DungeonId>(arguments.at(0));
bool isProtected = lexicalCast<bool>(arguments.at(1));
bool done = m_universe->executeForClient(connectionId, [dungeonId, isProtected](WorldServer* world, PlayerPtr const&) {
world->setTileProtection(dungeonId, isProtected);
});
return done ? "" : "Failed to set block protection.";
bool isProtected = lexicalCast<bool>(arguments.takeLast());
List<DungeonId> dungeonIds;
for (auto& banana : arguments) {
auto slices = banana.split("..");
auto it = slices.begin();
DungeonId previous = 0;
while (it != slices.end()) {
DungeonId current = lexicalCast<DungeonId>(*it);
dungeonIds.append(current);
if (it++ != slices.begin() && previous != current) {
if (current < previous) swap(previous, current);
for (DungeonId id = previous + 1; id != current; ++id)
dungeonIds.append(id);
}
previous = current;
}
}
size_t changed = 0;
if (!m_universe->executeForClient(connectionId, [&](WorldServer* world, PlayerPtr const&) {
changed = world->setTileProtection(dungeonIds, isProtected);
})) {
return "Invalid client state";
}
String output = strf("{} {} dungeon IDs", isProtected ? "Protected" : "Unprotected", changed);
return changed < dungeonIds.size() ? strf("{} ({} unchanged)", output, dungeonIds.size() - changed) : output;
} catch (BadLexicalCast const&) {
return strf("Could not parse /settileprotection parameters. Use /settileprotection <dungeonId> <protected>", argumentString);
return strf("Could not parse /settileprotection parameters. Use /settileprotection <dungeonId...> <protected>", argumentString);
}
}

View File

@ -94,14 +94,14 @@ void EffectEmitter::render(RenderCallback* renderCallback) {
Json EffectEmitter::toJson() const {
return JsonObject{{"activeSources",
jsonFromSet<pair<String, String>>(m_activeSources.get(),
jsonFromSet<Set<pair<String, String>>>(m_activeSources.get(),
[](pair<String, String> const& entry) {
return JsonObject{{"position", entry.first}, {"source", entry.second}};
})}};
}
void EffectEmitter::fromJson(Json const& diskStore) {
m_activeSources.set(jsonToSet<pair<String, String>>(diskStore.get("activeSources"),
m_activeSources.set(jsonToSet<Set<pair<String, String>>>(diskStore.get("activeSources"),
[](Json const& v) {
return pair<String, String>{v.getString("position"), v.getString("source")};
}));

View File

@ -40,57 +40,57 @@ EntityFactory::EntityFactory() {
m_versioningDatabase = root.versioningDatabase();
}
ByteArray EntityFactory::netStoreEntity(EntityPtr const& entity) const {
ByteArray EntityFactory::netStoreEntity(EntityPtr const& entity, NetCompatibilityRules rules) const {
RecursiveMutexLocker locker(m_mutex);
if (auto player = as<Player>(entity)) {
return player->netStore();
return player->netStore(rules);
} else if (auto monster = as<Monster>(entity)) {
return monster->netStore();
return monster->netStore(rules);
} else if (auto object = as<Object>(entity)) {
return object->netStore();
return object->netStore(rules);
} else if (auto plant = as<Plant>(entity)) {
return plant->netStore();
return plant->netStore(rules);
} else if (auto plantDrop = as<PlantDrop>(entity)) {
return plantDrop->netStore();
return plantDrop->netStore(rules);
} else if (auto projectile = as<Projectile>(entity)) {
return projectile->netStore();
return projectile->netStore(rules);
} else if (auto itemDrop = as<ItemDrop>(entity)) {
return itemDrop->netStore();
return itemDrop->netStore(rules);
} else if (auto npc = as<Npc>(entity)) {
return npc->netStore();
return npc->netStore(rules);
} else if (auto stagehand = as<Stagehand>(entity)) {
return stagehand->netStore();
return stagehand->netStore(rules);
} else if (auto vehicle = as<Vehicle>(entity)) {
return m_vehicleDatabase->netStore(vehicle);
return m_vehicleDatabase->netStore(vehicle, rules);
} else {
throw EntityFactoryException::format("Don't know how to make net store for entity type '{}'", EntityTypeNames.getRight(entity->entityType()));
}
}
EntityPtr EntityFactory::netLoadEntity(EntityType type, ByteArray const& netStore) const {
EntityPtr EntityFactory::netLoadEntity(EntityType type, ByteArray const& netStore, NetCompatibilityRules rules) const {
RecursiveMutexLocker locker(m_mutex);
if (type == EntityType::Player) {
return m_playerFactory->netLoadPlayer(netStore);
return m_playerFactory->netLoadPlayer(netStore, rules);
} else if (type == EntityType::Monster) {
return m_monsterDatabase->netLoadMonster(netStore);
return m_monsterDatabase->netLoadMonster(netStore, rules);
} else if (type == EntityType::Object) {
return m_objectDatabase->netLoadObject(netStore);
return m_objectDatabase->netLoadObject(netStore, rules);
} else if (type == EntityType::Plant) {
return make_shared<Plant>(netStore);
return make_shared<Plant>(netStore, rules);
} else if (type == EntityType::PlantDrop) {
return make_shared<PlantDrop>(netStore);
return make_shared<PlantDrop>(netStore, rules);
} else if (type == EntityType::Projectile) {
return m_projectileDatabase->netLoadProjectile(netStore);
return m_projectileDatabase->netLoadProjectile(netStore, rules);
} else if (type == EntityType::ItemDrop) {
return make_shared<ItemDrop>(netStore);
return make_shared<ItemDrop>(netStore, rules);
} else if (type == EntityType::Npc) {
return m_npcDatabase->netLoadNpc(netStore);
return m_npcDatabase->netLoadNpc(netStore, rules);
} else if (type == EntityType::Stagehand) {
return make_shared<Stagehand>(netStore);
return make_shared<Stagehand>(netStore, rules);
} else if (type == EntityType::Vehicle) {
return m_vehicleDatabase->netLoad(netStore);
return m_vehicleDatabase->netLoad(netStore, rules);
} else {
throw EntityFactoryException::format("Don't know how to create entity type '{}' from net store", EntityTypeNames.getRight(type));
}

View File

@ -20,8 +20,8 @@ class EntityFactory {
public:
EntityFactory();
ByteArray netStoreEntity(EntityPtr const& entity) const;
EntityPtr netLoadEntity(EntityType type, ByteArray const& netStore) const;
ByteArray netStoreEntity(EntityPtr const& entity, NetCompatibilityRules rules = {}) const;
EntityPtr netLoadEntity(EntityType type, ByteArray const& netStore, NetCompatibilityRules rules = {}) const;
Json diskStoreEntity(EntityPtr const& entity) const;
EntityPtr diskLoadEntity(EntityType type, Json const& diskStore) const;

View File

@ -243,6 +243,49 @@ EnumMap<Humanoid::State> const Humanoid::StateNames{
};
Humanoid::Humanoid(Json const& config) {
loadConfig(config);
m_twoHanded = false;
m_primaryHand.holdingItem = false;
m_altHand.holdingItem = false;
m_movingBackwards = false;
m_altHand.angle = 0;
m_facingDirection = Direction::Left;
m_rotation = 0;
m_scale = Vec2F::filled(1.f);
m_drawVaporTrail = false;
m_state = State::Idle;
m_emoteState = HumanoidEmote::Idle;
m_dance = {};
m_primaryHand.angle = 0;
m_animationTimer = m_emoteAnimationTimer = m_danceTimer = 0.0f;
}
Humanoid::Humanoid(HumanoidIdentity const& identity)
: Humanoid(Root::singleton().speciesDatabase()->species(identity.species)->humanoidConfig()) {
setIdentity(identity);
}
void Humanoid::setIdentity(HumanoidIdentity const& identity) {
m_identity = identity;
m_headFrameset = getHeadFromIdentity();
m_bodyFrameset = getBodyFromIdentity();
m_emoteFrameset = getFacialEmotesFromIdentity();
m_hairFrameset = getHairFromIdentity();
m_facialHairFrameset = getFacialHairFromIdentity();
m_facialMaskFrameset = getFacialMaskFromIdentity();
m_backArmFrameset = getBackArmFromIdentity();
m_frontArmFrameset = getFrontArmFromIdentity();
m_vaporTrailFrameset = getVaporTrailFrameset();
}
HumanoidIdentity const& Humanoid::identity() const {
return m_identity;
}
void Humanoid::loadConfig(Json const& config) {
m_timing = HumanoidTiming(config.getObject("humanoidTiming"));
m_globalOffset = jsonToVec2F(config.get("globalOffset")) / TilePixels;
@ -271,10 +314,13 @@ Humanoid::Humanoid(Json const& config) {
m_armWalkSeq = jsonToIntList(config.get("armWalkSeq"));
m_armRunSeq = jsonToIntList(config.get("armRunSeq"));
m_walkBob.clear();
for (auto const& v : config.get("walkBob").toArray())
m_walkBob.append(v.toDouble() / TilePixels);
m_runBob.clear();
for (auto const& v : config.get("runBob").toArray())
m_runBob.append(v.toDouble() / TilePixels);
m_swimBob.clear();
for (auto const& v : config.get("swimBob").toArray())
m_swimBob.append(v.toDouble() / TilePixels);
@ -290,46 +336,6 @@ Humanoid::Humanoid(Json const& config) {
m_particleEmitters = config.get("particleEmitters");
m_defaultMovementParameters = config.get("movementParameters");
m_twoHanded = false;
m_primaryHand.holdingItem = false;
m_altHand.holdingItem = false;
m_movingBackwards = false;
m_altHand.angle = 0;
m_facingDirection = Direction::Left;
m_rotation = 0;
m_scale = Vec2F::filled(1.f);
m_drawVaporTrail = false;
m_state = State::Idle;
m_emoteState = HumanoidEmote::Idle;
m_dance = {};
m_emoteAnimationTimer = 0;
m_primaryHand.angle = 0;
m_animationTimer = 0.0f;
}
Humanoid::Humanoid(HumanoidIdentity const& identity)
: Humanoid(Root::singleton().speciesDatabase()->species(identity.species)->humanoidConfig()) {
setIdentity(identity);
}
void Humanoid::setIdentity(HumanoidIdentity const& identity) {
m_identity = identity;
m_headFrameset = getHeadFromIdentity();
m_bodyFrameset = getBodyFromIdentity();
m_emoteFrameset = getFacialEmotesFromIdentity();
m_hairFrameset = getHairFromIdentity();
m_facialHairFrameset = getFacialHairFromIdentity();
m_facialMaskFrameset = getFacialMaskFromIdentity();
m_backArmFrameset = getBackArmFromIdentity();
m_frontArmFrameset = getFrontArmFromIdentity();
m_vaporTrailFrameset = getVaporTrailFrameset();
}
HumanoidIdentity const& Humanoid::identity() const {
return m_identity;
}
void Humanoid::setHeadArmorDirectives(Directives directives) {

View File

@ -125,6 +125,8 @@ public:
void setIdentity(HumanoidIdentity const& identity);
HumanoidIdentity const& identity() const;
void loadConfig(Json const& config);
// All of the image identifiers here are meant to be image *base* names, with
// a collection of frames specific to each piece. If an image is set to
// empty string, it is disabled.

View File

@ -92,9 +92,9 @@ ItemDrop::ItemDrop(Json const& diskStore)
m_itemDescriptor.set(m_item->descriptor());
}
ItemDrop::ItemDrop(ByteArray store)
: ItemDrop() {
ItemDrop::ItemDrop(ByteArray store, NetCompatibilityRules rules) : ItemDrop() {
DataStreamBuffer ds(std::move(store));
ds.setStreamCompatibilityVersion(rules);
Root::singleton().itemDatabase()->loadItem(ds.read<ItemDescriptor>(), m_item);
ds.read(m_eternal);
@ -116,8 +116,9 @@ Json ItemDrop::diskStore() const {
};
}
ByteArray ItemDrop::netStore() const {
ByteArray ItemDrop::netStore(NetCompatibilityRules rules) const {
DataStreamBuffer ds;
ds.setStreamCompatibilityVersion(rules);
ds.write(itemSafeDescriptor(m_item));
ds.write(m_eternal);
@ -146,12 +147,12 @@ String ItemDrop::description() const {
return m_item->description();
}
pair<ByteArray, uint64_t> ItemDrop::writeNetState(uint64_t fromVersion) {
return m_netGroup.writeNetState(fromVersion);
pair<ByteArray, uint64_t> ItemDrop::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
return m_netGroup.writeNetState(fromVersion, rules);
}
void ItemDrop::readNetState(ByteArray data, float interpolationTime) {
m_netGroup.readNetState(std::move(data), interpolationTime);
void ItemDrop::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetState(data, interpolationTime, rules);
}
void ItemDrop::enableInterpolation(float extrapolationHint) {

View File

@ -27,10 +27,10 @@ public:
ItemDrop(ItemPtr item);
ItemDrop(Json const& diskStore);
ItemDrop(ByteArray netStore);
ItemDrop(ByteArray netStore, NetCompatibilityRules rules = {});
Json diskStore() const;
ByteArray netStore() const;
ByteArray netStore(NetCompatibilityRules rules = {}) const;
EntityType entityType() const override;
@ -39,8 +39,8 @@ public:
String description() const override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f) override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0, NetCompatibilityRules rules = {}) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void enableInterpolation(float extrapolationHint = 0.0f) override;
void disableInterpolation() override;

View File

@ -110,8 +110,8 @@ Json Monster::diskStore() const {
};
}
ByteArray Monster::netStore() {
return Root::singleton().monsterDatabase()->writeMonsterVariant(m_monsterVariant);
ByteArray Monster::netStore(NetCompatibilityRules rules) {
return Root::singleton().monsterDatabase()->writeMonsterVariant(m_monsterVariant, rules);
}
EntityType Monster::entityType() const {
@ -210,12 +210,12 @@ Vec2F Monster::velocity() const {
return m_movementController->velocity();
}
pair<ByteArray, uint64_t> Monster::writeNetState(uint64_t fromVersion) {
return m_netGroup.writeNetState(fromVersion);
pair<ByteArray, uint64_t> Monster::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
return m_netGroup.writeNetState(fromVersion, rules);
}
void Monster::readNetState(ByteArray data, float interpolationTime) {
m_netGroup.readNetState(std::move(data), interpolationTime);
void Monster::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetState(data, interpolationTime, rules);
}
void Monster::enableInterpolation(float extrapolationHint) {

View File

@ -42,7 +42,7 @@ public:
Monster(Json const& diskStore);
Json diskStore() const;
ByteArray netStore();
ByteArray netStore(NetCompatibilityRules rules = {});
EntityType entityType() const override;
ClientEntityMode clientEntityMode() const override;
@ -60,8 +60,8 @@ public:
RectF collisionArea() const override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f) override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0, NetCompatibilityRules rules = {}) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void enableInterpolation(float extrapolationHint) override;
void disableInterpolation() override;

View File

@ -170,8 +170,9 @@ MonsterVariant MonsterDatabase::monsterVariant(String const& typeName, uint64_t
});
}
ByteArray MonsterDatabase::writeMonsterVariant(MonsterVariant const& variant) const {
ByteArray MonsterDatabase::writeMonsterVariant(MonsterVariant const& variant, NetCompatibilityRules rules) const {
DataStreamBuffer ds;
ds.setStreamCompatibilityVersion(rules);
ds.write(variant.type);
ds.write(variant.seed);
@ -180,8 +181,9 @@ ByteArray MonsterDatabase::writeMonsterVariant(MonsterVariant const& variant) co
return ds.data();
}
MonsterVariant MonsterDatabase::readMonsterVariant(ByteArray const& data) const {
MonsterVariant MonsterDatabase::readMonsterVariant(ByteArray const& data, NetCompatibilityRules rules) const {
DataStreamBuffer ds(data);
ds.setStreamCompatibilityVersion(rules);
String type = ds.read<String>();
uint64_t seed = ds.read<uint64_t>();
@ -216,8 +218,8 @@ MonsterPtr MonsterDatabase::diskLoadMonster(Json const& diskStore) const {
return make_shared<Monster>(diskStore);
}
MonsterPtr MonsterDatabase::netLoadMonster(ByteArray const& netStore) const {
return make_shared<Monster>(readMonsterVariant(netStore));
MonsterPtr MonsterDatabase::netLoadMonster(ByteArray const& netStore, NetCompatibilityRules rules) const {
return make_shared<Monster>(readMonsterVariant(netStore, rules));
}
List<Drawable> MonsterDatabase::monsterPortrait(MonsterVariant const& variant) const {

View File

@ -96,8 +96,8 @@ public:
MonsterVariant randomMonster(String const& typeName, Json const& uniqueParameters = JsonObject()) const;
MonsterVariant monsterVariant(String const& typeName, uint64_t seed, Json const& uniqueParameters = JsonObject()) const;
ByteArray writeMonsterVariant(MonsterVariant const& variant) const;
MonsterVariant readMonsterVariant(ByteArray const& data) const;
ByteArray writeMonsterVariant(MonsterVariant const& variant, NetCompatibilityRules rules = {}) const;
MonsterVariant readMonsterVariant(ByteArray const& data, NetCompatibilityRules rules = {}) const;
Json writeMonsterVariantToJson(MonsterVariant const& mVar) const;
MonsterVariant readMonsterVariantFromJson(Json const& variant) const;
@ -106,7 +106,7 @@ public:
// whatever world they're spawned in.
MonsterPtr createMonster(MonsterVariant monsterVariant, Maybe<float> level = {}, Json uniqueParameters = {}) const;
MonsterPtr diskLoadMonster(Json const& diskStore) const;
MonsterPtr netLoadMonster(ByteArray const& netStore) const;
MonsterPtr netLoadMonster(ByteArray const& netStore, NetCompatibilityRules rules = {}) const;
List<Drawable> monsterPortrait(MonsterVariant const& variant) const;

View File

@ -14,6 +14,7 @@
#include "StarWiring.hpp"
#include "StarClientContext.hpp"
#include "StarSystemWorld.hpp"
#include "StarNetCompatibility.hpp"
namespace Star {
@ -414,7 +415,7 @@ struct WorldStartPacket : PacketBase<PacketType::WorldStart> {
bool respawnInWorld;
HashMap<DungeonId, float> dungeonIdGravity;
HashMap<DungeonId, bool> dungeonIdBreathable;
Set<DungeonId> protectedDungeonIds;
StableHashSet<DungeonId> protectedDungeonIds;
Json worldProperties;
ConnectionId clientId;
bool localInterpolationMode;

View File

@ -152,8 +152,8 @@ Json Npc::diskStore() const {
};
}
ByteArray Npc::netStore() {
return Root::singleton().npcDatabase()->writeNpcVariant(m_npcVariant);
ByteArray Npc::netStore(NetCompatibilityRules rules) {
return Root::singleton().npcDatabase()->writeNpcVariant(m_npcVariant, rules);
}
EntityType Npc::entityType() const {
@ -252,7 +252,7 @@ RectF Npc::collisionArea() const {
return m_movementController->collisionPoly().boundBox();
}
pair<ByteArray, uint64_t> Npc::writeNetState(uint64_t fromVersion) {
pair<ByteArray, uint64_t> Npc::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
// client-side npcs error nearby vanilla NPC scripts because callScriptedEntity
// for now, scrungle the collision poly to avoid their queries. hacky :(
if (m_npcVariant.overrides && m_npcVariant.overrides.getBool("overrideNetPoly", false)) {
@ -260,18 +260,18 @@ pair<ByteArray, uint64_t> Npc::writeNetState(uint64_t fromVersion) {
if (*mode == EntityMode::Master && connectionForEntity(entityId()) != ServerConnectionId) {
PolyF poly = m_movementController->collisionPoly();
m_movementController->setCollisionPoly({ { 0.0f, -3.402823466e+38F }});
auto result = m_netGroup.writeNetState(fromVersion);
auto result = m_netGroup.writeNetState(fromVersion, rules);
m_movementController->setCollisionPoly(poly);
return result;
}
}
}
return m_netGroup.writeNetState(fromVersion);
return m_netGroup.writeNetState(fromVersion, rules);
}
void Npc::readNetState(ByteArray data, float interpolationTime) {
m_netGroup.readNetState(std::move(data), interpolationTime);
void Npc::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetState(data, interpolationTime, rules);
}
String Npc::description() const {

View File

@ -42,11 +42,12 @@ class Npc
public virtual PhysicsEntity,
public virtual EmoteEntity {
public:
Npc(ByteArray const& netStore, NetCompatibilityRules rules = {});
Npc(NpcVariant const& npcVariant);
Npc(NpcVariant const& npcVariant, Json const& initialState);
Json diskStore() const;
ByteArray netStore();
ByteArray netStore(NetCompatibilityRules rules = {});
EntityType entityType() const override;
ClientEntityMode clientEntityMode() const override;
@ -66,8 +67,8 @@ public:
RectF collisionArea() const override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f) override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0, NetCompatibilityRules rules = {}) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void enableInterpolation(float extrapolationHint = 0.0f) override;
void disableInterpolation() override;

View File

@ -157,8 +157,9 @@ NpcVariant NpcDatabase::generateNpcVariant(
return variant;
}
ByteArray NpcDatabase::writeNpcVariant(NpcVariant const& variant) const {
ByteArray NpcDatabase::writeNpcVariant(NpcVariant const& variant, NetCompatibilityRules rules) const {
DataStreamBuffer ds;
ds.setStreamCompatibilityVersion(rules);
ds.write(variant.species);
ds.write(variant.typeName);
@ -179,8 +180,9 @@ ByteArray NpcDatabase::writeNpcVariant(NpcVariant const& variant) const {
return ds.data();
}
NpcVariant NpcDatabase::readNpcVariant(ByteArray const& data) const {
NpcVariant NpcDatabase::readNpcVariant(ByteArray const& data, NetCompatibilityRules rules) const {
DataStreamBuffer ds(data);
ds.setStreamCompatibilityVersion(rules);
NpcVariant variant;
@ -325,8 +327,8 @@ NpcPtr NpcDatabase::diskLoadNpc(Json const& diskStore) const {
return make_shared<Npc>(npcVariant, diskStore);
}
NpcPtr NpcDatabase::netLoadNpc(ByteArray const& netStore) const {
return make_shared<Npc>(readNpcVariant(netStore));
NpcPtr NpcDatabase::netLoadNpc(ByteArray const& netStore, NetCompatibilityRules rules) const {
return make_shared<Npc>(readNpcVariant(netStore, rules));
}
List<Drawable> NpcDatabase::npcPortrait(NpcVariant const& npcVariant, PortraitMode mode) const {

View File

@ -58,15 +58,15 @@ public:
NpcVariant generateNpcVariant(String const& species, String const& typeName, float level) const;
NpcVariant generateNpcVariant(String const& species, String const& typeName, float level, uint64_t seed, Json const& overrides) const;
ByteArray writeNpcVariant(NpcVariant const& variant) const;
NpcVariant readNpcVariant(ByteArray const& data) const;
ByteArray writeNpcVariant(NpcVariant const& variant, NetCompatibilityRules rules = {}) const;
NpcVariant readNpcVariant(ByteArray const& data, NetCompatibilityRules rules = {}) const;
Json writeNpcVariantToJson(NpcVariant const& variant) const;
NpcVariant readNpcVariantFromJson(Json const& data) const;
NpcPtr createNpc(NpcVariant const& npcVariant) const;
NpcPtr diskLoadNpc(Json const& diskStoree) const;
NpcPtr netLoadNpc(ByteArray const& netStore) const;
NpcPtr diskLoadNpc(Json const& diskStore) const;
NpcPtr netLoadNpc(ByteArray const& netStore, NetCompatibilityRules rules = {}) const;
List<Drawable> npcPortrait(NpcVariant const& npcVariant, PortraitMode mode) const;

View File

@ -118,8 +118,9 @@ Json Object::diskStore() const {
return writeStoredData().setAll({{"name", m_config->name}, {"parameters", m_parameters.baseMap()}});
}
ByteArray Object::netStore() {
ByteArray Object::netStore(NetCompatibilityRules rules) {
DataStreamBuffer ds;
ds.setStreamCompatibilityVersion(rules);
ds.write(m_config->name);
ds.write<Json>(m_parameters.baseMap());
return ds.takeData();
@ -297,13 +298,12 @@ RectF Object::metaBoundBox() const {
}
}
pair<ByteArray, uint64_t> Object::writeNetState(uint64_t fromVersion) {
DataStreamBuffer ds;
return m_netGroup.writeNetState(fromVersion);
pair<ByteArray, uint64_t> Object::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
return m_netGroup.writeNetState(fromVersion, rules);
}
void Object::readNetState(ByteArray delta, float interpolationTime) {
m_netGroup.readNetState(std::move(delta), interpolationTime);
void Object::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetState(data, interpolationTime, rules);
}
Vec2I Object::tilePosition() const {

View File

@ -37,7 +37,7 @@ public:
Object(ObjectConfigConstPtr config, Json const& parameters = JsonObject());
Json diskStore() const;
ByteArray netStore();
ByteArray netStore(NetCompatibilityRules rules = {});
virtual EntityType entityType() const override;
virtual ClientEntityMode clientEntityMode() const override;
@ -48,8 +48,8 @@ public:
virtual Vec2F position() const override;
virtual RectF metaBoundBox() const override;
virtual pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0) override;
virtual void readNetState(ByteArray data, float interpolationTime = 0.0f) override;
virtual pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0, NetCompatibilityRules rules = {}) override;
virtual void readNetState(ByteArray data, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
virtual String description() const override;

View File

@ -380,8 +380,9 @@ ObjectPtr ObjectDatabase::diskLoadObject(Json const& diskStore) const {
return object;
}
ObjectPtr ObjectDatabase::netLoadObject(ByteArray const& netStore) const {
ObjectPtr ObjectDatabase::netLoadObject(ByteArray const& netStore, NetCompatibilityRules rules) const {
DataStreamBuffer ds(netStore);
ds.setStreamCompatibilityVersion(rules);
String name = ds.read<String>();
Json parameters = ds.read<Json>();
return createObject(name, parameters);

View File

@ -198,7 +198,7 @@ public:
ObjectPtr createObject(String const& objectName, Json const& objectParameters = JsonObject()) const;
ObjectPtr diskLoadObject(Json const& diskStore) const;
ObjectPtr netLoadObject(ByteArray const& netStore) const;
ObjectPtr netLoadObject(ByteArray const& netStore, NetCompatibilityRules rules = {}) const;
bool canPlaceObject(World const* world, Vec2I const& position, String const& objectName) const;
// If the object is placeable in the given position, creates the given object

View File

@ -411,8 +411,9 @@ Json Plant::diskStore() const {
};
}
ByteArray Plant::netStore() const {
ByteArray Plant::netStore(NetCompatibilityRules rules) const {
DataStreamBuffer ds;
ds.setStreamCompatibilityVersion(rules);
ds.viwrite(m_tilePosition[0]);
ds.viwrite(m_tilePosition[1]);
ds.write(m_ceiling);
@ -423,7 +424,7 @@ ByteArray Plant::netStore() const {
ds.write(m_ephemeral);
ds.write(m_tileDamageParameters);
ds.write(m_fallsWhenDead);
m_tileDamageStatus.netStore(ds);
m_tileDamageStatus.netStore(ds, rules);
ds.write(writePieces());
return ds.takeData();
@ -534,7 +535,7 @@ Plant::Plant(Json const& diskStore) : Plant() {
setupNetStates();
}
Plant::Plant(ByteArray const& netStore) : Plant() {
Plant::Plant(ByteArray const& netStore, NetCompatibilityRules rules) : Plant() {
m_broken = false;
m_tilePosition = Vec2I();
m_ceiling = false;
@ -545,6 +546,7 @@ Plant::Plant(ByteArray const& netStore) : Plant() {
m_piecesUpdated = true;
DataStreamBuffer ds(netStore);
ds.setStreamCompatibilityVersion(rules);
ds.viread(m_tilePosition[0]);
ds.viread(m_tilePosition[1]);
ds.read(m_ceiling);
@ -555,7 +557,7 @@ Plant::Plant(ByteArray const& netStore) : Plant() {
ds.read(m_ephemeral);
ds.read(m_tileDamageParameters);
ds.read(m_fallsWhenDead);
m_tileDamageStatus.netLoad(ds);
m_tileDamageStatus.netLoad(ds, rules);
readPieces(ds.read<ByteArray>());
setupNetStates();
@ -586,12 +588,12 @@ void Plant::init(World* world, EntityId entityId, EntityMode mode) {
m_tilePosition = world->geometry().xwrap(m_tilePosition);
}
pair<ByteArray, uint64_t> Plant::writeNetState(uint64_t fromVersion) {
return m_netGroup.writeNetState(fromVersion);
pair<ByteArray, uint64_t> Plant::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
return m_netGroup.writeNetState(fromVersion, rules);
}
void Plant::readNetState(ByteArray data, float interpolationTime) {
m_netGroup.readNetState(std::move(data), interpolationTime);
void Plant::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetState(data, interpolationTime, rules);
}
void Plant::enableInterpolation(float extrapolationHint) {

View File

@ -57,10 +57,10 @@ public:
Plant(GrassVariant const& config, uint64_t seed);
Plant(BushVariant const& config, uint64_t seed);
Plant(Json const& diskStore);
Plant(ByteArray const& netStore);
Plant(ByteArray const& netStore, NetCompatibilityRules rules = {});
Json diskStore() const;
ByteArray netStore() const;
ByteArray netStore(NetCompatibilityRules rules = {}) const;
EntityType entityType() const override;
@ -68,8 +68,8 @@ public:
virtual String description() const override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f) override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0, NetCompatibilityRules rules = {}) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void enableInterpolation(float extrapolationHint) override;
void disableInterpolation() override;

View File

@ -86,11 +86,12 @@ PlantDrop::PlantDrop(List<Plant::PlantPiece> pieces, Vec2F const& position, Vec2
m_collisionRect = fullBounds;
}
PlantDrop::PlantDrop(ByteArray const& netStore) {
PlantDrop::PlantDrop(ByteArray const& netStore, NetCompatibilityRules rules) {
m_netGroup.addNetElement(&m_movementController);
m_netGroup.addNetElement(&m_spawnedDrops);
DataStreamBuffer ds(netStore);
ds.setStreamCompatibilityVersion(rules);
ds >> m_time;
ds >> m_master;
ds >> m_description;
@ -113,7 +114,7 @@ PlantDrop::PlantDrop(ByteArray const& netStore) {
m_spawnedDropEffects = true;
}
ByteArray PlantDrop::netStore() {
ByteArray PlantDrop::netStore(NetCompatibilityRules rules) {
DataStreamBuffer ds;
ds << m_time;
ds << m_master;
@ -358,12 +359,12 @@ void PlantDrop::render(RenderCallback* renderCallback) {
}
}
pair<ByteArray, uint64_t> PlantDrop::writeNetState(uint64_t fromVersion) {
return m_netGroup.writeNetState(fromVersion);
pair<ByteArray, uint64_t> PlantDrop::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
return m_netGroup.writeNetState(fromVersion, rules);
}
void PlantDrop::readNetState(ByteArray data, float interpolationTime) {
m_netGroup.readNetState(std::move(data), interpolationTime);
void PlantDrop::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetState(data, interpolationTime, rules);
}
void PlantDrop::enableInterpolation(float extrapolationHint) {

View File

@ -15,9 +15,9 @@ public:
PlantDrop(List<Plant::PlantPiece> pieces, Vec2F const& position, Vec2F const& strikeVector, String const& description,
bool upsideDown, Json stemConfig, Json foliageConfig, Json saplingConfig,
bool master, float random);
PlantDrop(ByteArray const& netStore);
PlantDrop(ByteArray const& netStore, NetCompatibilityRules rules = {});
ByteArray netStore();
ByteArray netStore(NetCompatibilityRules rules = {});
EntityType entityType() const override;
@ -26,8 +26,8 @@ public:
String description() const override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f) override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0, NetCompatibilityRules rules = {}) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void enableInterpolation(float extrapolationHint = 0.0f) override;
void disableInterpolation() override;

View File

@ -187,8 +187,9 @@ Player::Player(PlayerConfigPtr config, Uuid uuid) {
m_netGroup.setNeedsStoreCallback(bind(&Player::setNetStates, this));
}
Player::Player(PlayerConfigPtr config, ByteArray const& netStore) : Player(config) {
Player::Player(PlayerConfigPtr config, ByteArray const& netStore, NetCompatibilityRules rules) : Player(config) {
DataStreamBuffer ds(netStore);
ds.setStreamCompatibilityVersion(rules);
setUniqueId(ds.read<String>());
@ -1618,12 +1619,12 @@ Direction Player::facingDirection() const {
return m_movementController->facingDirection();
}
pair<ByteArray, uint64_t> Player::writeNetState(uint64_t fromVersion) {
return m_netGroup.writeNetState(fromVersion);
pair<ByteArray, uint64_t> Player::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
return m_netGroup.writeNetState(fromVersion, rules);
}
void Player::readNetState(ByteArray data, float interpolationTime) {
m_netGroup.readNetState(std::move(data), interpolationTime);
void Player::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetState(data, interpolationTime, rules);
}
void Player::enableInterpolation(float) {
@ -2319,8 +2320,9 @@ Json Player::diskStore() {
};
}
ByteArray Player::netStore() {
ByteArray Player::netStore(NetCompatibilityRules rules) {
DataStreamBuffer ds;
ds.setStreamCompatibilityVersion(rules);
ds.write(*uniqueId());
ds.write(m_description);

View File

@ -76,7 +76,7 @@ public:
static EnumMap<State> const StateNames;
Player(PlayerConfigPtr config, Uuid uuid = Uuid());
Player(PlayerConfigPtr config, ByteArray const& netStore);
Player(PlayerConfigPtr config, ByteArray const& netStore, NetCompatibilityRules rules = {});
Player(PlayerConfigPtr config, Json const& diskStore);
void diskLoad(Json const& diskStore);
@ -92,7 +92,7 @@ public:
QuestManagerPtr questManager() const;
Json diskStore();
ByteArray netStore();
ByteArray netStore(NetCompatibilityRules rules = {});
EntityType entityType() const override;
ClientEntityMode clientEntityMode() const override;
@ -118,8 +118,8 @@ public:
// relative to current position
RectF collisionArea() const override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromStep = 0) override;
void readNetState(ByteArray data, float interpolationStep = 0.0f) override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0, NetCompatibilityRules rules = {}) override;
void readNetState(ByteArray data, float interpolationStep = 0.0f, NetCompatibilityRules rules = {}) override;
void enableInterpolation(float extrapolationHint = 0.0f) override;
void disableInterpolation() override;

View File

@ -60,8 +60,8 @@ PlayerPtr PlayerFactory::diskLoadPlayer(Json const& diskStore) const {
return make_shared<Player>(m_config, diskStore);
}
PlayerPtr PlayerFactory::netLoadPlayer(ByteArray const& netStore) const {
return make_shared<Player>(m_config, netStore);
PlayerPtr PlayerFactory::netLoadPlayer(ByteArray const& netStore, NetCompatibilityRules rules) const {
return make_shared<Player>(m_config, netStore, rules);
}
}

View File

@ -60,7 +60,7 @@ public:
PlayerPtr create() const;
PlayerPtr diskLoadPlayer(Json const& diskStore) const;
PlayerPtr netLoadPlayer(ByteArray const& netStore) const;
PlayerPtr netLoadPlayer(ByteArray const& netStore, NetCompatibilityRules rules = {}) const;
private:
PlayerConfigPtr m_config;

View File

@ -27,7 +27,7 @@ Projectile::Projectile(ProjectileConfigPtr const& config, Json const& parameters
setup();
}
Projectile::Projectile(ProjectileConfigPtr const& config, DataStreamBuffer& data) {
Projectile::Projectile(ProjectileConfigPtr const& config, DataStreamBuffer& data, NetCompatibilityRules rules) {
m_config = config;
data.read(m_parameters);
setup();
@ -41,8 +41,9 @@ Projectile::Projectile(ProjectileConfigPtr const& config, DataStreamBuffer& data
setTeam(data.read<EntityDamageTeam>());
}
ByteArray Projectile::netStore() const {
ByteArray Projectile::netStore(NetCompatibilityRules rules) const {
DataStreamBuffer ds;
ds.setStreamCompatibilityVersion(rules);
ds.write(m_config->typeName);
ds.write(m_parameters);
@ -141,12 +142,12 @@ Vec2F Projectile::velocity() const {
return m_movementController->velocity();
}
pair<ByteArray, uint64_t> Projectile::writeNetState(uint64_t fromVersion) {
return m_netGroup.writeNetState(fromVersion);
pair<ByteArray, uint64_t> Projectile::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
return m_netGroup.writeNetState(fromVersion, rules);
}
void Projectile::readNetState(ByteArray data, float interpolationTime) {
m_netGroup.readNetState(std::move(data), interpolationTime);
void Projectile::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetState(data, interpolationTime, rules);
}
void Projectile::enableInterpolation(float extrapolationHint) {

View File

@ -22,9 +22,9 @@ STAR_CLASS(Projectile);
class Projectile : public virtual Entity, public virtual ScriptedEntity, public virtual PhysicsEntity, public virtual StatusEffectEntity {
public:
Projectile(ProjectileConfigPtr const& config, Json const& parameters);
Projectile(ProjectileConfigPtr const& config, DataStreamBuffer& netState);
Projectile(ProjectileConfigPtr const& config, DataStreamBuffer& netState, NetCompatibilityRules rules = {});
ByteArray netStore() const;
ByteArray netStore(NetCompatibilityRules rules = {}) const;
EntityType entityType() const override;
@ -43,8 +43,8 @@ public:
ClientEntityMode clientEntityMode() const override;
bool masterOnly() const override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f) override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0, NetCompatibilityRules rules = {}) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void enableInterpolation(float extrapolationHint = 0.0f) override;
void disableInterpolation() override;

View File

@ -58,10 +58,11 @@ float ProjectileDatabase::gravityMultiplier(String const& type) const {
return config->movementSettings.getFloat("gravityMultiplier", 1);
}
ProjectilePtr ProjectileDatabase::netLoadProjectile(ByteArray const& netStore) const {
ProjectilePtr ProjectileDatabase::netLoadProjectile(ByteArray const& netStore, NetCompatibilityRules rules) const {
DataStreamBuffer ds(netStore);
ds.setStreamCompatibilityVersion(rules);
String typeName = ds.read<String>();
return make_shared<Projectile>(m_configs.get(typeName), ds);
return make_shared<Projectile>(m_configs.get(typeName), ds, rules);
}
ProjectileConfigPtr ProjectileDatabase::readConfig(String const& path) {

View File

@ -111,7 +111,7 @@ public:
float gravityMultiplier(String const& type) const;
ProjectilePtr createProjectile(String const& type, Json const& parameters = JsonObject()) const;
ProjectilePtr netLoadProjectile(ByteArray const& netStore) const;
ProjectilePtr netLoadProjectile(ByteArray const& netStore, NetCompatibilityRules rules = {}) const;
private:
ProjectileConfigPtr readConfig(String const& path);

View File

@ -10,10 +10,11 @@
namespace Star {
ServerClientContext::ServerClientContext(ConnectionId clientId, Maybe<HostAddress> remoteAddress, Uuid playerUuid,
ServerClientContext::ServerClientContext(ConnectionId clientId, Maybe<HostAddress> remoteAddress, NetCompatibilityRules netRules, Uuid playerUuid,
String playerName, String playerSpecies, bool canBecomeAdmin, WorldChunks initialShipChunks)
: m_clientId(clientId),
m_remoteAddress(remoteAddress),
m_netRules(netRules),
m_playerUuid(playerUuid),
m_playerName(playerName),
m_playerSpecies(playerSpecies),
@ -88,6 +89,10 @@ bool ServerClientContext::canBecomeAdmin() const {
return m_canBecomeAdmin;
}
NetCompatibilityRules ServerClientContext::netRules() const {
return m_netRules;
}
String ServerClientContext::descriptiveName() const {
RecursiveMutexLocker locker(m_mutex);
String hostName = m_remoteAddress ? toString(*m_remoteAddress) : "local";
@ -184,7 +189,7 @@ ByteArray ServerClientContext::writeUpdate() {
shipChunksUpdate = DataStreamBuffer::serialize(take(m_shipChunksUpdate));
ByteArray netGroupUpdate;
tie(netGroupUpdate, m_netVersion) = m_netGroup.writeNetState(m_netVersion);
tie(netGroupUpdate, m_netVersion) = m_netGroup.writeNetState(m_netVersion, m_netRules);
if (rpcUpdate.empty() && shipChunksUpdate.empty() && netGroupUpdate.empty())
return {};

View File

@ -19,7 +19,7 @@ STAR_CLASS(ServerClientContext);
class ServerClientContext {
public:
ServerClientContext(ConnectionId clientId, Maybe<HostAddress> remoteAddress, Uuid playerUuid,
ServerClientContext(ConnectionId clientId, Maybe<HostAddress> remoteAddress, NetCompatibilityRules netRules, Uuid playerUuid,
String playerName, String playerSpecies, bool canBecomeAdmin, WorldChunks initialShipChunks);
ConnectionId clientId() const;
@ -28,6 +28,7 @@ public:
String const& playerName() const;
String const& playerSpecies() const;
bool canBecomeAdmin() const;
NetCompatibilityRules netRules() const;
String descriptiveName() const;
// Register additional rpc methods from other server side services.
@ -87,6 +88,7 @@ public:
private:
ConnectionId const m_clientId;
Maybe<HostAddress> const m_remoteAddress;
NetCompatibilityRules m_netRules;
Uuid const m_playerUuid;
String const m_playerName;
String const m_playerSpecies;

View File

@ -66,12 +66,12 @@ void Sky::jumpTo(SkyParameters skyParameters) {
m_skyParametersUpdated = true;
}
pair<ByteArray, uint64_t> Sky::writeUpdate(uint64_t fromVersion) {
return m_netGroup.writeNetState(fromVersion);
pair<ByteArray, uint64_t> Sky::writeUpdate(uint64_t fromVersion, NetCompatibilityRules rules) {
return m_netGroup.writeNetState(fromVersion, rules);
}
void Sky::readUpdate(ByteArray data) {
m_netGroup.readNetState(std::move(data));
void Sky::readUpdate(ByteArray data, NetCompatibilityRules rules) {
m_netGroup.readNetState(std::move(data), 0.0f, rules);
}
void Sky::stateUpdate() {

View File

@ -30,8 +30,8 @@ public:
void jumpTo(SkyParameters SkyParameters);
pair<ByteArray, uint64_t> writeUpdate(uint64_t fromVersion = 0);
void readUpdate(ByteArray data);
pair<ByteArray, uint64_t> writeUpdate(uint64_t fromVersion = 0, NetCompatibilityRules rules = {});
void readUpdate(ByteArray data, NetCompatibilityRules rules = {});
// handles flying and warp state transitions
void stateUpdate();

View File

@ -14,8 +14,7 @@ Stagehand::Stagehand(Json const& config)
readConfig(config);
}
Stagehand::Stagehand(ByteArray const& netStore)
: Stagehand() {
Stagehand::Stagehand(ByteArray const& netStore, NetCompatibilityRules rules) : Stagehand() {
readConfig(DataStreamBuffer::deserialize<Json>(netStore));
}
@ -31,7 +30,7 @@ Json Stagehand::diskStore() const {
return saveData.set("scriptStorage", m_scriptComponent.getScriptStorage());
}
ByteArray Stagehand::netStore() {
ByteArray Stagehand::netStore(NetCompatibilityRules rules) {
return DataStreamBuffer::serialize(m_config);
}
@ -77,12 +76,12 @@ RectF Stagehand::metaBoundBox() const {
return m_boundBox;
}
pair<ByteArray, uint64_t> Stagehand::writeNetState(uint64_t fromVersion) {
return m_netGroup.writeNetState(fromVersion);
pair<ByteArray, uint64_t> Stagehand::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
return m_netGroup.writeNetState(fromVersion, rules);
}
void Stagehand::readNetState(ByteArray data, float) {
m_netGroup.readNetState(std::move(data));
void Stagehand::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetState(data, interpolationTime, rules);
}
void Stagehand::update(float dt, uint64_t) {

View File

@ -15,10 +15,10 @@ STAR_CLASS(Stagehand);
class Stagehand : public virtual ScriptedEntity {
public:
Stagehand(Json const& config);
Stagehand(ByteArray const& netStore);
Stagehand(ByteArray const& netStore, NetCompatibilityRules rules = {});
Json diskStore() const;
ByteArray netStore();
ByteArray netStore(NetCompatibilityRules rules = {});
void init(World* world, EntityId entityId, EntityMode mode) override;
void uninit() override;
@ -31,8 +31,8 @@ public:
RectF metaBoundBox() const override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f) override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0, NetCompatibilityRules rules = {}) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void update(float dt, uint64_t currentStep) override;

View File

@ -18,7 +18,32 @@ StatusController::StatusController(Json const& config) : m_statCollection(config
m_parentEntity = nullptr;
m_movementController = nullptr;
m_statusProperties.set(config.getObject("statusProperties", {}));
m_statusProperties.reset(config.getObject("statusProperties", {}));
m_statusProperties.setOverrides(
[&](DataStream& ds, NetCompatibilityRules rules) {
if (rules.version() <= 1) ds << m_statusProperties.baseMap();
else m_statusProperties.NetElementHashMap<String, Json>::netStore(ds, rules);
},
[&](DataStream& ds, NetCompatibilityRules rules) {
if (rules.version() <= 1) m_statusProperties.reset(ds.read<JsonObject>());
else m_statusProperties.NetElementHashMap<String, Json>::netLoad(ds, rules);
},
[&](DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) {
if (rules.version() <= 1) {
if (m_statusProperties.shouldWriteNetDelta(fromVersion, rules)) {
ds << m_statusProperties.baseMap();
return true;
}
return false;
}
return m_statusProperties.NetElementHashMap<String, Json>::writeNetDelta(ds, fromVersion, rules);
},
[&](DataStream& ds, float interp, NetCompatibilityRules rules) {
if (rules.version() <= 1) m_statusProperties.reset(ds.read<JsonObject>());
else m_statusProperties.NetElementHashMap<String, Json>::readNetDelta(ds, interp, rules);
}
);
m_minimumLiquidStatusEffectPercentage = config.getFloat("minimumLiquidStatusEffectPercentage");
m_appliesEnvironmentStatusEffects = config.getBool("appliesEnvironmentStatusEffects");
m_appliesWeatherStatusEffects = config.getBool("appliesWeatherStatusEffects");
@ -72,7 +97,7 @@ Json StatusController::diskStore() const {
}
return JsonObject{
{"statusProperties", m_statusProperties.get()},
{"statusProperties", m_statusProperties.baseMap()},
{"persistentEffectCategories", std::move(persistentEffectCategories)},
{"ephemeralEffects", std::move(ephemeralEffects)},
{"resourceValues", std::move(resourceValues)},
@ -84,7 +109,7 @@ void StatusController::diskLoad(Json const& store) {
clearAllPersistentEffects();
clearEphemeralEffects();
m_statusProperties.set(store.getObject("statusProperties"));
m_statusProperties.reset(store.getObject("statusProperties"));
for (auto const& p : store.getObject("persistentEffectCategories", {}))
addPersistentEffects(p.first, p.second.toArray().transformed(jsonToPersistentStatusEffect));
@ -103,17 +128,11 @@ void StatusController::diskLoad(Json const& store) {
}
Json StatusController::statusProperty(String const& name, Json const& def) const {
return m_statusProperties.get().value(name, def);
return m_statusProperties.value(name, def);
}
void StatusController::setStatusProperty(String const& name, Json value) {
m_statusProperties.update([&](JsonObject& statusProperties) {
if (statusProperties[name] != value) {
statusProperties[name] = std::move(value);
return true;
}
return false;
});
m_statusProperties.set(name, value);
}
StringList StatusController::statNames() const {
@ -415,15 +434,17 @@ void StatusController::initNetVersion(NetElementVersion const* version) {
m_netGroup.initNetVersion(version);
}
void StatusController::netStore(DataStream& ds) const {
m_netGroup.netStore(ds);
void StatusController::netStore(DataStream& ds, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return;
m_netGroup.netStore(ds, rules);
}
void StatusController::netLoad(DataStream& ds) {
void StatusController::netLoad(DataStream& ds, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
clearAllPersistentEffects();
clearEphemeralEffects();
m_netGroup.netLoad(ds);
m_netGroup.netLoad(ds, rules);
}
void StatusController::enableNetInterpolation(float extrapolationHint) {
@ -438,12 +459,12 @@ void StatusController::tickNetInterpolation(float dt) {
m_netGroup.tickNetInterpolation(dt);
}
bool StatusController::writeNetDelta(DataStream& ds, uint64_t fromStep) const {
return m_netGroup.writeNetDelta(ds, fromStep);
bool StatusController::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
return m_netGroup.writeNetDelta(ds, fromVersion, rules);
}
void StatusController::readNetDelta(DataStream& ds, float interpolationTime) {
m_netGroup.readNetDelta(ds, interpolationTime);
void StatusController::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetDelta(ds, interpolationTime, rules);
}
void StatusController::blankNetDelta(float interpolationTime) {
@ -576,15 +597,17 @@ void StatusController::EffectAnimator::initNetVersion(NetElementVersion const* v
animator.initNetVersion(version);
}
void StatusController::EffectAnimator::netStore(DataStream& ds) const {
void StatusController::EffectAnimator::netStore(DataStream& ds, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return;
ds.write(animationConfig);
animator.netStore(ds);
animator.netStore(ds, rules);
}
void StatusController::EffectAnimator::netLoad(DataStream& ds) {
void StatusController::EffectAnimator::netLoad(DataStream& ds, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
ds.read(animationConfig);
animator = animationConfig ? NetworkedAnimator(*animationConfig) : NetworkedAnimator();
animator.netLoad(ds);
animator.netLoad(ds, rules);
}
void StatusController::EffectAnimator::enableNetInterpolation(float extrapolationHint) {
@ -599,12 +622,12 @@ void StatusController::EffectAnimator::tickNetInterpolation(float dt) {
animator.tickNetInterpolation(dt);
}
bool StatusController::EffectAnimator::writeNetDelta(DataStream& ds, uint64_t fromVersion) const {
return animator.writeNetDelta(ds, fromVersion);
bool StatusController::EffectAnimator::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
return animator.writeNetDelta(ds, fromVersion, rules);
}
void StatusController::EffectAnimator::readNetDelta(DataStream& ds, float interpolationTime) {
animator.readNetDelta(ds, interpolationTime);
void StatusController::EffectAnimator::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
animator.readNetDelta(ds, interpolationTime, rules);
}
void StatusController::EffectAnimator::blankNetDelta(float interpolationTime) {

View File

@ -2,6 +2,7 @@
#include "StarObserverStream.hpp"
#include "StarNetElementSystem.hpp"
#include "StarNetElementExt.hpp"
#include "StarStatCollection.hpp"
#include "StarStatusEffectDatabase.hpp"
#include "StarDamage.hpp"
@ -103,15 +104,15 @@ public:
void initNetVersion(NetElementVersion const* version = nullptr) override;
void netStore(DataStream& ds) const override;
void netLoad(DataStream& ds) override;
void netStore(DataStream& ds, NetCompatibilityRules rules = {}) const override;
void netLoad(DataStream& ds, NetCompatibilityRules rules) override;
void enableNetInterpolation(float extrapolationHint = 0.0f) override;
void disableNetInterpolation() override;
void tickNetInterpolation(float dt) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules = {}) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void blankNetDelta(float interpolationTime) override;
void tickMaster(float dt);
@ -136,15 +137,15 @@ private:
void initNetVersion(NetElementVersion const* version = nullptr) override;
void netStore(DataStream& ds) const override;
void netLoad(DataStream& ds) override;
void netStore(DataStream& ds, NetCompatibilityRules rules = {}) const override;
void netLoad(DataStream& ds, NetCompatibilityRules rules) override;
void enableNetInterpolation(float extrapolationHint = 0.0f) override;
void disableNetInterpolation() override;
void tickNetInterpolation(float dt) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules = {}) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void blankNetDelta(float interpolationTime) override;
Maybe<String> animationConfig;
@ -203,7 +204,7 @@ private:
NetElementGroup m_netGroup;
StatCollection m_statCollection;
NetElementData<JsonObject> m_statusProperties;
NetElementOverride<NetElementHashMap<String, Json>> m_statusProperties;
NetElementData<DirectivesGroup> m_parentDirectives;
UniqueEffectMetadataGroup m_uniqueEffectMetadata;

View File

@ -453,12 +453,12 @@ void SystemObject::serverUpdate(SystemWorldServer* system, float dt) {
}
}
pair<ByteArray, uint64_t> SystemObject::writeNetState(uint64_t fromVersion) {
return m_netGroup.writeNetState(fromVersion);
pair<ByteArray, uint64_t> SystemObject::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
return m_netGroup.writeNetState(fromVersion, rules);
}
void SystemObject::readNetState(ByteArray data, float interpolationTime) {
m_netGroup.readNetState(std::move(data), interpolationTime);
void SystemObject::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetState(data, interpolationTime, rules);
}
ByteArray SystemObject::netStore() const {
@ -615,12 +615,12 @@ void SystemClientShip::serverUpdate(SystemWorld* system, float dt) {
}
}
pair<ByteArray, uint64_t> SystemClientShip::writeNetState(uint64_t fromVersion) {
return m_netGroup.writeNetState(fromVersion);
pair<ByteArray, uint64_t> SystemClientShip::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
return m_netGroup.writeNetState(fromVersion, rules);
}
void SystemClientShip::readNetState(ByteArray data, float interpolationTime) {
m_netGroup.readNetState(std::move(data), interpolationTime);
void SystemClientShip::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetState(data, interpolationTime, rules);
}
ByteArray SystemClientShip::netStore() const {

View File

@ -155,8 +155,8 @@ public:
void clientUpdate(float dt);
void serverUpdate(SystemWorldServer* system, float dt);
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion);
void readNetState(ByteArray data, float interpolationTime);
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion, NetCompatibilityRules rules = {});
void readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules = {});
ByteArray netStore() const;
Json diskStore() const;
@ -198,8 +198,8 @@ public:
void clientUpdate(float dt);
void serverUpdate(SystemWorld* system, float dt);
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion);
void readNetState(ByteArray data, float interpolationTime);
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion, NetCompatibilityRules rules = {});
void readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules = {});
ByteArray netStore() const;
private:

View File

@ -266,7 +266,7 @@ void SystemWorldServer::queueUpdatePackets() {
HashMap<Uuid, ByteArray> shipUpdates;
for (auto ship : m_ships.values()) {
uint64_t version = versions->ships.maybe(ship->uuid()).value(0);
auto shipUpdate = ship->writeNetState(version);
auto shipUpdate = ship->writeNetState(version, {});
versions->ships.set(ship->uuid(), shipUpdate.second);
if (!shipUpdate.first.empty())
shipUpdates.set(ship->uuid(), shipUpdate.first);
@ -275,7 +275,7 @@ void SystemWorldServer::queueUpdatePackets() {
HashMap<Uuid, ByteArray> objectUpdates;
for (auto object : m_objects.values()) {
uint64_t version = versions->objects.maybe(object->uuid()).value(0);
auto objectUpdate = object->writeNetState(version);
auto objectUpdate = object->writeNetState(version, {});
versions->objects.set(object->uuid(), objectUpdate.second);
if (!objectUpdate.first.empty())
objectUpdates.set(object->uuid(), objectUpdate.first);

View File

@ -358,15 +358,17 @@ void TechController::TechAnimator::initNetVersion(NetElementVersion const* versi
netGroup.initNetVersion(version);
}
void TechController::TechAnimator::netStore(DataStream& ds) const {
void TechController::TechAnimator::netStore(DataStream& ds, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return;
ds << animationConfig;
netGroup.netStore(ds);
netGroup.netStore(ds, rules);
}
void TechController::TechAnimator::netLoad(DataStream& ds) {
void TechController::TechAnimator::netLoad(DataStream& ds, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
ds >> animationConfig;
animator = animationConfig ? NetworkedAnimator(*animationConfig) : NetworkedAnimator();
netGroup.netLoad(ds);
netGroup.netLoad(ds, rules);
}
void TechController::TechAnimator::enableNetInterpolation(float extrapolationHint) {
@ -381,12 +383,12 @@ void TechController::TechAnimator::tickNetInterpolation(float dt) {
netGroup.tickNetInterpolation(dt);
}
bool TechController::TechAnimator::writeNetDelta(DataStream& ds, uint64_t fromVersion) const {
return netGroup.writeNetDelta(ds, fromVersion);
bool TechController::TechAnimator::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
return netGroup.writeNetDelta(ds, fromVersion, rules);
}
void TechController::TechAnimator::readNetDelta(DataStream& ds, float interpolationTime) {
netGroup.readNetDelta(ds, interpolationTime);
void TechController::TechAnimator::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
netGroup.readNetDelta(ds, interpolationTime, rules);
}
void TechController::TechAnimator::blankNetDelta(float interpolationTime) {

View File

@ -90,15 +90,15 @@ private:
void initNetVersion(NetElementVersion const* version = nullptr) override;
void netStore(DataStream& ds) const override;
void netLoad(DataStream& ds) override;
void netStore(DataStream& ds, NetCompatibilityRules rules = {}) const override;
void netLoad(DataStream& ds, NetCompatibilityRules rules) override;
void enableNetInterpolation(float extrapolationHint = 0.0f) override;
void disableNetInterpolation() override;
void tickNetInterpolation(float dt) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules = {}) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void blankNetDelta(float interpolationTime) override;
// If setting invisible, stops all playing audio

View File

@ -612,15 +612,17 @@ void ToolUser::NetItem::initNetVersion(NetElementVersion const* version) {
netItem->initNetVersion(m_netVersion);
}
void ToolUser::NetItem::netStore(DataStream& ds) const {
void ToolUser::NetItem::netStore(DataStream& ds, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return;
const_cast<NetItem*>(this)->updateItemDescriptor();
m_itemDescriptor.netStore(ds);
m_itemDescriptor.netStore(ds, rules);
if (auto netItem = as<NetElement>(m_item.get()))
netItem->netStore(ds);
netItem->netStore(ds, rules);
}
void ToolUser::NetItem::netLoad(DataStream& ds) {
m_itemDescriptor.netLoad(ds);
void ToolUser::NetItem::netLoad(DataStream& ds, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
m_itemDescriptor.netLoad(ds, rules);
auto itemDatabase = Root::singleton().itemDatabase();
if (itemDatabase->loadItem(m_itemDescriptor.get(), m_item)) {
@ -633,7 +635,7 @@ void ToolUser::NetItem::netLoad(DataStream& ds) {
}
if (auto netItem = as<NetElement>(m_item.get()))
netItem->netLoad(ds);
netItem->netLoad(ds, rules);
}
void ToolUser::NetItem::enableNetInterpolation(float extrapolationHint) {
@ -657,23 +659,24 @@ void ToolUser::NetItem::tickNetInterpolation(float dt) {
}
}
bool ToolUser::NetItem::writeNetDelta(DataStream& ds, uint64_t fromVersion) const {
bool ToolUser::NetItem::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return false;
bool deltaWritten = false;
const_cast<NetItem*>(this)->updateItemDescriptor();
m_buffer.clear();
if (m_itemDescriptor.writeNetDelta(m_buffer, fromVersion)) {
if (m_itemDescriptor.writeNetDelta(m_buffer, fromVersion, rules)) {
deltaWritten = true;
ds.write<uint8_t>(1);
ds.writeBytes(m_buffer.data());
if (auto netItem = as<NetElement>(m_item.get())) {
ds.write<uint8_t>(2);
netItem->netStore(ds);
netItem->netStore(ds, rules);
}
}
if (auto netItem = as<NetElement>(m_item.get())) {
m_buffer.clear();
if (netItem->writeNetDelta(m_buffer, fromVersion)) {
if (netItem->writeNetDelta(m_buffer, fromVersion, rules)) {
deltaWritten = true;
ds.write<uint8_t>(3);
ds.writeBytes(m_buffer.data());
@ -685,13 +688,14 @@ bool ToolUser::NetItem::writeNetDelta(DataStream& ds, uint64_t fromVersion) cons
return deltaWritten;
}
void ToolUser::NetItem::readNetDelta(DataStream& ds, float interpolationTime) {
void ToolUser::NetItem::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
while (true) {
uint8_t code = ds.read<uint8_t>();
if (code == 0) {
break;
} else if (code == 1) {
m_itemDescriptor.readNetDelta(ds);
m_itemDescriptor.readNetDelta(ds, 0.0f, rules);
if (!m_item || !m_item->matches(m_itemDescriptor.get(), true)) {
auto itemDatabase = Root::singleton().itemDatabase();
if (itemDatabase->loadItem(m_itemDescriptor.get(), m_item)) {
@ -705,12 +709,12 @@ void ToolUser::NetItem::readNetDelta(DataStream& ds, float interpolationTime) {
}
} else if (code == 2) {
if (auto netItem = as<NetElement>(m_item.get()))
netItem->netLoad(ds);
netItem->netLoad(ds, rules);
else
throw IOException("Server/Client disagreement about whether an Item is a NetElement in NetItem::readNetDelta");
} else if (code == 3) {
if (auto netItem = as<NetElement>(m_item.get()))
netItem->readNetDelta(ds, interpolationTime);
netItem->readNetDelta(ds, interpolationTime, rules);
else
throw IOException("Server/Client disagreement about whether an Item is a NetElement in NetItem::readNetDelta");
} else {

View File

@ -81,15 +81,15 @@ private:
public:
void initNetVersion(NetElementVersion const* version = nullptr) override;
void netStore(DataStream& ds) const override;
void netLoad(DataStream& ds) override;
void netStore(DataStream& ds, NetCompatibilityRules rules = {}) const override;
void netLoad(DataStream& ds, NetCompatibilityRules rules) override;
void enableNetInterpolation(float extrapolationHint = 0.0f) override;
void disableNetInterpolation() override;
void tickNetInterpolation(float dt) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0) override;
bool writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules = {}) const override;
void readNetDelta(DataStream& ds, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void blankNetDelta(float interpolationTime) override;
ItemPtr const& get() const;

View File

@ -94,27 +94,38 @@ Maybe<String> UniverseClient::connect(UniverseConnection connection, bool allowA
else if (!protocolResponsePacket->allowed)
return String(strf("Join failed! Server does not support connections with protocol version {}", StarProtocolVersion));
if (!(m_legacyServer = protocolResponsePacket->compressionMode() != PacketCompressionMode::Enabled)) {
if (auto compressedSocket = as<CompressedPacketSocket>(&connection.packetSocket())) {
if (protocolResponsePacket->info) {
auto compressionName = protocolResponsePacket->info.getString("compression", "None");
NetCompatibilityRules compatibilityRules;
compatibilityRules.setVersion(LegacyVersion);
bool legacyServer = protocolResponsePacket->compressionMode() != PacketCompressionMode::Enabled;
if (!legacyServer) {
auto compressedSocket = as<CompressedPacketSocket>(&connection.packetSocket());
if (protocolResponsePacket->info) {
compatibilityRules.setVersion(protocolResponsePacket->info.getUInt("openProtocolVersion", 1));
auto compressionName = protocolResponsePacket->info.getString("compression", "None");
if (compressedSocket) {
auto compressionMode = NetCompressionModeNames.maybeLeft(compressionName);
if (!compressionMode)
return String(strf("Join failed! Unknown net stream connection type '{}'", compressionName));
Logger::info("UniverseClient: Using '{}' network stream compression", NetCompressionModeNames.getRight(*compressionMode));
compressedSocket->setCompressionStreamEnabled(compressionMode == NetCompressionMode::Zstd);
} else if (!m_legacyServer) {
}
} else {
compatibilityRules.setVersion(1); // A version of 1 is OpenStarbound prior to the NetElement compatibility stuff
if (compressedSocket) {
Logger::info("UniverseClient: Defaulting to Zstd network stream compression (older server version)");
compressedSocket->setCompressionStreamEnabled(true);// old OpenSB server version always expects it!
compressedSocket->setCompressionStreamEnabled(true);
}
}
}
connection.packetSocket().setLegacy(m_legacyServer);
connection.packetSocket().setLegacy(legacyServer);
auto clientConnect = make_shared<ClientConnectPacket>(Root::singleton().assets()->digest(), allowAssetsMismatch, m_mainPlayer->uuid(), m_mainPlayer->name(),
m_mainPlayer->species(), m_playerStorage->loadShipData(m_mainPlayer->uuid()), m_mainPlayer->shipUpgrades(),
m_mainPlayer->log()->introComplete(), account);
clientConnect->info = JsonObject{ {"brand", "OpenStarbound"} };
clientConnect->info = JsonObject{
{"brand", "OpenStarbound"},
{"openProtocolVersion", OpenProtocolVersion }
};
connection.pushSingle(std::move(clientConnect));
connection.sendAll(timeout);
@ -136,11 +147,12 @@ Maybe<String> UniverseClient::connect(UniverseConnection connection, bool allowA
if (auto success = as<ConnectSuccessPacket>(packet)) {
m_universeClock = make_shared<Clock>();
m_clientContext = make_shared<ClientContext>(success->serverUuid, m_mainPlayer->uuid());
m_clientContext->setNetCompatibilityRules(compatibilityRules);
m_teamClient = make_shared<TeamClient>(m_mainPlayer, m_clientContext);
m_mainPlayer->setClientContext(m_clientContext);
m_mainPlayer->setStatistics(m_statistics);
m_worldClient = make_shared<WorldClient>(m_mainPlayer);
m_worldClient->clientState().setLegacy(m_legacyServer);
m_worldClient->clientState().setNetCompatibilityRules(compatibilityRules);
m_worldClient->setAsyncLighting(true);
for (auto& pair : m_luaCallbacks)
m_worldClient->setLuaCallbacks(pair.first, pair.second);
@ -149,7 +161,7 @@ Maybe<String> UniverseClient::connect(UniverseConnection connection, bool allowA
m_celestialDatabase = make_shared<CelestialSlaveDatabase>(std::move(success->celestialInformation));
m_systemWorldClient = make_shared<SystemWorldClient>(m_universeClock, m_celestialDatabase, m_mainPlayer->universeMap());
Logger::info("UniverseClient: Joined {} server as client {}", m_legacyServer ? "Starbound" : "OpenStarbound", success->clientId);
Logger::info("UniverseClient: Joined {} server as client {}", legacyServer ? "Starbound" : "OpenStarbound", success->clientId);
return {};
} else if (auto failure = as<ConnectFailurePacket>(packet)) {
Logger::error("UniverseClient: Join failed: {}", failure->reason);
@ -263,7 +275,7 @@ void UniverseClient::update(float dt) {
m_teamClient->update();
auto contextUpdate = m_clientContext->writeUpdate();
auto contextUpdate = m_clientContext->writeUpdate(m_clientContext->netCompatibilityRules());
if (!contextUpdate.empty())
m_connection->pushSingle(make_shared<ClientContextUpdatePacket>(std::move(contextUpdate)));
@ -650,7 +662,7 @@ void UniverseClient::handlePackets(List<PacketPtr> const& packets) {
for (auto const& packet : packets) {
try {
if (auto clientContextUpdate = as<ClientContextUpdatePacket>(packet)) {
m_clientContext->readUpdate(clientContextUpdate->updateData);
m_clientContext->readUpdate(clientContextUpdate->updateData, m_clientContext->netCompatibilityRules());
m_playerStorage->applyShipUpdates(m_clientContext->playerUuid(), m_clientContext->newShipUpdates());
if (playerIsOriginal())

View File

@ -126,7 +126,6 @@ private:
StatisticsPtr m_statistics;
PlayerPtr m_mainPlayer;
bool m_legacyServer;
bool m_pause;
ClockPtr m_universeClock;
WorldClientPtr m_worldClient;

View File

@ -835,7 +835,10 @@ void UniverseServer::warpPlayers() {
// Checking the spawn target validity then adding the client is not
// perfect, it can still become invalid in between, if we fail at
// adding the client we need to warp them back.
if (toWorld && toWorld->addClient(clientId, warpToWorld.target, !clientContext->remoteAddress(), clientContext->canBecomeAdmin())) {
if (toWorld && toWorld->addClient(clientId, warpToWorld.target,
!clientContext->remoteAddress(),
clientContext->canBecomeAdmin(),
clientContext->netRules())) {
clientContext->setPlayerWorld(toWorld);
m_chatProcessor->joinChannel(clientId, printWorldId(warpToWorld.world));
@ -1574,7 +1577,8 @@ void UniverseServer::acceptConnection(UniverseConnection connection, Maybe<HostA
auto compressionMode = NetCompressionModeNames.maybeLeft(compressionName).value(NetCompressionMode::None);
useCompressionStream = compressionMode == NetCompressionMode::Zstd;
protocolResponse->info = JsonObject{
{"compression", NetCompressionModeNames.getRight(compressionMode)}
{"compression", NetCompressionModeNames.getRight(compressionMode)},
{"openProtocolVersion", OpenProtocolVersion}
};
}
connection.pushSingle(protocolResponse);
@ -1671,7 +1675,10 @@ void UniverseServer::acceptConnection(UniverseConnection connection, Maybe<HostA
String connectionLog = strf("UniverseServer: Logged in account '{}' as player '{}' from address {}",
accountString, clientConnect->playerName, remoteAddressString);
NetCompatibilityRules netRules(legacyClient ? LegacyVersion : 1);
if (Json& info = clientConnect->info) {
if (auto openProtocolVersion = info.optUInt("openProtocolVersion"))
netRules.setVersion(*openProtocolVersion);
if (Json brand = info.get("brand", "custom"))
connectionLog += strf(" ({} client)", brand.toString());
if (info.getBool("legacy", false))
@ -1698,7 +1705,7 @@ void UniverseServer::acceptConnection(UniverseConnection connection, Maybe<HostA
}
ConnectionId clientId = m_clients.nextId();
auto clientContext = make_shared<ServerClientContext>(clientId, remoteAddress, clientConnect->playerUuid,
auto clientContext = make_shared<ServerClientContext>(clientId, remoteAddress, netRules, clientConnect->playerUuid,
clientConnect->playerName, clientConnect->playerSpecies, administrator, clientConnect->shipChunks);
m_clients.add(clientId, clientContext);
m_connectionServer->addConnection(clientId, std::move(connection));

View File

@ -242,12 +242,12 @@ Vec2F Vehicle::velocity() const {
return m_movementController.velocity();
}
pair<ByteArray, uint64_t> Vehicle::writeNetState(uint64_t fromVersion) {
return m_netGroup.writeNetState(fromVersion);
pair<ByteArray, uint64_t> Vehicle::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
return m_netGroup.writeNetState(fromVersion, rules);
}
void Vehicle::readNetState(ByteArray data, float interpolationTime) {
m_netGroup.readNetState(std::move(data), interpolationTime);
void Vehicle::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetState(data, interpolationTime, rules);
}
void Vehicle::enableInterpolation(float extrapolationHint) {

View File

@ -44,8 +44,8 @@ public:
RectF collisionArea() const override;
Vec2F velocity() const;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion) override;
void readNetState(ByteArray data, float interpolationTime = 0) override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0, NetCompatibilityRules rules = {}) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f, NetCompatibilityRules rules = {}) override;
void enableInterpolation(float extrapolationHint) override;
void disableInterpolation() override;

View File

@ -10,7 +10,7 @@ VehicleDatabase::VehicleDatabase() {
auto assets = Root::singleton().assets();
auto& files = assets->scanExtension("vehicle");
assets->queueJsons(files);
for (auto& file : files) {
for (String file : files) {
try {
auto config = assets->json(file);
String name = config.getString("name");
@ -32,15 +32,18 @@ VehiclePtr VehicleDatabase::create(String const& vehicleName, Json const& extraC
return make_shared<Vehicle>(configPair->second, configPair->first, extraConfig);
}
ByteArray VehicleDatabase::netStore(VehiclePtr const& vehicle) const {
ByteArray VehicleDatabase::netStore(VehiclePtr const& vehicle, NetCompatibilityRules rules) const {
DataStreamBuffer ds;
ds.setStreamCompatibilityVersion(rules);
ds.write(vehicle->baseConfig().getString("name"));
ds.write(vehicle->dynamicConfig());
return ds.takeData();
}
VehiclePtr VehicleDatabase::netLoad(ByteArray const& netStore) const {
VehiclePtr VehicleDatabase::netLoad(ByteArray const& netStore, NetCompatibilityRules rules) const {
DataStreamBuffer ds(netStore);
ds.setStreamCompatibilityVersion(rules);
String name = ds.read<String>();
auto dynamicConfig = ds.read<Json>();

View File

@ -13,8 +13,8 @@ public:
VehiclePtr create(String const& vehicleName, Json const& extraConfig = Json()) const;
ByteArray netStore(VehiclePtr const& vehicle) const;
VehiclePtr netLoad(ByteArray const& netStore) const;
ByteArray netStore(VehiclePtr const& vehicle, NetCompatibilityRules rules) const;
VehiclePtr netLoad(ByteArray const& netStore, NetCompatibilityRules rules) const;
Json diskStore(VehiclePtr const& vehicle) const;
VehiclePtr diskLoad(Json const& diskStore) const;

View File

@ -55,9 +55,9 @@ void ServerWeather::setClientVisibleRegions(List<RectI> regions) {
m_clientVisibleRegions = std::move(regions);
}
pair<ByteArray, uint64_t> ServerWeather::writeUpdate(uint64_t fromVersion) {
pair<ByteArray, uint64_t> ServerWeather::writeUpdate(uint64_t fromVersion, NetCompatibilityRules rules) {
setNetStates();
return m_netGroup.writeNetState(fromVersion);
return m_netGroup.writeNetState(fromVersion, rules);
}
void ServerWeather::update(double dt) {
@ -263,9 +263,9 @@ void ClientWeather::setup(WorldGeometry worldGeometry, WeatherEffectsActiveQuery
m_currentTime = 0.0;
}
void ClientWeather::readUpdate(ByteArray data) {
void ClientWeather::readUpdate(ByteArray data, NetCompatibilityRules rules) {
if (!data.empty()) {
m_netGroup.readNetState(std::move(data));
m_netGroup.readNetState(data, 0.0f, rules);
getNetStates();
}
}

View File

@ -29,7 +29,7 @@ public:
void setClientVisibleRegions(List<RectI> regions);
pair<ByteArray, uint64_t> writeUpdate(uint64_t fromVersion = 0);
pair<ByteArray, uint64_t> writeUpdate(uint64_t fromVersion = 0, NetCompatibilityRules rules = {});
void update(double dt);
@ -80,7 +80,7 @@ public:
void setup(WorldGeometry worldGeometry, WeatherEffectsActiveQuery weatherEffectsActiveQuery);
void readUpdate(ByteArray data);
void readUpdate(ByteArray data, NetCompatibilityRules rules);
void setVisibleRegion(RectI visibleRegion);

View File

@ -172,7 +172,8 @@ void WorldClient::removeEntity(EntityId entityId, bool andDie) {
}
if (auto version = m_masterEntitiesNetVersion.maybeTake(entity->entityId())) {
ByteArray finalNetState = entity->writeNetState(*version).first;
auto netRules = m_clientState.netCompatibilityRules();
ByteArray finalNetState = entity->writeNetState(*version, netRules).first;
m_outgoingPackets.append(make_shared<EntityDestroyPacket>(entity->entityId(), std::move(finalNetState), andDie));
}
@ -771,7 +772,7 @@ void WorldClient::handleIncomingPackets(List<PacketPtr> const& packets) {
}
auto entity = entityFactory->netLoadEntity(entityCreate->entityType, entityCreate->storeData);
entity->readNetState(entityCreate->firstNetState);
entity->readNetState(entityCreate->firstNetState, 0.0f, m_clientState.netCompatibilityRules());
entity->init(this, entityCreate->entityId, EntityMode::Slave);
m_entityMap->addEntity(entity);
@ -792,13 +793,13 @@ void WorldClient::handleIncomingPackets(List<PacketPtr> const& packets) {
EntityId entityId = entity->entityId();
if (connectionForEntity(entityId) == entityUpdateSet->forConnection) {
starAssert(entity->isSlave());
entity->readNetState(entityUpdateSet->deltas.value(entityId), interpolationLeadTime);
entity->readNetState(entityUpdateSet->deltas.value(entityId), interpolationLeadTime, m_clientState.netCompatibilityRules());
}
});
} else if (auto entityDestroy = as<EntityDestroyPacket>(packet)) {
if (auto entity = m_entityMap->entity(entityDestroy->entityId)) {
entity->readNetState(entityDestroy->finalNetState, m_interpolationTracker.interpolationLeadTime());
entity->readNetState(entityDestroy->finalNetState, m_interpolationTracker.interpolationLeadTime(), m_clientState.netCompatibilityRules());
// Before destroying the entity, we should make sure that the entity is
// using the absolute latest data, so we disable interpolation.
@ -913,8 +914,8 @@ void WorldClient::handleIncomingPackets(List<PacketPtr> const& packets) {
m_interpolationTracker.receiveTimeUpdate(stepUpdate->remoteTime);
} else if (auto environmentUpdatePacket = as<EnvironmentUpdatePacket>(packet)) {
m_sky->readUpdate(environmentUpdatePacket->skyDelta);
m_weather.readUpdate(environmentUpdatePacket->weatherDelta);
m_sky->readUpdate(environmentUpdatePacket->skyDelta, m_clientState.netCompatibilityRules());
m_weather.readUpdate(environmentUpdatePacket->weatherDelta, m_clientState.netCompatibilityRules());
} else if (auto hit = as<HitRequestPacket>(packet)) {
m_damageManager->pushRemoteHitRequest(hit->remoteHitRequest);
@ -1233,7 +1234,7 @@ void WorldClient::update(float dt) {
queueUpdatePackets(m_entityUpdateTimer.wrapTick(dt));
if ((!m_clientState.legacy() && m_currentStep % 3 == 0) || m_pingTime.isNothing()) {
if ((!m_clientState.netCompatibilityRules().isLegacy() && m_currentStep % 3 == 0) || m_pingTime.isNothing()) {
m_pingTime = Time::monotonicMilliseconds();
m_outgoingPackets.append(make_shared<PingPacket>(*m_pingTime));
}
@ -1329,7 +1330,8 @@ void WorldClient::addEntity(EntityPtr const& entity, EntityId entityId) {
notifyEntityCreate(entity);
} else {
auto entityFactory = Root::singleton().entityFactory();
m_outgoingPackets.append(make_shared<SpawnEntityPacket>(entity->entityType(), entityFactory->netStoreEntity(entity), entity->writeNetState().first));
auto netRules = m_clientState.netCompatibilityRules();
m_outgoingPackets.append(make_shared<SpawnEntityPacket>(entity->entityType(), entityFactory->netStoreEntity(entity), entity->writeNetState(0, netRules).first));
}
}
@ -1433,7 +1435,7 @@ bool WorldClient::isTileProtected(Vec2I const& pos) const {
if (!inWorld())
return true;
auto tile = m_tileArray->tile(pos);
auto const& tile = m_tileArray->tile(pos);
return m_protectedDungeonIds.contains(tile.dungeonId);
}
@ -1460,9 +1462,10 @@ void WorldClient::queueUpdatePackets(bool sendEntityUpdates) {
if (sendEntityUpdates) {
auto entityUpdateSet = make_shared<EntityUpdateSetPacket>();
entityUpdateSet->forConnection = *m_clientId;
auto netRules = m_clientState.netCompatibilityRules();
m_entityMap->forAllEntities([&](EntityPtr const& entity) {
if (auto version = m_masterEntitiesNetVersion.ptr(entity->entityId())) {
auto updateAndVersion = entity->writeNetState(*version);
auto updateAndVersion = entity->writeNetState(*version, netRules);
if (!updateAndVersion.first.empty())
entityUpdateSet->deltas[entity->entityId()] = std::move(updateAndVersion.first);
*version = updateAndVersion.second;
@ -1793,13 +1796,13 @@ void WorldClient::initWorld(WorldStartPacket const& startPacket) {
centerClientWindowOnPlayer();
m_sky = make_shared<Sky>();
m_sky->readUpdate(startPacket.skyData);
m_sky->readUpdate(startPacket.skyData, m_clientState.netCompatibilityRules());
m_weather.setup(m_geometry, [this](Vec2I const& pos) {
auto const& tile = m_tileArray->tile(pos);
return !isRealMaterial(tile.background) && !isSolidColliding(tile.getCollision());
});
m_weather.readUpdate(startPacket.weatherData);
m_weather.readUpdate(startPacket.weatherData, m_clientState.netCompatibilityRules());
m_lightingCalculator.setMonochrome(Root::singleton().configuration()->get("monochromeLighting").toBool());
m_lightingCalculator.setParameters(assets->json("/lighting.config:lighting"));
@ -1874,7 +1877,8 @@ void WorldClient::tryGiveMainPlayerItem(ItemPtr item, bool silent) {
void WorldClient::notifyEntityCreate(EntityPtr const& entity) {
if (entity->isMaster() && !m_masterEntitiesNetVersion.contains(entity->entityId())) {
// Server was unaware of this entity until now
auto firstNetState = entity->writeNetState();
auto netRules = m_clientState.netCompatibilityRules();
auto firstNetState = entity->writeNetState(0, netRules);
m_masterEntitiesNetVersion[entity->entityId()] = firstNetState.second;
m_outgoingPackets.append(make_shared<EntityCreatePacket>(entity->entityType(),
Root::singleton().entityFactory()->netStoreEntity(entity), std::move(firstNetState.first), entity->entityId()));

View File

@ -367,7 +367,7 @@ private:
HashMap<DungeonId, float> m_dungeonIdGravity;
HashMap<DungeonId, bool> m_dungeonIdBreathable;
Set<DungeonId> m_protectedDungeonIds;
StableHashSet<DungeonId> m_protectedDungeonIds;
HashMap<String, List<RpcPromiseKeeper<Vec2F>>> m_findUniqueEntityResponses;
HashMap<Uuid, RpcPromiseKeeper<Json>> m_entityMessageResponses;

View File

@ -22,8 +22,6 @@ WorldClientState::WorldClientState() {
m_netGroup.addNetElement(&m_playerId);
m_netGroup.addNetElement(&m_clientPresenceEntities);
m_legacy = false;
}
RectI WorldClientState::window() const {
@ -81,20 +79,20 @@ List<RectI> WorldClientState::monitoringRegions(function<Maybe<RectI>(EntityId)>
ByteArray WorldClientState::writeDelta() {
ByteArray delta;
tie(delta, m_netVersion) = m_netGroup.writeNetState(m_netVersion);
tie(delta, m_netVersion) = m_netGroup.writeNetState(m_netVersion, m_netCompatibilityRules);
return delta;
}
void WorldClientState::readDelta(ByteArray delta) {
m_netGroup.readNetState(std::move(delta));
m_netGroup.readNetState(std::move(delta), 0.0f, m_netCompatibilityRules);
}
void WorldClientState::setLegacy(bool legacy) {
m_legacy = legacy;
void WorldClientState::setNetCompatibilityRules(NetCompatibilityRules netCompatibilityRules) {
m_netCompatibilityRules = netCompatibilityRules;
}
bool WorldClientState::legacy() const {
return m_legacy;
NetCompatibilityRules WorldClientState::netCompatibilityRules() const {
return m_netCompatibilityRules;
}
void WorldClientState::reset() {

View File

@ -36,9 +36,8 @@ public:
ByteArray writeDelta();
void readDelta(ByteArray delta);
// Whether the client is connected to a legacy server.
void setLegacy(bool legacy);
bool legacy() const;
void setNetCompatibilityRules(NetCompatibilityRules netCompatibilityRules);
NetCompatibilityRules netCompatibilityRules() const;
void reset();
@ -57,7 +56,7 @@ private:
NetElementInt m_playerId;
NetElementData<List<EntityId>> m_clientPresenceEntities;
bool m_legacy;
NetCompatibilityRules m_netCompatibilityRules;
};
}

View File

@ -209,7 +209,7 @@ bool WorldServer::spawnTargetValid(SpawnTarget const& spawnTarget) const {
return true;
}
bool WorldServer::addClient(ConnectionId clientId, SpawnTarget const& spawnTarget, bool isLocal, bool isAdmin) {
bool WorldServer::addClient(ConnectionId clientId, SpawnTarget const& spawnTarget, bool isLocal, bool isAdmin, NetCompatibilityRules netRules) {
if (m_clientInfo.contains(clientId))
return false;
@ -246,6 +246,7 @@ bool WorldServer::addClient(ConnectionId clientId, SpawnTarget const& spawnTarge
auto& clientInfo = m_clientInfo.add(clientId, make_shared<ClientInfo>(clientId, tracker));
clientInfo->local = isLocal;
clientInfo->admin = isAdmin;
clientInfo->clientState.setNetCompatibilityRules(netRules);
auto worldStartPacket = make_shared<WorldStartPacket>();
auto& templateData = worldStartPacket->templateData = m_worldTemplate->store();
@ -254,8 +255,8 @@ bool WorldServer::addClient(ConnectionId clientId, SpawnTarget const& spawnTarge
&& Root::singletonPtr()->configuration()->getPath("compatibility.customDungeonWorld").optBool().value(false))
worldStartPacket->templateData = worldStartPacket->templateData.setPath("worldParameters.primaryDungeon", "testarena");
tie(worldStartPacket->skyData, clientInfo->skyNetVersion) = m_sky->writeUpdate();
tie(worldStartPacket->weatherData, clientInfo->weatherNetVersion) = m_weather.writeUpdate();
tie(worldStartPacket->skyData, clientInfo->skyNetVersion) = m_sky->writeUpdate(0, netRules);
tie(worldStartPacket->weatherData, clientInfo->weatherNetVersion) = m_weather.writeUpdate(0, netRules);
worldStartPacket->playerStart = playerStart;
worldStartPacket->playerRespawn = m_playerStart;
worldStartPacket->respawnInWorld = m_respawnInWorld;
@ -380,8 +381,9 @@ void WorldServer::handleIncomingPackets(ConnectionId clientId, List<PacketPtr> c
clientInfo->outgoingPackets.append(make_shared<GiveItemPacket>(item));
} else if (auto sepacket = as<SpawnEntityPacket>(packet)) {
auto entity = entityFactory->netLoadEntity(sepacket->entityType, std::move(sepacket->storeData));
entity->readNetState(std::move(sepacket->firstNetState));
auto netRules = clientInfo->clientState.netCompatibilityRules();
auto entity = entityFactory->netLoadEntity(sepacket->entityType, std::move(sepacket->storeData), netRules);
entity->readNetState(std::move(sepacket->firstNetState), 0.0f, netRules);
addEntity(std::move(entity));
} else if (auto rdpacket = as<RequestDropPacket>(packet)) {
@ -432,9 +434,9 @@ void WorldServer::handleIncomingPackets(ConnectionId clientId, List<PacketPtr> c
Logger::error("WorldServer received duplicate entity create packet from client, deleting old entity {}", entityCreate->entityId);
removeEntity(entityCreate->entityId, false);
}
auto entity = entityFactory->netLoadEntity(entityCreate->entityType, entityCreate->storeData);
entity->readNetState(entityCreate->firstNetState);
auto netRules = clientInfo->clientState.netCompatibilityRules();
auto entity = entityFactory->netLoadEntity(entityCreate->entityType, entityCreate->storeData, netRules);
entity->readNetState(entityCreate->firstNetState, 0.0f, netRules);
entity->init(this, entityCreate->entityId, EntityMode::Slave);
m_entityMap->addEntity(entity);
@ -448,14 +450,14 @@ void WorldServer::handleIncomingPackets(ConnectionId clientId, List<PacketPtr> c
EntityId entityId = entity->entityId();
if (connectionForEntity(entityId) == clientId) {
starAssert(entity->isSlave());
entity->readNetState(entityUpdateSet->deltas.value(entityId), interpolationLeadTime);
entity->readNetState(entityUpdateSet->deltas.value(entityId), interpolationLeadTime, clientInfo->clientState.netCompatibilityRules());
}
});
clientInfo->pendingForward = true;
} else if (auto entityDestroy = as<EntityDestroyPacket>(packet)) {
if (auto entity = m_entityMap->entity(entityDestroy->entityId)) {
entity->readNetState(entityDestroy->finalNetState, clientInfo->interpolationTracker.interpolationLeadTime());
entity->readNetState(entityDestroy->finalNetState, clientInfo->interpolationTracker.interpolationLeadTime(), clientInfo->clientState.netCompatibilityRules());
// Before destroying the entity, we should make sure that the entity is
// using the absolute latest data, so we disable interpolation.
entity->disableInterpolation();
@ -1188,10 +1190,14 @@ bool WorldServer::isTileProtected(Vec2I const& pos) const {
if (!m_tileProtectionEnabled)
return false;
auto tile = m_tileArray->tile(pos);
auto const& tile = m_tileArray->tile(pos);
return m_protectedDungeonIds.contains(tile.dungeonId);
}
bool WorldServer::getTileProtection(DungeonId dungeonId) const {
return m_protectedDungeonIds.contains(dungeonId);
}
void WorldServer::setTileProtection(DungeonId dungeonId, bool isProtected) {
bool updated = false;
if (isProtected) {
@ -1203,9 +1209,28 @@ void WorldServer::setTileProtection(DungeonId dungeonId, bool isProtected) {
if (updated) {
for (auto const& pair : m_clientInfo)
pair.second->outgoingPackets.append(make_shared<UpdateTileProtectionPacket>(dungeonId, isProtected));
}
Logger::info("Protected dungeonIds for world set to {}", m_protectedDungeonIds);
Logger::info("Protected dungeonIds for world set to {}", m_protectedDungeonIds);
}
}
size_t WorldServer::setTileProtection(List<DungeonId> const& dungeonIds, bool isProtected) {
List<PacketPtr> updates;
updates.reserve(dungeonIds.size());
for (auto const& dungeonId : dungeonIds)
if (isProtected ? m_protectedDungeonIds.add(dungeonId) : m_protectedDungeonIds.remove(dungeonId))
updates.append(make_shared<UpdateTileProtectionPacket>(dungeonId, isProtected));
if (updates.empty())
return 0;
for (auto const& pair : m_clientInfo)
pair.second->outgoingPackets.appendAll(updates);
auto newDungeonIds = m_protectedDungeonIds.values();
sort(newDungeonIds);
Logger::info("Protected dungeonIds for world set to {}", newDungeonIds);
return updates.size();
}
void WorldServer::setTileProtectionEnabled(bool enabled) {
@ -1789,10 +1814,10 @@ void WorldServer::queueUpdatePackets(ConnectionId clientId, bool sendRemoteUpdat
if (shouldRunThisStep("environmentUpdate")) {
ByteArray skyDelta;
tie(skyDelta, clientInfo->skyNetVersion) = m_sky->writeUpdate(clientInfo->skyNetVersion);
tie(skyDelta, clientInfo->skyNetVersion) = m_sky->writeUpdate(clientInfo->skyNetVersion, clientInfo->clientState.netCompatibilityRules());
ByteArray weatherDelta;
tie(weatherDelta, clientInfo->weatherNetVersion) = m_weather.writeUpdate(clientInfo->weatherNetVersion);
tie(weatherDelta, clientInfo->weatherNetVersion) = m_weather.writeUpdate(clientInfo->weatherNetVersion, clientInfo->clientState.netCompatibilityRules());
if (!skyDelta.empty() || !weatherDelta.empty())
clientInfo->outgoingPackets.append(make_shared<EnvironmentUpdatePacket>(std::move(skyDelta), std::move(weatherDelta)));
@ -1866,12 +1891,14 @@ void WorldServer::queueUpdatePackets(ConnectionId clientId, bool sendRemoteUpdat
EntityId entityId = monitoredEntity->entityId();
ConnectionId connectionId = connectionForEntity(entityId);
if (connectionId != clientId) {
auto netRules = clientInfo->clientState.netCompatibilityRules();
if (auto version = clientInfo->clientSlavesNetVersion.ptr(entityId)) {
if (auto updateSetPacket = updateSetPackets.value(connectionId)) {
auto pair = make_pair(entityId, *version);
auto i = m_netStateCache.find(pair);
if (i == m_netStateCache.end())
i = m_netStateCache.insert(pair, monitoredEntity->writeNetState(*version)).first;
auto& cache = m_netStateCache[netRules];
auto i = cache.find(pair);
if (i == cache.end())
i = cache.insert(pair, monitoredEntity->writeNetState(*version, netRules)).first;
const auto& netState = i->second;
if (!netState.first.empty())
updateSetPacket->deltas[entityId] = netState.first;
@ -1879,10 +1906,10 @@ void WorldServer::queueUpdatePackets(ConnectionId clientId, bool sendRemoteUpdat
}
} else if (!monitoredEntity->masterOnly()) {
// Client was unaware of this entity until now
auto firstUpdate = monitoredEntity->writeNetState();
auto firstUpdate = monitoredEntity->writeNetState(0, netRules);
clientInfo->clientSlavesNetVersion.add(entityId, firstUpdate.second);
clientInfo->outgoingPackets.append(make_shared<EntityCreatePacket>(monitoredEntity->entityType(),
entityFactory->netStoreEntity(monitoredEntity), std::move(firstUpdate.first), entityId));
entityFactory->netStoreEntity(monitoredEntity, netRules), std::move(firstUpdate.first), entityId));
}
}
}
@ -2075,7 +2102,8 @@ void WorldServer::removeEntity(EntityId entityId, bool andDie) {
for (auto const& pair : m_clientInfo) {
auto& clientInfo = pair.second;
if (auto version = clientInfo->clientSlavesNetVersion.maybeTake(entity->entityId())) {
ByteArray finalDelta = entity->writeNetState(*version).first;
auto netRules = clientInfo->clientState.netCompatibilityRules();
ByteArray finalDelta = entity->writeNetState(*version, netRules).first;
clientInfo->outgoingPackets.append(make_shared<EntityDestroyPacket>(entity->entityId(), std::move(finalDelta), andDie));
}
}
@ -2340,7 +2368,7 @@ void WorldServer::readMetadata() {
m_adjustPlayerStart = metadata.getBool("adjustPlayerStart");
m_worldTemplate = make_shared<WorldTemplate>(metadata.get("worldTemplate"));
m_centralStructure = WorldStructure(metadata.get("centralStructure"));
m_protectedDungeonIds = jsonToSet<DungeonId>(metadata.get("protectedDungeonIds"), mem_fn(&Json::toUInt));
m_protectedDungeonIds = jsonToSet<StableHashSet<DungeonId>>(metadata.get("protectedDungeonIds"), mem_fn(&Json::toUInt));
m_worldProperties = metadata.getObject("worldProperties");
m_spawner.setActive(metadata.getBool("spawningEnabled"));

View File

@ -87,7 +87,7 @@ public:
// Returns false if the client id already exists, or the spawn target is
// invalid.
bool addClient(ConnectionId clientId, SpawnTarget const& spawnTarget, bool isLocal, bool isAdmin = false);
bool addClient(ConnectionId clientId, SpawnTarget const& spawnTarget, bool isLocal, bool isAdmin = false, NetCompatibilityRules netRules = {});
// Removes client, sends the WorldStopPacket, and returns any pending packets
// for that client
@ -180,7 +180,10 @@ public:
RpcPromise<Json> sendEntityMessage(Variant<EntityId, String> const& entity, String const& message, JsonArray const& args = {}) override;
bool isTileProtected(Vec2I const& pos) const override;
bool getTileProtection(DungeonId dungeonId) const;
void setTileProtection(DungeonId dungeonId, bool isProtected);
// sets a provided list of DungeonIds all at once and returns how many were changed
size_t setTileProtection(List<DungeonId> const& dungeonIds, bool isProtected);
// used to globally, temporarily disable protection for certain operations
void setTileProtectionEnabled(bool enabled);
@ -374,7 +377,7 @@ private:
CollisionGenerator m_collisionGenerator;
List<CollisionBlock> m_workingCollisionBlocks;
HashMap<pair<EntityId, uint64_t>, pair<ByteArray, uint64_t>> m_netStateCache;
HashMap<NetCompatibilityRules, HashMap<pair<EntityId, uint64_t>, pair<ByteArray, uint64_t>>> m_netStateCache;
OrderedHashMap<ConnectionId, shared_ptr<ClientInfo>> m_clientInfo;
GameTimer m_entityUpdateTimer;
@ -396,7 +399,7 @@ private:
bool m_generatingDungeon;
HashMap<DungeonId, float> m_dungeonIdGravity;
HashMap<DungeonId, bool> m_dungeonIdBreathable;
Set<DungeonId> m_protectedDungeonIds;
StableHashSet<DungeonId> m_protectedDungeonIds;
bool m_tileProtectionEnabled;
HashMap<Uuid, pair<ConnectionId, MVariant<ConnectionId, RpcPromiseKeeper<Json>>>> m_entityMessageResponses;

Some files were not shown because too many files have changed in this diff Show More