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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use crate::client::*;
use crate::gfx;
use crate::os;
use crate::reloader;
use std::process::ExitStatus;
use std::process::Command;
use std::io::{self, Write};
pub struct PluginReloadResponder {
pub name: String,
pub path: String,
pub output_filepath: String,
pub files: Vec<String>
}
pub trait Plugin<D: gfx::Device, A: os::App> {
fn create() -> Self where Self: Sized;
fn setup(&mut self, client: Client<D, A>) -> Client<D, A>;
fn update(&mut self, client: Client<D, A>) -> Client<D, A>;
fn ui(&mut self, client: Client<D, A>) -> Client<D, A>;
fn unload(&mut self, client: Client<D, A>) -> Client<D, A>;
}
pub fn build_all() {
let path = super::get_data_path("..");
let output = if super::get_config_name() == "release" {
Command::new("cargo")
.current_dir(format!("{}", path))
.arg("build")
.arg(format!("{}", "--release"))
.output()
.expect("hotline::hot_lib:: hot lib failed to build!")
}
else {
Command::new("cargo")
.current_dir(format!("{}", path))
.arg("build")
.output()
.expect("hotline::hot_lib:: hot lib failed to build!")
};
if output.stdout.len() > 0 {
println!("{}", String::from_utf8(output.stdout).unwrap());
}
if output.stderr.len() > 0 {
println!("{}", String::from_utf8(output.stderr).unwrap());
}
}
impl reloader::ReloadResponder for PluginReloadResponder {
fn add_file(&mut self, path: &str) {
self.files.push(path.to_string());
}
fn get_files(&self) -> Vec<String> {
let src_path = self.path.to_string() + "/" + &self.name.to_string() + "/src";
let src_files = super::get_files_recursive(&src_path, Vec::new());
let mut result = self.files.to_vec();
result.extend(src_files);
result
}
fn get_last_mtime(&self) -> std::time::SystemTime {
let meta = std::fs::metadata(&self.output_filepath);
if meta.is_ok() {
std::fs::metadata(&self.output_filepath).unwrap().modified().unwrap()
}
else {
std::time::SystemTime::now()
}
}
fn build(&mut self) -> ExitStatus {
let output = if super::get_config_name() == "release" {
Command::new("cargo")
.current_dir(format!("{}", self.path))
.arg("build")
.arg(format!("{}", "--release"))
.arg("-p")
.arg(format!("{}", self.name))
.output()
.expect("hotline::hot_lib:: hot lib failed to build!")
}
else {
Command::new("cargo")
.current_dir(format!("{}", self.path))
.arg("build")
.arg("-p")
.arg(format!("{}", self.name))
.output()
.expect("hotline::hot_lib:: hot lib failed to build!")
};
let mut stdout = io::stdout().lock();
if output.stdout.len() > 0 {
stdout.write_all(&output.stdout).unwrap();
}
if output.stderr.len() > 0 {
stdout.write_all(&output.stderr).unwrap();
}
output.status
}
}
#[macro_export]
macro_rules! hotline_plugin {
($input:ident) => {
fn new_plugin<T : Plugin<crate::gfx_platform::Device, crate::os_platform::App> + Sized>() -> *mut T {
unsafe {
let layout = std::alloc::Layout::from_size_align(
std::mem::size_of::<T>(),
8,
)
.unwrap();
std::alloc::alloc_zeroed(layout) as *mut T
}
}
#[no_mangle]
pub fn create() -> *mut core::ffi::c_void {
let ptr = new_plugin::<$input>() as *mut core::ffi::c_void;
unsafe {
let plugin = std::mem::transmute::<*mut core::ffi::c_void, *mut $input>(ptr);
let plugin = plugin.as_mut().unwrap();
*plugin = $input::create();
}
ptr
}
#[no_mangle]
pub fn update(mut client: client::Client<gfx_platform::Device, os_platform::App>, ptr: *mut core::ffi::c_void) -> client::Client<gfx_platform::Device, os_platform::App> {
unsafe {
let plugin = std::mem::transmute::<*mut core::ffi::c_void, *mut $input>(ptr);
let plugin = plugin.as_mut().unwrap();
plugin.update(client)
}
}
#[no_mangle]
pub fn setup(mut client: client::Client<gfx_platform::Device, os_platform::App>, ptr: *mut core::ffi::c_void) -> client::Client<gfx_platform::Device, os_platform::App> {
unsafe {
let plugin = std::mem::transmute::<*mut core::ffi::c_void, *mut $input>(ptr);
let plugin = plugin.as_mut().unwrap();
plugin.setup(client)
}
}
#[no_mangle]
pub fn unload(mut client: client::Client<gfx_platform::Device, os_platform::App>, ptr: *mut core::ffi::c_void) -> client::Client<gfx_platform::Device, os_platform::App> {
unsafe {
let plugin = std::mem::transmute::<*mut core::ffi::c_void, *mut $input>(ptr);
let plugin = plugin.as_mut().unwrap();
plugin.unload(client)
}
}
#[no_mangle]
pub fn ui(mut client: client::Client<gfx_platform::Device, os_platform::App>, ptr: *mut core::ffi::c_void, imgui_ctx: *mut core::ffi::c_void) -> client::Client<gfx_platform::Device, os_platform::App> {
unsafe {
let plugin = std::mem::transmute::<*mut core::ffi::c_void, *mut $input>(ptr);
let plugin = plugin.as_mut().unwrap();
client.imgui.set_current_context(imgui_ctx);
plugin.ui(client)
}
}
}
}
pub type PluginInstance = *mut core::ffi::c_void;