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

106 lines
2.8 KiB
C++
Raw Permalink Normal View History

2024-04-28 20:19:37 -03:00
#ifndef XNA_CONTENT_MANAGER_HPP
#define XNA_CONTENT_MANAGER_HPP
2024-06-03 21:55:09 -03:00
#include "../csharp/service.hpp"
#include "../csharp/stream.hpp"
#include "../default.hpp"
2024-05-05 15:50:17 -03:00
#include "reader.hpp"
2024-04-28 20:19:37 -03:00
namespace xna {
2024-06-02 20:36:41 -03:00
//The run-time component which loads managed objects from the binary files produced by the design time content pipeline.
2024-06-03 10:13:59 -03:00
class ContentManager : public std::enable_shared_from_this<ContentManager> {
2024-04-28 20:19:37 -03:00
public:
2024-06-02 20:36:41 -03:00
ContentManager(sptr<IServiceProvider> const& services) :
2024-07-13 22:50:52 -03:00
rootDirectory("") {
serviceProvider = services;
2024-06-02 20:36:41 -03:00
};
2024-05-05 15:50:17 -03:00
2024-06-02 20:36:41 -03:00
ContentManager(sptr<IServiceProvider> const& services, String const& rootDirectory) :
2024-07-13 22:50:52 -03:00
rootDirectory(rootDirectory){
serviceProvider = services;
2024-05-08 20:42:15 -03:00
};
2024-04-28 20:19:37 -03:00
2024-06-02 20:36:41 -03:00
//Gets the service provider associated with the ContentManager.
sptr<IServiceProvider> ServiceProvider() const {
2024-07-13 22:50:52 -03:00
return serviceProvider;
2024-05-06 10:32:17 -03:00
}
2024-06-02 20:36:41 -03:00
//Gets or sets the root directory associated with this ContentManager.
2024-04-28 20:19:37 -03:00
constexpr String RootDirectory() const {
2024-07-13 22:50:52 -03:00
return rootDirectory;
2024-04-28 20:19:37 -03:00
}
2024-06-02 20:36:41 -03:00
//Gets or sets the root directory associated with this ContentManager.
2024-04-28 20:19:37 -03:00
void RootDirectory(String const& value) {
2024-07-13 22:50:52 -03:00
rootDirectory = value;
2024-04-28 20:19:37 -03:00
}
2024-06-02 20:36:41 -03:00
//Loads an asset that has been processed by the Content Pipeline.
2024-04-28 20:19:37 -03:00
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
}
if constexpr (XnaHelper::is_shared_ptr<T>::value) {
2024-07-13 22:50:52 -03:00
if (loadedAssets.contains(assetName)) {
auto& voidAsset = loadedAssets[assetName];
using TYPE = T::element_type;
auto asset = reinterpret_pointer_cast<TYPE>(voidAsset);
return asset;
}
}
2024-04-28 20:19:37 -03:00
2024-06-02 20:36:41 -03:00
const auto obj2 = ReadAsset<T>(assetName);
2024-05-06 14:54:13 -03:00
if constexpr (XnaHelper::is_shared_ptr<T>::value) {
if(obj2)
2024-07-13 22:50:52 -03:00
loadedAssets.emplace( assetName, obj2 );
}
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
//Disposes all data that was loaded by this ContentManager.
void Unload() {
2024-07-13 22:50:52 -03:00
loadedAssets.clear();
}
2024-06-02 20:36:41 -03:00
//Gets the service provider associated with the main Game.
static sptr<IServiceProvider> GameServiceProvider() {
2024-07-13 22:50:52 -03:00
return mainGameService;
2024-06-02 20:36:41 -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
2024-06-02 20:36:41 -03:00
if (!input)
2024-06-01 20:45:00 -03:00
return XnaHelper::ReturnDefaultOrNull<T>();
2024-05-09 09:13:26 -03:00
2024-06-03 10:13:59 -03:00
const auto _this = shared_from_this();
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
}
2024-06-02 20:36:41 -03:00
sptr<Stream> OpenStream(String const& assetName) const;
2024-04-28 20:19:37 -03:00
private:
2024-06-02 20:36:41 -03:00
friend class ContentReader;
friend class Game;
2024-07-13 22:50:52 -03:00
String rootDirectory;
sptr<IServiceProvider> serviceProvider = nullptr;
std::map<String, sptr<void>> loadedAssets;
2024-05-06 11:35:27 -03:00
2024-07-13 22:50:52 -03:00
inline static sptr<IServiceProvider> mainGameService = nullptr;
2024-05-08 20:42:15 -03:00
inline const static String contentExtension = ".xnb";
2024-04-28 20:19:37 -03:00
};
}
#endif