| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- //
- // config.h
- // pokemon-engine
- //
- // Created by Sam Jaffe on 12/22/18.
- // Copyright © 2018 Sam Jaffe. All rights reserved.
- //
- #pragma once
- #include <filesystem>
- #include <map>
- #include <memory>
- #include <string>
- #include <json/forwards.h>
- namespace engine::env {
- class Config {
- private:
- std::filesystem::path config_path_{""};
- std::filesystem::path resource_path_;
- std::map<std::string, std::string> properties_;
- public:
- /**
- * @brief Load this configuration from a JSON file on disk.
- * @param filepath The absolute path to a JSON config file.
- * @throws std::logic_error if any of the following:
- * - filepath is empty (i.e. Config cfg = Config("");)
- * - filepath is not an absolute UNIX-style path
- * - The file is readable and exists.
- */
- Config(std::filesystem::path const & filepath);
- ~Config();
- /**
- * @brief Obtain the absolute path to a resource, as determined by the
- * properties loaded into this object. For example,
- * @code std::string image_path = cfg.resource("sprites/nurse_joy.png");
- * @param rel_path The relative path to the resource file we are searching
- * for.
- * @return The absolute path to the file
- */
- std::filesystem::path resource(std::filesystem::path const & rel_path) const;
- /**
- * @brief Fetch a singluar property that exists within the object
- * @param name The property name being fetched
- * @param fallback A fallback value in case that the object is absent
- * @return The property being read in
- */
- std::string const & property(std::string const & name,
- std::string const & fallback) const;
- protected:
- /**
- * @brief Construct a config from a raw json blob. Primarily for testing.
- * @param json A json object containing certain properties
- */
- Config(Json::Value const & json,
- std::filesystem::path const & config_path = "/");
- private:
- std::filesystem::path fix(std::filesystem::path input) const;
- };
- }
|