Skip to content

Commit

Permalink
fix compile issue + using new rust features
Browse files Browse the repository at this point in the history
  • Loading branch information
catornot committed Mar 22, 2024
1 parent 0da0815 commit 117a9fc
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 63 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "Apache-2.0 OR MIT"
keywords = ["plugin", "northstar", "titanfall"]
exclude = ["/rrplug_template", ".gitignore"]
edition = "2021"
rust-version = "1.77"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion rrplug_proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{format_ident, quote, quote_spanned, ToTokens};
use syn::{
self, parse, parse_macro_input, parse_str, punctuated::Punctuated, spanned::Spanned,
parse, parse_macro_input, parse_str, punctuated::Punctuated, spanned::Spanned,
AngleBracketedGenericArguments, DeriveInput, Error as SynError, FnArg, Ident, ImplItem,
ImplItemFn, ItemFn, ItemImpl, ReturnType, Stmt, Token, Type, TypePath,
};
Expand Down
4 changes: 2 additions & 2 deletions rrplug_proc/src/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use proc_macro::TokenStream;
use quote::{quote, spanned::Spanned, ToTokens};
use syn::{
self, parse::Parse, parse::ParseStream, punctuated::Punctuated, token::Comma, FnArg, Ident,
LitStr, Result as SynResult, Token, Type, __private::TokenStream2, parse_quote, parse_str,
parse::Parse, parse::ParseStream, punctuated::Punctuated, token::Comma, FnArg, Ident, LitStr,
Result as SynResult, Token, Type, __private::TokenStream2, parse_quote, parse_str,
Error as SynError,
};

Expand Down
4 changes: 2 additions & 2 deletions src/high/engine/convars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ pub struct ConVarValues {
///
/// the strings should always be valid utf8
pub value: String,
/// float value
/// float value will be 0 if it's a string
pub value_float: f32,
///
/// int representation of the convar will be 0 if it's a string
pub value_int: i32,
}

Expand Down
2 changes: 1 addition & 1 deletion src/high/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ impl EngineData {
// hacky way to test compile failure

#[cfg(doctest)]
#[allow(unused)]
mod doctest {
#![allow(unused)]
use super::*;

/// ```compile_fail
Expand Down
15 changes: 10 additions & 5 deletions src/high/engine_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use std::{
mem::MaybeUninit,
ptr::NonNull,
sync::mpsc::{self, Receiver, SendError, Sender},
};

Expand Down Expand Up @@ -119,7 +120,7 @@ pub unsafe fn run_async_routine() {

let result = unsafe {
(sqfunctions.sq_getfunction)(
sqvm,
sqvm.as_ptr(),
to_cstring(&function_name).as_ptr(), // TODO: safe or not?
function_obj.as_mut_ptr(),
std::ptr::null(),
Expand All @@ -130,13 +131,17 @@ pub unsafe fn run_async_routine() {
log::warn!("async squirrel function failed to execute; it may not be global");
} else {
unsafe {
(sqfunctions.sq_pushobject)(sqvm, function_obj.as_mut_ptr());
(sqfunctions.sq_pushroottable)(sqvm);
(sqfunctions.sq_pushobject)(sqvm.as_ptr(), function_obj.as_mut_ptr());
(sqfunctions.sq_pushroottable)(sqvm.as_ptr());

let amount = args(sqvm, sqfunctions);

if (sqfunctions.sq_call)(sqvm, amount + 1, true as u32, true as u32)
== SQRESULT::SQRESULT_ERROR
if (sqfunctions.sq_call)(
sqvm.as_ptr(),
amount + 1,
true as u32,
true as u32,
) == SQRESULT::SQRESULT_ERROR
{
log::warn!("async squirrel function failed to execute!")
}
Expand Down
2 changes: 1 addition & 1 deletion src/high/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub mod engine_sync;

#[doc(hidden)]
#[cfg(not(feature = "async_engine"))]
#[doc = "enable engine sync feature if you want to use it"]
pub mod engine_sync {
//! enable engine sync feature if you want to use it

#[doc(hidden)]
#[inline(always)]
Expand Down
39 changes: 16 additions & 23 deletions src/high/northstar.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//! wrappers for structs that are passed to the plugin
use std::ffi::CStr;

use crate::bindings::plugin_abi::PluginContext;

/// information about the plugin that the plugins system requests
#[derive(Debug)]
pub struct PluginInfo {
name: &'static str,
log_name: &'static str,
dependency_name: &'static str,
name: &'static CStr,
log_name: &'static CStr,
dependency_name: &'static CStr,
context: PluginContext,
}

Expand All @@ -21,30 +23,21 @@ impl PluginInfo {
/// - strings cannot be empty
/// - log name has to be 9 chars in lenght (not counting the null terminator)
pub const fn new(
name: &'static str,
log_name: &'static str,
dependency_name: &'static str,
name: &'static CStr,
log_name: &'static CStr,
dependency_name: &'static CStr,
context: PluginContext,
) -> Self {
assert!(name.to_bytes().len() > 1, "consider actually having a name");
assert!(
name.as_bytes()[name.len().saturating_sub(1)] == 0,
"name has to end with a null char to be a null terminated string"
);
assert!(
log_name.as_bytes()[log_name.len().saturating_sub(1)] == 0,
"log_name has to end with a null char to be a null terminated string"
);
assert!(
dependency_name.as_bytes()[dependency_name.len().saturating_sub(1)] == 0,
"dependency_name has to end with a null char to be a null terminated string"
log_name.to_bytes().len() > 1,
"consider actually having a log_name"
);
assert!(name.len() > 1, "consider actually having a name");
assert!(log_name.len() > 1, "consider actually having a log_name");
assert!(
dependency_name.len() > 1,
dependency_name.to_bytes().len() > 1,
"consider actually having a dependency_name"
);
assert!(log_name.len().saturating_sub(1) == 9, "log name is used for logging and ideally should be 9 chars long and all upercase to look like every other log str");
assert!(log_name.to_bytes().len() == 9, "log name is used for logging and ideally should be 9 chars long and all upercase to look like every other log str");
Self {
name,
log_name,
Expand All @@ -54,15 +47,15 @@ impl PluginInfo {
}

/// Returns a reference to the get name of the plugin.
pub const fn get_name(&self) -> &'static str {
pub const fn get_name(&self) -> &'static CStr {
self.name
}
/// Returns a reference to the get log name of the plugin.
pub const fn get_log_name(&self) -> &'static str {
pub const fn get_log_name(&self) -> &'static CStr {
self.log_name
}
/// Returns a reference to the get dependency name of the plugin.
pub const fn get_dependency_name(&self) -> &'static str {
pub const fn get_dependency_name(&self) -> &'static CStr {
self.dependency_name
}
/// Returns the get context of the plugin.
Expand Down
10 changes: 3 additions & 7 deletions src/high/squirrel_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@
pub use rrplug_proc::{GetFromSQObject, GetFromSquirrelVm, PushToSquirrelVm, SQVMName};
use std::{mem::MaybeUninit, ptr::NonNull};

use super::{
squirrel::{SQHandle, SquirrelFn},
vector::Vector3,
};
use crate::{
bindings::{
class_types::cplayer::CPlayer,
squirrelclasstypes::SQRESULT,
squirreldatatypes::{
HSquirrelVM, SQArray, SQBool, SQClosure, SQFloat, SQFunctionProto, SQInteger,
SQNativeClosure, SQObject, SQObjectType, SQString, SQStructInstance, SQTable,
SQArray, SQBool, SQClosure, SQFloat, SQFunctionProto, SQInteger, SQNativeClosure,
SQObject, SQObjectType, SQString, SQStructInstance, SQTable,
},
squirrelfunctions::SquirrelFunctions,
},
high::squirrel::SQHandle,
mid::{
server::cplayer::CPLAYER_VTABLE,
squirrel::{
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod test {
use crate::rrplug;
use rrplug_proc::as_interface;

#[allow(dead_code)] // TODO: fix later
#[repr(C)]
struct TestInterface {
the_line: &'static str,
Expand Down
4 changes: 2 additions & 2 deletions src/macros/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
///
/// impl Plugin for BasicPlugin {
/// const PLUGIN_INFO: PluginInfo =
/// PluginInfo::new("test\0", "Testttttt\0", "test\0", PluginContext::all());
/// PluginInfo::new(c"test", c"Testttttt", c"test", PluginContext::all());
///
/// fn new(reloaded: bool) -> Self {
/// Self {}
Expand Down Expand Up @@ -314,7 +314,7 @@ mod test_entry {

impl Plugin for Test {
const PLUGIN_INFO: PluginInfo =
PluginInfo::new("test\0", "Testttttt\0", "test\0", PluginContext::all());
PluginInfo::new(c"test", c"Testttttt", c"test", PluginContext::all());

fn new(_reloaded: bool) -> Self {
Self {}
Expand Down
6 changes: 1 addition & 5 deletions src/macros/sq_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,13 @@ pub const fn __arg_count_helper<const N: usize>(_: [(); N]) -> usize {
}

#[cfg(test)]
#[allow(unused_mut)]
mod test {
#![allow(unused_mut)]
use crate as rrplug;
use rrplug::prelude::*;
use rrplug::{bindings::squirreldatatypes::SQClosure, high::squirrel::SQHandle};
use rrplug_proc::*;

use rrplug::{call_sq_function, call_sq_object_function};

#[sqfunction(VM = "Server")]
fn test_call_funcs2(mut func: SQHandle<SQClosure>, test: String) -> Result<String, String> {
call_sq_object_function!(sqvm, sq_functions, func, test.clone())
Expand All @@ -187,8 +185,6 @@ mod test {
call_sq_function!(sqvm, sq_functions, "SomeSQFunc", 9347, 3892, 23423)
.map_err(|err| err.to_string())?;

// async_call_sq_function!(ScriptVmType::Server, "SomeSQFunc", test, 9347);

Ok("test".to_string())
}
}
2 changes: 1 addition & 1 deletion src/macros/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ macro_rules! impl_sqvm_name {
}

#[cfg(test)]
#[allow(dead_code)]
mod test {
#![allow(dead_code)]

struct Test;

Expand Down
20 changes: 7 additions & 13 deletions src/nslog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ use crate::bindings::plugin_abi::LogLevel;
use crate::high::UnsafeHandle;
use crate::mid::northstar::{NorthstarSys, NORTHSTAR_DATA};
use log::{Level, LevelFilter, Metadata, Record, SetLoggerError};
use std::ffi::{c_char, CStr, CString};
use once_cell::sync::OnceCell;
use std::ffi::{CStr, CString};
use std::panic;
use windows::Win32::Foundation::HMODULE;

const C_STRING_ERROR: *const c_char =
"rrplug logger failed to transform a String to a CString\0".as_ptr() as *const c_char;
const C_STRING_ERROR: &CStr = c"rrplug logger failed to transform a String to a CString";

static mut LOGGER: NorthstarLogger = NorthstarLogger {
ns_sys: None,
plugin_handle: HMODULE(0),
};
static LOGGER: OnceCell<NorthstarLogger> = OnceCell::new();

pub fn try_init(plugin_handle: HMODULE) -> Result<(), SetLoggerError> {
panic::set_hook(Box::new(|info| {
Expand All @@ -29,11 +26,9 @@ pub fn try_init(plugin_handle: HMODULE) -> Result<(), SetLoggerError> {
log::error!("");
}));

unsafe {
LOGGER = NorthstarLogger::init(plugin_handle);
_ = LOGGER.set(NorthstarLogger::init(plugin_handle));

log::set_logger(&LOGGER).map(|()| log::set_max_level(LevelFilter::Info))
}
log::set_logger(LOGGER.wait()).map(|()| log::set_max_level(LevelFilter::Info))
}

struct NorthstarLogger {
Expand Down Expand Up @@ -82,8 +77,7 @@ fn to_cstring<T>(string: T) -> CString
where
T: ToString,
{
CString::new(string.to_string())
.unwrap_or_else(|_| CString::from(unsafe { CStr::from_ptr(C_STRING_ERROR) }))
CString::new(string.to_string()).unwrap_or_else(|_| CString::from(C_STRING_ERROR))
}

/// this is needed because [`Level`] doesn't have the same order
Expand Down

0 comments on commit 117a9fc

Please sign in to comment.