summaryrefslogtreecommitdiffstats
path: root/src/AssetManager.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2019-02-15 18:24:04 +0100
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2019-04-21 12:55:48 +0200
commit8a3d208ba81a7413607b0feb3e1414b03a3dde11 (patch)
tree8e91dd050c1a789d311f474b9e1f3c01fbe68817 /src/AssetManager.cpp
parentMerge pull request #36 from LaG1924/fix-windows-vs2019 (diff)
downloadAltCraft-8a3d208ba81a7413607b0feb3e1414b03a3dde11.tar
AltCraft-8a3d208ba81a7413607b0feb3e1414b03a3dde11.tar.gz
AltCraft-8a3d208ba81a7413607b0feb3e1414b03a3dde11.tar.bz2
AltCraft-8a3d208ba81a7413607b0feb3e1414b03a3dde11.tar.lz
AltCraft-8a3d208ba81a7413607b0feb3e1414b03a3dde11.tar.xz
AltCraft-8a3d208ba81a7413607b0feb3e1414b03a3dde11.tar.zst
AltCraft-8a3d208ba81a7413607b0feb3e1414b03a3dde11.zip
Diffstat (limited to 'src/AssetManager.cpp')
-rw-r--r--src/AssetManager.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index 71800d7..1ae9929 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -11,6 +11,7 @@
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_PNG
#include <stb_image.h>
+#include <sol.hpp>
#include "Utility.hpp"
@@ -24,10 +25,12 @@ std::map<BlockId, std::string> blockIdToBlockName;
std::unique_ptr<AssetTreeNode> assetTree;
std::unique_ptr<TextureAtlas> atlas;
std::map<BlockId, BlockFaces> blockIdToBlockFaces;
+sol::state lua;
void LoadIds();
void LoadAssets();
void LoadTextures();
+void LoadScripts();
void WalkDirEntry(const fs::directory_entry &dirEntry, AssetTreeNode *node);
void ParseAsset(AssetTreeNode &node);
@@ -35,6 +38,7 @@ void ParseAssetTexture(AssetTreeNode &node);
void ParseAssetBlockModel(AssetTreeNode &node);
void ParseAssetBlockState(AssetTreeNode &node);
void ParseAssetShader(AssetTreeNode &node);
+void ParseAssetScript(AssetTreeNode &node);
void ParseBlockModels();
@@ -58,6 +62,7 @@ void AssetManager::InitAssetManager()
LoadIds();
ParseBlockModels();
+ LoadScripts();
}
void LoadIds() {
@@ -96,6 +101,38 @@ void LoadTextures() {
atlas = std::make_unique<TextureAtlas>(textureData);
}
+void LoadScripts() {
+ lua.open_libraries(sol::lib::base, sol::lib::table);
+
+ LOG(INFO) << "Loading lua-init-scripts";
+ std::vector<std::string> loadedScripts;
+ std::vector<std::string> failedScripts;
+
+ AssetTreeNode *node = AssetManager::GetAssetByAssetName("/");
+ for (auto &it : node->childs) {
+ for (auto &child : it->childs) {
+ if (child->name == "init") {
+ AssetScript *asset = dynamic_cast<AssetScript *>(child->asset.get());
+ if (!asset) {
+ LOG(ERROR) << "Unrecognised script file /" << it->name;
+ continue;
+ }
+ try {
+ lua.script(asset->code);
+ }
+ catch (sol::error &e) {
+ LOG(ERROR) << "LUA init-script " << child->name << " failed: " << e.what();
+ failedScripts.push_back(it->name);
+ continue;
+ }
+ loadedScripts.push_back(it->name);
+ }
+ }
+ }
+
+ LOG(INFO) << "Lua loaded: " << loadedScripts.size() << " failed: " << failedScripts.size();
+}
+
void WalkDirEntry(const fs::directory_entry &dirEntry, AssetTreeNode *node) {
for (auto &file : fs::directory_iterator(dirEntry)) {
node->childs.push_back(std::make_unique<AssetTreeNode>());
@@ -138,6 +175,11 @@ void ParseAsset(AssetTreeNode &node) {
ParseAssetShader(node);
return;
}
+
+ if (node.name == "init") {
+ ParseAssetScript(node);
+ return;
+ }
}
void ParseAssetTexture(AssetTreeNode &node) {
@@ -380,6 +422,14 @@ void ParseAssetShader(AssetTreeNode &node) {
}
}
+void ParseAssetScript(AssetTreeNode &node) {
+ node.asset = std::make_unique<AssetScript>();
+ AssetScript *asset = dynamic_cast<AssetScript*>(node.asset.get());
+ asset->code = std::string((char*)node.data.data(), (char*)node.data.data() + node.data.size());
+ node.data.clear();
+ node.data.shrink_to_fit();
+}
+
void ParseBlockModels() {
std::string textureName;