16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-23 00:29:19 +02:00

scn parsing with directive and trainset matching

This commit is contained in:
milek7
2019-08-10 12:00:37 +02:00
parent 84d2065a8a
commit da6343166a
4 changed files with 51 additions and 10 deletions

View File

@@ -1,5 +1,6 @@
#include "stdafx.h" #include "stdafx.h"
#include "scenery_scanner.h" #include "scenery_scanner.h"
#include "Logs.h"
void scenery_scanner::scan() void scenery_scanner::scan()
{ {
@@ -10,24 +11,35 @@ void scenery_scanner::scan()
continue; continue;
if (string_ends_with(path.string(), ".scn")) if (string_ends_with(path.string(), ".scn"))
scan_scn(path.string()); scan_scn(path);
} }
std::sort(scenarios.begin(), scenarios.end(), std::sort(scenarios.begin(), scenarios.end(),
[](const scenery_desc &a, const scenery_desc &b) { return a.path < b.path; }); [](const scenery_desc &a, const scenery_desc &b) { return a.path < b.path; });
} }
void scenery_scanner::scan_scn(std::string path) void scenery_scanner::scan_scn(std::filesystem::path path)
{ {
scenarios.emplace_back(); scenarios.emplace_back();
scenery_desc &desc = scenarios.back(); scenery_desc &desc = scenarios.back();
desc.path = path; desc.path = path;
std::ifstream stream(path, std::ios_base::binary | std::ios_base::in); cParser parser(path.string(), cParser::buffer_FILE);
parser.expandIncludes = false;
while (!parser.eof()) {
parser.getTokens();
if (parser.peek() == "trainset")
parse_trainset(parser);
}
std::ifstream stream(path.string(), std::ios_base::binary | std::ios_base::in);
int line_counter = 0;
std::string line; std::string line;
while (std::getline(stream, line)) while (std::getline(stream, line))
{ {
line_counter++;
if (line.size() < 6 || !string_starts_with(line, "//$")) if (line.size() < 6 || !string_starts_with(line, "//$"))
continue; continue;
@@ -49,9 +61,34 @@ void scenery_scanner::scan_scn(std::string path)
desc.links.push_back(std::make_pair(file, label)); desc.links.push_back(std::make_pair(file, label));
} }
else if (line[3] == 'o') { else if (line[3] == 'o') {
desc.trainsets.emplace_back(); for (trainset_desc &trainset : desc.trainsets) {
trainset_desc &trainset = desc.trainsets.back(); if (line_counter < trainset.file_bounds.first
trainset.name = line.substr(5); || line_counter > trainset.file_bounds.second)
continue;
trainset.description = line.substr(5);
}
} }
} }
WriteLog(path.string() + "----------");
for (trainset_desc &trainset : desc.trainsets) {
WriteLog(trainset.name + "=" + std::to_string(trainset.file_bounds.first) + ":" + std::to_string(trainset.file_bounds.second) + "@" + trainset.description);
}
}
void scenery_scanner::parse_trainset(cParser &parser)
{
scenery_desc &desc = scenarios.back();
desc.trainsets.emplace_back();
trainset_desc &trainset = desc.trainsets.back();
trainset.file_bounds.first = parser.Line();
parser.getTokens(4);
parser >> trainset.name >> trainset.track >> trainset.offset >> trainset.velocity;
while (parser.peek() != "endtrainset")
parser.getTokens();
trainset.file_bounds.second = parser.Line();
} }

View File

@@ -3,8 +3,11 @@
#include <filesystem> #include <filesystem>
#include "Texture.h" #include "Texture.h"
#include "utilities.h" #include "utilities.h"
#include "parser.h"
struct trainset_desc { struct trainset_desc {
std::pair<int, int> file_bounds;
std::string description; std::string description;
std::string name; std::string name;
@@ -13,8 +16,7 @@ struct trainset_desc {
float offset { 0.f }; float offset { 0.f };
float velocity { 0.f }; float velocity { 0.f };
std::vector<std::string> vehicles; std::vector<std::pair<std::string, int>> vehicles;
std::vector<int> couplings;
}; };
struct scenery_desc { struct scenery_desc {
@@ -42,5 +44,6 @@ public:
void scan(); void scan();
private: private:
void scan_scn(std::string path); void scan_scn(std::filesystem::path path);
void parse_trainset(cParser &parser);
}; };

View File

@@ -231,7 +231,7 @@ std::string cParser::readToken( bool ToLower, const char *Break ) {
} }
} }
if( token == "include" ) { if( expandIncludes && token == "include" ) {
// launch child parser if include directive found. // launch child parser if include directive found.
// NOTE: parameter collecting uses default set of token separators. // NOTE: parameter collecting uses default set of token separators.
std::string includefile = readToken(ToLower); // nazwa pliku std::string includefile = readToken(ToLower); // nazwa pliku

View File

@@ -81,6 +81,7 @@ class cParser //: public std::stringstream
std::string Name() const; std::string Name() const;
// returns number of currently processed line // returns number of currently processed line
std::size_t Line() const; std::size_t Line() const;
bool expandIncludes = true;
private: private:
// methods: // methods: