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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! Allow overriding the Spine C Runtime extension functions.
//!
//! Because this library is primarily a wrapper around the official C runtime, it tries to mimic
//! the usage of that runtime as closely as possible. One awkward outcome of this goal is that the
//! C runtime expects you to implement a few functions directly. This module allows you to set
//! those callbacks which have been wrapped in Rust.
//!
//! The callbacks from C are:
//!
//! * `void _spAtlasPage_createTexture (spAtlasPage* self, const char* path);`
//! * `void _spAtlasPage_disposeTexture (spAtlasPage* self);`
//! * `char* _spUtil_readFile (const char* path, int* length);`
//!
//! They can be set with the functions found on this page.
//!
//! You can read more about these functions on the
//! [spine-c Runtime Docs](http://en.esotericsoftware.com/spine-c#Integrating-spine-c-in-your-engine).

use std::ffi::CStr;
use std::fs::read;
use std::sync::{Arc, Mutex, Once};

use crate::c::{c_int, c_void, size_t};
use crate::c_interface::NewFromPtr;
use crate::{
    atlas::AtlasPage,
    c::{c_char, spAtlasPage},
};

type CreateTextureCb = Box<dyn Fn(&mut AtlasPage, &str)>;
type DisposeTextureCb = Box<dyn Fn(&mut AtlasPage)>;
type ReadFileCb = Box<dyn Fn(&str) -> Option<Vec<u8>>>;

#[derive(Default)]
pub(crate) struct Extension {
    create_texture_cb: Option<CreateTextureCb>,
    dispose_texture_cb: Option<DisposeTextureCb>,
    read_file_cb: Option<ReadFileCb>,
}

impl Extension {
    fn singleton() -> Arc<Mutex<Extension>> {
        static START: Once = Once::new();
        static mut INSTANCE: Option<Arc<Mutex<Extension>>> = None;
        START.call_once(|| unsafe {
            INSTANCE = Some(Arc::new(Mutex::new(Extension::default())));
        });
        unsafe {
            let singleton = INSTANCE.as_ref().unwrap();
            singleton.clone()
        }
    }
}

/// Set `_spAtlasPage_createTexture`
///
/// The purpose of this callback is to allow loading textures in whichever engine is being used.
/// The following example shows the intended usage by storing the texture on the renderer object
/// of the AtlasPage which can be acquired later.
/// ```
/// struct SpineTexture(pub String);
///
/// rusty_spine::extension::set_create_texture_cb(|atlas_page, path| {
///     atlas_page.renderer_object().set(SpineTexture(path.to_owned()));
/// });
/// rusty_spine::extension::set_dispose_texture_cb(|atlas_page| unsafe {
///     atlas_page.renderer_object().dispose::<SpineTexture>();
/// });
/// ```
pub fn set_create_texture_cb<F>(create_texture_cb: F)
where
    F: Fn(&mut AtlasPage, &str) + 'static,
{
    let singleton = Extension::singleton();
    let mut extension = singleton.lock().unwrap();
    extension.create_texture_cb = Some(Box::new(create_texture_cb));
}

/// Set `_spAtlasPage_disposeTexture`
///
/// For an example, see [`set_create_texture_cb`].
pub fn set_dispose_texture_cb<F>(dispose_texture_cb: F)
where
    F: Fn(&mut AtlasPage) + 'static,
{
    let singleton = Extension::singleton();
    let mut extension = singleton.lock().unwrap();
    extension.dispose_texture_cb = Some(Box::new(dispose_texture_cb));
}

/// Set `_spUtil_readFile`
///
/// Can be used to customize file loading when using functions which read files from disk. This
/// callback is largely unnecessary as its possible to avoid calling these sorts of functions
/// if read-from-disk is not desirable. Additionally, a default implementation using Rust's
/// `std::fs::read` is provided if this callback remains unset.
///
/// ```
/// rusty_spine::extension::set_read_file_cb(|path| {
///     std::fs::read(path).ok()
/// });
/// ```
pub fn set_read_file_cb<F>(read_file_cb: F)
where
    F: Fn(&str) -> Option<Vec<u8>> + 'static,
{
    let singleton = Extension::singleton();
    let mut extension = singleton.lock().unwrap();
    extension.read_file_cb = Some(Box::new(read_file_cb));
}

#[no_mangle]
extern "C" fn _spAtlasPage_createTexture(c_atlas_page: *mut spAtlasPage, c_path: *const c_char) {
    let singleton = Extension::singleton();
    let extension = singleton.lock().unwrap();
    if let Some(cb) = &extension.create_texture_cb {
        unsafe {
            cb(
                &mut AtlasPage::new_from_ptr(c_atlas_page),
                CStr::from_ptr(c_path).to_str().unwrap(),
            );
        }
    }
}

#[no_mangle]
extern "C" fn _spAtlasPage_disposeTexture(c_atlas_page: *mut spAtlasPage) {
    let singleton = Extension::singleton();
    let extension = singleton.lock().unwrap();
    if let Some(cb) = &extension.dispose_texture_cb {
        unsafe {
            cb(&mut AtlasPage::new_from_ptr(c_atlas_page));
        }
    }
}

extern "C" {
    fn spine_malloc(__size: size_t) -> *mut c_void;
    fn spine_memcpy(__dest: *mut c_void, __src: *const c_void, __n: size_t) -> *mut c_void;
}

#[no_mangle]
extern "C" fn _spUtil_readFile(c_path: *const c_char, c_length: *mut c_int) -> *mut c_char {
    let singleton = Extension::singleton();
    let extension = singleton.lock().unwrap();
    if let Some(cb) = &extension.read_file_cb {
        if let Some(data) = cb(unsafe { CStr::from_ptr(c_path).to_str().unwrap() }) {
            unsafe {
                *c_length = data.len() as c_int;
                let c_data = spine_malloc(data.len() as size_t);
                spine_memcpy(c_data, data.as_ptr() as *const c_void, data.len() as size_t);
                c_data as *mut c_char
            }
        } else {
            std::ptr::null_mut()
        }
    } else {
        let str = unsafe { CStr::from_ptr(c_path).to_str().unwrap().to_owned() };
        if let Ok(data) = read(str) {
            let c_data = unsafe { spine_malloc(data.len() as size_t) };
            unsafe {
                spine_memcpy(c_data, data.as_ptr() as *const c_void, data.len() as size_t);
                *c_length = data.len() as c_int;
            }
            c_data as *mut c_char
        } else {
            std::ptr::null_mut()
        }
    }
}