forked from GokuMK/TSRE5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EngLib.cpp
101 lines (89 loc) · 2.72 KB
/
EngLib.cpp
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
/* This file is part of TSRE5.
*
* TSRE5 - train sim game engine and MSTS/OR Editors.
* Copyright (C) 2016 Piotr Gadecki <[email protected]>
*
* Licensed under GNU General Public License 3.0 or later.
*
* See LICENSE.md or https://www.gnu.org/licenses/gpl.html
*/
#include "EngLib.h"
#include "Eng.h"
#include <QDebug>
#include <QFile>
#include <QDir>
//int EngLib::jesteng = 0;
//std::unordered_map<int, Eng*> EngLib::eng;
EngLib::EngLib() {
}
EngLib::~EngLib() {
}
int EngLib::getEngByPointer(Eng* pointer){
for ( auto it = eng.begin(); it != eng.end(); ++it ){
if(it->second == pointer)
return it->first;
}
return -1;
}
int EngLib::addEng(QString path, QString name) {
QString pathid = (path + "/" + name).toLower();
pathid.replace("\\", "/");
pathid.replace("//", "/");
//qDebug() << pathid;
for ( auto it = eng.begin(); it != eng.end(); ++it ){
if(it->second == NULL) continue;
if (((Eng*) it->second)->pathid.length() == pathid.length())
if (((Eng*) it->second)->pathid == pathid) {
((Eng*) it->second)->ref++;
//qDebug() <<"engid "<< pathid;
return (int)it->first;
}
}
//qDebug() << "Nowy " << jesteng << " eng: " << pathid;
eng[jesteng] = new Eng(pathid, path, name);
return jesteng++;
}
int EngLib::removeBroken() {
for (int i = 0; i < jesteng; i++){
if(eng[i] == NULL) continue;
if (eng[i]->loaded != 1)
eng[i] = NULL;
}
}
void EngLib::removeAll(){
eng.clear();
jesteng = 0;
}
int EngLib::getEngByPathid(QString pathid) {
for ( auto it = eng.begin(); it != eng.end(); ++it ){
if(it->second == NULL) continue;
if (((Eng*) it->second)->pathid.length() == pathid.length())
if (((Eng*) it->second)->pathid == pathid) {
return (int)it->first;
}
}
return -1;
}
int EngLib::loadAll(QString gameRoot){
QString path;
path = gameRoot + "/trains/trainset/";
QDir dir(path);
QDir trainDir;
trainDir.setFilter(QDir::Files);
trainDir.setNameFilters(QStringList()<<"*.eng"<<"*.wag");
qDebug() << path;
if(!dir.exists())
qDebug() << "not exist";
dir.setFilter(QDir::Dirs);
qDebug() << dir.count() <<" dirs";
foreach(QString dirFile, dir.entryList()){
//qDebug() << dirFile;
trainDir.setPath(path+dirFile);
foreach(QString engfile, trainDir.entryList()){
qDebug() << path << dirFile <<"/"<< engfile;
addEng(path+dirFile,engfile);
}
}
qDebug() << "loaded";
return 0;
}