From 8c033fff3d82d5f0e4c5d2eb3c5d10efc60ee851 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Fri, 18 Jun 2021 17:14:19 +0500 Subject: Implemented RmlFileInterface --- src/AssetManager.cpp | 3 +++ src/AssetManager.hpp | 2 ++ src/Plugin.cpp | 2 +- src/Render.cpp | 14 +++++--------- src/Render.hpp | 4 +++- src/Rml.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/Rml.hpp | 26 ++++++++++++++++++++++++++ 7 files changed, 86 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 0db495a..8528a1b 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -64,6 +64,9 @@ void AssetManager::InitAssetManager() ParseBlockModels(); PluginSystem::Init(); +} + +void AssetManager::InitPostRml() { LoadScripts(); } diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index bf948b3..b67d920 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -174,6 +174,8 @@ struct AssetScript : Asset { namespace AssetManager { void InitAssetManager(); + void InitPostRml(); + BlockFaces &GetBlockModelByBlockId(BlockId block); std::string GetAssetNameByBlockId(BlockId block); diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 13045e1..776ba8d 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -42,8 +42,8 @@ namespace PluginApi { plugin["onRequestBlockInfo"].get_or(std::function()), }; plugins.push_back(nativePlugin); + LOG(INFO)<<"Loading plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name); nativePlugin.onLoad(); - LOG(INFO) << "Loaded plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name); } diff --git a/src/Render.cpp b/src/Render.cpp index 39322f9..c0885e3 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -63,6 +63,8 @@ Render::Render(unsigned int windowWidth, unsigned int windowHeight, PrepareToRendering(); glCheckError(); InitRml(); + glCheckError(); + AssetManager::InitPostRml(); glCheckError(); //Read settings @@ -545,19 +547,13 @@ void Render::InitRml() { Rml::SetRenderInterface(rmlRender.get()); rmlRender->Update(renderState.WindowWidth, renderState.WindowHeight); + rmlFile = std::make_unique(); + Rml::SetFileInterface(rmlFile.get()); + if (!Rml::Initialise()) LOG(WARNING) << "Rml not initialized"; Rml::Lua::Initialise(PluginSystem::GetLuaState()); rmlContext = Rml::CreateContext("default", Rml::Vector2i(renderState.WindowWidth, renderState.WindowHeight)); - - if (!Rml::LoadFontFace("OpenSans-Regular.ttf")) - LOG(WARNING) << "Rml font not loaded"; - - Rml::ElementDocument* document = rmlContext->LoadDocument("test.rml"); - if (document) - document->Show(); - else - LOG(WARNING) << "Rml document not loaded"; } diff --git a/src/Render.hpp b/src/Render.hpp index 7b2313c..a7b510a 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -15,6 +15,7 @@ class RendererWorld; class Framebuffer; class RmlRenderInterface; class RmlSystemInterface; +class RmlFileInterface; namespace Rml { class Context; @@ -51,8 +52,9 @@ class Render { float fieldResolutionScale; std::unique_ptr rmlRender; std::unique_ptr rmlSystem; + std::unique_ptr rmlFile; Rml::Context* rmlContext; - unsigned short sdlKeyMods; + unsigned short sdlKeyMods = 0; void SetMouseCapture(bool IsCaptured); diff --git a/src/Rml.cpp b/src/Rml.cpp index abec7db..cbc795f 100644 --- a/src/Rml.cpp +++ b/src/Rml.cpp @@ -162,3 +162,49 @@ void RmlRenderInterface::Update(unsigned int windowWidth, unsigned int windowHei AssetManager::GetAsset("/altcraft/shaders/rmltex")->shader->SetUniform("fontTexture", 0); glCheckError(); } + +Rml::FileHandle RmlFileInterface::Open(const Rml::String& path) { + Rml::FileHandle fileId = handles.rbegin() != handles.rend() ? handles.rbegin()->first + 1 : 1; + while (handles.find(fileId) != handles.end()) + fileId++; + + AssetHandle handle; + handle.fileName = path; + std::string assetName = path; + if (*assetName.begin() != '/') + assetName = "/" + assetName; + handle.assetPtr = AssetManager::GetAssetByAssetName(assetName); + handle.filePos = 0; + + if (handle.assetPtr != nullptr) + handles.insert(std::make_pair(fileId, handle)); + else + fileId = 0; + return fileId; +} + +void RmlFileInterface::Close(Rml::FileHandle file) { + handles.erase(file); +} + +size_t RmlFileInterface::Read(void* buffer, size_t size, Rml::FileHandle file) { + size_t readed = 0; + readed = _min(handles[file].assetPtr->data.size() - handles[file].filePos, size); + std::memcpy(buffer, handles[file].assetPtr->data.data() + handles[file].filePos, readed); + handles[file].filePos += readed; + return readed; +} + +bool RmlFileInterface::Seek(Rml::FileHandle file, long offset, int origin) { + unsigned long long base = 0; + if (origin == SEEK_CUR) + base = handles[file].filePos; + else if (origin == SEEK_END) + base = handles[file].assetPtr->data.size(); + handles[file].filePos = base + offset; + return true; +} + +size_t RmlFileInterface::Tell(Rml::FileHandle file) { + return handles[file].filePos; +} diff --git a/src/Rml.hpp b/src/Rml.hpp index 7b312c0..6e4d857 100644 --- a/src/Rml.hpp +++ b/src/Rml.hpp @@ -1,10 +1,15 @@ #pragma once +#include + #include #include +#include #include "Renderer.hpp" +class AssetTreeNode; + class RmlSystemInterface : public Rml::SystemInterface { double totalTime; public: @@ -58,3 +63,24 @@ public: void Update(unsigned int windowWidth, unsigned int windowHeight); }; + +class RmlFileInterface : public Rml::FileInterface { + struct AssetHandle { + std::string fileName; + unsigned long long filePos; + AssetTreeNode* assetPtr; + }; + std::map handles; +public: + + virtual Rml::FileHandle Open(const Rml::String& path) override; + + virtual void Close(Rml::FileHandle file) override; + + virtual size_t Read(void* buffer, size_t size, Rml::FileHandle file) override; + + virtual bool Seek(Rml::FileHandle file, long offset, int origin) override; + + virtual size_t Tell(Rml::FileHandle file) override; + +}; -- cgit v1.2.3