Skip to content

Commit

Permalink
resman_rustabi: Add Rust ABI for resman
Browse files Browse the repository at this point in the history
  • Loading branch information
caseif committed Jul 14, 2024
1 parent a8470ab commit ba024ff
Show file tree
Hide file tree
Showing 13 changed files with 809 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(STATIC_MODULE_IDS "core;ecs;resman;"
"wm;input;render;ui;sound;game2d")
set(DYNAMIC_MODULE_IDS "")
set(EXE_MODULE_IDS "bootstrap")
set(AUX_MODULE_IDS "lowlevel_rustabi;core_rustabi;wm_rustabi")
set(AUX_MODULE_IDS "lowlevel_rustabi;core_rustabi;wm_rustabi;resman_rustabi")

set(ARGUS_ROOT_DIR "${CMAKE_SOURCE_DIR}")
set(ENGINE_SRC_PATH "${CMAKE_SOURCE_DIR}/engine")
Expand Down
14 changes: 14 additions & 0 deletions engine/auxiliary/resman_rustabi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "resman_rustabi"
version = "0.0.1"
edition = "2021"

[lib]
name = "resman_rustabi"
crate-type = ["rlib"]

[dependencies]
libc = "0.2"
num_enum = "0.7"
core_rustabi = { path = "../core_rustabi" }
lowlevel_rustabi = { path = "../lowlevel_rustabi" }
5 changes: 5 additions & 0 deletions engine/auxiliary/resman_rustabi/module.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = resman_rustabi
type = aux
languages = RUST
engine_module_deps = wm
engine_aux_deps = lowlevel_rustabi,core_rustabi
19 changes: 19 additions & 0 deletions engine/auxiliary/resman_rustabi/src/argus/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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/>.
*/

pub mod resman;
27 changes: 27 additions & 0 deletions engine/auxiliary/resman_rustabi/src/argus/resman/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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/>.
*/

pub mod resource;
pub mod resource_event;
pub mod resource_loader;
pub mod resource_manager;

pub use self::resource::*;
pub use self::resource_event::*;
pub use self::resource_loader::*;
pub use self::resource_manager::*;
122 changes: 122 additions & 0 deletions engine/auxiliary/resman_rustabi/src/argus/resman/resource.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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/>.
*/

use lowlevel_rustabi::util::*;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use std::path::PathBuf;

use crate::resman_cabi::*;

pub struct Resource {
handle: argus_resource_const_t,
}

pub struct ResourcePrototype {
pub uid: String,
pub media_type: String,
pub fs_path: PathBuf,
}

pub(crate) unsafe fn unwrap_resource_prototype(
proto: &argus_resource_prototype_t,
) -> ResourcePrototype {
ResourcePrototype {
uid: cstr_to_string(proto.uid),
media_type: cstr_to_string(proto.media_type),
fs_path: PathBuf::from(cstr_to_str(proto.fs_path)),
}
}

impl Resource {
#[allow(dead_code)]
pub(crate) fn get_handle(&self) -> argus_resource_const_t {
self.handle
}

pub fn of(handle: argus_resource_const_t) -> Self {
Self { handle }
}

pub fn get_prototype(&self) -> ResourcePrototype {
unsafe { unwrap_resource_prototype(&argus_resource_get_prototype(self.handle)) }
}

pub fn release(&self) -> () {
unsafe { argus_resource_release(self.handle) }
}

pub fn get_data_ptr(&self) -> *const u8 {
unsafe { argus_resource_get_data_ptr(self.handle).cast() }
}
}

#[derive(Eq, Ord, PartialEq, PartialOrd, IntoPrimitive, TryFromPrimitive)]
#[repr(u32)]
pub enum ResourceErrorReason {
Generic = RESOURCE_ERROR_REASON_GENERIC,
NotFound = RESOURCE_ERROR_REASON_NOT_FOUND,
NotLoaded = RESOURCE_ERROR_REASON_NOT_LOADED,
AlreadyLoaded = RESOURCE_ERROR_REASON_ALREADY_LOADED,
NoLoader = RESOURCE_ERROR_REASON_NO_LOADER,
LoadFailed = RESOURCE_ERROR_REASON_LOAD_FAILED,
MalformedContent = RESOURCE_ERROR_REASON_MALFORMED_CONTENT,
InvalidContent = RESOURCE_ERROR_REASON_INVALID_CONTENT,
UnsupportedContent = RESOURCE_ERROR_REASON_UNSUPPORTED_CONTENT,
UnexpectedReferenceType = RESOURCE_ERROR_REASON_UNEXPECTED_REFERENCE_TYPE,
}

pub struct ResourceError {
pub reason: ResourceErrorReason,
pub uid: String,
pub info: String,
}

impl ResourceError {
pub fn new(reason: ResourceErrorReason, uid: &str, info: &str) -> Self {
Self {
reason,
uid: uid.to_string(),
info: info.to_string(),
}
}
}

impl From<argus_resource_error_t> for ResourceError {
fn from(value: argus_resource_error_t) -> Self {
unsafe {
Self {
reason: ResourceErrorReason::try_from(argus_resource_error_get_reason(value))
.expect("Invalid ResourceErrorReason ordinal"),
uid: cstr_to_string(argus_resource_error_get_uid(value)),
info: cstr_to_string(argus_resource_error_get_info(value)),
}
}
}
}

impl Into<argus_resource_error_t> for ResourceError {
fn into(self) -> argus_resource_error_t {
unsafe {
argus_resource_error_new(
self.reason.into(),
string_to_cstring(&self.uid).as_ptr(),
string_to_cstring(&self.info).as_ptr(),
)
}
}
}
54 changes: 54 additions & 0 deletions engine/auxiliary/resman_rustabi/src/argus/resman/resource_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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/>.
*/

use num_enum::{IntoPrimitive, TryFromPrimitive};

use crate::argus::resman::{unwrap_resource_prototype, Resource, ResourcePrototype};
use crate::resman_cabi::*;

#[derive(Eq, Ord, PartialEq, PartialOrd, IntoPrimitive, TryFromPrimitive)]
#[repr(u32)]
pub enum ResourceEventType {
Load = RESOURCE_EVENT_TYPE_LOAD,
Unload = RESOURCE_EVENT_TYPE_UNLOAD,
}

pub struct ResourceEvent {
handle: argus_resource_event_t,
}

impl ResourceEvent {
pub fn of(handle: argus_resource_event_t) -> Self {
Self { handle }
}

pub fn get_subtype(&self) -> ResourceEventType {
unsafe {
ResourceEventType::try_from(argus_resource_event_get_subtype(self.handle))
.expect("Invalid ResourceEventType ordinal")
}
}

pub fn get_prototype(&self) -> ResourcePrototype {
unsafe { unwrap_resource_prototype(&argus_resource_event_get_prototype(self.handle)) }
}

pub fn get_resource(&mut self) -> Resource {
unsafe { Resource::of(argus_resource_event_get_resource(self.handle)) }
}
}
Loading

0 comments on commit ba024ff

Please sign in to comment.