Skip to content

Commit

Permalink
feat(chunk): add ChunkSource
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jan 25, 2025
1 parent 9d28eea commit 6294e9e
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/bedrock/forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,23 @@ class BaseLightTextureImageBuilder;
class BehaviorFactory;
class BiomeComponentFactory;
class BiomeManager;
class BlendingDataProvider;
class BlockClimberDefinition;
class BlockComponentFactory;
class BlockDefinitionGroup;
class BlockEventDispatcher;
class BlockGraphicsModeChangeContext;
class BlockItem;
class BlockReducer;
class BlockState;
class BodyControl;
class BossEventSubscriptionManager;
class BoundingBox;
class Bounds;
class CameraPresets;
class ChalkboardBlockActor;
class ChangeDimensionPacket;
class ChunkKey;
class ChunkSource;
class ChunkTickRangeManager;
class ChunkViewSource;
class ClientNetworkSystem;
Expand Down Expand Up @@ -111,7 +113,9 @@ class ItemComponent;
class ItemComponentPacket;
class ItemData;
class JigsawStructureRegistry;
class LevelChunkBuilderData;
class LevelChunkMetaData;
class LevelChunkMetaDataDictionary;
class LevelData;
class LevelSoundManager;
class LevelStorageObserver;
Expand All @@ -138,6 +142,7 @@ class PlayerMovementSettings;
class PortalForcer;
class PositionTrackingDBClient;
class PositionTrackingDBServer;
class PostprocessingManager;
class PrivateKeyManager;
class PropertyGroupManager;
class PropertiesSettings;
Expand Down Expand Up @@ -166,6 +171,7 @@ class SpawnConditions;
class Spawner;
class StorageVersion;
class StructureManager;
class SubChunkInterlocker;
class SubChunkPacket;
class SubChunkPos;
class SurfaceBuilderRegistry;
Expand All @@ -176,6 +182,7 @@ class TaskGroup;
class TickingAreaList;
class TickingAreasManager;
class TrackedPacketDataContainer;
class TrackedUniqueChunkPtr;
class TrimMaterialRegistry;
class TrimPatternRegistry;
class UpdateEntityAfterFallOnInterface;
Expand Down
78 changes: 78 additions & 0 deletions src/bedrock/world/level/chunk/chunk_source.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "bedrock/core/utility/enable_non_owner_references.h"
#include "bedrock/core/utility/pub_sub/subscription.h"
#include "bedrock/world/level/block_source.h"
#include "bedrock/world/level/chunk/chunk_source_lookup_map.h"
#include "bedrock/world/scores/identity_dictionary.h"

class ChunkSource : public Bedrock::EnableNonOwnerReferences {
public:
enum class LoadMode : int {
None = 0,
Deferred = 1,
};

ChunkSource(Dimension *, int);

virtual ~ChunkSource() = 0;
virtual void shutdown() = 0;
virtual bool isShutdownDone() = 0;
virtual std::shared_ptr<LevelChunk> getExistingChunk(const ChunkPos &) = 0;
virtual std::shared_ptr<LevelChunk> getRandomChunk(Random &) = 0;
virtual bool isChunkKnown(const ChunkPos &) = 0;
virtual bool isChunkSaved(const ChunkPos &) = 0;
virtual std::shared_ptr<LevelChunk> createNewChunk(const ChunkPos &, LoadMode, bool) = 0;
virtual std::shared_ptr<LevelChunk> getOrLoadChunk(const ChunkPos &, LoadMode, bool) = 0;
virtual bool postProcess(ChunkViewSource &) = 0;
virtual void checkAndReplaceChunk(ChunkViewSource &, LevelChunk &) = 0;
virtual void loadChunk(LevelChunk &, bool) = 0;
virtual void postProcessMobsAt(BlockSource &, int, int, Random &) = 0;
virtual void postProcessMobsAt(BlockSource &, const BoundingBox &) const = 0;
virtual bool saveLiveChunk(LevelChunk &) = 0;
virtual void writeEntityChunkTransfer(LevelChunk &) = 0;
virtual void writeEntityChunkTransfersToUnloadedChunk(const ChunkKey &,
const std::vector<ActorUnloadedChunkTransferEntry> &) = 0;
virtual void deserializeActorStorageToLevelChunk(LevelChunk &) = 0;
virtual void hintDiscardBatchBegin() = 0;
virtual void hintDiscardBatchEnd() = 0;
virtual void acquireDiscarded(TrackedUniqueChunkPtr) = 0;
virtual void compact() = 0;
virtual void flushPendingDiscardedChunkWrites() = 0;
virtual void flushThreadBatch() = 0;
virtual bool isWithinWorldLimit(const ChunkPos &) const = 0;
virtual const ChunkSourceLookupMap *getChunkMap() = 0;
virtual const ChunkSourceLookupMap &getStorage() const = 0;
virtual void clearDeletedEntities() = 0;
virtual bool canCreateViews() const = 0;
virtual std::unique_ptr<BlendingDataProvider> tryGetBlendingDataProvider() = 0;
virtual std::shared_ptr<LevelChunkMetaDataDictionary> loadLevelChunkMetaDataDictionary() = 0;
virtual void setLevelChunk(std::shared_ptr<LevelChunk>) = 0;
virtual bool canLaunchTasks() const = 0;
virtual bool chunkPosNeedsBlending(const ChunkPos &) = 0;

private:
int chunk_side_;
Level *level_;
Dimension *dimension_;
ChunkSource *parent_;
std::unique_ptr<ChunkSource> owned_parent_;
LevelChunkBuilderData *level_chunk_builder_data_;
std::atomic<bool> shutting_down_;
Bedrock::PubSub::Subscription on_save_subscription_;
Bedrock::PubSub::Subscription on_level_storage_app_suspend_subscription_;
};
22 changes: 22 additions & 0 deletions src/bedrock/world/level/chunk/chunk_source_lookup_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <unordered_map>

#include "bedrock/world/level/chunk/level_chunk.h"
#include "bedrock/world/level/chunk_pos.h"

using ChunkSourceLookupMap = std::unordered_map<ChunkPos, std::weak_ptr<LevelChunk>>;
2 changes: 2 additions & 0 deletions src/bedrock/world/level/chunk/level_chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

#include "bedrock/platform/threading/mutex_details.h"
#include "bedrock/platform/threading/spin_lock.h"
#include "bedrock/world/level/block_pos.h"
#include "bedrock/world/level/chunk_pos.h"
#include "bedrock/world/level/tick.h"

class Level;
class Dimension;
class ChunkSource;

class LevelChunk {
public:
Expand Down
5 changes: 5 additions & 0 deletions src/bedrock/world/level/dimension/dimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ Level &Dimension::getLevel() const
return *level_;
}

ChunkSource &Dimension::getChunkSource() const
{
return *chunk_source_;
}

const std::string &Dimension::getName() const
{
return name_;
Expand Down
16 changes: 15 additions & 1 deletion src/bedrock/world/level/dimension/dimension.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "bedrock/world/actor/actor_unique_id.h"
#include "bedrock/world/level/biome/registry/biome_registry.h"
#include "bedrock/world/level/block_source.h"
#include "bedrock/world/level/chunk/chunk_source.h"
#include "bedrock/world/level/dimension/dimension_height_range.h"
#include "bedrock/world/level/level_listener.h"
#include "bedrock/world/level/levelgen/v2/providers/int_provider.h"
Expand Down Expand Up @@ -56,7 +57,10 @@ class Dimension : public IDimension,
public EnableGetWeakRef<Dimension>,
public std::enable_shared_from_this<Dimension> {
public:
Dimension(ILevel &, DimensionType, DimensionHeightRange, Scheduler &, std::string);

[[nodiscard]] Level &getLevel() const;
[[nodiscard]] ChunkSource &getChunkSource() const;
BlockSource &getBlockSourceFromMainChunkSource() const;

[[nodiscard]] const std::string &getName() const; // Endstone
Expand All @@ -81,5 +85,15 @@ class Dimension : public IDimension,
std::unique_ptr<RuntimeLightingManager> runtime_lighting_manager_; // +352
std::string name_; // +360
DimensionType id_; // +392
// ...
bool ultra_warm_; // +356
bool has_ceiling_; // +357
bool has_weather_; // +358
bool has_skylight_; // +359
Brightness sky_darken_; // +360
std::unique_ptr<BlockEventDispatcher> dispatcher_; // +368
std::unique_ptr<TaskGroup> task_group_; // +376
std::unique_ptr<TaskGroup> chunk_gen_task_group_; // +384
std::unique_ptr<PostprocessingManager> post_processing_manager_; // +392
std::unique_ptr<SubChunkInterlocker> sub_chunk_interlocker_; // +400
std::unique_ptr<ChunkSource> chunk_source_; // +408
};
1 change: 1 addition & 0 deletions src/bedrock/world/level/storage/level_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "bedrock/core/threading/async.h"
#include "bedrock/forward.h"
#include "bedrock/nbt/compound_tag.h"
#include "bedrock/world/level/chunk/chunk_source.h"
#include "bedrock/world/level/storage/db_storage_performance_data.h"

struct PlayerStorageIds {
Expand Down

0 comments on commit 6294e9e

Please sign in to comment.