Managers: add a VFS to help with #36.

This commit is contained in:
Guillaume Jacquemin 2024-07-11 19:48:02 +02:00
parent 79e2ff38c5
commit e11fa34c09
Signed by: williamjcm
SSH key fingerprint: SHA256:AYLOg+iTV0ElElnlu4vqM4edFazVdRiuQB0Y5LoKc4A
3 changed files with 137 additions and 0 deletions

View file

@ -199,6 +199,8 @@ add_executable(MassBuilderSaveTool
Managers/MassManager.cpp
Managers/ProfileManager.h
Managers/ProfileManager.cpp
Managers/Vfs/VirtualFileSystem.h
Managers/Vfs/VfsDirectory.h
Maps/ArmourSlots.hpp
Maps/BulletLauncherAttachmentStyles.hpp
Maps/BulletLauncherSockets.hpp

View file

@ -0,0 +1,88 @@
#pragma once
// MassBuilderSaveTool
// Copyright (C) 2021-2024 Guillaume Jacquemin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/String.h>
#include <Corrade/Containers/StringView.h>
using namespace Corrade;
namespace mbst { namespace Managers { namespace Vfs {
CORRADE_HAS_TYPE(HasFilename, decltype(T::filename));
template<typename FileType>
class VfsDirectory {
public:
static_assert(HasFilename<FileType>::value && (std::is_same<decltype(FileType::filename), Containers::String>::value || std::is_same<decltype(FileType::filename), Containers::StringView>::value),
"FileType has no string-like member named filename.");
using DirType = VfsDirectory<FileType>;
explicit VfsDirectory(Containers::StringView name): _name{name} {
// ctor
}
VfsDirectory(const VfsDirectory<FileType>& other) = delete;
VfsDirectory& operator=(const VfsDirectory<FileType>& other) = delete;
VfsDirectory(VfsDirectory<FileType>&& other) = default;
VfsDirectory& operator=(VfsDirectory<FileType>&& other) = default;
auto name() const -> Containers::StringView {
return _name;
}
auto subdirs() const -> Containers::ArrayView<const DirType> {
return _subdirs;
}
auto files() const -> Containers::ArrayView<const FileType*> {
return _files;
}
protected:
void buildNestedPath(VfsDirectory<FileType>& root, Containers::ArrayView<const Containers::MutableStringView> components,
FileType& file)
{
if(components.size() > 1) {
bool found = false;
for(auto& subdir : root._subdirs) {
if(subdir._name == components[0]) {
found = true;
buildNestedPath(subdir, components.exceptPrefix(1), file);
break;
}
}
if(!found) {
arrayAppend(root._subdirs, InPlaceInit, components[0]);
buildNestedPath(root._subdirs.back(), components.exceptPrefix(1), file);
}
}
else if(components.size() == 1) {
arrayAppend(root._files, &file);
}
}
Containers::String _name;
Containers::Array<DirType> _subdirs;
Containers::Array<FileType*> _files;
};
}}}

View file

@ -0,0 +1,47 @@
#pragma once
// MassBuilderSaveTool
// Copyright (C) 2021-2024 Guillaume Jacquemin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "VfsDirectory.h"
namespace mbst { namespace Managers { namespace Vfs {
template<typename FileType>
class VirtualFileSystem: public VfsDirectory<FileType> {
public:
explicit VirtualFileSystem():
VfsDirectory<FileType>("")
{
// ctor
}
explicit VirtualFileSystem(Containers::ArrayView<FileType> files):
VfsDirectory<FileType>("")
{
build(files);
}
void build(Containers::ArrayView<FileType> files) {
for(auto& file : files) {
auto components = file.filename.split('/');
CORRADE_INTERNAL_ASSERT(components.size() == 0);
VfsDirectory<FileType>::buildNestedPath(*this, components, file);
}
}
};
}}}