Optimize parameter passing to avoid unnecessary copying
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <glm/glm.hpp>
|
||||
#include <utility>
|
||||
|
||||
#include "../voxels/Block.hpp"
|
||||
#include "../items/ItemDef.hpp"
|
||||
@@ -13,8 +14,8 @@
|
||||
ContentIndices::ContentIndices(
|
||||
std::vector<Block*> blockDefs,
|
||||
std::vector<ItemDef*> itemDefs
|
||||
) : blockDefs(blockDefs),
|
||||
itemDefs(itemDefs)
|
||||
) : blockDefs(std::move(blockDefs)),
|
||||
itemDefs(std::move(itemDefs))
|
||||
{}
|
||||
|
||||
Content::Content(
|
||||
@@ -35,7 +36,7 @@ Content::Content(
|
||||
Content::~Content() {
|
||||
}
|
||||
|
||||
Block* Content::findBlock(std::string id) const {
|
||||
Block* Content::findBlock(const std::string& id) const {
|
||||
auto found = blockDefs.find(id);
|
||||
if (found == blockDefs.end()) {
|
||||
return nullptr;
|
||||
@@ -43,7 +44,7 @@ Block* Content::findBlock(std::string id) const {
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
Block& Content::requireBlock(std::string id) const {
|
||||
Block& Content::requireBlock(const std::string& id) const {
|
||||
auto found = blockDefs.find(id);
|
||||
if (found == blockDefs.end()) {
|
||||
throw std::runtime_error("missing block "+id);
|
||||
@@ -51,7 +52,7 @@ Block& Content::requireBlock(std::string id) const {
|
||||
return *found->second;
|
||||
}
|
||||
|
||||
ItemDef* Content::findItem(std::string id) const {
|
||||
ItemDef* Content::findItem(const std::string& id) const {
|
||||
auto found = itemDefs.find(id);
|
||||
if (found == itemDefs.end()) {
|
||||
return nullptr;
|
||||
@@ -59,7 +60,7 @@ ItemDef* Content::findItem(std::string id) const {
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
ItemDef& Content::requireItem(std::string id) const {
|
||||
ItemDef& Content::requireItem(const std::string& id) const {
|
||||
auto found = itemDefs.find(id);
|
||||
if (found == itemDefs.end()) {
|
||||
throw std::runtime_error("missing item "+id);
|
||||
@@ -67,7 +68,7 @@ ItemDef& Content::requireItem(std::string id) const {
|
||||
return *found->second;
|
||||
}
|
||||
|
||||
const BlockMaterial* Content::findBlockMaterial(std::string id) const {
|
||||
const BlockMaterial* Content::findBlockMaterial(const std::string& id) const {
|
||||
auto found = blockMaterials.find(id);
|
||||
if (found == blockMaterials.end()) {
|
||||
return nullptr;
|
||||
@@ -75,7 +76,7 @@ const BlockMaterial* Content::findBlockMaterial(std::string id) const {
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
const ContentPackRuntime* Content::getPackRuntime(std::string id) const {
|
||||
const ContentPackRuntime* Content::getPackRuntime(const std::string& id) const {
|
||||
auto found = packs.find(id);
|
||||
if (found == packs.end()) {
|
||||
return nullptr;
|
||||
|
||||
@@ -107,15 +107,15 @@ public:
|
||||
return indices.get();
|
||||
}
|
||||
|
||||
Block* findBlock(std::string id) const;
|
||||
Block& requireBlock(std::string id) const;
|
||||
Block* findBlock(const std::string& id) const;
|
||||
Block& requireBlock(const std::string& id) const;
|
||||
|
||||
ItemDef* findItem(std::string id) const;
|
||||
ItemDef& requireItem(std::string id) const;
|
||||
ItemDef* findItem(const std::string& id) const;
|
||||
ItemDef& requireItem(const std::string& id) const;
|
||||
|
||||
const BlockMaterial* findBlockMaterial(std::string id) const;
|
||||
const BlockMaterial* findBlockMaterial(const std::string& id) const;
|
||||
|
||||
const ContentPackRuntime* getPackRuntime(std::string id) const;
|
||||
const ContentPackRuntime* getPackRuntime(const std::string& id) const;
|
||||
|
||||
const std::unordered_map<std::string, std::unique_ptr<BlockMaterial>>& getBlockMaterials() const;
|
||||
const std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>>& getPacks() const;
|
||||
|
||||
@@ -6,7 +6,7 @@ void ContentBuilder::add(std::unique_ptr<ContentPackRuntime> pack) {
|
||||
packs[pack->getId()] = std::move(pack);
|
||||
}
|
||||
|
||||
Block& ContentBuilder::createBlock(std::string id) {
|
||||
Block& ContentBuilder::createBlock(const std::string& id) {
|
||||
auto found = blockDefs.find(id);
|
||||
if (found != blockDefs.end()) {
|
||||
return *found->second;
|
||||
@@ -17,7 +17,7 @@ Block& ContentBuilder::createBlock(std::string id) {
|
||||
return *blockDefs[id];
|
||||
}
|
||||
|
||||
ItemDef& ContentBuilder::createItem(std::string id) {
|
||||
ItemDef& ContentBuilder::createItem(const std::string& id) {
|
||||
auto found = itemDefs.find(id);
|
||||
if (found != itemDefs.end()) {
|
||||
return *found->second;
|
||||
@@ -28,21 +28,21 @@ ItemDef& ContentBuilder::createItem(std::string id) {
|
||||
return *itemDefs[id];
|
||||
}
|
||||
|
||||
BlockMaterial& ContentBuilder::createBlockMaterial(std::string id) {
|
||||
BlockMaterial& ContentBuilder::createBlockMaterial(const std::string& id) {
|
||||
blockMaterials[id] = std::make_unique<BlockMaterial>();
|
||||
auto& material = *blockMaterials[id];
|
||||
material.name = id;
|
||||
return material;
|
||||
}
|
||||
|
||||
void ContentBuilder::checkIdentifier(std::string id) {
|
||||
void ContentBuilder::checkIdentifier(const std::string& id) {
|
||||
contenttype result;
|
||||
if (((result = checkContentType(id)) != contenttype::none)) {
|
||||
throw namereuse_error("name "+id+" is already used", result);
|
||||
}
|
||||
}
|
||||
|
||||
contenttype ContentBuilder::checkContentType(std::string id) {
|
||||
contenttype ContentBuilder::checkContentType(const std::string& id) {
|
||||
if (blockDefs.find(id) != blockDefs.end()) {
|
||||
return contenttype::block;
|
||||
}
|
||||
|
||||
@@ -24,12 +24,12 @@ public:
|
||||
|
||||
void add(std::unique_ptr<ContentPackRuntime> pack);
|
||||
|
||||
Block& createBlock(std::string id);
|
||||
ItemDef& createItem(std::string id);
|
||||
BlockMaterial& createBlockMaterial(std::string id);
|
||||
Block& createBlock(const std::string& id);
|
||||
ItemDef& createItem(const std::string& id);
|
||||
BlockMaterial& createBlockMaterial(const std::string& id);
|
||||
|
||||
void checkIdentifier(std::string id);
|
||||
contenttype checkContentType(std::string id);
|
||||
void checkIdentifier(const std::string& id);
|
||||
contenttype checkContentType(const std::string& id);
|
||||
|
||||
std::unique_ptr<Content> build();
|
||||
};
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "../constants.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
@@ -44,7 +45,7 @@ public:
|
||||
|
||||
inline void setBlock(blockid_t index, std::string name, blockid_t id) {
|
||||
blocks[index] = id;
|
||||
blockNames[index] = name;
|
||||
blockNames[index] = std::move(name);
|
||||
if (id == BLOCK_VOID) {
|
||||
missingContent = true;
|
||||
} else if (index != id) {
|
||||
@@ -62,7 +63,7 @@ public:
|
||||
|
||||
inline void setItem(itemid_t index, std::string name, itemid_t id) {
|
||||
items[index] = id;
|
||||
itemNames[index] = name;
|
||||
itemNames[index] = std::move(name);
|
||||
if (id == ITEM_VOID) {
|
||||
missingContent = true;
|
||||
} else if (index != id) {
|
||||
|
||||
@@ -28,15 +28,15 @@ ContentLoader::ContentLoader(ContentPack* pack) : pack(pack) {
|
||||
}
|
||||
|
||||
bool ContentLoader::fixPackIndices(
|
||||
fs::path folder,
|
||||
const fs::path& folder,
|
||||
dynamic::Map* indicesRoot,
|
||||
std::string contentSection
|
||||
const std::string& contentSection
|
||||
) {
|
||||
std::vector<std::string> detected;
|
||||
std::vector<std::string> indexed;
|
||||
if (fs::is_directory(folder)) {
|
||||
for (auto entry : fs::directory_iterator(folder)) {
|
||||
fs::path file = entry.path();
|
||||
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||
const fs::path& file = entry.path();
|
||||
if (fs::is_regular_file(file) && file.extension() == ".json") {
|
||||
std::string name = file.stem().string();
|
||||
if (name[0] == '_')
|
||||
@@ -46,8 +46,8 @@ bool ContentLoader::fixPackIndices(
|
||||
std::string space = file.stem().string();
|
||||
if (space[0] == '_')
|
||||
continue;
|
||||
for (auto entry : fs::directory_iterator(file)) {
|
||||
fs::path file = entry.path();
|
||||
for (const auto& entry : fs::directory_iterator(file)) {
|
||||
const fs::path& file = entry.path();
|
||||
if (fs::is_regular_file(file) && file.extension() == ".json") {
|
||||
std::string name = file.stem().string();
|
||||
if (name[0] == '_')
|
||||
@@ -109,7 +109,7 @@ void ContentLoader::fixPackIndices() {
|
||||
}
|
||||
}
|
||||
|
||||
void ContentLoader::loadBlock(Block& def, std::string name, fs::path file) {
|
||||
void ContentLoader::loadBlock(Block& def, const std::string& name, const fs::path& file) {
|
||||
auto root = files::read_json(file);
|
||||
|
||||
root->str("caption", def.caption);
|
||||
@@ -266,7 +266,7 @@ void ContentLoader::loadCustomBlockModel(Block& def, dynamic::Map* primitives) {
|
||||
}
|
||||
}
|
||||
|
||||
void ContentLoader::loadItem(ItemDef& def, std::string name, fs::path file) {
|
||||
void ContentLoader::loadItem(ItemDef& def, const std::string& name, const fs::path& file) {
|
||||
auto root = files::read_json(file);
|
||||
root->str("caption", def.caption);
|
||||
|
||||
@@ -295,7 +295,7 @@ void ContentLoader::loadItem(ItemDef& def, std::string name, fs::path file) {
|
||||
}
|
||||
}
|
||||
|
||||
void ContentLoader::loadBlock(Block& def, std::string full, std::string name) {
|
||||
void ContentLoader::loadBlock(Block& def, const std::string& full, const std::string& name) {
|
||||
auto folder = pack->folder;
|
||||
|
||||
fs::path configFile = folder/fs::path("blocks/"+name+".json");
|
||||
@@ -307,7 +307,7 @@ void ContentLoader::loadBlock(Block& def, std::string full, std::string name) {
|
||||
}
|
||||
}
|
||||
|
||||
void ContentLoader::loadItem(ItemDef& def, std::string full, std::string name) {
|
||||
void ContentLoader::loadItem(ItemDef& def, const std::string& full, const std::string& name) {
|
||||
auto folder = pack->folder;
|
||||
|
||||
fs::path configFile = folder/fs::path("items/"+name+".json");
|
||||
@@ -319,7 +319,7 @@ void ContentLoader::loadItem(ItemDef& def, std::string full, std::string name) {
|
||||
}
|
||||
}
|
||||
|
||||
void ContentLoader::loadBlockMaterial(BlockMaterial& def, fs::path file) {
|
||||
void ContentLoader::loadBlockMaterial(BlockMaterial& def, const fs::path& file) {
|
||||
auto root = files::read_json(file);
|
||||
root->str("steps-sound", def.stepsSound);
|
||||
root->str("place-sound", def.placeSound);
|
||||
@@ -394,8 +394,8 @@ void ContentLoader::load(ContentBuilder& builder) {
|
||||
|
||||
fs::path materialsDir = folder / fs::u8path("block_materials");
|
||||
if (fs::is_directory(materialsDir)) {
|
||||
for (auto entry : fs::directory_iterator(materialsDir)) {
|
||||
fs::path file = entry.path();
|
||||
for (const auto& entry : fs::directory_iterator(materialsDir)) {
|
||||
const fs::path& file = entry.path();
|
||||
std::string name = pack->id+":"+file.stem().u8string();
|
||||
loadBlockMaterial(builder.createBlockMaterial(name), file);
|
||||
}
|
||||
|
||||
@@ -22,21 +22,21 @@ class ContentLoader {
|
||||
const ContentPack* pack;
|
||||
scriptenv env;
|
||||
|
||||
void loadBlock(Block& def, std::string full, std::string name);
|
||||
void loadBlock(Block& def, const std::string& full, const std::string& name);
|
||||
void loadCustomBlockModel(Block& def, dynamic::Map* primitives);
|
||||
void loadItem(ItemDef& def, std::string full, std::string name);
|
||||
void loadBlockMaterial(BlockMaterial& def, fs::path file);
|
||||
void loadItem(ItemDef& def, const std::string& full, const std::string& name);
|
||||
void loadBlockMaterial(BlockMaterial& def, const fs::path& file);
|
||||
public:
|
||||
ContentLoader(ContentPack* pack);
|
||||
|
||||
bool fixPackIndices(
|
||||
fs::path folder,
|
||||
const fs::path& folder,
|
||||
dynamic::Map* indicesRoot,
|
||||
std::string contentSection
|
||||
const std::string& contentSection
|
||||
);
|
||||
void fixPackIndices();
|
||||
void loadBlock(Block& def, std::string name, fs::path file);
|
||||
void loadItem(ItemDef& def, std::string name, fs::path file);
|
||||
void loadBlock(Block& def, const std::string& name, const fs::path& file);
|
||||
void loadItem(ItemDef& def, const std::string& name, const fs::path& file);
|
||||
void load(ContentBuilder& builder);
|
||||
};
|
||||
|
||||
|
||||
+11
-10
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
#include "../coders/json.hpp"
|
||||
#include "../files/files.hpp"
|
||||
@@ -21,8 +22,8 @@ const std::vector<std::string> ContentPack::RESERVED_NAMES = {
|
||||
contentpack_error::contentpack_error(
|
||||
std::string packId,
|
||||
fs::path folder,
|
||||
std::string message)
|
||||
: std::runtime_error(message), packId(packId), folder(folder) {
|
||||
const std::string& message)
|
||||
: std::runtime_error(message), packId(std::move(packId)), folder(std::move(folder)) {
|
||||
}
|
||||
|
||||
std::string contentpack_error::getPackId() const {
|
||||
@@ -36,7 +37,7 @@ fs::path ContentPack::getContentFile() const {
|
||||
return folder/fs::path(CONTENT_FILENAME);
|
||||
}
|
||||
|
||||
bool ContentPack::is_pack(fs::path folder) {
|
||||
bool ContentPack::is_pack(const fs::path& folder) {
|
||||
return fs::is_regular_file(folder/fs::path(PACKAGE_FILENAME));
|
||||
}
|
||||
|
||||
@@ -61,7 +62,7 @@ static void checkContentPackId(const std::string& id, const fs::path& folder) {
|
||||
}
|
||||
}
|
||||
|
||||
ContentPack ContentPack::read(fs::path folder) {
|
||||
ContentPack ContentPack::read(const fs::path& folder) {
|
||||
auto root = files::read_json(folder/fs::path(PACKAGE_FILENAME));
|
||||
ContentPack pack;
|
||||
root->str("id", pack.id);
|
||||
@@ -89,14 +90,14 @@ ContentPack ContentPack::read(fs::path folder) {
|
||||
}
|
||||
|
||||
void ContentPack::scanFolder(
|
||||
fs::path folder,
|
||||
const fs::path& folder,
|
||||
std::vector<ContentPack>& packs
|
||||
) {
|
||||
if (!fs::is_directory(folder)) {
|
||||
return;
|
||||
}
|
||||
for (auto entry : fs::directory_iterator(folder)) {
|
||||
fs::path folder = entry.path();
|
||||
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||
const fs::path& folder = entry.path();
|
||||
if (!fs::is_directory(folder))
|
||||
continue;
|
||||
if (!is_pack(folder))
|
||||
@@ -112,7 +113,7 @@ void ContentPack::scanFolder(
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> ContentPack::worldPacksList(fs::path folder) {
|
||||
std::vector<std::string> ContentPack::worldPacksList(const fs::path& folder) {
|
||||
fs::path listfile = folder / fs::path("packs.list");
|
||||
if (!fs::is_regular_file(listfile)) {
|
||||
std::cerr << "warning: packs.list not found (will be created)";
|
||||
@@ -122,7 +123,7 @@ std::vector<std::string> ContentPack::worldPacksList(fs::path folder) {
|
||||
return files::read_list(listfile);
|
||||
}
|
||||
|
||||
fs::path ContentPack::findPack(const EnginePaths* paths, fs::path worldDir, std::string name) {
|
||||
fs::path ContentPack::findPack(const EnginePaths* paths, const fs::path& worldDir, const std::string& name) {
|
||||
fs::path folder = worldDir / fs::path("content") / fs::path(name);
|
||||
if (fs::is_directory(folder)) {
|
||||
return folder;
|
||||
@@ -141,7 +142,7 @@ fs::path ContentPack::findPack(const EnginePaths* paths, fs::path worldDir, std:
|
||||
ContentPackRuntime::ContentPackRuntime(
|
||||
ContentPack info,
|
||||
scriptenv env
|
||||
) : info(info), env(std::move(env))
|
||||
) : info(std::move(info)), env(std::move(env))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ class contentpack_error : public std::runtime_error {
|
||||
std::string packId;
|
||||
fs::path folder;
|
||||
public:
|
||||
contentpack_error(std::string packId, fs::path folder, std::string message);
|
||||
contentpack_error(std::string packId, fs::path folder, const std::string& message);
|
||||
|
||||
std::string getPackId() const;
|
||||
fs::path getFolder() const;
|
||||
@@ -52,20 +52,20 @@ struct ContentPack {
|
||||
static const fs::path ITEMS_FOLDER;
|
||||
static const std::vector<std::string> RESERVED_NAMES;
|
||||
|
||||
static bool is_pack(fs::path folder);
|
||||
static ContentPack read(fs::path folder);
|
||||
static bool is_pack(const fs::path& folder);
|
||||
static ContentPack read(const fs::path& folder);
|
||||
|
||||
static void scanFolder(
|
||||
fs::path folder,
|
||||
const fs::path& folder,
|
||||
std::vector<ContentPack>& packs
|
||||
);
|
||||
|
||||
static std::vector<std::string> worldPacksList(fs::path folder);
|
||||
static std::vector<std::string> worldPacksList(const fs::path& folder);
|
||||
|
||||
static fs::path findPack(
|
||||
const EnginePaths* paths,
|
||||
fs::path worldDir,
|
||||
std::string name
|
||||
const fs::path& worldDir,
|
||||
const std::string& name
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user