Merge pull request #145 from jozsefsallai/bugfix/filesystem-errors

fix filesystem errors and UB warnings on rust >= 1.62.0
This commit is contained in:
József Sallai 2022-07-07 17:02:18 +03:00 committed by GitHub
commit 9d7c63571d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 31 deletions

View File

@ -1811,7 +1811,7 @@ impl EngineConstants {
pub fn apply_constant_json_files(&mut self) {}
pub fn load_texture_size_hints(&mut self, ctx: &mut Context) -> GameResult {
if let Ok(file) = filesystem::open_find(ctx, &self.base_paths, "/texture_sizes.json") {
if let Ok(file) = filesystem::open_find(ctx, &self.base_paths, "texture_sizes.json") {
match serde_json::from_reader::<_, TextureSizeTable>(file) {
Ok(tex_overrides) => {
for (key, (x, y)) in tex_overrides.sizes {
@ -1828,7 +1828,7 @@ impl EngineConstants {
/// even though they match vanilla 1:1, we should load them for completeness
/// or if any crazy person uses it for a CS+ mod...
pub fn load_csplus_tables(&mut self, ctx: &mut Context) -> GameResult {
if let Ok(mut file) = filesystem::open_find(ctx, &self.base_paths, "/bullet.tbl") {
if let Ok(mut file) = filesystem::open_find(ctx, &self.base_paths, "bullet.tbl") {
let mut data = Vec::new();
file.read_to_end(&mut data)?;
let bullets = data.len() / 0x2A;
@ -1859,7 +1859,7 @@ impl EngineConstants {
log::info!("Loaded bullet.tbl.");
}
if let Ok(mut file) = filesystem::open_find(ctx, &self.base_paths, "/arms_level.tbl") {
if let Ok(mut file) = filesystem::open_find(ctx, &self.base_paths, "arms_level.tbl") {
let mut data = Vec::new();
file.read_to_end(&mut data)?;
let mut f = Cursor::new(data);
@ -1887,7 +1887,7 @@ impl EngineConstants {
// Bugfix for Malco cutscene - this face should be used but the original tsc has the wrong ID
self.animated_face_table.push(AnimatedFace { face_id: 5, anim_id: 4, anim_frames: vec![(4, 0)] });
if let Ok(file) = filesystem::open_find(ctx, &self.base_paths, "/faceanm.dat") {
if let Ok(file) = filesystem::open_find(ctx, &self.base_paths, "faceanm.dat") {
let buf = BufReader::new(file);
let mut face_id = 1;
let mut anim_id = 0;

View File

@ -206,12 +206,12 @@ impl Map {
)?;
}
if let Ok(mut attrib_data) = filesystem::open_find(ctx, roots, ["/Stage/", &tileset_fg, ".pxa"].join("")) {
if let Ok(mut attrib_data) = filesystem::open_find(ctx, roots, ["Stage/", &tileset_fg, ".pxa"].join("")) {
if attrib_data.read_exact(&mut attrib).is_err() {
log::warn!("Map attribute data is shorter than 256 bytes!");
}
} else if let Ok(mut attrib_data) =
filesystem::open_find(ctx, roots, ["/Stage/", &tileset_fg, ".pxattr"].join(""))
filesystem::open_find(ctx, roots, ["Stage/", &tileset_fg, ".pxattr"].join(""))
{
attrib_data.read_exact(&mut magic)?;

View File

@ -1,4 +1,4 @@
use std::mem::MaybeUninit;
use std::mem::{transmute, MaybeUninit};
use std::ops::Deref;
use crate::common::{interpolate_fix9_scale, Direction};
@ -33,17 +33,21 @@ pub struct BossNPC {
impl BossNPC {
pub fn new() -> BossNPC {
let mut parts = unsafe {
let mut parts_uninit: [NPC; 20] = MaybeUninit::uninit().assume_init();
let mut parts: [NPC; 20] = unsafe {
const PART: MaybeUninit<NPC> = MaybeUninit::uninit();
let mut parts_uninit: [MaybeUninit<NPC>; 20] = [PART; 20];
for part in &mut parts_uninit {
*part = NPC::empty();
part.cond.set_drs_boss(true);
part.write(NPC::empty());
}
parts_uninit
transmute(parts_uninit)
};
for part in &mut parts {
part.cond.set_drs_boss(true);
}
parts[0].cond.set_alive(true);
BossNPC { boss_type: 0, parts, hurt_sound: [0; 20], death_sound: [0; 20] }

View File

@ -1,5 +1,5 @@
use std::cell::{Cell, UnsafeCell};
use std::mem::MaybeUninit;
use std::mem::{transmute, MaybeUninit};
use crate::framework::error::{GameError, GameResult};
@ -23,13 +23,14 @@ impl NPCList {
pub fn new() -> NPCList {
let map = NPCList {
npcs: Box::new(UnsafeCell::new(unsafe {
let mut parts_uninit: [NPC; NPC_LIST_MAX_CAP] = MaybeUninit::uninit().assume_init();
const PART: MaybeUninit<NPC> = MaybeUninit::uninit();
let mut parts_uninit: [MaybeUninit<NPC>; NPC_LIST_MAX_CAP] = [PART; NPC_LIST_MAX_CAP];
for part in &mut parts_uninit {
*part = NPC::empty();
part.write(NPC::empty());
}
parts_uninit
transmute(parts_uninit)
})),
max_npc: Cell::new(0),
seed: 0,

View File

@ -88,7 +88,7 @@ impl BasicPlayerSkin {
pub fn new(texture_name: String, state: &SharedGameState, ctx: &mut Context) -> BasicPlayerSkin {
let mut metadata = DEFAULT_SKINMETA.clone();
let meta_path = format!("/{}.dskinmeta", texture_name);
let meta_path = format!("{}.dskinmeta", texture_name);
if let Ok(file) = filesystem::open_find(ctx, &state.constants.base_paths, &meta_path) {
match serde_json::from_reader::<File, SkinMeta>(file) {

View File

@ -337,19 +337,19 @@ impl SharedGameState {
let font = BMFontRenderer::load(&constants.base_paths, &active_locale.font.path, ctx).or_else(|e| {
log::warn!("Failed to load font, using built-in: {}", e);
BMFontRenderer::load(&vec!["/".to_owned()], "/builtin/builtin_font.fnt", ctx)
BMFontRenderer::load(&vec!["/".to_owned()], "builtin/builtin_font.fnt", ctx)
})?;
let mod_list = ModList::load(ctx, &constants.string_table)?;
for i in 0..0xffu8 {
let path = format!("/pxt/fx{:02x}.pxt", i);
let path = format!("pxt/fx{:02x}.pxt", i);
if let Ok(file) = filesystem::open_find(ctx, &constants.base_paths, path) {
sound_manager.set_sample_params_from_file(i, file)?;
continue;
}
let path = format!("/PixTone/{:03}.pxt", i);
let path = format!("PixTone/{:03}.pxt", i);
if let Ok(file) = filesystem::open_find(ctx, &constants.base_paths, path) {
sound_manager.set_sample_params_from_file(i, file)?;
continue;
@ -452,23 +452,23 @@ impl SharedGameState {
let stages = StageData::load_stage_table(ctx, &self.constants.base_paths, self.constants.is_switch)?;
self.stages = stages;
let npc_tbl = filesystem::open_find(ctx, &self.constants.base_paths, "/npc.tbl")?;
let npc_tbl = filesystem::open_find(ctx, &self.constants.base_paths, "npc.tbl")?;
let npc_table = NPCTable::load_from(npc_tbl)?;
self.npc_table = npc_table;
let head_tsc = filesystem::open_find(ctx, &self.constants.base_paths, "/Head.tsc")?;
let head_tsc = filesystem::open_find(ctx, &self.constants.base_paths, "Head.tsc")?;
let head_script = TextScript::load_from(head_tsc, &self.constants)?;
self.textscript_vm.set_global_script(head_script);
let arms_item_tsc = filesystem::open_find(ctx, &self.constants.base_paths, "/ArmsItem.tsc")?;
let arms_item_tsc = filesystem::open_find(ctx, &self.constants.base_paths, "ArmsItem.tsc")?;
let arms_item_script = TextScript::load_from(arms_item_tsc, &self.constants)?;
self.textscript_vm.set_inventory_script(arms_item_script);
let stage_select_tsc = filesystem::open_find(ctx, &self.constants.base_paths, "/StageSelect.tsc")?;
let stage_select_tsc = filesystem::open_find(ctx, &self.constants.base_paths, "StageSelect.tsc")?;
let stage_select_script = TextScript::load_from(stage_select_tsc, &self.constants)?;
self.textscript_vm.set_stage_select_script(stage_select_script);
let credit_tsc = filesystem::open_find(ctx, &self.constants.base_paths, "/Credit.tsc")?;
let credit_tsc = filesystem::open_find(ctx, &self.constants.base_paths, "Credit.tsc")?;
let credit_script = CreditScript::load_from(credit_tsc, &self.constants)?;
self.creditscript_vm.set_script(credit_script);
@ -490,7 +490,7 @@ impl SharedGameState {
let font = BMFontRenderer::load(&self.constants.base_paths, &active_locale.font.path, ctx)
.or_else(|e| {
log::warn!("Failed to load font, using built-in: {}", e);
BMFontRenderer::load(&vec!["/".to_owned()], "/builtin/builtin_font.fnt", ctx)
BMFontRenderer::load(&vec!["/".to_owned()], "builtin/builtin_font.fnt", ctx)
})
.unwrap();

View File

@ -548,13 +548,13 @@ impl Stage {
pub fn load(roots: &Vec<String>, data: &StageData, ctx: &mut Context) -> GameResult<Self> {
let mut data = data.clone();
if let Ok(pxpack_file) = filesystem::open_find(ctx, roots, ["/Stage/", &data.map, ".pxpack"].join("")) {
if let Ok(pxpack_file) = filesystem::open_find(ctx, roots, ["Stage/", &data.map, ".pxpack"].join("")) {
let map = Map::load_pxpack(pxpack_file, roots, &mut data, ctx)?;
let stage = Self { map, data };
return Ok(stage);
} else if let Ok(map_file) = filesystem::open_find(ctx, roots, ["/Stage/", &data.map, ".pxm"].join("")) {
let attrib_file = filesystem::open_find(ctx, roots, ["/Stage/", &data.tileset.name, ".pxa"].join(""))?;
} else if let Ok(map_file) = filesystem::open_find(ctx, roots, ["Stage/", &data.map, ".pxm"].join("")) {
let attrib_file = filesystem::open_find(ctx, roots, ["Stage/", &data.tileset.name, ".pxa"].join(""))?;
let map = Map::load_pxm(map_file, attrib_file)?;
@ -572,14 +572,14 @@ impl Stage {
constants: &EngineConstants,
ctx: &mut Context,
) -> GameResult<TextScript> {
let tsc_file = filesystem::open_find(ctx, roots, ["/Stage/", &self.data.map, ".tsc"].join(""))?;
let tsc_file = filesystem::open_find(ctx, roots, ["Stage/", &self.data.map, ".tsc"].join(""))?;
let text_script = TextScript::load_from(tsc_file, constants)?;
Ok(text_script)
}
pub fn load_npcs(&self, roots: &Vec<String>, ctx: &mut Context) -> GameResult<Vec<NPCData>> {
let pxe_file = filesystem::open_find(ctx, roots, ["/Stage/", &self.data.map, ".pxe"].join(""))?;
let pxe_file = filesystem::open_find(ctx, roots, ["Stage/", &self.data.map, ".pxe"].join(""))?;
let npc_data = NPCData::load_from(pxe_file)?;
Ok(npc_data)