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

75 lines
1.7 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-08 20:42:15 -03:00
#include "../game/servicecontainer.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>
namespace xna {
class ContentManager {
public:
2024-05-05 15:50:17 -03:00
friend class ContentReader;
2024-05-27 16:44:01 -03:00
ContentManager(String const& rootDirectory, sptr<IServiceProvider> const& services) :
2024-05-06 14:54:13 -03:00
_rootDirectory(rootDirectory){
2024-05-06 11:35:27 -03:00
_services = services;
2024-05-08 20:42:15 -03:00
};
2024-04-28 20:19:37 -03:00
2024-05-27 16:44:01 -03:00
static sptr<IServiceProvider> 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;
}
template <typename T>
2024-06-01 20:33:35 -03:00
auto Load(String const& assetName) {
if (assetName.empty()) {
2024-06-01 20:45:00 -03:00
return XnaHelper::ReturnDefaultOrNull<T>();
2024-06-01 20:33:35 -03:00
}
2024-04-28 20:19:37 -03:00
auto obj2 = ReadAsset<T>(assetName);
2024-05-06 14:54:13 -03:00
2024-04-28 20:19:37 -03:00
return obj2;
2024-05-08 20:42:15 -03:00
}
2024-04-28 20:19:37 -03:00
protected:
2024-04-28 20:19:37 -03:00
template <typename T>
2024-06-01 20:33:35 -03:00
auto ReadAsset(String const& assetName) {
2024-04-28 20:19:37 -03:00
auto input = OpenStream(assetName);
2024-05-09 09:13:26 -03:00
if (input->IsClosed())
2024-06-01 20:45:00 -03:00
return XnaHelper::ReturnDefaultOrNull<T>();
2024-05-09 09:13:26 -03:00
auto contentReader = ContentReader::Create(this, input, assetName);
2024-06-01 20:33:35 -03:00
auto asset = contentReader->ReadAsset<T>();
return asset;
2024-04-28 20:19:37 -03:00
}
sptr<Stream> OpenStream(String const& assetName) {
String filePath = _rootDirectory + "\\" + assetName + contentExtension;
2024-05-09 09:13:26 -03:00
const auto stream = New<FileStream>(filePath, FileMode::Open);
//const auto stream = New<FileStream>(filePath);
2024-04-28 20:19:37 -03:00
return reinterpret_pointer_cast<Stream>(stream);
}
private:
2024-05-08 20:42:15 -03:00
String _rootDirectory;
2024-05-05 15:50:17 -03:00
std::vector<Byte> byteBuffer;
2024-05-06 11:35:27 -03:00
2024-05-08 20:42:15 -03:00
inline const static String contentExtension = ".xnb";
2024-05-27 16:44:01 -03:00
inline static sptr<IServiceProvider> _services = nullptr;
2024-04-28 20:19:37 -03:00
};
}
#endif