-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathujlparser.cpp
More file actions
executable file
·159 lines (127 loc) · 5.38 KB
/
Copy pathujlparser.cpp
File metadata and controls
executable file
·159 lines (127 loc) · 5.38 KB
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
#include "ujlparser.h"
#include <algorithm>
#include <sstream>
char buff[] = {0};
std::fstream rawFile;
int ujlparser::loadFile(QString path)
{
rawFile.open(path.toUtf8(), std::ios_base::in | std::ios_base::binary);
if(!rawFile.is_open()) return 4;
ujldata::ClearAll();
// Starts reading the file process
parseInfo();
qDebug() << "-- Searching cutscene lines past " << rawFile.tellg();
getCutscene(fileInfo.addrList.cutsceneAddress);
qDebug() << "-- Searching gameplay lines past " << rawFile.tellg();
getGameplay();
if(fileInfo.addrList.cutsceneEndAddress != (char)0xFFFF) {
qDebug() << "-- Searching cutscene end lines past " << rawFile.tellg();
getCutscene(fileInfo.addrList.cutsceneEndAddress);
}
qDebug() << "file is loaded! " << path;
fileInfo.orgFilePath = path;
rawFile.close();
return 0;
}
int ujlparser::parseInfo() {
char magicbyte;
rawFile.get(magicbyte);
for(BINInfo stage : StageDatabase) {
if((char)magicbyte != (char)stage.Magic) continue;
if(stage.STGRRegAddr[fileInfo.currentRegion].gameplayAddress != (char)0xFFFF) fileInfo.isStageFile = true;
fileInfo.languageCount = 1; // TODO: NTSC-J/PAL & where the fuck are the others?
fileInfo.Name = stage.STGName;
fileInfo.Description = stage.STGDescription;
fileInfo.addrList.cutsceneAddress = stage.STGRRegAddr[fileInfo.currentRegion].cutsceneAddress;
fileInfo.addrList.cutsceneEndAddress = stage.STGRRegAddr[fileInfo.currentRegion].cutsceneEndAddress;
fileInfo.addrList.gameplayAddress = stage.STGRRegAddr[fileInfo.currentRegion].gameplayAddress;
fileInfo.emptyArea = stage.STGEmptyArea;
break;
};
return 0;
}
void ujlparser::getCutscene(uint32_t cutsceneAddress) {
rawFile.seekg(cutsceneAddress);
processCutscene();
return;
}
void ujlparser::processCutscene() {
for(int cl = 0; cl < fileInfo.languageCount; cl++) {
CTable cutscene;
cutscene.Language = cl;
cutscene.Position = rawFile.tellg();
while(rawFile.peek() != EOF) {
cLine tmpLine;
uint32_t tmpLangPos;
rawFile.read(reinterpret_cast<char *>(&tmpLangPos), sizeof(tmpLangPos));
rawFile.read(reinterpret_cast<char *>(&tmpLine.timestart), sizeof(tmpLine.timestart));
rawFile.read(reinterpret_cast<char *>(&tmpLine.timeend), sizeof(tmpLine.timeend));
if(tmpLangPos == 0 || tmpLangPos < fileInfo.binPositionStart) break;
bool isLinked;
int tmpSubID = processLine(tmpLangPos, (fileInfo.languageCount == 2 && cl == 1), isLinked);
tmpLine.localisation = tmpSubID;
tmpLine.localelinked = isLinked;
cutscene.Subtitles.push_back(tmpLine);
}
cutscene.EndPosition = rawFile.tellg();
Cutscenes.push_back(cutscene);
}
qDebug() << "-- End cutscene on " << rawFile.tellg();
return;
}
void ujlparser::getGameplay() {
rawFile.seekg(fileInfo.addrList.gameplayAddress);
processGameplay();
}
void ujlparser::processGameplay() {
for(int cl = 0; cl < fileInfo.languageCount; cl++) {
uint32_t lastLangPos = 0;
GTable gameplay;
gameplay.Language = cl;
gameplay.Position = rawFile.tellg();
while(rawFile.peek() != EOF) {
gLine tmpLine;
uint32_t tmpLangPos;
rawFile.ignore(4);
rawFile.read(reinterpret_cast<char *>(&tmpLine.unknown), sizeof(tmpLine.unknown));
rawFile.read(reinterpret_cast<char *>(&tmpLangPos), sizeof(tmpLangPos));
if(tmpLangPos == 0 || tmpLangPos < fileInfo.binPositionStart) break;
tmpLine.owner = (lastLangPos == tmpLangPos) ? 1 : 0;
lastLangPos = (tmpLine.owner == 0) ? tmpLangPos : 0;
lastLangPos = tmpLangPos;
bool isLinked;
int tmpSubID = processLine(tmpLangPos, (fileInfo.languageCount == 2 && cl == 1), isLinked);
tmpLine.localisation = tmpSubID;
tmpLine.localelinked = isLinked;
gameplay.Subtitles.push_back(tmpLine);
}
gameplay.EndPosition = rawFile.tellg();
Gameplays.push_back(gameplay);
}
qDebug() << "-- End cutscene on " << rawFile.tellg();
return;
}
std::string ujlparser::parseLine(uint32_t position) {
std::stringstream subtitle;
int lastPos = rawFile.tellg();
char a[1];
rawFile.seekg(position - fileInfo.binPositionStart);
while(rawFile.peek() != 0x00 && rawFile.peek() != EOF) {
rawFile.read(a, 1);
if(a[0] == (char)0x0A) subtitle << "@";
else if(a[0] != (char)0x00) subtitle << a[0];
}
rawFile.seekg(lastPos);
return subtitle.str();
}
int ujlparser::processLine(uint32_t position, bool eucJP, bool &isLinked) {
auto subFinder = std::find_if(Subtitles.begin(), Subtitles.end(), [&position](const Subtitle& obj) {return obj.Position == position;});
isLinked = (subFinder != Subtitles.end());
if(isLinked) return std::distance(Subtitles.begin(), subFinder);
Subtitle tmpSub;
tmpSub.Position = position;
std::string rawText = parseLine(position);
tmpSub.Text = QString::fromLatin1(rawText);
Subtitles.push_back(tmpSub);
return Subtitles.size() - 1;
}