mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 08:08:12 +00:00
LibCore: Add Resource for platform agnostic application resource loading
The first implementation is simply raw files.
This commit is contained in:
parent
f4a89c31c6
commit
0d417cd604
8 changed files with 393 additions and 0 deletions
48
Userland/Libraries/LibCore/ResourceImplementationFile.cpp
Normal file
48
Userland/Libraries/LibCore/ResourceImplementationFile.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <LibCore/DirIterator.h>
|
||||
#include <LibCore/Resource.h>
|
||||
#include <LibCore/ResourceImplementationFile.h>
|
||||
|
||||
namespace Core {
|
||||
ResourceImplementationFile::ResourceImplementationFile(String base_directory)
|
||||
: m_base_directory(move(base_directory))
|
||||
{
|
||||
}
|
||||
|
||||
ErrorOr<NonnullRefPtr<Resource>> ResourceImplementationFile::load_from_resource_scheme_uri(StringView uri)
|
||||
{
|
||||
StringView const resource_scheme = "resource://"sv;
|
||||
|
||||
VERIFY(uri.starts_with(resource_scheme));
|
||||
|
||||
auto path = TRY(String::from_utf8(uri.substring_view(resource_scheme.length())));
|
||||
auto full_path = TRY(String::from_deprecated_string(LexicalPath::join(m_base_directory, path).string()));
|
||||
if (is_directory(full_path))
|
||||
return make_directory_resource(move(path));
|
||||
|
||||
return make_resource(path, TRY(MappedFile::map(full_path)));
|
||||
}
|
||||
|
||||
Vector<String> ResourceImplementationFile::child_names_for_resource_scheme(Resource const& resource)
|
||||
{
|
||||
Vector<String> children;
|
||||
Core::DirIterator it(resource.filesystem_path().release_value().to_deprecated_string(), Core::DirIterator::SkipParentAndBaseDir);
|
||||
while (it.has_next())
|
||||
children.append(MUST(String::from_deprecated_string(it.next_path())));
|
||||
|
||||
return children;
|
||||
}
|
||||
|
||||
Optional<String> ResourceImplementationFile::filesystem_path_for_resource_scheme(String const& relative_path)
|
||||
{
|
||||
return MUST(String::from_deprecated_string(LexicalPath::join(m_base_directory, relative_path).string()));
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue