Skip to content

Commit

Permalink
refactor: compress dim nbt data
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Jan 20, 2024
1 parent 46f101b commit 521e491
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 21 deletions.
20 changes: 17 additions & 3 deletions src/ll/api/dimension/CustomDimensionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,16 @@ struct CustomDimensionManager::Impl {

CustomDimensionManager::CustomDimensionManager() : impl(std::make_unique<Impl>()) {
std::lock_guard lock{impl->mMapMutex};
CustomDimensionConfig::setDimensionConfigPath();
CustomDimensionConfig::loadConfigFile();
if (!CustomDimensionConfig::dimConfig.dimensionList.empty()) {
for (auto& [name, info] : CustomDimensionConfig::dimConfig.dimensionList) {
impl->customDimensionMap.emplace(
name,
Impl::DimensionInfo{info.dimId, *CompoundTag::fromBinaryNbt(base64_utils::decode(info.base64Nbt))}
Impl::DimensionInfo{
info.dimId,
*CompoundTag::fromBinaryNbt(string_utils::decompress(base64_utils::decode(info.base64Nbt)))
}
);
}
impl->mNewDimensionId += static_cast<int>(impl->customDimensionMap.size());
Expand All @@ -150,12 +154,19 @@ DimensionType CustomDimensionManager::addDimension(
bool newDim{};
if (impl->customDimensionMap.contains(dimName)) {
info = impl->customDimensionMap.at(dimName);
loggerMoreDimMag.debug("The dimension already registry. use old id, name: {}, id: {}", dimName, info.id.id);
loggerMoreDimMag.debug(
"The dimension already registry. use old id, name: {}, id: {}, \ndata: {}",
dimName,
info.id.id,
info.nbt.toSnbt()
);
} else {
// Assign new id
info.id = impl->mNewDimensionId++;
info.nbt = data();
newDim = true;
loggerMoreDimMag
.debug("registry new dimension, name: {}, id: {}, \ndata: {}", dimName, info.id.id, info.nbt.toSnbt());
};

// registry create dimension function
Expand Down Expand Up @@ -191,7 +202,10 @@ DimensionType CustomDimensionManager::addDimension(
impl->customDimensionMap.emplace(dimName, info);
CustomDimensionConfig::dimConfig.dimensionList.emplace(
dimName,
CustomDimensionConfig::Config::Info{info.id, base64_utils::encode(info.nbt.toBinaryNbt())}
CustomDimensionConfig::Config::Info{
info.id,
base64_utils::encode(string_utils::compress(info.nbt.toBinaryNbt()))
}
);
CustomDimensionConfig::saveConfigFile();
}
Expand Down
13 changes: 6 additions & 7 deletions src/ll/core/dimension/CustomDimensionConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@

namespace ll::dimension::CustomDimensionConfig {

static ll::Logger logger("CustomDimensionConfig");
static std::string dimensionConfigPath{R"(worlds\Bedrock level\dimension_config.json)"};
Config dimConfig{};
static ll::Logger logger("CustomDimensionConfig");
static std::filesystem::path dimensionConfigPath{u8"./worlds"};
Config dimConfig{};

void setDimensionConfigPath() {
if (!ll::service::getLevel()) {
return;
throw std::runtime_error("Level nullptr");
}
auto levelName = ll::service::getLevel()->getLevelData().getLevelName();
dimensionConfigPath.replace(dimensionConfigPath.find("Bedrock level"), 13, levelName);
dimensionConfigPath /= string_utils::str2u8str(ll::service::getLevel()->getLevelName());
dimensionConfigPath /= u8"dimension_config.json";
}

bool loadConfigFile() {
setDimensionConfigPath(); // set real world path
if (!ll::config::loadConfig(dimConfig, dimensionConfigPath, [](auto&, auto&) -> bool { return false; })) {
if (ll::config::saveConfig(dimConfig, dimensionConfigPath)) {
logger.warn("Config file rewrite success!");
Expand Down
2 changes: 1 addition & 1 deletion src/ll/core/dimension/CustomDimensionConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct Config {
int dimId{};
std::string base64Nbt;
};
int version = 2;
int version = 3;
std::unordered_map<std::string, Info> dimensionList{};
};
extern Config dimConfig;
Expand Down
11 changes: 5 additions & 6 deletions src/ll/test/customWorldGenerator/MyFlatWorldGenerator.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#pragma once

#include "ll/api/utils/RandomUtils.h"

#include "mc/common/wrapper/FeatureRefTraits.h"
#include "mc/common/wrapper/WeakRefT.h"
#include "mc/deps/core/debug/log/ContentLog.h"
Expand Down Expand Up @@ -70,19 +73,15 @@ class MyFlatWorldGenerator : public FlatWorldGenerator {

// r->place(blockSource, {14, -60, 14}, random);

srand(time(0));

XoroshiroPositionalRandomFactory wxoroshiroPositionalRandomFactory(rand(), rand());
LakeFeature wlakeFeature(
BlockTypeRegistry::getDefaultBlockState(VanillaBlockTypeIds::Water, 1),
wxoroshiroPositionalRandomFactory
XoroshiroPositionalRandomFactory{ll::random_utils::rand<uint64>(), ll::random_utils::rand<uint64>()}
);
wlakeFeature.place(blockSource, {14, 0, 14}, random);

XoroshiroPositionalRandomFactory lxoroshiroPositionalRandomFactory(rand(), rand());
LakeFeature llakeFeature(
BlockTypeRegistry::getDefaultBlockState(VanillaBlockTypeIds::Lava, 1),
lxoroshiroPositionalRandomFactory
XoroshiroPositionalRandomFactory{ll::random_utils::rand<uint64>(), ll::random_utils::rand<uint64>()}
);
llakeFeature.place(blockSource, {-14, 0, -14}, random);

Expand Down
6 changes: 3 additions & 3 deletions src/mc/util/random/XoroshiroPositionalRandomFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace Crypto::Hash { class md5; }

class XoroshiroPositionalRandomFactory : public ::IPositionalRandomFactory {
public:
uint64_t mSeedLo; // this+0x8
uint64_t mSeedHi; // this+0x10
uint64 mSeedLo; // this+0x8
uint64 mSeedHi; // this+0x10

public:
XoroshiroPositionalRandomFactory(uint64_t seedLo, uint64_t seedHi) : mSeedLo(seedLo), mSeedHi(seedHi) {}
XoroshiroPositionalRandomFactory(uint64 seedLo, uint64 seedHi) : mSeedLo(seedLo), mSeedHi(seedHi) {}

public:
// NOLINTBEGIN
Expand Down
1 change: 0 additions & 1 deletion src/mc/world/level/Level.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class ServerLevel;
class Level : public ILevel, public BlockSourceListener, public IWorldRegistriesProvider {
public:
[[nodiscard]] std::string const& getLevelName() const { return getLevelData().getLevelName(); }
[[nodiscard]] std::string getLevelPath() const { return "./worlds/" + getLevelName(); }

[[nodiscard]] ServerLevel& asServer() { return *reinterpret_cast<ServerLevel*>(this); }

Expand Down
5 changes: 5 additions & 0 deletions src/mc/world/level/levelgen/feature/IFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

#include "mc/_HeaderOutputPredefine.h"

class IBlockWorldGenAPI;
class BlockPos;
class Random;
class RenderParams;

class IFeature {
public:
// prevent constructor by default
Expand Down

0 comments on commit 521e491

Please sign in to comment.