Simplified model I/O, uses filename extensions to figure out format

dev
Tomasz Kapuściński 2022-03-11 22:40:50 +01:00
parent 920a4c3fc8
commit aa3efd5841
12 changed files with 29 additions and 106 deletions

View File

@ -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

View File

@ -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)
{

View File

@ -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

View File

@ -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

View File

@ -20,8 +20,8 @@
#pragma once
#include "graphics/model/model.h"
#include "graphics/model/model_format.h"
#include <filesystem>
#include <istream>
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

View File

@ -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];

View File

@ -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

View File

@ -20,8 +20,8 @@
#pragma once
#include "graphics/model/model.h"
#include "graphics/model/model_format.h"
#include <filesystem>
#include <istream>
/**
@ -31,6 +31,6 @@
namespace Gfx::ModelIO
{
void ReadOldModel(CModel& model, std::istream& stream);
void ReadOldModel(CModel& model, const std::filesystem::path& path);
}

View File

@ -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 <stdexcept>
#include <fstream>
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");
}

View File

@ -19,10 +19,9 @@
#pragma once
#include "graphics/model/model_format.h"
#include "graphics/model/model.h"
#include <ostream>
#include <string>
#include <filesystem>
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

View File

@ -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
{

View File

@ -20,8 +20,8 @@
#pragma once
#include "graphics/model/model.h"
#include "graphics/model/model_format.h"
#include <filesystem>
#include <istream>
/**
@ -31,6 +31,6 @@
namespace Gfx::ModelIO
{
void ReadTextModel(CModel& model, std::istream& stream);
void ReadTextModel(CModel& model, const std::filesystem::path& path);
}