Please fix
This commit is contained in:
parent
f154eac1e9
commit
d377e99d31
@ -15,8 +15,8 @@ namespace Deer {
|
||||
bool includeServer = false;
|
||||
bool includeClient = true;
|
||||
|
||||
std::function<Scope<MeshData>(const Path&)> meshLoadingFunction = nullptr;
|
||||
std::function<Scope<ShaderData>(const Path&)> shaderLoadingFunction = nullptr;
|
||||
std::function<Scope<MeshData>(StorageType)> meshLoadingFunction = nullptr;
|
||||
std::function<Scope<ShaderData>(StorageType)> shaderLoadingFunction = nullptr;
|
||||
#else
|
||||
bool includeServer = true;
|
||||
bool includeClient = false;
|
||||
|
||||
@ -2,8 +2,6 @@
|
||||
#include "DeerCore/Serialization/WorldSettings.h"
|
||||
#include "DeerCore/Tools/Path.h"
|
||||
|
||||
#include <strstream>
|
||||
|
||||
namespace Deer {
|
||||
class World;
|
||||
class WorldSettings;
|
||||
|
||||
@ -13,6 +13,9 @@ namespace Deer {
|
||||
class ResourceManager;
|
||||
typedef uint32_t StorageType;
|
||||
|
||||
StorageType generatePhyisicalStorageId();
|
||||
StorageType generateRuntimeStorageId();
|
||||
|
||||
template <typename DataSource>
|
||||
class StorageBackend {
|
||||
public:
|
||||
@ -57,9 +60,8 @@ namespace Deer {
|
||||
private:
|
||||
struct ResourceData {
|
||||
public:
|
||||
ResourceData(Scope<T> _data, StorageType _storageId) : data(_data), storageId(_storageId) {}
|
||||
Scope<T> data;
|
||||
StorageType storageId;
|
||||
Scope<T> data = nullptr;
|
||||
StorageType storageId = 0;
|
||||
};
|
||||
|
||||
static std::vector<ResourceData> resources;
|
||||
@ -78,7 +80,12 @@ namespace Deer {
|
||||
data = ResourceBuilder<T>::buildResource(*baseData.get());
|
||||
|
||||
Resource<T> resource = Resource<T>::unsafeFromId(resources.size());
|
||||
resources.push_back({storageId, std::move(data)});
|
||||
resources.push_back();
|
||||
|
||||
ResourceData& rd = resources.back();
|
||||
rd.data = std::move(data);
|
||||
rd.storageId = storageId;
|
||||
|
||||
resourceCache[storageId] = resource;
|
||||
|
||||
return resource;
|
||||
@ -98,8 +105,11 @@ namespace Deer {
|
||||
Scope<T> data = ResourceBuilder<T>::buildResource(resourceData);
|
||||
|
||||
Resource<T> resource = Resource<T>::unsafeFromId(resources.size());
|
||||
resources.push_back({std::move(data), storageId});
|
||||
resourceCache[storageId] = resource;
|
||||
resources.push_back({});
|
||||
|
||||
ResourceData& rd = resources.back();
|
||||
rd.data = std::move(data);
|
||||
rd.storageId = storageId;
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
13
Deer/src/DeerRender/Core/StorageTypeGenerator.cpp
Normal file
13
Deer/src/DeerRender/Core/StorageTypeGenerator.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include "DeerRender/Resource.h"
|
||||
#include <random>
|
||||
|
||||
namespace Deer {
|
||||
StorageType generatePhyisicalStorageId() {
|
||||
return std::random_device{}() | 0x80000000;
|
||||
}
|
||||
|
||||
StorageType generateRuntimeStorageId() {
|
||||
return std::random_device{}() & 0x7FFFFFF;
|
||||
}
|
||||
} // namespace Deer
|
||||
@ -6,6 +6,7 @@ namespace Deer {
|
||||
// Sphere parameters
|
||||
constexpr int SPHERE_SEGMENTS = 32; // longitude
|
||||
constexpr int SPHERE_RINGS = 16; // latitude
|
||||
|
||||
} // namespace Builtin
|
||||
|
||||
Resource<GPUMesh> Builtin::sphere() {
|
||||
|
||||
@ -24,70 +24,9 @@ namespace Deer {
|
||||
namespace Scripting {
|
||||
extern asIScriptEngine* scriptEngine;
|
||||
|
||||
ResourceType getResourceType(std::string& dir) {
|
||||
Path path(dir);
|
||||
std::string extension = path.extension().string();
|
||||
|
||||
if (extension == ".obj" || extension == ".fbx" || extension == ".dae" ||
|
||||
extension == ".3ds" || extension == ".ply" || extension == ".stl" ||
|
||||
extension == ".glb" || extension == ".gltf" || extension == ".mtl") {
|
||||
return ResourceType::MESH;
|
||||
}
|
||||
|
||||
if (extension == ".glsl")
|
||||
return ResourceType::SHADER;
|
||||
|
||||
if (extension == ".png")
|
||||
return ResourceType::TEXTURE;
|
||||
|
||||
return ResourceType::NONE;
|
||||
};
|
||||
|
||||
CScriptArray* getResourceFolders(std::string& path_s) {
|
||||
asITypeInfo* arrayString = scriptEngine->GetTypeInfoByName("array<string>");
|
||||
CScriptArray* array = CScriptArray::Create(arrayString);
|
||||
|
||||
Path path_p = Path("Resources") / Path(path_s).lexically_normal();
|
||||
try {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(path_p)) {
|
||||
if (entry.is_directory()) {
|
||||
std::string s = entry.path().lexically_relative("Resources").string();
|
||||
|
||||
array->Resize(array->GetSize() + 1);
|
||||
array->SetValue(array->GetSize() - 1, &s);
|
||||
}
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
DEER_EDITOR_ENGINE_ERROR("Error in getResourceFolders(), {}", e.what());
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
CScriptArray* getResourceFiles(std::string& path_s) {
|
||||
asITypeInfo* arrayString = scriptEngine->GetTypeInfoByName("array<string>");
|
||||
CScriptArray* array = CScriptArray::Create(arrayString);
|
||||
|
||||
Path path_p = Path("Resources") / Path(path_s).lexically_normal();
|
||||
try {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(path_p)) {
|
||||
if (entry.is_regular_file()) {
|
||||
std::string s = entry.path().lexically_relative("Resources").string();
|
||||
|
||||
array->Resize(array->GetSize() + 1);
|
||||
array->SetValue(array->GetSize() - 1, &s);
|
||||
}
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
DEER_EDITOR_ENGINE_ERROR("Error in getResourceFiles(), {}", e.what());
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
Resource<FrameBuffer> createFrameBuffer(std::string& name, int width, int height) {
|
||||
Resource<FrameBuffer> createFrameBuffer(int width, int height) {
|
||||
FrameBufferData spec(width, height, 1, FrameBufferData::FrameBufferType::RGBA8);
|
||||
return ResourceManager<FrameBuffer>::loadResourceFromData(spec, name);
|
||||
return ResourceManager<FrameBuffer>::loadResourceFromData(spec, generateRuntimeStorageId());
|
||||
}
|
||||
|
||||
int frameBuffer_getWidth(Resource<FrameBuffer>& frameBuffer) {
|
||||
|
||||
@ -30,16 +30,9 @@ namespace Deer {
|
||||
TEXTURE = 3,
|
||||
};
|
||||
|
||||
CScriptArray* getResourceFolders(std::string& path);
|
||||
CScriptArray* getResourceFiles(std::string& path);
|
||||
|
||||
// GENERIC RESOURCE
|
||||
template <typename T>
|
||||
bool resource_isValid(Resource<T>& resource) { return resource.isValid(); }
|
||||
template <typename T>
|
||||
std::string resource_getPath(Resource<T>& resource) { return resource.getStorageId(); }
|
||||
template <typename T>
|
||||
std::string resource_getName(Resource<T>& resource) { return Path(resource.getStorageId()).stem().string(); }
|
||||
|
||||
// Frame Buffer
|
||||
int frameBuffer_getWidth(Resource<FrameBuffer>&);
|
||||
@ -49,6 +42,6 @@ namespace Deer {
|
||||
void frameBuffer_resize(int, int, Resource<FrameBuffer>&);
|
||||
void frameBuffer_drawMesh(Resource<GPUMesh>, Resource<Shader>, TransformComponent, WorldCamera, Resource<FrameBuffer>&);
|
||||
|
||||
Resource<FrameBuffer> createFrameBuffer(std::string& name, int sixeX, int sizeY);
|
||||
Resource<FrameBuffer> createFrameBuffer(int sixeX, int sizeY);
|
||||
} // namespace Scripting
|
||||
} // namespace Deer
|
||||
@ -21,8 +21,6 @@ namespace Deer {
|
||||
template <class T>
|
||||
void registerResourceBasics(const char* objName) {
|
||||
REGISTER_EXT_OBJECT_METHOD(scriptEngine, objName, "bool isValid() const", static_cast<bool (*)(Resource<T>&)>(&resource_isValid<T>));
|
||||
REGISTER_EXT_OBJECT_METHOD(scriptEngine, objName, "string get_name() const property", static_cast<std::string (*)(Resource<T>&)>(&resource_getName<T>));
|
||||
REGISTER_EXT_OBJECT_METHOD(scriptEngine, objName, "string get_path() const property", static_cast<std::string (*)(Resource<T>&)>(&resource_getPath<T>));
|
||||
}
|
||||
} // namespace Scripting
|
||||
|
||||
@ -32,9 +30,7 @@ namespace Deer {
|
||||
AS_CHECK(scriptEngine->RegisterObjectType("Texture", sizeof(Resource<Texture>), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_ALLINTS | asGetTypeTraits<Resource<Texture>>()));
|
||||
AS_CHECK(scriptEngine->RegisterObjectType("FrameBuffer", sizeof(Resource<FrameBuffer>), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_ALLINTS | asGetTypeTraits<Resource<FrameBuffer>>()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Resource<FrameBuffer> createFrameBuffer(const std::string name, int sixeX, int sizeY);
|
||||
|
||||
void Scripting::registerResourceFunctions() {
|
||||
|
||||
@ -19,8 +19,8 @@ namespace Deer {
|
||||
|
||||
template <class Archive>
|
||||
void load(Archive& archive) {
|
||||
std::string storageId_mesh;
|
||||
std::string storageId_shader;
|
||||
StorageType storageId_mesh;
|
||||
StorageType storageId_shader;
|
||||
|
||||
archive(cereal::make_nvp("IsActive", component.active));
|
||||
archive(cereal::make_nvp("Mesh", storageId_mesh));
|
||||
|
||||
@ -91,8 +91,7 @@ void main()
|
||||
} // namespace Builtin
|
||||
|
||||
Resource<Shader> Builtin::simpleShader() {
|
||||
Resource<Shader> shader = ResourceManager<Shader>::getResource("Builtin:SimpleShader");
|
||||
|
||||
Resource<Shader> shader = ResourceManager<Shader>::getResource(RESOURCE_BASIC_SHADER_ID);
|
||||
if (shader.isValid())
|
||||
return shader;
|
||||
|
||||
@ -100,6 +99,6 @@ void main()
|
||||
shaderData.fragmentShader = fragmentSimpleShader;
|
||||
shaderData.vertexShader = vertexSimpleShader;
|
||||
|
||||
return ResourceManager<Shader>::loadResourceFromData(shaderData, "Builtin:SimpleShader");
|
||||
return ResourceManager<Shader>::loadResourceFromData(shaderData, RESOURCE_BASIC_SHADER_ID);
|
||||
}
|
||||
} // namespace Deer
|
||||
@ -93,7 +93,7 @@ namespace Deer {
|
||||
meshData.getIndexData()[i] = indices[i];
|
||||
}
|
||||
|
||||
return ResourceManager<GPUMesh>::loadResourceFromData(meshData, "tmp&&");
|
||||
return ResourceManager<GPUMesh>::loadResourceFromData(meshData, generateRuntimeStorageId());
|
||||
}
|
||||
|
||||
VoxelBuilder::VoxelData& VoxelBuilder::getVoxelData(int x, int y, int z) {
|
||||
|
||||
@ -12,8 +12,14 @@ namespace Deer {
|
||||
template <>
|
||||
class StorageBackend<EditorDataSource> {
|
||||
public:
|
||||
template <typename T>
|
||||
static Scope<T> load(StorageType storageId);
|
||||
|
||||
template <>
|
||||
static Scope<MeshData> load(StorageType storageId);
|
||||
template <>
|
||||
static Scope<ShaderData> load(StorageType storageId);
|
||||
template <>
|
||||
static Scope<TextureData> load(StorageType storageId);
|
||||
};
|
||||
} // namespace Deer
|
||||
@ -7,9 +7,7 @@
|
||||
#include "DeerRender/Universe.h"
|
||||
#include "DeerRender/World.h"
|
||||
|
||||
#include "DeerStudio/EditorDataImporter.h"
|
||||
#include "DeerStudio/EditorDataSource.h"
|
||||
#include "DeerStudio/ResourceDataSource.h"
|
||||
#include "DeerStudio/ResourceStorageBackend.h"
|
||||
#include "DeerStudio/StudioResources.h"
|
||||
|
||||
// TMP
|
||||
|
||||
@ -1,59 +0,0 @@
|
||||
#include "DeerStudio/EditorDataSource.h"
|
||||
#include "DeerRender/Log.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
namespace Deer {
|
||||
StorageData StorageBackend<EditorDataSource>::loadData(const std::string& location) {
|
||||
Path path = Path("Editor") / location;
|
||||
|
||||
if (!std::filesystem::exists(path)) {
|
||||
return StorageData();
|
||||
}
|
||||
|
||||
std::ifstream file(path, std::ios::binary | std::ios::ate);
|
||||
if (!file.is_open()) {
|
||||
DEER_CORE_ERROR("Failed to open file '{}'", location.c_str());
|
||||
return StorageData();
|
||||
}
|
||||
|
||||
auto size = file.tellg();
|
||||
if (size <= 0) {
|
||||
DEER_CORE_ERROR("File '{}' is empty or has invalid size", location.c_str());
|
||||
return StorageData();
|
||||
}
|
||||
|
||||
StorageData data = StorageData(size);
|
||||
uint8_t* data_raw = data.getData();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
if (!file.read(reinterpret_cast<char*>(data_raw), size)) {
|
||||
DEER_CORE_ERROR("Failed to read full file '{}'", location.c_str());
|
||||
return StorageData();
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void StorageBackend<EditorDataSource>::saveData(const std::string& location, const StorageData& data) {
|
||||
Path path = Path("Editor") / location;
|
||||
std::ofstream file(path, std::ios::binary);
|
||||
if (!file.is_open()) {
|
||||
DEER_CORE_ERROR("Failed to open file '{}' for writing", location.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t* data_raw = data.getData();
|
||||
size_t size = data.getSize();
|
||||
|
||||
if (!file.write(reinterpret_cast<const char*>(data_raw), size)) {
|
||||
DEER_CORE_ERROR("Failed to write full data to file '{}'", location.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
file.flush();
|
||||
}
|
||||
|
||||
} // namespace Deer
|
||||
Loading…
x
Reference in New Issue
Block a user