From 8a5ff8c854cd086ea3c9fa20221bf1a51792d41f Mon Sep 17 00:00:00 2001 From: Richard Jenkins Date: Sun, 21 Jan 2018 22:51:27 +0000 Subject: [PATCH] rework load_g1() --- src/openloco/graphics/gfx.cpp | 39 +++++++++++++++++++++++++---------- src/openloco/graphics/gfx.h | 2 +- src/openloco/openloco.cpp | 5 +---- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/openloco/graphics/gfx.cpp b/src/openloco/graphics/gfx.cpp index 15d02ca817..04e6999241 100644 --- a/src/openloco/graphics/gfx.cpp +++ b/src/openloco/graphics/gfx.cpp @@ -16,7 +16,7 @@ namespace openloco::gfx } // 0x0044733C - bool load_g1() + void load_g1() { auto g1Path = environment::get_path(environment::path_id::g1); @@ -26,30 +26,47 @@ namespace openloco::gfx file = fopen(g1Path.make_preferred().u8string().c_str(), "rb"); - if (file == nullptr) + try { - return false; - } + if (file == nullptr) + { + throw std::exception("Opening g1 file failed."); + } - bool fileReadable = (fread(&header, 8, 1, file) == 1); + if (fread(&header, 8, 1, file) != 1) + { + throw std::exception("Reading g1 file header failed."); + } - if (fileReadable) - { // Read element headers - fread(_g1Elements, header.num_entries * sizeof(g1_element_t), 1, file); + if (fread(_g1Elements, header.num_entries * sizeof(g1_element_t), 1, file) != 1) + { + throw std::exception("Reading g1 element headers failed."); + } // Read element data g1Buffer = malloc(header.total_size); - fread(g1Buffer, header.total_size, 1, file); + if (fread(g1Buffer, header.total_size, 1, file) != 1) + { + free(g1Buffer); + throw std::exception("Reading g1 elements failed."); + } // Adjust memory offsets for (uint32_t i = 0; i < header.num_entries; i++) { _g1Elements[i].offset += (int32_t)g1Buffer; } + fclose(file); + } + catch (const std::exception &e) + { + if (file == nullptr) + { + fclose(file); + } + throw std::runtime_error(e.what()); } - fclose(file); - return fileReadable; } // 0x00447485 diff --git a/src/openloco/graphics/gfx.h b/src/openloco/graphics/gfx.h index 6c19d351f1..5db70099c2 100644 --- a/src/openloco/graphics/gfx.h +++ b/src/openloco/graphics/gfx.h @@ -42,7 +42,7 @@ namespace openloco::gfx int16_t unused; // 0x0E }; - bool load_g1(); + void load_g1(); void clear(drawpixelinfo_t &dpi, uint32_t fill); void clear_single(drawpixelinfo_t &dpi, uint8_t paletteId); void draw_string_494B3F( diff --git a/src/openloco/openloco.cpp b/src/openloco/openloco.cpp index 54ae03d0ca..cc756970fc 100644 --- a/src/openloco/openloco.cpp +++ b/src/openloco/openloco.cpp @@ -253,10 +253,7 @@ namespace openloco scenariomgr::load_index(0); progressbar::begin(string_ids::loading, 0); progressbar::set_progress(60); - if (!gfx::load_g1()) - { - throw std::runtime_error("Unable to load g1.dat."); - } + gfx::load_g1(); progressbar::set_progress(220); call(0x004949BC); progressbar::set_progress(235);