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 14:54:13 -03:00
|
|
|
_rootDirectory(rootDirectory){
|
2024-05-06 11:35:27 -03:00
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Unload() {
|
|
|
|
if (_loadedAssets.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
_loadedAssets.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2024-05-08 10:51:49 -03:00
|
|
|
T Load(String const& assetName) {
|
|
|
|
if (assetName.empty()) return T();
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-08 10:51:49 -03:00
|
|
|
/*if (_loadedAssets.contains(assetName)) {
|
2024-05-06 09:58:40 -03:00
|
|
|
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-05-08 10:51:49 -03:00
|
|
|
}*/
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-06 09:58:40 -03:00
|
|
|
auto obj2 = ReadAsset<T>(assetName);
|
2024-05-06 14:54:13 -03:00
|
|
|
//auto voidAsset = reinterpret_pointer_cast<void>(obj2);
|
2024-05-08 10:51:49 -03:00
|
|
|
//_loadedAssets.insert({ assetName , obj2 });
|
2024-05-06 14:54:13 -03:00
|
|
|
|
2024-04-28 20:19:37 -03:00
|
|
|
return obj2;
|
|
|
|
}
|
|
|
|
|
2024-05-03 11:49:01 -03:00
|
|
|
protected:
|
2024-04-28 20:19:37 -03:00
|
|
|
template <typename T>
|
2024-05-08 10:51:49 -03:00
|
|
|
T ReadAsset(String const& assetName) {
|
2024-04-28 20:19:37 -03:00
|
|
|
auto input = OpenStream(assetName);
|
2024-05-03 11:49:01 -03:00
|
|
|
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::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
|