summaryrefslogtreecommitdiffstats
path: root/src/object/level/parser.cpp
blob: 2bd43a35109fb509702b5164c84a72989fb3597a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// * This file is part of the COLOBOT source code
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
// *
// * 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://www.gnu.org/licenses/.

#include "object/level/parser.h"


#include "app/app.h"

#include "common/resources/inputstream.h"

#include "object/level/parserexceptions.h"

#include "object/robotmain.h"

#include <string>
#include <exception>
#include <sstream>
#include <iomanip>

#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/lexical_cast.hpp>

CLevelParser::CLevelParser()
{
    m_filename = "";
}

CLevelParser::CLevelParser(std::string filename)
{
    m_filename = filename;
}

CLevelParser::CLevelParser(std::string category, int chapter, int rank)
{
    m_filename = BuildSceneName(category, chapter, rank);
}

CLevelParser::~CLevelParser()
{
    for(auto line : m_lines)
    {
        delete line;
    }
}

std::string CLevelParser::BuildSceneName(std::string category, int chapter, int rank, bool sceneFile)
{
    std::ostringstream outstream;
    if(category == "custom")
    {
        outstream << "levels/custom/";
        outstream << CRobotMain::GetInstancePointer()->GetUserLevelName(chapter);
        if(rank == 000)
        {
            if(sceneFile)
            {
                outstream << "/chaptertitle.txt";
            }
        }
        else
        {
            outstream << "/level" << std::setfill('0') << std::setw(3) << rank;
            if(sceneFile)
            {
                outstream << "/scene.txt";
            }
        }
    }
    else if(category == "perso")
    {
        outstream << "levels/other/perso.txt";
    }
    else if(category == "win" || category == "lost")
    {
        outstream << "levels/other/";
        outstream << category << std::setfill('0') << std::setw(3) << chapter*100+rank << ".txt";
    }
    else
    {
        outstream << "levels/" << category << "/";
        outstream << "chapter" << std::setfill('0') << std::setw(3) << chapter;
        if(rank == 000)
        {
            if(sceneFile)
            {
                outstream << "/chaptertitle.txt";
            }
        }
        else
        {
            outstream << "/level" << std::setfill('0') << std::setw(3) << rank;
            if(sceneFile)
            {
                outstream << "/scene.txt";
            }
        }
    }
    return outstream.str();
}

void CLevelParser::Load()
{
    CInputStream file;
    file.open(m_filename);
    if(!file.is_open())
        throw CLevelParserException("Failed to open file: "+m_filename);
    
    char lang = CApplication::GetInstancePointer()->GetLanguageChar();
    
    std::string line;
    int lineNumber = 0;
    std::map<std::string, CLevelParserLine*> translatableLines;
    while(getline(file,line))
    {
        lineNumber++;
        
        boost::replace_all(line, "\t", " "); // replace tab by space
        
        // ignore comments
        std::size_t comment = line.find("//");
        if(comment != std::string::npos)
            line = line.substr(0, comment);
        
        boost::algorithm::trim(line);
        
        std::size_t pos = line.find_first_of(" \t\n");
        std::string command = line.substr(0, pos);
        if(pos != std::string::npos) {
            line = line.substr(pos+1);
            boost::algorithm::trim(line);
        } else {
            line = "";
        }
        if(command.empty()) continue;
        
        CLevelParserLine* parserLine = new CLevelParserLine(lineNumber, command);
        
        std::string baseCommand = command;
        if(command[command.length()-2] == '.') {
            baseCommand = command.substr(0, command.length()-2);
            if(command[command.length()-1] == 'E' && translatableLines[baseCommand] == nullptr) {
                parserLine->SetCommand(baseCommand);
                translatableLines[baseCommand] = parserLine;
            } else if(command[command.length()-1] == lang) {
                if(translatableLines[baseCommand] != nullptr) {
                    m_lines.erase(std::remove(m_lines.begin(), m_lines.end(), translatableLines[baseCommand]), m_lines.end());
                    delete translatableLines[baseCommand];
                }
                parserLine->SetCommand(baseCommand);
                translatableLines[baseCommand] = parserLine;
            } else {
                delete parserLine;
                continue;
            }
        }
        
        while(!line.empty()) {
            pos = line.find_first_of("=");
            std::string paramName = line.substr(0, pos);
            boost::algorithm::trim(paramName);
            line = line.substr(pos+1);
            boost::algorithm::trim(line);
            
            if(line[0] == '\"') {
                pos = line.find_first_of("\"", 1);
                if(pos == std::string::npos)
                    throw CLevelParserException("Unclosed \" in "+m_filename+":"+boost::lexical_cast<std::string>(lineNumber));
            } else if(line[0] == '\'') {
                pos = line.find_first_of("'", 1);
                if(pos == std::string::npos)
                    throw CLevelParserException("Unclosed ' in "+m_filename+":"+boost::lexical_cast<std::string>(lineNumber));
            } else {
                pos = line.find_first_of("=");
                if(pos != std::string::npos) {
                    std::size_t pos2 = line.find_last_of(" \t\n", line.find_last_not_of(" \t\n", pos-1));
                    if(pos2 != std::string::npos)
                        pos = pos2;
                } else {
                    pos = line.length()-1;
                }
            }
            std::string paramValue = line.substr(0, pos+1);
            boost::algorithm::trim(paramValue);
            
            parserLine->AddParam(paramName, new CLevelParserParam(paramName, paramValue));
            
            if(pos == std::string::npos)
                break;
            line = line.substr(pos+1);
            boost::algorithm::trim(line);
        }
        
        AddLine(parserLine);
    }
    
    file.close();
}

void CLevelParser::Save(std::string filename)
{
    assert(false); //TODO
}

const std::string& CLevelParser::GetFilename()
{
    return m_filename;
}

std::vector<CLevelParserLine*> CLevelParser::GetLines()
{
    return m_lines;
}

void CLevelParser::AddLine(CLevelParserLine* line)
{
    line->SetLevel(this);
    m_lines.push_back(line);
}

CLevelParserLine* CLevelParser::Get(std::string command)
{
    for(auto& line : m_lines) {
        if(line->GetCommand() == command)
            return line;
    }
    throw CLevelParserException("Command not found: "+command);
}