2014-10-15 19:14:34 +00:00
|
|
|
|
/*
|
|
|
|
|
* This file is part of the Colobot: Gold Edition source code
|
|
|
|
|
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
|
|
|
|
|
* http://epsiteс.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
|
|
|
|
|
*/
|
2014-09-24 20:54:26 +00:00
|
|
|
|
|
|
|
|
|
#include "object/level/parser.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "app/app.h"
|
|
|
|
|
|
2015-07-17 18:36:01 +00:00
|
|
|
|
#include "common/make_unique.h"
|
2014-10-25 10:01:21 +00:00
|
|
|
|
#include "common/resources/resourcemanager.h"
|
2014-09-24 20:54:26 +00:00
|
|
|
|
#include "common/resources/inputstream.h"
|
2014-11-10 10:41:05 +00:00
|
|
|
|
#include "common/resources/outputstream.h"
|
2014-09-24 20:54:26 +00:00
|
|
|
|
|
|
|
|
|
#include "object/level/parserexceptions.h"
|
|
|
|
|
|
2014-09-26 18:53:11 +00:00
|
|
|
|
#include "object/robotmain.h"
|
|
|
|
|
|
2014-09-24 20:54:26 +00:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <exception>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <iomanip>
|
2015-06-21 18:59:23 +00:00
|
|
|
|
#include <set>
|
2014-09-24 20:54:26 +00:00
|
|
|
|
|
|
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2014-09-28 17:25:28 +00:00
|
|
|
|
#include <boost/lexical_cast.hpp>
|
2014-09-24 20:54:26 +00:00
|
|
|
|
|
|
|
|
|
CLevelParser::CLevelParser()
|
|
|
|
|
{
|
|
|
|
|
m_filename = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CLevelParser::CLevelParser(std::string filename)
|
|
|
|
|
{
|
|
|
|
|
m_filename = filename;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CLevelParser::CLevelParser(std::string category, int chapter, int rank)
|
|
|
|
|
{
|
2015-03-22 12:51:14 +00:00
|
|
|
|
m_filename = BuildScenePath(category, chapter, rank);
|
2014-09-24 20:54:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 12:51:14 +00:00
|
|
|
|
std::string CLevelParser::BuildCategoryPath(std::string category)
|
2014-09-24 20:54:26 +00:00
|
|
|
|
{
|
|
|
|
|
std::ostringstream outstream;
|
2015-03-22 12:51:14 +00:00
|
|
|
|
outstream << "levels/";
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (category == "perso" || category == "win" || category == "lost")
|
2015-03-22 12:51:14 +00:00
|
|
|
|
{
|
|
|
|
|
outstream << "other/";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
outstream << category << "/";
|
|
|
|
|
}
|
|
|
|
|
return outstream.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string CLevelParser::BuildScenePath(std::string category, int chapter, int rank, bool sceneFile)
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream outstream;
|
|
|
|
|
outstream << BuildCategoryPath(category);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (category == "custom")
|
2014-09-24 20:54:26 +00:00
|
|
|
|
{
|
2014-09-29 19:53:21 +00:00
|
|
|
|
outstream << CRobotMain::GetInstancePointer()->GetUserLevelName(chapter);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (rank == 000)
|
2014-09-26 18:53:11 +00:00
|
|
|
|
{
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (sceneFile)
|
2014-09-26 18:53:11 +00:00
|
|
|
|
{
|
|
|
|
|
outstream << "/chaptertitle.txt";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
outstream << "/level" << std::setfill('0') << std::setw(3) << rank;
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (sceneFile)
|
2014-09-26 18:53:11 +00:00
|
|
|
|
{
|
|
|
|
|
outstream << "/scene.txt";
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-24 20:54:26 +00:00
|
|
|
|
}
|
2015-06-21 18:59:23 +00:00
|
|
|
|
else if (category == "perso")
|
2014-09-24 20:54:26 +00:00
|
|
|
|
{
|
2015-03-22 12:51:14 +00:00
|
|
|
|
outstream << "perso.txt";
|
2014-09-24 20:54:26 +00:00
|
|
|
|
}
|
2015-06-21 18:59:23 +00:00
|
|
|
|
else if (category == "win" || category == "lost")
|
2014-09-24 20:54:26 +00:00
|
|
|
|
{
|
2015-06-21 18:59:23 +00:00
|
|
|
|
outstream << category << std::setfill('0') << std::setw(3) << chapter*100 + rank << ".txt";
|
2014-09-24 20:54:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
outstream << "chapter" << std::setfill('0') << std::setw(3) << chapter;
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (rank == 000)
|
2014-09-24 20:54:26 +00:00
|
|
|
|
{
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (sceneFile)
|
2014-09-24 20:54:26 +00:00
|
|
|
|
{
|
|
|
|
|
outstream << "/chaptertitle.txt";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
outstream << "/level" << std::setfill('0') << std::setw(3) << rank;
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (sceneFile)
|
2014-09-24 20:54:26 +00:00
|
|
|
|
{
|
|
|
|
|
outstream << "/scene.txt";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return outstream.str();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-25 10:01:21 +00:00
|
|
|
|
bool CLevelParser::Exists()
|
|
|
|
|
{
|
|
|
|
|
return CResourceManager::Exists(m_filename);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-24 20:54:26 +00:00
|
|
|
|
void CLevelParser::Load()
|
|
|
|
|
{
|
|
|
|
|
CInputStream file;
|
|
|
|
|
file.open(m_filename);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (!file.is_open())
|
|
|
|
|
throw CLevelParserException("Failed to open file: " + m_filename);
|
|
|
|
|
|
2014-09-24 20:54:26 +00:00
|
|
|
|
char lang = CApplication::GetInstancePointer()->GetLanguageChar();
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
2014-09-24 20:54:26 +00:00
|
|
|
|
std::string line;
|
|
|
|
|
int lineNumber = 0;
|
2015-06-21 18:59:23 +00:00
|
|
|
|
std::set<std::string> translatableLines;
|
|
|
|
|
while (getline(file, line))
|
2014-09-24 20:54:26 +00:00
|
|
|
|
{
|
|
|
|
|
lineNumber++;
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
2014-09-24 20:54:26 +00:00
|
|
|
|
boost::replace_all(line, "\t", " "); // replace tab by space
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
2014-09-24 20:54:26 +00:00
|
|
|
|
// ignore comments
|
|
|
|
|
std::size_t comment = line.find("//");
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (comment != std::string::npos)
|
2014-09-24 20:54:26 +00:00
|
|
|
|
line = line.substr(0, comment);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
2014-09-24 20:54:26 +00:00
|
|
|
|
boost::algorithm::trim(line);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
2014-09-24 20:54:26 +00:00
|
|
|
|
std::size_t pos = line.find_first_of(" \t\n");
|
|
|
|
|
std::string command = line.substr(0, pos);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
line = line.substr(pos + 1);
|
2014-09-24 20:54:26 +00:00
|
|
|
|
boost::algorithm::trim(line);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-09-24 20:54:26 +00:00
|
|
|
|
line = "";
|
|
|
|
|
}
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
|
|
|
|
if (command.empty())
|
|
|
|
|
continue;
|
|
|
|
|
|
2015-07-17 18:36:01 +00:00
|
|
|
|
auto parserLine = MakeUnique<CLevelParserLine>(lineNumber, command);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
|
|
|
|
if (command.length() > 2 && command[command.length() - 2] == '.')
|
|
|
|
|
{
|
|
|
|
|
std::string baseCommand = command.substr(0, command.length() - 2);
|
|
|
|
|
parserLine->SetCommand(baseCommand);
|
|
|
|
|
|
|
|
|
|
char languageChar = command[command.length() - 1];
|
|
|
|
|
if (languageChar == 'E' && translatableLines.count(baseCommand) == 0)
|
|
|
|
|
{
|
|
|
|
|
translatableLines.insert(baseCommand);
|
|
|
|
|
}
|
|
|
|
|
else if (languageChar == lang)
|
|
|
|
|
{
|
|
|
|
|
if (translatableLines.count(baseCommand) > 0)
|
|
|
|
|
{
|
|
|
|
|
auto it = std::remove_if(
|
|
|
|
|
m_lines.begin(),
|
|
|
|
|
m_lines.end(),
|
|
|
|
|
[&baseCommand](const CLevelParserLineUPtr& line)
|
|
|
|
|
{
|
|
|
|
|
return line->GetCommand() == baseCommand;
|
|
|
|
|
});
|
|
|
|
|
m_lines.erase(it, m_lines.end());
|
2014-09-24 20:54:26 +00:00
|
|
|
|
}
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
|
|
|
|
translatableLines.insert(baseCommand);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-09-24 20:54:26 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
|
|
|
|
while (!line.empty())
|
|
|
|
|
{
|
2014-09-24 20:54:26 +00:00
|
|
|
|
pos = line.find_first_of("=");
|
|
|
|
|
std::string paramName = line.substr(0, pos);
|
|
|
|
|
boost::algorithm::trim(paramName);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
line = line.substr(pos + 1);
|
2014-09-24 20:54:26 +00:00
|
|
|
|
boost::algorithm::trim(line);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
|
|
|
|
if (line[0] == '\"')
|
|
|
|
|
{
|
2014-09-24 20:54:26 +00:00
|
|
|
|
pos = line.find_first_of("\"", 1);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
|
throw CLevelParserException("Unclosed \" in " + m_filename + ":" + boost::lexical_cast<std::string>(lineNumber));
|
|
|
|
|
}
|
|
|
|
|
else if (line[0] == '\'')
|
|
|
|
|
{
|
2014-09-24 20:54:26 +00:00
|
|
|
|
pos = line.find_first_of("'", 1);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
|
throw CLevelParserException("Unclosed ' in " + m_filename + ":" + boost::lexical_cast<std::string>(lineNumber));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-09-24 20:54:26 +00:00
|
|
|
|
pos = line.find_first_of("=");
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
|
{
|
2014-09-24 20:54:26 +00:00
|
|
|
|
std::size_t pos2 = line.find_last_of(" \t\n", line.find_last_not_of(" \t\n", pos-1));
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (pos2 != std::string::npos)
|
2014-09-24 20:54:26 +00:00
|
|
|
|
pos = pos2;
|
2015-06-21 18:59:23 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-09-24 20:54:26 +00:00
|
|
|
|
pos = line.length()-1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-21 18:59:23 +00:00
|
|
|
|
std::string paramValue = line.substr(0, pos + 1);
|
2014-09-24 20:54:26 +00:00
|
|
|
|
boost::algorithm::trim(paramValue);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
2015-07-17 18:36:01 +00:00
|
|
|
|
parserLine->AddParam(paramName, MakeUnique<CLevelParserParam>(paramName, paramValue));
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
|
|
|
|
if (pos == std::string::npos)
|
2014-09-24 20:54:26 +00:00
|
|
|
|
break;
|
2015-06-21 18:59:23 +00:00
|
|
|
|
line = line.substr(pos + 1);
|
2014-09-24 20:54:26 +00:00
|
|
|
|
boost::algorithm::trim(line);
|
|
|
|
|
}
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
|
|
|
|
AddLine(std::move(parserLine));
|
2014-09-24 20:54:26 +00:00
|
|
|
|
}
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
2014-09-24 20:54:26 +00:00
|
|
|
|
file.close();
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-10 10:41:05 +00:00
|
|
|
|
void CLevelParser::Save()
|
2014-09-24 20:54:26 +00:00
|
|
|
|
{
|
2014-11-10 10:41:05 +00:00
|
|
|
|
COutputStream file;
|
|
|
|
|
file.open(m_filename);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
if (!file.is_open())
|
|
|
|
|
throw CLevelParserException("Failed to open file: " + m_filename);
|
|
|
|
|
|
|
|
|
|
for (auto& line : m_lines)
|
|
|
|
|
{
|
2015-06-22 19:23:05 +00:00
|
|
|
|
file << *(line.get()) << "\n";
|
2014-11-10 10:41:05 +00:00
|
|
|
|
}
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
2014-11-10 10:41:05 +00:00
|
|
|
|
file.close();
|
2014-09-24 20:54:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::string& CLevelParser::GetFilename()
|
|
|
|
|
{
|
|
|
|
|
return m_filename;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-21 18:59:23 +00:00
|
|
|
|
void CLevelParser::AddLine(CLevelParserLineUPtr line)
|
2014-09-24 20:54:26 +00:00
|
|
|
|
{
|
|
|
|
|
line->SetLevel(this);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
m_lines.push_back(std::move(line));
|
2014-09-24 20:54:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CLevelParserLine* CLevelParser::Get(std::string command)
|
|
|
|
|
{
|
2015-06-21 18:59:23 +00:00
|
|
|
|
for (auto& line : m_lines)
|
|
|
|
|
{
|
|
|
|
|
if (line->GetCommand() == command)
|
|
|
|
|
return line.get();
|
2014-09-26 18:53:11 +00:00
|
|
|
|
}
|
2015-06-21 18:59:23 +00:00
|
|
|
|
throw CLevelParserException("Command not found: " + command);
|
2014-09-28 17:25:28 +00:00
|
|
|
|
}
|