1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::{
ffi::{CStr, CString},
path::Path,
sync::Arc,
};
use crate::{
c::{
spSkeletonJson, spSkeletonJson_create, spSkeletonJson_dispose,
spSkeletonJson_readSkeletonData, spSkeletonJson_readSkeletonDataFile,
},
c_interface::SyncPtr,
error::SpineError,
skeleton_data::SkeletonData,
Atlas,
};
#[derive(Debug)]
pub struct SkeletonJson {
c_skeleton_json: SyncPtr<spSkeletonJson>,
owns_memory: bool,
atlas: Option<Arc<Atlas>>,
}
impl SkeletonJson {
pub fn new(atlas: Arc<Atlas>) -> Self {
let c_skeleton_json = unsafe { spSkeletonJson_create(atlas.c_ptr()) };
Self {
c_skeleton_json: SyncPtr(c_skeleton_json),
owns_memory: true,
atlas: Some(atlas),
}
}
pub fn read_skeleton_data(&self, json: &[u8]) -> Result<SkeletonData, SpineError> {
let c_json = CString::new(json)?;
let c_skeleton_data =
unsafe { spSkeletonJson_readSkeletonData(self.c_skeleton_json.0, c_json.as_ptr()) };
if !c_skeleton_data.is_null() {
Ok(SkeletonData::new(c_skeleton_data, self.atlas.clone()))
} else {
let c_error = unsafe { CStr::from_ptr((*self.c_skeleton_json.0).error) };
Err(SpineError::new_from_spine(c_error.to_str().unwrap()))
}
}
pub fn read_skeleton_data_file<P: AsRef<Path>>(
&self,
path: P,
) -> Result<SkeletonData, SpineError> {
let c_path = CString::new(path.as_ref().to_str().unwrap())?;
let c_skeleton_data =
unsafe { spSkeletonJson_readSkeletonDataFile(self.c_skeleton_json.0, c_path.as_ptr()) };
if !c_skeleton_data.is_null() {
Ok(SkeletonData::new(c_skeleton_data, self.atlas.clone()))
} else {
let c_error = unsafe { CStr::from_ptr((*self.c_skeleton_json.0).error) };
Err(SpineError::new_from_spine(c_error.to_str().unwrap()))
}
}
c_accessor_mut!(scale, set_scale, scale, f32);
c_ptr!(c_skeleton_json, spSkeletonJson);
}
impl Drop for SkeletonJson {
fn drop(&mut self) {
if self.owns_memory {
unsafe {
spSkeletonJson_dispose(self.c_skeleton_json.0);
}
}
}
}