1
0
mirror of https://github.com/medigor/example-native-api-rs.git synced 2025-06-04 23:17:27 +02:00

актуализировал код

This commit is contained in:
medigor 2025-05-18 00:52:24 +03:00
parent d8476441b6
commit 9be4bc6c1a
4 changed files with 51 additions and 67 deletions

View File

@ -13,4 +13,4 @@ panic = "abort" # Abort on panic
strip = true # Automatically strip symbols from the binary.
[dependencies]
addin1c = "0.1"
addin1c = "0.7"

View File

@ -1,6 +1,6 @@
use addin1c::{name, ParamValue, RawAddin, Tm, Variant};
use addin1c::{name, CStr1C, ParamValue, RawAddin, Tm, Variant};
const PROPS: &[&[u16]] = &[
const PROPS: &[&CStr1C] = &[
name!("Test"),
name!("PropI32"),
name!("PropF64"),
@ -10,7 +10,7 @@ const PROPS: &[&[u16]] = &[
name!("PropBlob"),
];
const METHODS: &[&[u16]] = &[name!("Method1"), name!("Method2")];
const METHODS: &[&CStr1C] = &[name!("Method1"), name!("Method2")];
pub struct Addin1 {
test: i32,
@ -41,7 +41,7 @@ impl Drop for Addin1 {
}
impl RawAddin for Addin1 {
fn register_extension_as(&mut self) -> &'static [u16] {
fn register_extension_as(&mut self) -> &'static CStr1C {
name!("Class1")
}
@ -49,11 +49,11 @@ impl RawAddin for Addin1 {
PROPS.len()
}
fn find_prop(&mut self, name: &[u16]) -> Option<usize> {
fn find_prop(&mut self, name: &CStr1C) -> Option<usize> {
PROPS.iter().position(|&x| x == name)
}
fn get_prop_name(&mut self, num: usize, _alias: usize) -> Option<&'static [u16]> {
fn get_prop_name(&mut self, num: usize, _alias: usize) -> Option<&'static CStr1C> {
PROPS.get(num).copied()
}
@ -66,61 +66,61 @@ impl RawAddin for Addin1 {
4 => val.set_date(self.prop_date),
5 => {
let s: Vec<u16> = self.prop_str.encode_utf16().collect();
return val.set_str(s.as_slice());
return val.set_str1c(s.as_slice()).is_ok();
}
6 => {
return val.set_blob(self.prop_blob.as_slice());
return val.set_blob(self.prop_blob.as_slice()).is_ok();
}
_ => return false,
};
true
}
fn set_prop_val(&mut self, num: usize, val: &ParamValue) -> bool {
fn set_prop_val(&mut self, num: usize, val: &Variant) -> bool {
match num {
0 => match val {
0 => match val.get() {
ParamValue::I32(x) => {
self.test = *x;
self.test = x;
true
}
_ => false,
},
1 => match val {
1 => match val.get() {
ParamValue::I32(x) => {
self.prop_i32 = *x;
self.prop_i32 = x;
true
}
_ => false,
},
2 => match val {
2 => match val.get() {
ParamValue::F64(x) => {
self.prop_f64 = *x;
self.prop_f64 = x;
true
}
_ => false,
},
3 => match val {
3 => match val.get() {
ParamValue::Bool(x) => {
self.prop_bool = *x;
self.prop_bool = x;
true
}
_ => false,
},
4 => match val {
4 => match val.get() {
ParamValue::Date(x) => {
self.prop_date = *x;
self.prop_date = x;
true
}
_ => false,
},
5 => match val {
5 => match val.get() {
ParamValue::Str(x) => {
self.prop_str = String::from_utf16(x).unwrap();
true
}
_ => false,
},
6 => match val {
6 => match val.get() {
ParamValue::Blob(x) => {
self.prop_blob.clear();
self.prop_blob.extend_from_slice(x);
@ -137,27 +137,18 @@ impl RawAddin for Addin1 {
}
fn is_prop_writable(&mut self, num: usize) -> bool {
match num {
0 => true,
1 => true,
2 => true,
3 => true,
4 => true,
5 => true,
6 => true,
_ => false,
}
matches!(num, 0..=6)
}
fn get_n_methods(&mut self) -> usize {
METHODS.len()
}
fn find_method(&mut self, name: &[u16]) -> Option<usize> {
fn find_method(&mut self, name: &CStr1C) -> Option<usize> {
METHODS.iter().position(|&x| x == name)
}
fn get_method_name(&mut self, num: usize, _alias: usize) -> Option<&'static [u16]> {
fn get_method_name(&mut self, num: usize, _alias: usize) -> Option<&'static CStr1C> {
METHODS.get(num).copied()
}
@ -179,11 +170,7 @@ impl RawAddin for Addin1 {
}
fn has_ret_val(&mut self, num: usize) -> bool {
match num {
0 => true,
1 => true,
_ => false,
}
matches!(num, 0 | 1)
}
fn call_as_proc(&mut self, _num: usize, _params: &mut [Variant]) -> bool {
@ -205,7 +192,7 @@ impl RawAddin for Addin1 {
_ => return false,
}
}
ret_value.set_str(buf.as_slice())
ret_value.set_str1c(buf.as_slice()).is_ok()
}
1 => {
for (i, param) in params.iter_mut().enumerate() {
@ -213,7 +200,7 @@ impl RawAddin for Addin1 {
ParamValue::Empty => {
if i == 0 {
let s = "Return value".encode_utf16().collect::<Vec<u16>>();
if !param.set_str(&s) {
if param.set_str1c(s.as_slice()).is_err() {
return false;
}
} else {

View File

@ -1,4 +1,4 @@
use addin1c::{name, MethodInfo, Methods, ParamValue, PropInfo, SimpleAddin, Variant};
use addin1c::{name, AddinResult, CStr1C, MethodInfo, Methods, PropInfo, SimpleAddin, Variant};
pub struct Addin2 {
prop1: i32,
@ -9,13 +9,11 @@ impl Addin2 {
Addin2 { prop1: 0 }
}
fn method1(&mut self, param: &mut Variant, ret_value: &mut Variant) -> bool {
let ParamValue::I32(value) = param.get() else {
return false;
};
fn method1(&mut self, param: &mut Variant, ret_value: &mut Variant) -> AddinResult {
let value = param.get_i32()?;
self.prop1 = value;
ret_value.set_i32(value * 2);
true
Ok(())
}
fn method2(
@ -23,34 +21,27 @@ impl Addin2 {
param1: &mut Variant,
param2: &mut Variant,
ret_value: &mut Variant,
) -> bool {
let ParamValue::I32(value1) = param1.get() else {
return false;
};
let ParamValue::I32(value2) = param2.get() else {
return false;
};
) -> AddinResult {
let value1 = param1.get_i32()?;
let value2 = param2.get_i32()?;
self.prop1 = value1 + value2;
ret_value.set_i32(self.prop1);
true
Ok(())
}
fn set_prop1(&mut self, value: &ParamValue) -> bool {
let ParamValue::I32(value) = value else {
return false;
};
self.prop1 = *value;
true
fn set_prop1(&mut self, value: &Variant) -> AddinResult {
self.prop1 = value.get_i32()?;
Ok(())
}
fn get_prop1(&mut self, value: &mut Variant) -> bool {
fn get_prop1(&mut self, value: &mut Variant) -> AddinResult {
value.set_i32(self.prop1);
true
Ok(())
}
}
impl SimpleAddin for Addin2 {
fn name() -> &'static [u16] {
fn name() -> &'static CStr1C {
name!("Class2")
}

View File

@ -7,11 +7,14 @@ use std::{
};
use addin1::Addin1;
use addin2::Addin2;
use addin1c::{create_component, destroy_component, name, AttachType};
use addin2::Addin2;
pub static mut PLATFORM_CAPABILITIES: AtomicI32 = AtomicI32::new(-1);
pub static PLATFORM_CAPABILITIES: AtomicI32 = AtomicI32::new(-1);
/// # Safety
///
/// Component must be non-null.
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn GetClassObject(name: *const u16, component: *mut *mut c_void) -> c_long {
@ -28,6 +31,9 @@ pub unsafe extern "C" fn GetClassObject(name: *const u16, component: *mut *mut c
}
}
/// # Safety
///
/// Component must be returned from `GetClassObject`, the function must be called once for each component.
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn DestroyObject(component: *mut *mut c_void) -> c_long {
@ -43,7 +49,7 @@ pub extern "C" fn GetClassNames() -> *const u16 {
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn SetPlatformCapabilities(capabilities: c_int) -> c_int {
pub extern "C" fn SetPlatformCapabilities(capabilities: c_int) -> c_int {
PLATFORM_CAPABILITIES.store(capabilities, Ordering::Relaxed);
3
}