Skip to content

Commit

Permalink
resman: Fix create_resource C binding, make ptr param to copy_resourc…
Browse files Browse the repository at this point in the history
…e const
  • Loading branch information
caseif committed Dec 6, 2024
1 parent b181d2c commit 903ae28
Show file tree
Hide file tree
Showing 29 changed files with 53 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub trait ResourceLoader {
handle: WrappedResourceLoader,
manager: ResourceManager,
prototype: ResourcePrototype,
src_data: *mut u8,
src_data: *const u8,
) -> Result<*mut u8, ResourceError>;

fn unload_resource(&mut self, handle: WrappedResourceLoader, ptr: *mut u8);
Expand Down Expand Up @@ -131,7 +131,7 @@ unsafe extern "C" fn copy_resource_proxy(
loader: argus_resource_loader_t,
manager: argus_resource_manager_t,
prototype: argus_resource_prototype_t,
src: *mut c_void,
src: *const c_void,
src_len: usize,
user_data: *mut c_void,
) -> VoidPtrOrResourceError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ impl ResourceManager {
uid_c.as_ptr(),
media_type_c.as_ptr(),
data.as_ptr().cast(),
data.len(),
))
}
}
Expand Down
5 changes: 2 additions & 3 deletions engine/auxiliary/resman_rustabi/src/resman_cabi/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub type argus_resource_copy_fn_t = ::std::option::Option<
loader: argus_resource_loader_t,
manager: argus_resource_manager_t,
proto: argus_resource_prototype_t,
src: *mut ::std::os::raw::c_void,
src: *const ::std::os::raw::c_void,
src_len: usize,
data: *mut ::std::os::raw::c_void,
) -> VoidPtrOrResourceError,
Expand Down Expand Up @@ -167,8 +167,7 @@ extern "C" {
mgr: argus_resource_manager_t,
uid: *const ::std::os::raw::c_char,
media_type: *const ::std::os::raw::c_char,
data: *const ::std::os::raw::c_void,
len: usize,
handle: *const ::std::os::raw::c_void,
) -> ResourceOrResourceError;
pub fn argus_resource_loader_new(
media_types: *const *const ::std::os::raw::c_char,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace argus {
std::istream &stream, size_t size) override;

Result<void *, ResourceError> copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) override;
const void *src, std::optional<std::type_index> type) override;

void unload(void *data_ptr) override;

Expand Down
6 changes: 3 additions & 3 deletions engine/dynamic/render_opengl/src/loader/shader_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ namespace argus {
}

Result<void *, ResourceError> ShaderLoader::copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) {
const void *src, std::optional<std::type_index> type) {
UNUSED(manager);

if (type != std::type_index(typeid(Shader))) {
if (type.has_value() && type.value() != std::type_index(typeid(Shader))) {
return make_err_result(ResourceErrorReason::UnexpectedReferenceType, proto);
}

// no dependencies to load so we can just do a blind copy

return make_ok_result(new Shader(*static_cast<Shader *>(src)));
return make_ok_result(new Shader(*static_cast<const Shader *>(src)));
}

void ShaderLoader::unload(void *const data_buf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace argus {
std::istream &stream, size_t size) override;

Result<void *, ResourceError> copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) override;
const void *src, std::optional<std::type_index> type) override;

void unload(void *data_ptr) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ namespace argus {
}

Result<void *, ResourceError> ShaderLoader::copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) {
const void *src, std::optional<std::type_index> type) {
UNUSED(manager);

if (type != std::type_index(typeid(Shader))) {
if (type.has_value() && type.value() != std::type_index(typeid(Shader))) {
return make_err_result(ResourceErrorReason::UnexpectedReferenceType, proto);
}

// no dependencies to load so we can just do a blind copy

return make_ok_result(new Shader(*static_cast<Shader *>(src)));
return make_ok_result(new Shader(*static_cast<const Shader *>(src)));
}

void ShaderLoader::unload(void *const data_buf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl ResourceLoader for ShaderLoader {
_handle: WrappedResourceLoader,
_manager: ResourceManager,
_prototype: ResourcePrototype,
src_data: *mut u8,
src_data: *const u8,
) -> Result<*mut u8, ResourceError> {
let shader: &Shader = unsafe { *src_data.cast() };
Ok(Box::into_raw(Box::new(shader.copy())).cast())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace argus {
std::istream &stream, size_t size) override;

Result<void *, ResourceError> copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) override;
const void *src, std::optional<std::type_index> type) override;

void unload(void *data_ptr) override;

Expand Down
6 changes: 3 additions & 3 deletions engine/dynamic/render_opengles/src/loader/shader_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ namespace argus {
}

Result<void *, ResourceError> ShaderLoader::copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) {
const void *src, std::optional<std::type_index> type) {
UNUSED(manager);

if (type != std::type_index(typeid(Shader))) {
if (type.has_value() && type.value() != std::type_index(typeid(Shader))) {
return make_err_result(ResourceErrorReason::UnexpectedReferenceType, proto);
}

// no dependencies to load so we can just do a blind copy

return make_ok_result(new Shader(*static_cast<Shader *>(src)));
return make_ok_result(new Shader(*static_cast<const Shader *>(src)));
}

void ShaderLoader::unload(void *const data_buf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace argus {
std::istream &stream, size_t size) override;

Result<void *, ResourceError> copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) override;
const void *src, std::optional<std::type_index> type) override;

void unload(void *data_ptr) override;

Expand Down
6 changes: 3 additions & 3 deletions engine/dynamic/render_vulkan/src/loader/shader_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ namespace argus {
}

Result<void *, ResourceError> ShaderLoader::copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) {
const void *src, std::optional<std::type_index> type) {
UNUSED(manager);

if (type != std::type_index(typeid(Shader))) {
if (type.has_value() && type.value() != std::type_index(typeid(Shader))) {
return make_err_result(ResourceErrorReason::UnexpectedReferenceType, proto);
}

// no dependencies to load so we can just do a blind copy

return make_ok_result(new Shader(*static_cast<Shader *>(src)));
return make_ok_result(new Shader(*static_cast<const Shader *>(src)));
}

void ShaderLoader::unload(void *const data_buf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace argus {
std::istream &stream, size_t size) override;

[[nodiscard]] Result<void *, ResourceError> copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) override;
const void *src, std::optional<std::type_index> type) override;

void unload(void *data_ptr) override;
};
Expand Down
6 changes: 3 additions & 3 deletions engine/static/game2d/src/loader/sprite_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,12 @@ namespace argus {
}

Result<void *, ResourceError> SpriteLoader::copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) {
if (type == std::type_index(typeid(SpriteDef))) {
const void *src, std::optional<std::type_index> type) {
if (type.has_value() && type.value() == std::type_index(typeid(SpriteDef))) {
return make_err_result(ResourceErrorReason::UnexpectedReferenceType, proto);
}

auto &src_sprite = *static_cast<SpriteDef *>(src);
auto &src_sprite = *static_cast<const SpriteDef *>(src);

std::vector<std::string> dep_uids;

Expand Down
2 changes: 1 addition & 1 deletion engine/static/game2d_rs/src/sprite_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl ResourceLoader for SpriteLoader {
_handle: WrappedResourceLoader,
_manager: ResourceManager,
_prototype: ResourcePrototype,
src_data: *mut u8,
src_data: *const u8,
) -> Result<*mut u8, ResourceError> {
let defn: &SpriteDefinition = unsafe { *src_data.cast() };
Ok(Box::into_raw(Box::new(defn.clone())).cast())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace argus {
std::istream &stream, size_t size) override;

[[nodiscard]] Result<void *, ResourceError> copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) override;
const void *src, std::optional<std::type_index> type) override;

void unload(void *data_ptr) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace argus {
std::istream &stream, size_t size) override;

[[nodiscard]] Result<void *, ResourceError> copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) override;
const void *src, std::optional<std::type_index> type) override;

void unload(void *data_ptr) override;

Expand Down
6 changes: 3 additions & 3 deletions engine/static/render/src/loader/material_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ namespace argus {
}

Result<void *, ResourceError> MaterialLoader::copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) {
if (type != std::type_index(typeid(Material))) {
const void *src, std::optional<std::type_index> type) {
if (type.has_value() && type.value() != std::type_index(typeid(Material))) {
return make_err_result(ResourceErrorReason::UnexpectedReferenceType, proto);
}

auto &src_mat = *reinterpret_cast<Material *>(src);
auto &src_mat = *reinterpret_cast<const Material *>(src);

// need to load shaders as dependencies before doing a copy

Expand Down
6 changes: 3 additions & 3 deletions engine/static/render/src/loader/texture_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,16 @@ namespace argus {
}

Result<void *, ResourceError> PngTextureLoader::copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) {
const void *src, std::optional<std::type_index> type) {
UNUSED(manager);

if (type != std::type_index(typeid(TextureData))) {
if (type.has_value() && type.value() != std::type_index(typeid(TextureData))) {
return make_err_result(ResourceErrorReason::UnexpectedReferenceType, proto);
}

// no dependencies to load so we can just do a blind copy

return make_ok_result(new TextureData(*static_cast<TextureData *>(src)));
return make_ok_result(new TextureData(*static_cast<const TextureData *>(src)));
}

void PngTextureLoader::unload(void *const data_buf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef VoidPtrOrResourceError (*argus_resource_load_fn_t)(argus_resource_loader

typedef VoidPtrOrResourceError (*argus_resource_copy_fn_t)(argus_resource_loader_t loader,
argus_resource_manager_t manager, argus_resource_prototype_t proto,
void *src, size_t src_len, void *data);
const void *src, size_t src_len, void *data);

typedef void (*argus_resource_unload_fn_t)(argus_resource_loader_t loader, void *ptr, size_t len, void *user_data);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void argus_resource_manager_get_resource_async(argus_resource_manager_t mgr, con
void(*callback)(ResourceOrResourceError));

ResourceOrResourceError argus_resource_manager_create_resource(argus_resource_manager_t mgr, const char *uid,
const char *media_type, const void *data, size_t len);
const char *media_type, const void *handle);

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace argus {
std::istream &stream, size_t size) = 0;

virtual Result<void *, ResourceError> copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) = 0;
const void *src, std::optional<std::type_index> type) = 0;

/**
* @brief Performs necessary deinitialization for loaded resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,16 @@ namespace argus {

/**
* @brief Creates a Resource with the given UID from data presently
* in memory.
* in memory without validating the source object type.
*
* @param uid The UID of the new Resource.
* @param media_type The media type of the Resource.
* @param data The in-memory data of the Resource.
* @param len The size of the Resource in bytes.
* @param handle A handle to the object in memory to be copied.
*
* @return The created Resource.
*/
[[nodiscard]] Result<Resource &, ResourceError> create_resource(const std::string &uid,
const std::string &media_type, const void *data, size_t len);
[[nodiscard]] Result<Resource &, ResourceError> create_resource_unchecked(const std::string &uid,
const std::string &media_type, const void *handle);

template<typename T>
[[nodiscard]] Result<Resource &, ResourceError> create_resource(const std::string &uid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ProxiedResourceLoader : public ResourceLoader {
std::istream &stream, size_t size) override;

Result<void *, ResourceError> copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) override;
const void *src, std::optional<std::type_index> type) override;

void unload(void *data_ptr) override;

Expand Down
2 changes: 1 addition & 1 deletion engine/static/resman/src/cabi/resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Result<void *, ResourceError> ProxiedResourceLoader::load(ResourceManager &manag
}

Result<void *, ResourceError> ProxiedResourceLoader::copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) {
const void *src, std::optional<std::type_index> type) {
UNUSED(type);
auto wrapped_proto = argus_resource_prototype_t {
proto.uid.c_str(),
Expand Down
4 changes: 2 additions & 2 deletions engine/static/resman/src/cabi/resource_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ void argus_resource_manager_get_resource_async(argus_resource_manager_t mgr, con
}

ResourceOrResourceError argus_resource_manager_create_resource(argus_resource_manager_t mgr, const char *uid,
const char *media_type, const void *data, size_t len) {
return _wrap_result(_as_ref(mgr).create_resource(uid, media_type, data, len));
const char *media_type, const void *handle) {
return _wrap_result(_as_ref(mgr).create_resource_unchecked(uid, media_type, handle));
}

#ifdef __cplusplus
Expand Down
12 changes: 5 additions & 7 deletions engine/static/resman/src/resource_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,9 @@ namespace argus {
}
}

Result<Resource &, ResourceError> ResourceManager::create_resource(const std::string &uid,
const std::string &media_type, const void *data, size_t len) {
Logger::default_logger().debug("Creating ad hoc resource %s (%zu bytes)", uid.c_str(), len);
Result<Resource &, ResourceError> ResourceManager::create_resource_unchecked(const std::string &uid,
const std::string &media_type, const void *data) {
Logger::default_logger().debug("Creating ad hoc resource %s", uid.c_str());

if (m_pimpl->loaded_resources.find(uid) != m_pimpl->loaded_resources.cend()) {
return make_err_res(ResourceErrorReason::AlreadyLoaded, uid);
Expand All @@ -417,13 +417,11 @@ namespace argus {
return make_err_res(ResourceErrorReason::NoLoader, media_type);
}

IMemStream stream(data, len);

ResourcePrototype proto = { uid, media_type, "" };

auto &loader = *loader_it->second;
loader.m_pimpl->last_dependencies = {};
auto load_res = loader.load(*this, proto, stream, len);
auto load_res = loader.copy(*this, proto, data, std::nullopt);

if (load_res.is_err()) {
return err<Resource &, ResourceError>(load_res.unwrap_err());
Expand Down Expand Up @@ -454,7 +452,7 @@ namespace argus {

auto &loader = *loader_it->second;
loader.m_pimpl->last_dependencies = {};
auto load_res = loader.copy(*this, proto, obj, type);
auto load_res = loader.copy(*this, proto, obj, std::make_optional(type));

if (load_res.is_err()) {
return err<Resource &, ResourceError>(load_res.unwrap_err());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace argus {
std::istream &stream, size_t size) override;

[[nodiscard]] Result<void *, ResourceError> copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) override;
const void *src, std::optional<std::type_index> type) override;

void unload(void *data_ptr) override;
};
Expand Down
4 changes: 2 additions & 2 deletions engine/static/scripting_lua/src/loader/lua_script_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ namespace argus {
}

Result<void *, ResourceError> LuaScriptLoader::copy(ResourceManager &manager, const ResourcePrototype &proto,
void *src, std::type_index type) {
const void *src, std::optional<std::type_index> type) {
UNUSED(manager);
UNUSED(proto);
UNUSED(type);

auto *loaded_script = reinterpret_cast<LoadedScript *>(src);
auto *loaded_script = reinterpret_cast<const LoadedScript *>(src);
return make_ok_result(new LoadedScript(loaded_script->source));
}

Expand Down

0 comments on commit 903ae28

Please sign in to comment.