diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8a25f8c0..b3fd2f09 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -129,7 +129,6 @@ add_library(colobotbase STATIC graphics/model/model.cpp graphics/model/model.h graphics/model/model_crash_sphere.h - graphics/model/model_format.h graphics/model/model_input.cpp graphics/model/model_input.h graphics/model/model_io_exception.h diff --git a/src/graphics/engine/oldmodelmanager.cpp b/src/graphics/engine/oldmodelmanager.cpp index e1207ac9..c0891d49 100644 --- a/src/graphics/engine/oldmodelmanager.cpp +++ b/src/graphics/engine/oldmodelmanager.cpp @@ -52,23 +52,9 @@ bool COldModelManager::LoadModel(const std::string& fileName, bool mirrored, int CModel model; try { - CInputStream stream; - stream.open("models/" + fileName); - if (!stream.is_open()) - throw CModelIOException(std::string("Could not open file '") + fileName + "'"); + std::filesystem::path path = "models/" + fileName; - std::string::size_type extension_index = fileName.find_last_of('.'); - if (extension_index == std::string::npos) - throw CModelIOException(std::string("Filename '") + fileName + "' has no extension"); - - std::string extension = fileName.substr(extension_index + 1); - - if (extension == "mod") - model = ModelInput::Read(stream, ModelFormat::Old); - else if (extension == "txt") - model = ModelInput::Read(stream, ModelFormat::Text); - else - throw CModelIOException(std::string("Filename '") + fileName + "' has unknown extension"); + ModelInput::Read(model, path); } catch (const CModelIOException& e) { diff --git a/src/graphics/model/model_format.h b/src/graphics/model/model_format.h deleted file mode 100644 index c046bc79..00000000 --- a/src/graphics/model/model_format.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * This file is part of the Colobot: Gold Edition source code - * Copyright (C) 2001-2021, Daniel Roux, EPSITEC SA & TerranovaTeam - * http://epsitec.ch; http://colobot.info; http://github.com/colobot - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://gnu.org/licenses - */ - -#pragma once - -namespace Gfx -{ - -/** - * \enum ModelFormat - * \brief Describes model format to use - */ -enum class ModelFormat -{ - Text, //!< new text format - Old //!< old binary format, deprecated -}; - -} // namespace Gfx diff --git a/src/graphics/model/model_input.cpp b/src/graphics/model/model_input.cpp index 2dc29792..dca3c8b0 100644 --- a/src/graphics/model/model_input.cpp +++ b/src/graphics/model/model_input.cpp @@ -27,37 +27,22 @@ namespace Gfx { -CModel ModelInput::Read(std::istream &stream, ModelFormat format) +void ModelInput::Read(CModel& model, const std::filesystem::path& path) { - stream.exceptions(std::ios_base::failbit | std::ios_base::badbit); + auto extension = path.extension(); - CModel model; - - try + if (extension == ".mod") { - switch (format) - { - case ModelFormat::Text: - ModelIO::ReadTextModel(model, stream); - break; - - case ModelFormat::Old: - ModelIO::ReadOldModel(model, stream); - break; - } + ModelIO::ReadOldModel(model, path); } - catch (const CModelIOException& e) + else if (extension == ".txt") { - throw; + ModelIO::ReadTextModel(model, path); } - catch (const std::exception& e) + else { - throw CModelIOException(std::string("Other error while reading model data: ") + e.what()); + throw CModelIOException(std::string("Unknown model format: ") + extension.string()); } - - return model; } - - } // namespace Gfx diff --git a/src/graphics/model/model_input.h b/src/graphics/model/model_input.h index d5f7bf44..195fb647 100644 --- a/src/graphics/model/model_input.h +++ b/src/graphics/model/model_input.h @@ -20,8 +20,8 @@ #pragma once #include "graphics/model/model.h" -#include "graphics/model/model_format.h" +#include #include namespace Gfx @@ -33,11 +33,7 @@ namespace Gfx */ namespace ModelInput { - //! Reads model from \a stream in given \a format - /** - * @throws CModelIOException on read/write error - */ - CModel Read(std::istream &stream, ModelFormat format); + void Read(CModel& model, const std::filesystem::path& path); } } // namespace Gfx diff --git a/src/graphics/model/model_manager.cpp b/src/graphics/model/model_manager.cpp index e0fa0951..d51e586e 100644 --- a/src/graphics/model/model_manager.cpp +++ b/src/graphics/model/model_manager.cpp @@ -35,16 +35,12 @@ CModel& CModelManager::GetModel(const std::string& modelName) if (it != m_models.end()) return it->second; - std::string modelFile = "models-new/" + modelName + ".txt"; + std::filesystem::path modelFile = "models-new/" + modelName + ".txt"; GetLogger()->Debug("Loading new model: %s\n", modelFile.c_str()); - CInputStream stream; - stream.open(modelFile.c_str()); - if (!stream.is_open()) - throw CModelIOException(std::string("Could not open file '") + modelName + "'"); - - CModel model = ModelInput::Read(stream, ModelFormat::Text); + CModel model; + ModelInput::Read(model, modelFile); m_models[modelName] = model; return m_models[modelName]; diff --git a/src/graphics/model/model_mod.cpp b/src/graphics/model/model_mod.cpp index bd082b35..7aacbbad 100644 --- a/src/graphics/model/model_mod.cpp +++ b/src/graphics/model/model_mod.cpp @@ -47,8 +47,10 @@ void ConvertOldTex1Name(ModelTriangle& triangle, const char* tex1Name); void ConvertFromOldRenderState(ModelTriangle& triangle, int state); ModelLODLevel MinMaxToLodLevel(float min, float max); -void ReadOldModel(CModel& model, std::istream& stream) +void ReadOldModel(CModel& model, const std::filesystem::path& path) { + CInputStream stream(path); + OldModelHeader header; try diff --git a/src/graphics/model/model_mod.h b/src/graphics/model/model_mod.h index 61232e0d..8c4056ba 100644 --- a/src/graphics/model/model_mod.h +++ b/src/graphics/model/model_mod.h @@ -20,8 +20,8 @@ #pragma once #include "graphics/model/model.h" -#include "graphics/model/model_format.h" +#include #include /** @@ -31,6 +31,6 @@ namespace Gfx::ModelIO { -void ReadOldModel(CModel& model, std::istream& stream); +void ReadOldModel(CModel& model, const std::filesystem::path& path); } diff --git a/src/graphics/model/model_output.cpp b/src/graphics/model/model_output.cpp index 3fb44280..2d67a3c1 100644 --- a/src/graphics/model/model_output.cpp +++ b/src/graphics/model/model_output.cpp @@ -19,19 +19,12 @@ #include "graphics/model/model_output.h" -#include "common/ioutils.h" - -#include "graphics/model/model.h" -#include "graphics/model/model_io_exception.h" -#include "graphics/model/model_io_structs.h" - #include -#include namespace Gfx { -void ModelOutput::Write(const CModel& model, std::ostream &stream, ModelFormat format) +void ModelOutput::Write(const CModel& model, const std::filesystem::path& path) { throw std::logic_error("Not implemented"); } diff --git a/src/graphics/model/model_output.h b/src/graphics/model/model_output.h index 799bc01f..e31da236 100644 --- a/src/graphics/model/model_output.h +++ b/src/graphics/model/model_output.h @@ -19,10 +19,9 @@ #pragma once -#include "graphics/model/model_format.h" +#include "graphics/model/model.h" -#include -#include +#include namespace Gfx { @@ -35,7 +34,7 @@ namespace ModelOutput /** * @throws CModelIOException on read/write error */ - void Write(const CModel& model, std::ostream& stream, ModelFormat format); + void Write(const CModel& model, const std::filesystem::path& path); } } // namespace Gfx diff --git a/src/graphics/model/model_txt.cpp b/src/graphics/model/model_txt.cpp index fe188401..9543d936 100644 --- a/src/graphics/model/model_txt.cpp +++ b/src/graphics/model/model_txt.cpp @@ -51,8 +51,10 @@ std::string ParseSpecialMark(const std::string& text); void ConvertFromOldRenderState(ModelTriangle& triangle, int state); -void ReadTextModel(CModel& model, std::istream& stream) +void ReadTextModel(CModel& model, const std::filesystem::path& path) { + CInputStream stream(path); + int version = 0; try { diff --git a/src/graphics/model/model_txt.h b/src/graphics/model/model_txt.h index 80b87229..46a9dc16 100644 --- a/src/graphics/model/model_txt.h +++ b/src/graphics/model/model_txt.h @@ -20,8 +20,8 @@ #pragma once #include "graphics/model/model.h" -#include "graphics/model/model_format.h" +#include #include /** @@ -31,6 +31,6 @@ namespace Gfx::ModelIO { -void ReadTextModel(CModel& model, std::istream& stream); +void ReadTextModel(CModel& model, const std::filesystem::path& path); }