colobot/colobot-base/level/parser/parser.h

111 lines
3.4 KiB
C
Raw Permalink Normal View History

2014-10-15 19:14:34 +00:00
/*
* This file is part of the Colobot: Gold Edition source code
2023-08-06 21:15:48 +00:00
* Copyright (C) 2001-2023, Daniel Roux, EPSITEC SA & TerranovaTeam
2015-08-22 14:40:02 +00:00
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
2014-10-15 19:14:34 +00:00
*
* 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
*/
/**
* \file level/parser/parser.h
* \brief Parser for level files
*/
#pragma once
#include "level/level_category.h"
#include "level/robotmain.h"
2015-08-05 17:27:26 +00:00
#include "level/parser/parserexceptions.h"
#include "level/parser/parserline.h"
#include "level/parser/parserparam.h"
2015-07-18 16:52:37 +00:00
#include <string>
#include <vector>
#include <memory>
class CLevelParser
{
public:
//! Create an empty level file
CLevelParser();
//! Load level from file
CLevelParser(std::string filename);
//! Load given level
2015-07-18 16:52:37 +00:00
//@{
CLevelParser(LevelCategory category, int chapter, int rank);
CLevelParser(std::string category, int chapter, int rank);
2015-07-18 16:52:37 +00:00
//@}
//! Build category path
2015-07-18 16:52:37 +00:00
//@{
static std::string BuildCategoryPath(LevelCategory category);
static std::string BuildCategoryPath(std::string category);
2015-07-18 16:52:37 +00:00
//@}
//! Build level filename
2015-07-18 16:52:37 +00:00
//@{
static std::string BuildScenePath(LevelCategory category, int chapter, int rank, bool sceneFile = true);
static std::string BuildScenePath(std::string category, int chapter, int rank, bool sceneFile = true);
2015-07-18 16:52:37 +00:00
//@}
2014-10-25 10:01:21 +00:00
//! Check if level file exists
bool Exists();
//! Load file
void Load();
//! Save file
2014-11-10 10:41:05 +00:00
void Save();
//! Configure level paths for the given level
void SetLevelPaths(LevelCategory category, int chapter = 0, int rank = 0);
2015-08-05 17:27:26 +00:00
//! Inject %something% paths
std::string InjectLevelPaths(const std::string& path, const std::string& defaultDir = "");
//! Get filename
const std::string& GetFilename();
//! Get all lines from file
inline const std::vector<CLevelParserLineUPtr>& GetLines()
{
return m_lines;
}
//! Insert new line to file
void AddLine(CLevelParserLineUPtr line);
//! Find first line with given command
2015-08-07 18:48:55 +00:00
CLevelParserLine* Get(const std::string& command);
//! Find first line with given command, null if doesn't exist
CLevelParserLine* GetIfDefined(const std::string &command);
2015-08-07 18:48:55 +00:00
//! Count lines with given command
int CountLines(const std::string& command);
private:
std::string m_filename;
std::vector<CLevelParserLineUPtr> m_lines;
2015-08-05 17:27:26 +00:00
std::string m_pathCat;
std::string m_pathChap;
std::string m_pathLvl;
};
2015-08-05 17:27:26 +00:00
inline std::string InjectLevelPathsForCurrentLevel(const std::string& path, const std::string& defaultDir = "")
{
CRobotMain* main = CRobotMain::GetInstancePointer();
auto levelParser = std::make_unique<CLevelParser>();
levelParser->SetLevelPaths(main->GetLevelCategory(), main->GetLevelChap(), main->GetLevelRank());
return levelParser->InjectLevelPaths(path, defaultDir);
2015-08-05 17:27:26 +00:00
}