arapelle / arba-rsce

MIT License
0 stars 0 forks source link

resource_manager handles resource libraries. #7

Open arapelle opened 1 year ago

arapelle commented 1 year ago
./dir/subdir/rsc.ext       // real relative path
R:/dir/subdir/rsc.ext     // real absolute path

rscm.get(">FPATH:/dir/sdir/tile.txt")
rscm.get("*RSCLIB:/dir/sdir/tile.txt")
rscm.get("#RSCPACK:/dir/sdir/tile.txt")
rscm.get("VROOT:/dir/sdir/tile.txt")

rscm.register_lib(strn::string64 lib_name, std::optional<...>(*)(...));
arapelle commented 1 year ago
#include "main.hpp"
#include <strn/string64.hpp>
#include <strn/io.hpp>
#include <regex>
#include <filesystem>
#include <iostream>
#include <bit>

rsce::resource_manager rscm;
Image_sptr image_sptr = rscm.get_shared<Image>(">IMG:/dir/subdir/icon.png");
// Image_sptr image_sptr = rscm.get_shared<Image>("#IMG:/dir/subdir/icon.png");
Image_sptr image_sptr = rscm.get_shared<Image>("*IMG:/dir/subdir/icon.png");
Image_sptr image_sptr = rscm.get_shared<Image>("IMG:/dir/subdir/icon.png"); // <=> Image_sptr image_sptr = rscm.get_shared<Image>("?IMG:/dir/subdir/icon.png");

 virtual root: [a-Z][a-Z_-.]{1-7}

class resource_manager
{
public:
    using path_char_type = std::filesystem::path::value_type;
    using path_view = std::basic_string_view<path_char_type>;

    constexpr static path_char_type file_marker = path_char_type('>');
    constexpr static path_char_type rscpack_marker = path_char_type('#');
    constexpr static path_char_type rsclib_marker = path_char_type('*');
    constexpr static path_char_type unknown_source_marker = path_char_type('?');

    template <class T>
    std::shared_ptr<T> load(std::filesystem::path& rsc_path)
    {
        const path_char_type& ch = rsc_path.native().front();
        switch (ch)
        {
        case file_marker:
        {
            path_view rsc_path_view(&rsc_path.native()[1], rsc_path.native().size() - 1);
            return load_resource_from_file<T>(rsc_path_view);
        }
        case rscpack_marker:
        {
            path_view rsc_path_view(&rsc_path.native()[1], rsc_path.native().size() - 1);
            return load_resource_from_rscpack<T>(rsc_path_view);
        }
//        case rsclib_marker:
//        {
//            path_view rsc_path_view(&rsc_path.native()[1], rsc_path.native().size() - 1);
//            return load_resource_from_rsclib<T>(rsc_path_view);
//        }
        case unknown_source_marker:
        {
            path_view rsc_path_view(&rsc_path.native()[1], rsc_path.native().size() - 1);
            return load_resource_from_unkwown_source<T>(rsc_path_view);
        }
        default:
        {
            if ((ch >= path_char_type('a') && ch <= path_char_type('z'))
                || (ch >= path_char_type('A') && ch <= path_char_type('Z')))
            {
                break;
            }
            throw std::runtime_error("Wrong format.");
        }
        }
    }

    template <class T>
    std::shared_ptr<T> load_resource_from_file(path_view rsc_path)
    {
        strn::string64 vroot;
        if (virtual_system_.extract_vroot_rpath(vroot, rsc_path))
        {
        }
        else
        {
        }
    }

private:
    vlfs::virtual_filesystem virtual_system_;
};

int main(int argc, char** argv)
{
    std::cout << "EXIT SUCCESS" << std::endl;
    return EXIT_SUCCESS;
}