1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00
xn65/framework/content/manager.hpp

89 lines
2.0 KiB
C++
Raw Normal View History

2024-04-28 20:19:37 -03:00
#ifndef XNA_CONTENT_MANAGER_HPP
#define XNA_CONTENT_MANAGER_HPP
2024-05-05 15:50:17 -03:00
#include "../csharp/stream.hpp"
2024-04-28 20:19:37 -03:00
#include "../default.hpp"
2024-05-05 15:50:17 -03:00
#include "reader.hpp"
#include <algorithm>
2024-04-28 20:19:37 -03:00
#include <filesystem>
#include <map>
2024-05-06 10:32:17 -03:00
#include "../game/servicecontainer.hpp"
2024-04-28 20:19:37 -03:00
namespace xna {
class ContentManager {
public:
2024-05-05 15:50:17 -03:00
friend class ContentReader;
2024-05-06 10:32:17 -03:00
ContentManager(String const& rootDirectory, sptr<GameServiceContainer> const& services) :
2024-05-06 11:35:27 -03:00
_rootDirectory(rootDirectory),
_path(rootDirectory){
_services = services;
};
2024-04-28 20:19:37 -03:00
virtual ~ContentManager(){
Unload();
}
2024-05-06 11:35:27 -03:00
static sptr<GameServiceContainer> Services() {
2024-05-06 10:32:17 -03:00
return _services;
}
2024-04-28 20:19:37 -03:00
constexpr String RootDirectory() const {
return _rootDirectory;
}
void RootDirectory(String const& value) {
_rootDirectory = value;
_path = value;
}
virtual void Unload() {
if (_loadedAssets.empty())
return;
_loadedAssets.clear();
}
template <typename T>
2024-05-06 11:35:27 -03:00
sptr<T> Load(String const& assetName) {
2024-04-28 20:19:37 -03:00
if (assetName.empty()) return nullptr;
if (_loadedAssets.contains(assetName)) {
auto& ptr = _loadedAssets[assetName];
auto obj1 = reinterpret_pointer_cast<T>(ptr);
2024-04-28 20:19:37 -03:00
2024-05-06 11:35:27 -03:00
return obj1;
2024-04-28 20:19:37 -03:00
}
auto obj2 = ReadAsset<T>(assetName);
//auto obj3 = reinterpret_pointer_cast<T>(obj2);
2024-04-28 20:19:37 -03:00
return obj2;
}
protected:
2024-04-28 20:19:37 -03:00
template <typename T>
2024-05-06 11:35:27 -03:00
sptr<T> ReadAsset(String const& assetName) {
2024-04-28 20:19:37 -03:00
auto input = OpenStream(assetName);
auto contentReader = ContentReader::Create(this, input, assetName);
return contentReader->ReadAsset<T>();
2024-04-28 20:19:37 -03:00
}
sptr<Stream> OpenStream(String const& assetName) {
String filePath = _rootDirectory + "\\" + assetName + contentExtension;
const auto stream = New<FileStream>(filePath);
return reinterpret_pointer_cast<Stream>(stream);
}
private:
String _rootDirectory;
std::filesystem::path _path;
std::map<String, sptr<void>> _loadedAssets;
inline const static String contentExtension = ".xnb";
2024-05-05 15:50:17 -03:00
std::vector<Byte> byteBuffer;
2024-05-06 11:35:27 -03:00
inline static sptr<GameServiceContainer> _services = nullptr;
2024-04-28 20:19:37 -03:00
};
}
#endif