-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlib.rs
385 lines (302 loc) · 10.8 KB
/
lib.rs
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#![feature(proc_macro_hygiene, new_uninit)]
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use parking_lot::Mutex;
use image::DynamicImage;
use arcropolis_api::{arc_callback, load_original_file};
use skyline::hooks::{
getRegionAddress,
Region,
InlineCtx
};
use smash::lib::lua_const::FIGHTER_KIND_PICKEL;
mod keyboard;
mod skin_menu;
mod skin_files;
mod modern_skin;
mod minecraft_api;
mod color_correct;
mod stock_generation;
use skin_files::*;
use modern_skin::convert_to_modern_skin;
use color_correct::color_correct;
lazy_static::lazy_static! {
static ref SKINS: Mutex<skin_menu::Skins> = Mutex::new(
skin_menu::Skins::from_cache().unwrap_or_default()
);
}
// Default 13.0.1 offset
static mut FIGHTER_SELECTED_OFFSET: usize = 0x66e120;
static FIGHTER_SELECTED_SEARCH_CODE: &[u8] = &[
0x04, 0xdc, 0x45, 0x94,
0xe0, 0x03, 0x1c, 0x32,
0xe1, 0x03, 0x1a, 0x32,
];
static SELECTED_SKINS: [Mutex<Option<PathBuf>>; 8] = [
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
];
static RENDERS: [Mutex<Option<image::RgbaImage>>; 8] = [
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
parking_lot::const_mutex(None),
];
static LAST_SELECTED: AtomicUsize = AtomicUsize::new(0xFF);
extern "C" {
#[link_name = "_ZN2nn5prepo10PlayReport3AddEPKcS3_"]
fn prepo_add_play_report(a: u64, b: u64, c: u64) -> u64;
}
#[skyline::hook(replace = prepo_add_play_report)]
fn prepo_add_play_report_hook(a: u64, b: u64, c: u64) -> u64 {
LAST_SELECTED.store(0xFF, Ordering::SeqCst);
original!()(a, b, c)
}
const MAX_HEIGHT: usize = 1024;
const MAX_WIDTH: usize = 1024;
const MAX_DATA_SIZE: usize = MAX_HEIGHT * MAX_WIDTH * 4;
const MAX_FILE_SIZE: usize = MAX_DATA_SIZE + 0xb0;
#[arc_callback]
fn steve_callback(hash: u64, data: &mut [u8]) -> Option<usize> {
if let Some(slot) = STEVE_NUTEXB_FILES.iter().position(|&x| x == hash) {
let skin_path = SELECTED_SKINS[slot].lock();
let skin_path: Option<&Path> = skin_path.as_deref();
let mut writer = std::io::Cursor::new(data);
let skin_data = if let Some(path) = skin_path {
image::load_from_memory(&fs::read(path).unwrap()).unwrap()
} else {
// load skin for arcrop, temp fix, TODO: change back to "return false" after arcrop works
let data = fs::read(
Path::new("sd:/ultimate/mods/minecraft_2_layer")
.join(STEVE_NUTEXB_FILES_STR[slot])
).ok()?;
use std::io::Write;
let real_size = data.len();
writer.write_all(&data).unwrap();
let data_out = writer.into_inner();
if real_size != MAX_FILE_SIZE {
let start_of_header = real_size - 0xb0;
let (from, to) = data_out.split_at_mut(MAX_DATA_SIZE);
to.copy_from_slice(&from[start_of_header..real_size]);
}
return Some(MAX_FILE_SIZE);
};
let mut skin_data = skin_data.to_rgba8();
let (width, height) = skin_data.dimensions();
if width == height * 2 {
skin_data = convert_to_modern_skin(&skin_data);
}
color_correct(&mut skin_data);
//skin_data.save("sd:/test.png");
let real_size = (skin_data.height() as usize * skin_data.width() as usize * 4) + 0xb0;
nutexb::writer::write_nutexb("steve_minecraft???", &DynamicImage::ImageRgba8(skin_data), &mut writer).unwrap();
let data_out = writer.into_inner();
if real_size != MAX_FILE_SIZE {
let start_of_header = real_size - 0xb0;
let (from, to) = data_out.split_at_mut(MAX_DATA_SIZE);
to.copy_from_slice(&from[start_of_header..real_size]);
}
Some(MAX_FILE_SIZE)
} else {
None
}
}
#[arc_callback]
fn steve_stock_callback(hash: u64, data: &mut [u8]) -> Option<usize> {
if let Some(slot) = STEVE_STOCK_ICONS.iter().position(|&x| x == hash) {
let skin_path = SELECTED_SKINS[slot].lock();
let skin_path: Option<&Path> = skin_path.as_deref();
let skin_data = image::load_from_memory(
&fs::read(skin_path?).unwrap()
).unwrap();
let skin = skin_data.to_rgba8();
let (width, height) = skin.dimensions();
let skin = if width == height * 2 {
convert_to_modern_skin(&skin)
} else {
skin
};
let stock_icon = stock_generation::gen_stock_image(&skin);
let mut writer = std::io::Cursor::new(data);
bntx::BntxFile::from_image(DynamicImage::ImageRgba8(stock_icon), "steve")
.write(&mut writer)
.unwrap();
Some(writer.position() as usize)
} else {
None
}
}
#[derive(Debug)]
struct UnkPtr1 {
ptrs: [&'static u64; 7],
}
#[derive(Debug)]
struct UnkPtr2 {
bunch_bytes: [u8; 0x20],
bunch_bytes2: [u8; 0x20]
}
#[derive(Debug)]
#[repr(C)]
pub struct FighterInfo {
unk_ptr1: &'static UnkPtr1,
unk_ptr2: &'static UnkPtr2,
unk1: [u8; 0x20],
unk2: [u8; 0x20],
unk3: [u8; 0x8],
fighter_id: u8,
unk4: [u8;0xB],
fighter_slot: u8,
}
#[skyline::hook(offset = FIGHTER_SELECTED_OFFSET, inline)]
fn css_fighter_selected(ctx: &InlineCtx) {
let infos = unsafe { &*(ctx.registers[0].bindgen_union_field as *const FighterInfo) };
let is_steve = *FIGHTER_KIND_PICKEL == infos.fighter_id as i32;
if is_steve {
let slot = infos.fighter_slot as usize;
let path = SKINS.lock().get_skin_path();
*SELECTED_SKINS[slot].lock() = path.clone();
let mut render = RENDERS[slot].lock();
#[cfg(feature = "renders")] {
let mut skin_data = if let Some(path) = path {
image::load_from_memory(&fs::read(path).unwrap())
.unwrap()
.into_rgba8()
} else {
*render = None;
return
};
color_correct(&mut skin_data);
*render = Some(minecraft_render::create_render(&convert_to_modern_skin(&skin_data)));
}
}
}
const MAX_STOCK_ICON_SIZE: usize = 0x9c68;
const MAX_CHARA_3_SIZE: usize = 0x727068;
const MAX_CHARA_4_SIZE: usize = 0x2d068;
const MAX_CHARA_6_SIZE: usize = 0x81068;
static CHARA_3_MASK: &[u8] = include_bytes!("chara_3_mask.png");
static CHARA_4_MASK: &[u8] = include_bytes!("chara_4_mask.png");
static CHARA_6_MASK: &[u8] = include_bytes!("chara_6_mask.png");
use parking_lot::{MutexGuard, MappedMutexGuard};
fn get_render<'a>(slot: usize) -> Option<MappedMutexGuard<'a, image::RgbaImage>> {
let lock = RENDERS[slot].lock();
if lock.is_none() {
None
} else {
Some(MutexGuard::map(lock, |x| x.as_mut().unwrap()))
}
}
#[cfg(feature = "renders")]
#[arc_callback]
fn chara_3_callback(hash: u64, data: &mut [u8]) -> Option<usize> {
if let Some(slot) = STEVE_CHARA_3.iter().position(|&x| x == hash) {
let output = get_render(slot)?;
let mut writer = std::io::Cursor::new(data);
let chara_3_mask = image::load_from_memory_with_format(CHARA_3_MASK, image::ImageFormat::Png)
.unwrap()
.into_rgba8();
let chara_3 = minecraft_render::create_chara_image(
&output,
&chara_3_mask,
1.28451252f32,
-456.55612f32,
11.757321f32,
);
bntx::BntxFile::from_image(DynamicImage::ImageRgba8(chara_3), "steve")
.write(&mut writer)
.unwrap();
Some(writer.position() as usize)
} else {
load_original_file(hash, data)
}
}
#[cfg(feature = "renders")]
#[arc_callback]
fn chara_4_callback(hash: u64, data: &mut [u8]) -> Option<usize> {
let slot = STEVE_CHARA_4.iter().position(|&x| x == hash)?;
let output = get_render(slot)?;
let mut writer = std::io::Cursor::new(data);
let chara_4_mask = image::load_from_memory_with_format(CHARA_4_MASK, image::ImageFormat::Png)
.unwrap()
.into_rgba8();
let chara_4 = minecraft_render::create_chara_image(
&output,
&chara_4_mask,
0.232882008f32,
-90.16959f32,
9.084564f32,
);
bntx::BntxFile::from_image(DynamicImage::ImageRgba8(chara_4), "steve")
.write(&mut writer)
.unwrap();
Some(writer.position() as usize)
}
#[cfg(feature = "renders")]
#[arc_callback]
fn chara_6_callback(hash: u64, data: &mut [u8]) -> Option<usize> {
let slot = STEVE_CHARA_6.iter().position(|&x| x == hash)?;
let output = get_render(slot)?;
let mut writer = std::io::Cursor::new(data);
let chara_6_mask = image::load_from_memory_with_format(CHARA_6_MASK, image::ImageFormat::Png)
.unwrap()
.into_rgba8();
let chara_6 = minecraft_render::create_chara_image(
&output,
&chara_6_mask,
0.938028f32,
-480.87906f32,
-96.13269f32,
);
bntx::BntxFile::from_image(DynamicImage::ImageRgba8(chara_6), "steve")
.write(&mut writer)
.unwrap();
Some(writer.position() as usize)
}
fn find_subsequence(haystack: &[u8], needle: &[u8]) -> Option<usize> {
haystack.windows(needle.len()).position(|window| window == needle)
}
fn search_offsets() {
unsafe {
let text_ptr = getRegionAddress(Region::Text) as *const u8;
let text_size = (getRegionAddress(Region::Rodata) as usize) - (text_ptr as usize);
let text = std::slice::from_raw_parts(text_ptr, text_size);
if let Some(offset) = find_subsequence(text, FIGHTER_SELECTED_SEARCH_CODE) {
FIGHTER_SELECTED_OFFSET = offset;
} else {
println!("Error: no offset found for 'css_fighter_selected'. Defaulting to 13.0.1 offset. This likely won't work.");
}
}
}
#[skyline::main(name = "minecraft_skins")]
pub fn main() {
search_offsets();
skyline::install_hooks!(prepo_add_play_report_hook, css_fighter_selected);
for &hash in &STEVE_NUTEXB_FILES {
steve_callback::install(hash, MAX_FILE_SIZE);
}
for &hash in &STEVE_STOCK_ICONS {
steve_stock_callback::install(hash, MAX_STOCK_ICON_SIZE);
}
#[cfg(feature = "renders")] {
for &hash in &STEVE_CHARA_3 {
chara_3_callback::install(hash, MAX_CHARA_3_SIZE);
}
for &hash in &STEVE_CHARA_4 {
chara_4_callback::install(hash, MAX_CHARA_4_SIZE);
}
for &hash in &STEVE_CHARA_6 {
chara_6_callback::install(hash, MAX_CHARA_6_SIZE);
}
}
}