# HG changeset patch # User David Demelier # Date 1485076277 -3600 # Node ID 301599387b408a9f7697a05f89038c8aaf07d2b5 # Parent a162f380f02e6931e7037dd525061a307452c886 Common: remove class prefix in resources_locator|loader diff -r a162f380f02e -r 301599387b40 examples/animation/main.cpp --- a/examples/animation/main.cpp Sun Jan 22 09:59:14 2017 +0100 +++ b/examples/animation/main.cpp Sun Jan 22 10:11:17 2017 +0100 @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include int main() @@ -32,7 +32,7 @@ try { mlk::window win(400, 400); - mlk::directory_resources_locator locator(SOURCE_DIRECTORY); + mlk::directory_locator locator(SOURCE_DIRECTORY); mlk::client_resources_loader loader(locator); mlk::animation animation = loader.load_animation("animations/margins.json"); mlk::animator animator(animation); diff -r a162f380f02e -r 301599387b40 examples/font/main.cpp --- a/examples/font/main.cpp Sun Jan 22 09:59:14 2017 +0100 +++ b/examples/font/main.cpp Sun Jan 22 10:11:17 2017 +0100 @@ -22,8 +22,8 @@ #include #include #include +#include #include -#include #include using namespace std::chrono_literals; @@ -108,7 +108,7 @@ { try { mlk::window window(400, 400); - mlk::directory_resources_locator locator(SOURCE_DIRECTORY); + mlk::directory_locator locator(SOURCE_DIRECTORY); mlk::client_resources_loader loader(locator); mlk::font font = loader.load_font("DejaVuSans.ttf", 10); diff -r a162f380f02e -r 301599387b40 examples/image/main.cpp --- a/examples/image/main.cpp Sun Jan 22 09:59:14 2017 +0100 +++ b/examples/image/main.cpp Sun Jan 22 10:11:17 2017 +0100 @@ -21,7 +21,7 @@ #include #include -#include +#include #include using namespace std::chrono_literals; @@ -48,7 +48,7 @@ { try { mlk::window window(400, 400); - mlk::directory_resources_locator locator(SOURCE_DIRECTORY); + mlk::directory_locator locator(SOURCE_DIRECTORY); mlk::client_resources_loader loader(locator); draw(window, loader); diff -r a162f380f02e -r 301599387b40 examples/js-animation/main.cpp --- a/examples/js-animation/main.cpp Sun Jan 22 09:59:14 2017 +0100 +++ b/examples/js-animation/main.cpp Sun Jan 22 10:11:17 2017 +0100 @@ -23,14 +23,14 @@ #include #include #include -#include +#include using namespace std::chrono_literals; int main() { try { - mlk::directory_resources_locator locator(SOURCE_DIRECTORY); + mlk::directory_locator locator(SOURCE_DIRECTORY); mlk::client_resources_loader loader(locator); dukx_context m_ctx; diff -r a162f380f02e -r 301599387b40 examples/js-font/main.cpp --- a/examples/js-font/main.cpp Sun Jan 22 09:59:14 2017 +0100 +++ b/examples/js-font/main.cpp Sun Jan 22 10:11:17 2017 +0100 @@ -22,7 +22,7 @@ #include #include #include -#include +#include using namespace std::chrono_literals; @@ -50,7 +50,7 @@ int main() { try { - mlk::directory_resources_locator locator(SOURCE_DIRECTORY); + mlk::directory_locator locator(SOURCE_DIRECTORY); mlk::client_resources_loader loader(locator); dukx_context ctx; diff -r a162f380f02e -r 301599387b40 examples/js-image/main.cpp --- a/examples/js-image/main.cpp Sun Jan 22 09:59:14 2017 +0100 +++ b/examples/js-image/main.cpp Sun Jan 22 10:11:17 2017 +0100 @@ -22,7 +22,7 @@ #include #include #include -#include +#include using namespace std::chrono_literals; @@ -65,7 +65,7 @@ int main() { try { - mlk::directory_resources_locator locator(SOURCE_DIRECTORY); + mlk::directory_locator locator(SOURCE_DIRECTORY); mlk::client_resources_loader loader(locator); dukx_context ctx; diff -r a162f380f02e -r 301599387b40 examples/js-sprite/main.cpp --- a/examples/js-sprite/main.cpp Sun Jan 22 09:59:14 2017 +0100 +++ b/examples/js-sprite/main.cpp Sun Jan 22 10:11:17 2017 +0100 @@ -23,7 +23,7 @@ #include #include #include -#include +#include using namespace std::chrono_literals; @@ -58,7 +58,7 @@ int main() { try { - mlk::directory_resources_locator locator(SOURCE_DIRECTORY); + mlk::directory_locator locator(SOURCE_DIRECTORY); mlk::client_resources_loader loader(locator); dukx_context ctx; diff -r a162f380f02e -r 301599387b40 examples/sprite/main.cpp --- a/examples/sprite/main.cpp Sun Jan 22 09:59:14 2017 +0100 +++ b/examples/sprite/main.cpp Sun Jan 22 10:11:17 2017 +0100 @@ -21,7 +21,7 @@ #include #include -#include +#include #include #include @@ -46,7 +46,7 @@ int main() { try { - mlk::directory_resources_locator locator(SOURCE_DIRECTORY); + mlk::directory_locator locator(SOURCE_DIRECTORY); mlk::client_resources_loader loader(locator); mlk::window window(400, 400); diff -r a162f380f02e -r 301599387b40 libclient/malikania/client_resources_loader.hpp --- a/libclient/malikania/client_resources_loader.hpp Sun Jan 22 09:59:14 2017 +0100 +++ b/libclient/malikania/client_resources_loader.hpp Sun Jan 22 10:11:17 2017 +0100 @@ -24,7 +24,7 @@ * \brief Load client assets. */ -#include +#include #include "animation.hpp" #include "font.hpp" @@ -37,7 +37,7 @@ /** * \brief Load client resources. */ -class client_resources_loader : public resources_loader { +class client_resources_loader : public loader { protected: /** * Require a size object from an object property. @@ -77,8 +77,8 @@ * \param window the window * \param locator the resources locator */ - inline client_resources_loader(resources_locator& locator) - : resources_loader(locator) + inline client_resources_loader(mlk::locator& locator) + : loader(locator) { } diff -r a162f380f02e -r 301599387b40 libcommon-js/malikania/js_resources_loader.cpp --- a/libcommon-js/malikania/js_resources_loader.cpp Sun Jan 22 09:59:14 2017 +0100 +++ b/libcommon-js/malikania/js_resources_loader.cpp Sun Jan 22 10:11:17 2017 +0100 @@ -28,7 +28,7 @@ } // !namespace -void dukx_put_loader(duk_context *ctx, resources_loader& loader) +void dukx_put_loader(duk_context *ctx, loader& loader) { assert(ctx); @@ -38,17 +38,17 @@ duk_put_global_string(ctx, variable.c_str()); } -resources_loader& duk_require_loader(duk_context* ctx) +loader& duk_require_loader(duk_context* ctx) { assert(ctx); dukx_stack_assert sa(ctx); duk_get_global_string(ctx, variable.c_str()); - auto ptr = static_cast(duk_to_pointer(ctx, -1)); + auto ptr = static_cast(duk_to_pointer(ctx, -1)); duk_pop(ctx); - return *static_cast(ptr); + return *static_cast(ptr); } } // !mlk diff -r a162f380f02e -r 301599387b40 libcommon-js/malikania/js_resources_loader.hpp --- a/libcommon-js/malikania/js_resources_loader.hpp Sun Jan 22 09:59:14 2017 +0100 +++ b/libcommon-js/malikania/js_resources_loader.hpp Sun Jan 22 10:11:17 2017 +0100 @@ -20,13 +20,13 @@ #define MALIKANIA_JS_RESOURCES_LOADER_H #include "duktape.hpp" -#include "resources_loader.hpp" +#include "loader.hpp" namespace mlk { -void dukx_put_loader(duk_context* ctx, resources_loader&); +void dukx_put_loader(duk_context* ctx, loader&); -resources_loader& dukx_get_loader(duk_context* ctx); +loader& dukx_get_loader(duk_context* ctx); } // !mlk diff -r a162f380f02e -r 301599387b40 libcommon/CMakeLists.txt --- a/libcommon/CMakeLists.txt Sun Jan 22 09:59:14 2017 +0100 +++ b/libcommon/CMakeLists.txt Sun Jan 22 10:11:17 2017 +0100 @@ -22,15 +22,15 @@ HEADERS ${libmlk-common_SOURCE_DIR}/malikania/game.hpp ${libmlk-common_SOURCE_DIR}/malikania/id.hpp - ${libmlk-common_SOURCE_DIR}/malikania/resources_loader.hpp - ${libmlk-common_SOURCE_DIR}/malikania/resources_locator.hpp + ${libmlk-common_SOURCE_DIR}/malikania/loader.hpp + ${libmlk-common_SOURCE_DIR}/malikania/locator.hpp ${libmlk-common_SOURCE_DIR}/malikania/util.hpp ) set( SOURCES - ${libmlk-common_SOURCE_DIR}/malikania/resources_loader.cpp - ${libmlk-common_SOURCE_DIR}/malikania/resources_locator.cpp + ${libmlk-common_SOURCE_DIR}/malikania/loader.cpp + ${libmlk-common_SOURCE_DIR}/malikania/locator.cpp ${libmlk-common_SOURCE_DIR}/malikania/util.cpp ) diff -r a162f380f02e -r 301599387b40 libcommon/malikania/common-config.hpp --- a/libcommon/malikania/common-config.hpp Sun Jan 22 09:59:14 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -#ifndef COMMON_CONFIG_HPP -#define COMMON_CONFIG_HPP - -#if defined(_WIN32) -# if defined(MALIKANIA_COMMON_BUILD) -# define MALIKANIA_COMMON_EXPORT __declspec(dllexport) -# else -# define MALIKANIA_COMMON_EXPORT __declspec(dllimport) -# endif -#else -# define MALIKANIA_COMMON_EXPORT -#endif - -#endif // !COMMON_CONFIG_HPP \ No newline at end of file diff -r a162f380f02e -r 301599387b40 libcommon/malikania/loader.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libcommon/malikania/loader.cpp Sun Jan 22 10:11:17 2017 +0100 @@ -0,0 +1,108 @@ +/* + * loader.cpp -- load shared resources files + * + * Copyright (c) 2013-2017 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include + +#include "game.hpp" +#include "loader.hpp" +#include "locator.hpp" + +namespace mlk { + +void loader::requires(const std::string& id, + const nlohmann::json& object, + const std::unordered_map& props) const +{ + assert(object.is_object()); + + for (const auto& pair : props) { + auto it = object.find(pair.first); + + if (it == object.end() || it->type() != pair.second) { + std::string type; + + switch (pair.second) { + case nlohmann::json::value_t::array: + type = "array"; + break; + case nlohmann::json::value_t::boolean: + type = "boolean"; + break; + case nlohmann::json::value_t::number_unsigned: + type = "number"; + break; + case nlohmann::json::value_t::object: + type = "object"; + break; + case nlohmann::json::value_t::string: + type = "string"; + break; + default: + break; + } + + throw std::runtime_error(boost::str( + boost::format("%s: missing '%s' property (%s expected)") % id % pair.first % type)); + } + } +} + +std::string loader::require_string(const std::string& id, + const nlohmann::json& object, + const std::string& property) const +{ + assert(object.is_object()); + + auto it = object.find(property); + + if (it == object.end() || !it->is_string()) { + throw std::runtime_error(id + ": missing '" + property + "' property (string expected)"); + } + + return *it; +} + +loader::loader(mlk::locator& locator) + : m_locator(locator) +{ +} + +game loader::load_game() const +{ + auto value = nlohmann::json::parse(m_locator.read("game.json")); + + if (!value.is_object()) { + throw std::runtime_error("game.json: not a JSON object"); + } + + requires("game.json", value, { + { "name", nlohmann::json::value_t::string }, + { "version", nlohmann::json::value_t::string }, + { "requires", nlohmann::json::value_t::string } + }); + + return game{value["name"], + value["version"], + value["requires"], + value.count("license") > 0 ? value["license"] : "", + value.count("author") > 0 ? value["author"] : ""}; +} + +} // !mlk diff -r a162f380f02e -r 301599387b40 libcommon/malikania/loader.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libcommon/malikania/loader.hpp Sun Jan 22 10:11:17 2017 +0100 @@ -0,0 +1,114 @@ +/* + * loader.hpp -- load shared resources files + * + * Copyright (c) 2013-2017 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MALIKANIA_LOADER_HPP +#define MALIKANIA_LOADER_HPP + +#include +#include + +#include + +#include "locator.hpp" + +namespace mlk { + +class game; + +/** + * \brief Open resources files using a locator. + * + * This class is used to load resources files that are common to the server and + * the client. + * + * \see client_loader + */ +class loader { +private: + mlk::locator& m_locator; + +protected: + /** + * Check that an object has the specified properties of the given type. + * + * Throws an error when any of the property is missing or not the correct + * type. + * + * You can use this function when you have lot of properties to extract, + * otherwise, you can use one of the require* or get* functions to avoid + * performances overhead. + * + * \pre object.is_object() + * \param id the resource id + * \param object the object + * \param props the properties + * \throw std::runtime_error when a property is missing / invalid + */ + void requires(const std::string& id, + const nlohmann::json& object, + const std::unordered_map& props) const; + + /** + * Require a string. + * + * \pre object.isObject() + * \param id the resource id + * \param object the object + * \param property the property + * \return the string + * \throw std::runtime_error if the property is not a string or missing + */ + std::string require_string(const std::string& id, + const nlohmann::json& object, + const std::string& property) const; + +public: + /** + * Construct the resources loader. + * + * \param locator the locator + */ + loader(mlk::locator& locator); + + /** + * Virtual destructor defaulted. + */ + virtual ~loader() noexcept = default; + + /** + * Get the underlying locator. + * + * \return the locator + */ + inline mlk::locator& locator() noexcept + { + return m_locator; + } + + /** + * Load a game. + * + * \return the game + * \throw std::runtime_error on errors + */ + virtual game load_game() const; +}; + +} // !mlk + +#endif // !MALIKANIA_LOADER_HPP diff -r a162f380f02e -r 301599387b40 libcommon/malikania/locator.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libcommon/malikania/locator.cpp Sun Jan 22 10:11:17 2017 +0100 @@ -0,0 +1,56 @@ +/* + * locator.cpp -- file and stream loader + * + * Copyright (c) 2013-2017 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "locator.hpp" + +namespace mlk { + +directory_locator::directory_locator(std::string path) noexcept + : m_path(std::move(path)) +{ +} + +std::string directory_locator::read(const std::string &id) +{ + std::ifstream in(m_path + "/" + id, std::ifstream::in | std::ifstream::binary); + + if (!in) { + throw std::runtime_error(std::strerror(errno)); + } + + return std::string(std::istreambuf_iterator(in.rdbuf()), std::istreambuf_iterator()); +} + +std::unique_ptr directory_locator::open(const std::string &id) +{ + auto ptr = std::make_unique(m_path + "/" + id); + + if (!(*ptr)) { + throw std::runtime_error(std::strerror(errno)); + } + + return std::move(ptr); +} + +} // !mlk diff -r a162f380f02e -r 301599387b40 libcommon/malikania/locator.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libcommon/malikania/locator.hpp Sun Jan 22 10:11:17 2017 +0100 @@ -0,0 +1,85 @@ +/* + * locator.hpp -- file and stream loader + * + * Copyright (c) 2013-2017 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MALIKANIA_LOCATOR_HPP +#define MALIKANIA_LOCATOR_HPP + +#include +#include +#include + +namespace mlk { + +/** + * \brief Abstract assets locator. + */ +class locator { +public: + /** + * Default destructor. + */ + virtual ~locator() = default; + + /** + * Read a whole resource as a string. + * + * \param id the resource id + * \return the string + * \throw std::runtime_error on any errors + */ + virtual std::string read(const std::string& id) = 0; + + /** + * Open a resource as a stream. + * + * \param id the resource id + * \return the stream + * \throw std::runtime_error on any errors + */ + virtual std::unique_ptr open(const std::string& id) = 0; +}; + +/** + * \brief Load a game from a directory. + */ +class directory_locator : public locator { +private: + std::string m_path; + +public: + /** + * Load the game from the directory. + * + * \param path the base directory + */ + directory_locator(std::string path) noexcept; + + /** + * \copydoc locator::read + */ + std::string read(const std::string& id) override; + + /** + * \copydoc locator::open + */ + std::unique_ptr open(const std::string& id) override; +}; + +} // !mlk + +#endif // !MALIKANIA_RESOURCES_LOCATOR_HPP diff -r a162f380f02e -r 301599387b40 libcommon/malikania/resources_loader.cpp --- a/libcommon/malikania/resources_loader.cpp Sun Jan 22 09:59:14 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,108 +0,0 @@ -/* - * resources_loader.cpp -- load shared resources files - * - * Copyright (c) 2013-2017 Malikania Authors - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include - -#include - -#include "game.hpp" -#include "resources_loader.hpp" -#include "resources_locator.hpp" - -namespace mlk { - -void resources_loader::requires(const std::string& id, - const nlohmann::json& object, - const std::unordered_map& props) const -{ - assert(object.is_object()); - - for (const auto& pair : props) { - auto it = object.find(pair.first); - - if (it == object.end() || it->type() != pair.second) { - std::string type; - - switch (pair.second) { - case nlohmann::json::value_t::array: - type = "array"; - break; - case nlohmann::json::value_t::boolean: - type = "boolean"; - break; - case nlohmann::json::value_t::number_unsigned: - type = "number"; - break; - case nlohmann::json::value_t::object: - type = "object"; - break; - case nlohmann::json::value_t::string: - type = "string"; - break; - default: - break; - } - - throw std::runtime_error(boost::str( - boost::format("%s: missing '%s' property (%s expected)") % id % pair.first % type)); - } - } -} - -std::string resources_loader::require_string(const std::string& id, - const nlohmann::json& object, - const std::string& property) const -{ - assert(object.is_object()); - - auto it = object.find(property); - - if (it == object.end() || !it->is_string()) { - throw std::runtime_error(id + ": missing '" + property + "' property (string expected)"); - } - - return *it; -} - -resources_loader::resources_loader(resources_locator& locator) - : m_locator(locator) -{ -} - -game resources_loader::load_game() const -{ - auto value = nlohmann::json::parse(m_locator.read("game.json")); - - if (!value.is_object()) { - throw std::runtime_error("game.json: not a JSON object"); - } - - requires("game.json", value, { - { "name", nlohmann::json::value_t::string }, - { "version", nlohmann::json::value_t::string }, - { "requires", nlohmann::json::value_t::string } - }); - - return game{value["name"], - value["version"], - value["requires"], - value.count("license") > 0 ? value["license"] : "", - value.count("author") > 0 ? value["author"] : ""}; -} - -} // !mlk diff -r a162f380f02e -r 301599387b40 libcommon/malikania/resources_loader.hpp --- a/libcommon/malikania/resources_loader.hpp Sun Jan 22 09:59:14 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,114 +0,0 @@ -/* - * resources_loader.hpp -- load shared resources files - * - * Copyright (c) 2013-2017 Malikania Authors - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef MALIKANIA_RESOURCES_LOADER_HPP -#define MALIKANIA_RESOURCES_LOADER_HPP - -#include -#include - -#include - -#include "resources_locator.hpp" - -namespace mlk { - -class game; - -/** - * \brief Open resources files using a resources_locator. - * - * This class is used to load resources files that are common to the server and - * the client. - * - * \see client_resources_loader - */ -class resources_loader { -private: - resources_locator& m_locator; - -protected: - /** - * Check that an object has the specified properties of the given type. - * - * Throws an error when any of the property is missing or not the correct - * type. - * - * You can use this function when you have lot of properties to extract, - * otherwise, you can use one of the require* or get* functions to avoid - * performances overhead. - * - * \pre object.is_object() - * \param id the resource id - * \param object the object - * \param props the properties - * \throw std::runtime_error when a property is missing / invalid - */ - void requires(const std::string& id, - const nlohmann::json& object, - const std::unordered_map& props) const; - - /** - * Require a string. - * - * \pre object.isObject() - * \param id the resource id - * \param object the object - * \param property the property - * \return the string - * \throw std::runtime_error if the property is not a string or missing - */ - std::string require_string(const std::string& id, - const nlohmann::json& object, - const std::string& property) const; - -public: - /** - * Construct the resources loader. - * - * \param locator the locator - */ - resources_loader(resources_locator& locator); - - /** - * Virtual destructor defaulted. - */ - virtual ~resources_loader() noexcept = default; - - /** - * Get the underlying locator. - * - * \return the locator - */ - inline resources_locator& locator() noexcept - { - return m_locator; - } - - /** - * Load a game. - * - * \return the game - * \throw std::runtime_error on errors - */ - virtual game load_game() const; -}; - -} // !mlk - -#endif // !MALIKANIA_RESOURCES_LOADER_HPP diff -r a162f380f02e -r 301599387b40 libcommon/malikania/resources_locator.cpp --- a/libcommon/malikania/resources_locator.cpp Sun Jan 22 09:59:14 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -/* - * resources_locator.cpp -- file and stream loader - * - * Copyright (c) 2013-2017 Malikania Authors - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include -#include -#include - -#include "resources_locator.hpp" - -namespace mlk { - -directory_resources_locator::directory_resources_locator(std::string path) noexcept - : m_path(std::move(path)) -{ -} - -std::string directory_resources_locator::read(const std::string &id) -{ - std::ifstream in(m_path + "/" + id, std::ifstream::in | std::ifstream::binary); - - if (!in) { - throw std::runtime_error(std::strerror(errno)); - } - - return std::string(std::istreambuf_iterator(in.rdbuf()), std::istreambuf_iterator()); -} - -std::unique_ptr directory_resources_locator::open(const std::string &id) -{ - auto ptr = std::make_unique(m_path + "/" + id); - - if (!(*ptr)) { - throw std::runtime_error(std::strerror(errno)); - } - - return std::move(ptr); -} - -} // !mlk diff -r a162f380f02e -r 301599387b40 libcommon/malikania/resources_locator.hpp --- a/libcommon/malikania/resources_locator.hpp Sun Jan 22 09:59:14 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +0,0 @@ -/* - * resources_locator.hpp -- file and stream loader - * - * Copyright (c) 2013-2017 Malikania Authors - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef MALIKANIA_RESOURCES_LOCATOR_HPP -#define MALIKANIA_RESOURCES_LOCATOR_HPP - -#include -#include -#include - -namespace mlk { - -/** - * \brief Abstract assets locator. - */ -class resources_locator { -public: - /** - * Default destructor. - */ - virtual ~resources_locator() = default; - - /** - * Read a whole resource as a string. - * - * \param id the resource id - * \return the string - * \throw std::runtime_error on any errors - */ - virtual std::string read(const std::string& id) = 0; - - /** - * Open a resource as a stream. - * - * \param id the resource id - * \return the stream - * \throw std::runtime_error on any errors - */ - virtual std::unique_ptr open(const std::string& id) = 0; -}; - -/** - * \brief Load a game from a directory. - */ -class directory_resources_locator : public resources_locator { -private: - std::string m_path; - -public: - /** - * Load the game from the directory. - * - * \param path the base directory - */ - directory_resources_locator(std::string path) noexcept; - - /** - * \copydoc resources_locator::read - */ - std::string read(const std::string& id) override; - - /** - * \copydoc resources_locator::open - */ - std::unique_ptr open(const std::string& id) override; -}; - -} // !mlk - -#endif // !MALIKANIA_RESOURCES_LOCATOR_HPP