|
@@ -0,0 +1,185 @@
|
|
1
|
+#![allow(non_snake_case)]
|
|
2
|
+extern crate simple_logging;
|
|
3
|
+extern crate log;
|
|
4
|
+extern crate dirs;
|
|
5
|
+
|
|
6
|
+use std::ffi::CString;
|
|
7
|
+use std::os::raw::{c_char, c_void};
|
|
8
|
+use std::path::Path;
|
|
9
|
+use log::{info, error, LevelFilter};
|
|
10
|
+
|
|
11
|
+type PluginHandle = u32;
|
|
12
|
+#[repr(C)]
|
|
13
|
+pub struct SKSEInterface {
|
|
14
|
+ pub skseVersion: u32,
|
|
15
|
+ pub runtimeVersion: u32,
|
|
16
|
+ pub editorVersion: u32,
|
|
17
|
+ pub isEditor: u32,
|
|
18
|
+ pub QueryInterface: fn(KInterface) -> *const c_void,
|
|
19
|
+ pub GetPluginHandle: fn() -> PluginHandle,
|
|
20
|
+ pub GetReleaseIndex: fn() -> u32,
|
|
21
|
+}
|
|
22
|
+
|
|
23
|
+#[repr(C)]
|
|
24
|
+pub struct PluginInfo {
|
|
25
|
+ pub infoVersion: u32,
|
|
26
|
+ pub name: *mut c_char,
|
|
27
|
+ pub version: u32,
|
|
28
|
+}
|
|
29
|
+
|
|
30
|
+#[repr(C)]
|
|
31
|
+pub struct VMValue;
|
|
32
|
+
|
|
33
|
+#[repr(C)]
|
|
34
|
+pub struct VMState;
|
|
35
|
+
|
|
36
|
+#[repr(C)]
|
|
37
|
+pub struct NativeFunction {
|
|
38
|
+ InitParams: fn(*const VMClassRegistry) -> (),
|
|
39
|
+ Run: fn(*const VMValue, *const VMClassRegistry, u32, *const VMValue, *const VMState) -> bool,
|
|
40
|
+}
|
|
41
|
+
|
|
42
|
+impl NativeFunction {
|
|
43
|
+ fn new() -> NativeFunction {
|
|
44
|
+ NativeFunction {
|
|
45
|
+ InitParams: |registry| {
|
|
46
|
+ info!("NativeFunction InitParams begin");
|
|
47
|
+ info!("registry: {:?}", registry);
|
|
48
|
+ info!("NativeFunction InitParams successful");
|
|
49
|
+ },
|
|
50
|
+ Run: |baseValue, registry, stackId, resultValue, state| {
|
|
51
|
+ info!("NativeFunction Run begin");
|
|
52
|
+ info!("baseValue: {:?}", baseValue);
|
|
53
|
+ info!("registry: {:?}", registry);
|
|
54
|
+ info!("stackId: {:?}", stackId);
|
|
55
|
+ info!("resultValue: {:?}", resultValue);
|
|
56
|
+ info!("state: {:?}", state);
|
|
57
|
+ info!("NativeFunction Run successful");
|
|
58
|
+ true
|
|
59
|
+ },
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+#[repr(C)]
|
|
66
|
+pub struct VMClassInfo;
|
|
67
|
+
|
|
68
|
+#[repr(C)]
|
|
69
|
+pub struct VMIdentifier;
|
|
70
|
+
|
|
71
|
+#[repr(C)]
|
|
72
|
+pub struct StringCacheRef;
|
|
73
|
+
|
|
74
|
+#[repr(C)]
|
|
75
|
+pub struct VMClassRegistry {
|
|
76
|
+ pub Unk_01: fn(c_void) -> c_void,
|
|
77
|
+ pub PrintToDebugLog: fn(*const c_char, u32, u32) -> c_void,
|
|
78
|
+ pub Unk_03: fn(c_void) -> c_void,
|
|
79
|
+ pub Unk_04: fn(c_void) -> c_void,
|
|
80
|
+ pub Unk_05: fn(c_void) -> c_void,
|
|
81
|
+ pub Unk_06: fn(c_void) -> c_void,
|
|
82
|
+ pub Unk_07: fn(c_void) -> c_void,
|
|
83
|
+ pub RegisterForm: fn(u32, *const c_char) -> c_void,
|
|
84
|
+ pub Unk_09: fn(c_void) -> c_void,
|
|
85
|
+ pub GetFormTypeClass: fn(u32, *const VMClassInfo) -> bool,
|
|
86
|
+ pub Unk_0B: fn(c_void) -> c_void,
|
|
87
|
+ pub Unk_0C: fn(c_void) -> c_void,
|
|
88
|
+ pub Unk_0D: fn(*const StringCacheRef, *const u32) -> bool,
|
|
89
|
+ pub Unk_0E: fn(c_void) -> c_void,
|
|
90
|
+ pub Unk_0F: fn(c_void) -> c_void,
|
|
91
|
+ pub Unk_10: fn(c_void) -> c_void,
|
|
92
|
+ pub Unk_11: fn(c_void) -> c_void,
|
|
93
|
+ pub Unk_12: fn(c_void) -> c_void,
|
|
94
|
+ pub Unk_13: fn(c_void) -> c_void,
|
|
95
|
+ pub Unk_14: fn(c_void) -> c_void,
|
|
96
|
+ pub Unk_15: fn(*const StringCacheRef, *const VMIdentifier) -> bool,
|
|
97
|
+ pub CreateArray: fn(*const VMValue, u32, *const VMValue) -> bool,
|
|
98
|
+ pub Unk_17: fn(c_void) -> c_void,
|
|
99
|
+ pub RegisterFunction: fn(*const NativeFunction) -> c_void,
|
|
100
|
+ // more...
|
|
101
|
+}
|
|
102
|
+
|
|
103
|
+type RegisterFunctions = fn(*const VMClassRegistry) -> bool;
|
|
104
|
+
|
|
105
|
+#[repr(C)]
|
|
106
|
+pub struct SKSEPapyrusInterface {
|
|
107
|
+ pub interfaceVersion: u32,
|
|
108
|
+ pub Register: fn(*const RegisterFunctions) -> bool,
|
|
109
|
+}
|
|
110
|
+
|
|
111
|
+#[repr(u32)]
|
|
112
|
+pub enum KInterface {
|
|
113
|
+ Invalid = 0,
|
|
114
|
+ Scaleform,
|
|
115
|
+ Papyrus,
|
|
116
|
+ Serialization,
|
|
117
|
+ Task,
|
|
118
|
+ Messaging,
|
|
119
|
+ Object,
|
|
120
|
+ Max,
|
|
121
|
+}
|
|
122
|
+
|
|
123
|
+const fn make_exe_version(major: u32, minor: u32, build: u32, sub: u32) -> u32 {
|
|
124
|
+ (((major) & 0xFF) << 24) | (((minor) & 0xFF) << 16) | (((build) & 0xFFF) << 4) | ((sub) & 0xF)
|
|
125
|
+}
|
|
126
|
+
|
|
127
|
+const RUNTIME_VERSION_1_5_97: u32 = make_exe_version(1, 5, 97, 0);
|
|
128
|
+
|
|
129
|
+unsafe fn RegisterFuncs(a_registry: *const VMClassRegistry) -> bool {
|
|
130
|
+ info!("RegisterFuncs begin");
|
|
131
|
+ info!("a_registry: {:?}", a_registry);
|
|
132
|
+ let registerFunction = (*a_registry).RegisterFunction;
|
|
133
|
+ info!("registerFunction: {:?}", registerFunction);
|
|
134
|
+ let nativeFunction: *const NativeFunction = &NativeFunction::new();
|
|
135
|
+ info!("nativeFunction: {:?}", nativeFunction);
|
|
136
|
+ // This is as far as I can get. I have no idea how to register a native papyrus function.
|
|
137
|
+ // Doesn't work, throws exception "Access violation executing location"
|
|
138
|
+ // registerFunction(nativeFunction);
|
|
139
|
+ info!("RegisterFuncs successful");
|
|
140
|
+ true
|
|
141
|
+}
|
|
142
|
+
|
|
143
|
+#[no_mangle]
|
|
144
|
+pub unsafe extern "C" fn SKSEPlugin_Query(a_skse: *const SKSEInterface, a_info: *mut PluginInfo) -> bool {
|
|
145
|
+ let mut log_dir = dirs::document_dir().expect("could not get Documents directory");
|
|
146
|
+ log_dir.push(Path::new(r#"My Games\Skyrim Special Edition\SKSE\RustSKSEPlugin.log"#));
|
|
147
|
+ simple_logging::log_to_file(log_dir, LevelFilter::Info).unwrap();
|
|
148
|
+ info!("SKSEPlugin_Query begin");
|
|
149
|
+
|
|
150
|
+ (*a_info).infoVersion = 1;
|
|
151
|
+ (*a_info).name = CString::new("My Rust SKSE Plugin").expect("could not create CString").into_raw();
|
|
152
|
+ (*a_info).version = 1;
|
|
153
|
+
|
|
154
|
+ if (*a_skse).isEditor != 0 {
|
|
155
|
+ error!("Loaded in editor, marking as incompatible!");
|
|
156
|
+ return false;
|
|
157
|
+ } else if (*a_skse).runtimeVersion != RUNTIME_VERSION_1_5_97 {
|
|
158
|
+ error!("Unsupported runtime version {}!", (*a_skse).runtimeVersion);
|
|
159
|
+ return false;
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+ info!("SKSEPlugin_Query successful");
|
|
163
|
+ true
|
|
164
|
+}
|
|
165
|
+
|
|
166
|
+#[no_mangle]
|
|
167
|
+pub unsafe extern "C" fn SKSEPlugin_Load(a_skse: *const SKSEInterface) -> bool {
|
|
168
|
+ info!("SKSEPlugin_Load begin");
|
|
169
|
+
|
|
170
|
+ let queryInterfaceFunc = (*a_skse).QueryInterface;
|
|
171
|
+ info!("queryInterfaceFunc: {:?}", queryInterfaceFunc);
|
|
172
|
+ let queryInterface = queryInterfaceFunc(KInterface::Papyrus);
|
|
173
|
+ info!("queryInterface: {:?}", queryInterface);
|
|
174
|
+ let papyrusInterface: *const SKSEPapyrusInterface = queryInterface as *const SKSEPapyrusInterface;
|
|
175
|
+ info!("papyrusInterface: {:?}", papyrusInterface);
|
|
176
|
+ let papyrusRegister = (*papyrusInterface).Register;
|
|
177
|
+ info!("papyrusRegister: {:?}", papyrusRegister);
|
|
178
|
+ if !papyrusRegister(RegisterFuncs as *const RegisterFunctions) {
|
|
179
|
+ error!("RegisterFuncs returned false!");
|
|
180
|
+ return false;
|
|
181
|
+ }
|
|
182
|
+
|
|
183
|
+ info!("SKSEPlugin_Load successful");
|
|
184
|
+ true
|
|
185
|
+}
|