Skip to content

Commit

Permalink
resman: Add C ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
caseif committed Jul 14, 2024
1 parent 05b2a2a commit a8470ab
Show file tree
Hide file tree
Showing 9 changed files with 673 additions and 0 deletions.
67 changes: 67 additions & 0 deletions engine/static/resman/include/argus/resman/cabi/resource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* This file is a part of Argus.
* Copyright (c) 2019-2024, Max Roncace <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

typedef void *argus_resource_t;
typedef const void *argus_resource_const_t;

typedef struct argus_resource_prototype_t {
const char *uid;
const char *media_type;
const char *fs_path;
} argus_resource_prototype_t;

typedef enum ResourceErrorReason {
RESOURCE_ERROR_REASON_GENERIC,
RESOURCE_ERROR_REASON_NOT_FOUND,
RESOURCE_ERROR_REASON_NOT_LOADED,
RESOURCE_ERROR_REASON_ALREADY_LOADED,
RESOURCE_ERROR_REASON_NO_LOADER,
RESOURCE_ERROR_REASON_LOAD_FAILED,
RESOURCE_ERROR_REASON_MALFORMED_CONTENT,
RESOURCE_ERROR_REASON_INVALID_CONTENT,
RESOURCE_ERROR_REASON_UNSUPPORTED_CONTENT,
RESOURCE_ERROR_REASON_UNEXPECTED_REFERENCE_TYPE,
} ResourceErrorReason;

typedef void *argus_resource_error_t;

argus_resource_prototype_t argus_resource_get_prototype(argus_resource_const_t resource);

void argus_resource_release(argus_resource_const_t resource);

const void *argus_resource_get_data_ptr(argus_resource_const_t resource);

argus_resource_error_t argus_resource_error_new(ResourceErrorReason reason, const char *uid, const char *info);

void argus_resource_error_destruct(argus_resource_error_t error);

ResourceErrorReason argus_resource_error_get_reason(argus_resource_error_t error);

const char *argus_resource_error_get_uid(argus_resource_error_t error);

const char *argus_resource_error_get_info(argus_resource_error_t error);

#ifdef __cplusplus
}
#endif
45 changes: 45 additions & 0 deletions engine/static/resman/include/argus/resman/cabi/resource_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This file is a part of Argus.
* Copyright (c) 2019-2024, Max Roncace <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "argus/resman/cabi/resource.h"

#ifdef __cplusplus
extern "C" {
#endif

const char *k_event_type_resource = "resource";

typedef enum ResourceEventType {
RESOURCE_EVENT_TYPE_LOAD,
RESOURCE_EVENT_TYPE_UNLOAD,
} ResourceEventType;

typedef void *argus_resource_event_t;
typedef const void *argus_resource_event_const_t;

ResourceEventType argus_resource_event_get_subtype(argus_resource_event_const_t event);

const argus_resource_prototype_t argus_resource_event_get_prototype(argus_resource_event_const_t event);

argus_resource_t argus_resource_event_get_resource(argus_resource_event_t event);

#ifdef __cplusplus
}
#endif
83 changes: 83 additions & 0 deletions engine/static/resman/include/argus/resman/cabi/resource_loader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* This file is a part of Argus.
* Copyright (c) 2019-2024, Max Roncace <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "argus/resman/cabi/resource_manager.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef void *argus_resource_loader_t;
typedef const void *argus_resource_loader_const_t;

typedef struct VoidPtrOrResourceError {
bool is_ok;
union {
void *value;
argus_resource_error_t error;
} ve;
} VoidPtrOrResourceError;

typedef void *argus_loaded_dependency_set_t;

typedef struct LoadedDependencySetOrResourceError {
bool is_ok;
union {
argus_loaded_dependency_set_t value;
argus_resource_error_t error;
} ve;
} LoadedDependencySetOrResourceError;

typedef bool (*argus_resource_read_callback_t)(void *dst, size_t len,
void *data);

typedef VoidPtrOrResourceError (*argus_resource_load_fn_t)(argus_resource_loader_t loader,
argus_resource_manager_t manager, argus_resource_prototype_t proto,
argus_resource_read_callback_t read_callback, size_t size, void *user_data, void *engine_data);

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

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

argus_resource_loader_t argus_resource_loader_new(
const char *const *media_types,
size_t media_types_count,
argus_resource_load_fn_t load_fn,
argus_resource_copy_fn_t copy_fn,
argus_resource_unload_fn_t unload_fn,
void *user_data
);

LoadedDependencySetOrResourceError argus_resource_loader_load_dependencies(argus_resource_loader_t loader,
argus_resource_manager_t manager, const char *const *dependencies, size_t dependencies_count);

size_t argus_loaded_dependency_set_get_count(argus_loaded_dependency_set_t set);

const char *argus_loaded_dependency_set_get_name_at(argus_loaded_dependency_set_t set, size_t index);

argus_resource_const_t argus_loaded_dependency_set_get_resource_at(argus_loaded_dependency_set_t set, size_t index);

void argus_loaded_dependency_set_destruct(argus_loaded_dependency_set_t set);

#ifdef __cplusplus
}
#endif
68 changes: 68 additions & 0 deletions engine/static/resman/include/argus/resman/cabi/resource_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* This file is a part of Argus.
* Copyright (c) 2019-2024, Max Roncace <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include "argus/resman/cabi/resource.h"

#include <stdbool.h>
#include <stddef.h>

typedef void *argus_resource_manager_t;
typedef const void *argus_resource_manager_const_t;

typedef void *argus_resource_loader_t;

typedef struct ResourceOrResourceError {
bool is_ok;
union {
argus_resource_t value;
argus_resource_error_t error;
} ve;
} ResourceOrResourceError;

argus_resource_manager_t argus_resource_manager_get_instance(void);

void argus_resource_manager_discover_resources(argus_resource_manager_t mgr);

void argus_resource_manager_add_memory_package(argus_resource_manager_t mgr, const unsigned char *buf, size_t len);

void argus_resource_manager_register_loader(argus_resource_manager_t mgr, const char *const *media_types,
size_t media_types_count, argus_resource_loader_t loader);

//TODO: register_extension_mappings

ResourceOrResourceError argus_resource_manager_get_resource(argus_resource_manager_t mgr, const char *uid);

ResourceOrResourceError argus_resource_manager_get_resource_weak(argus_resource_manager_t mgr, const char *uid);

ResourceOrResourceError argus_resource_manager_try_get_resource(argus_resource_manager_t mgr, const char *uid);

void argus_resource_manager_get_resource_async(argus_resource_manager_t mgr, const char *uid,
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);

#ifdef __cplusplus
}
#endif
22 changes: 22 additions & 0 deletions engine/static/resman/include/argus/resman_cabi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* This file is a part of Argus.
* Copyright (c) 2019-2024, Max Roncace <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "argus/resman/cabi/resource.h"
#include "argus/resman/cabi/resource_event.h"
#include "argus/resman/cabi/resource_loader.h"
#include "argus/resman/cabi/resource_manager.h"
78 changes: 78 additions & 0 deletions engine/static/resman/src/cabi/resource.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* This file is a part of Argus.
* Copyright (c) 2019-2024, Max Roncace <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "argus/resman/cabi/resource.h"

#include "argus/resman/resource.hpp"
#include "argus/resman/resource_manager.hpp"

#ifdef __cplusplus
extern "C" {
#endif

using argus::Resource;
using argus::ResourceError;

static inline const Resource &_as_ref(argus_resource_const_t ptr) {
return *reinterpret_cast<const Resource *>(ptr);
}

static inline ResourceError &_error_as_ref(argus_resource_error_t &error) {
return *reinterpret_cast<ResourceError *>(error);
}

argus_resource_prototype_t argus_resource_get_prototype(argus_resource_const_t resource) {
auto cpp_proto = _as_ref(resource).prototype;
return argus_resource_prototype_t {
cpp_proto.uid.c_str(),
cpp_proto.media_type.c_str(),
cpp_proto.fs_path.c_str(),
};
}

void argus_resource_release(argus_resource_const_t resource) {
_as_ref(resource).release();
}

argus_resource_error_t argus_resource_error_new(ResourceErrorReason reason, const char *uid, const char *info) {
return new argus::ResourceError { argus::ResourceErrorReason(reason), uid, info };
}

void argus_resource_error_destruct(argus_resource_error_t error) {
delete &_error_as_ref(error);
}

const void *argus_resource_get_data_ptr(argus_resource_const_t resource) {
return _as_ref(resource).get_data_ptr();
}

ResourceErrorReason argus_resource_error_get_reason(argus_resource_error_t error) {
return ResourceErrorReason(_error_as_ref(error).reason);
}

const char *argus_resource_error_get_uid(argus_resource_error_t error) {
return _error_as_ref(error).uid.c_str();
}

const char *argus_resource_error_get_info(argus_resource_error_t error) {
return _error_as_ref(error).info.c_str();
}

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit a8470ab

Please sign in to comment.