summaryrefslogtreecommitdiff
path: root/src/savegamegamebryo.cpp
blob: f3794347bfb9086312e6b929307df45d7ff22953 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.

This file is part of Mod Organizer.

Mod Organizer 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.

Mod Organizer 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 Mod Organizer.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "savegamegamebyro.h"
#include <QFile>
#include <QBuffer>
#include <set>
#include "gameinfo.h"
#include <QFileInfo>
#include <QDateTime>
#include <limits>


using namespace MOShared;


template <typename T>
void FileRead(QFile &file, T &value)
{
  int read = file.read(reinterpret_cast<char*>(&value), sizeof(T));
  if (read != sizeof(T)) {
    throw std::runtime_error("unexpected end of file");
  }
}


template <typename T>
void FileSkip(QFile &file, int count = 1)
{
  char ignore[sizeof(T)];
  for (int i = 0; i < count; ++i) {
    if (file.read(ignore, sizeof(T)) != sizeof(T)) {
      throw std::runtime_error("unexpected end of file");
    }
  }
}


QString ReadBString(QFile &file)
{
  char buffer[256];
  file.read(buffer, 1); // size including zero termination
  unsigned char size = buffer[0];
  file.read(buffer, size);
  return QString::fromLatin1(buffer, size);
}


QString ReadFOSString(QFile &file, bool delimiter)
{
  union {
    char lengthBuffer[2];
    unsigned short length;
  };

  file.read(lengthBuffer, 2);
  if (delimiter) {
    FileSkip<char>(file); // 0x7c
  }
  char *buffer = new char[length];
  file.read(buffer, length);

  QString result = QString::fromLatin1(buffer, length);
  delete [] buffer;

  return result;
}


SaveGameGamebryo::SaveGameGamebryo(QObject *parent)
  : SaveGame(parent), m_Plugins()
{
}


SaveGameGamebryo::SaveGameGamebryo(QObject *parent, const QString &fileName)
  : SaveGame(parent, fileName), m_Plugins()
{
  readFile(fileName);
}


SaveGameGamebryo::SaveGameGamebryo(const SaveGameGamebryo& reference)
  : SaveGame(reference), m_Plugins(reference.m_Plugins)
{
}


SaveGameGamebryo& SaveGameGamebryo::operator=(const SaveGameGamebryo &reference)
{
  if (&reference != this) {
    SaveGame::operator =(reference);
    m_Plugins = reference.m_Plugins;
  }
  return *this;
}


SaveGameGamebryo::~SaveGameGamebryo()
{
}


QStringList SaveGameGamebryo::attachedFiles() const
{
  QStringList result;
  QString seFileFile = fileName().mid(0).replace(".ess", ".skse");
  QFileInfo seFile(seFileFile);
  if (seFile.exists()) {
    result.append(seFile.absoluteFilePath());
  }
  return result;
}


void SaveGameGamebryo::readSkyrimFile(QFile &saveFile)
{
  char fileID[14];

  saveFile.read(fileID, 13);
  fileID[13] = '\0';
  if (strncmp(fileID, "TESV_SAVEGAME", 13) != 0) {
    throw std::runtime_error(QObject::tr("wrong file format").toUtf8().constData());
  }

  FileSkip<unsigned long>(saveFile); // header size
  FileSkip<unsigned long>(saveFile); // header version, -> 8
  FileRead(saveFile, m_SaveNumber);

  m_PCName = ReadFOSString(saveFile, false);

  unsigned long temp;
  FileRead(saveFile, temp); // player level
  m_PCLevel = static_cast<unsigned short>(temp);

  m_PCLocation = ReadFOSString(saveFile, false);
  ReadFOSString(saveFile, false); // playtime as ascii hhh.mm.ss
  ReadFOSString(saveFile, false); // race name (i.e. BretonRace)


  FileSkip<unsigned short>(saveFile); // ???
  FileSkip<float>(saveFile, 2); // ???
  FileSkip<unsigned char>(saveFile, 8); // filetime

//  FileSkip<unsigned char>(saveFile, 18); // ??? 18 bytes of data. not completely random, maybe a time stamp? maybe

  unsigned long width, height;
  FileRead(saveFile, width); // 320
  FileRead(saveFile, height); // 192

  QScopedArrayPointer<unsigned char> buffer(new unsigned char[width * height * 3]);
  saveFile.read(reinterpret_cast<char*>(buffer.data()), width * height * 3);
  // why do I have to copy here? without the copy, the buffer seems to get deleted after the
  // temporary vanishes, but Qts implicit sharing should handle that?
  m_Screenshot = QImage(buffer.data(), width, height, QImage::Format_RGB888).copy();

  FileSkip<unsigned char>(saveFile); // form version
  FileSkip<unsigned long>(saveFile); // plugin info size

  unsigned char pluginCount;
  FileRead(saveFile, pluginCount);

  for (int i = 0; i < pluginCount; ++i) {
    m_Plugins.push_back(ReadFOSString(saveFile, false));
  }
}


void SaveGameGamebryo::readESSFile(QFile &saveFile)
{
  char fileID[13];
  unsigned char versionMinor;
  unsigned long headerVersion, saveHeaderSize;
// *** format is different for fallout!
  saveFile.read(fileID, 12);
  fileID[12] = '\0';
  FileSkip<unsigned char>(saveFile); FileRead(saveFile, versionMinor);
  FileSkip<SYSTEMTIME>(saveFile); // modified time
  FileRead(saveFile, headerVersion); FileRead(saveFile, saveHeaderSize);

  if (strncmp(fileID, "TES4SAVEGAME", 12) != 0) {
    throw std::runtime_error(QObject::tr("wrong file format").toUtf8().constData());
  }

  FileRead(saveFile, m_SaveNumber);

  m_PCName = ReadBString(saveFile);

  FileRead(saveFile, m_PCLevel);
  m_PCLocation = ReadBString(saveFile);
  FileSkip<float>(saveFile);         // game days
  FileSkip<unsigned long>(saveFile); // game ticks
  FileRead(saveFile, m_CreationTime);

  unsigned long size;
  FileRead(saveFile, size); // screenshot size

  unsigned long width, height;
  FileRead(saveFile, width); FileRead(saveFile, height);
  QScopedArrayPointer<unsigned char> buffer(new unsigned char[width * height * 3]);
  saveFile.read(reinterpret_cast<char*>(buffer.data()), width * height * 3);
  // why do I have to copy here? without the copy, the buffer seems to get deleted after the
  // temporary vanishes, but Qts implicit sharing should handle that?
  m_Screenshot = QImage(buffer.data(), width, height, QImage::Format_RGB888).copy();

  unsigned char pluginCount;
  FileRead(saveFile, pluginCount);

  for (int i = 0; i < pluginCount; ++i) {
    QString name = ReadBString(saveFile);
    m_Plugins.push_back(name);
  }
}


void SaveGameGamebryo::readFOSFile(QFile &saveFile, bool newVegas)
{
  char fileID[13];
  saveFile.read(fileID, 12);
  // the signature is only 11 characters, the 12th is random?
  fileID[11] = '\0';

  if (strncmp(fileID, "FO3SAVEGAME", 11) != 0) {
    throw std::runtime_error(QObject::tr("wrong file format").toUtf8().constData());
  }

  char ignore = 0x00;
  while (ignore != 0x7c) {
    FileRead<char>(saveFile, ignore); // unknown
  }
  if (newVegas) {
    ignore = 0x00;
    // in new vegas there is another block of uninteresting (?) information
    FileSkip<char>(saveFile); // 0x7c
    while (ignore != 0x7c) {
      FileRead<char>(saveFile, ignore); // unknown
    }
  }

  unsigned long width, height;
  FileRead(saveFile, width);
  FileSkip<char>(saveFile); // 0x7c
  FileRead(saveFile, height);
  FileSkip<char>(saveFile); // 0x7c

  FileRead(saveFile, m_SaveNumber);
  FileSkip<char>(saveFile); // 0x7c

  m_PCName = ReadFOSString(saveFile, true);
  FileSkip<char>(saveFile); // 0x7c

  ReadFOSString(saveFile, true);
  FileSkip<char>(saveFile); // 0x7c

  long Level;
  FileRead(saveFile, Level);
  m_PCLevel = Level;
  FileSkip<char>(saveFile); // 0x7c

  m_PCLocation = ReadFOSString(saveFile, true);
  FileSkip<char>(saveFile); // 0x7c

  ReadFOSString(saveFile, true); // playtime

  FileSkip<char>(saveFile);

  QScopedArrayPointer<unsigned char> buffer(new unsigned char[width * height * 3]);
  saveFile.read(reinterpret_cast<char*>(buffer.data()), width * height * 3);
  // why do I have to copy here? without the copy, the buffer seems to get deleted after the
  // temporary vanishes, but Qts implicit sharing should handle that?
  m_Screenshot = QImage(buffer.data(), width, height, QImage::Format_RGB888).scaledToWidth(256);

  FileSkip<char>(saveFile, 5); // unknown

  unsigned char pluginCount = 0;
  FileRead(saveFile, pluginCount);
  FileSkip<char>(saveFile); // 0x7c

  for (int i = 0; i < pluginCount; ++i) {
    QString name = ReadFOSString(saveFile, true);
    m_Plugins.push_back(name);
    FileSkip<char>(saveFile); // 0x7c
  }
}


void SaveGameGamebryo::setCreationTime(const QString &fileName)
{
  QFileInfo creationTime(fileName);
  QDateTime modified = creationTime.lastModified();
  memset(&m_CreationTime, 0, sizeof(SYSTEMTIME));

  m_CreationTime.wDay = static_cast<WORD>(modified.date().day());
  m_CreationTime.wDayOfWeek = static_cast<WORD>(modified.date().dayOfWeek());
  m_CreationTime.wMonth = static_cast<WORD>(modified.date().month());
  m_CreationTime.wYear =static_cast<WORD>( modified.date().year());

  m_CreationTime.wHour = static_cast<WORD>(modified.time().hour());
  m_CreationTime.wMinute = static_cast<WORD>(modified.time().minute());
  m_CreationTime.wSecond = static_cast<WORD>(modified.time().second());
  m_CreationTime.wMilliseconds = static_cast<WORD>(modified.time().msec());
}


void SaveGameGamebryo::readFile(const QString &fileName)
{
  m_FileName = fileName;
  QFile saveFile(fileName);
  if (!saveFile.open(QIODevice::ReadOnly)) {
    throw std::runtime_error(QObject::tr("failed to open %1").arg(fileName).toUtf8().constData());
  }
  switch (GameInfo::instance().getType()) {
    case GameInfo::TYPE_FALLOUT3: {
      setCreationTime(fileName);
      readFOSFile(saveFile, false);
    } break;
    case GameInfo::TYPE_FALLOUTNV: {
      setCreationTime(fileName);
      readFOSFile(saveFile, true);
    } break;
    case GameInfo::TYPE_OBLIVION: {
      readESSFile(saveFile);
    } break;
    case GameInfo::TYPE_SKYRIM: {
      setCreationTime(fileName);
      readSkyrimFile(saveFile);
    } break;
  }

  saveFile.close();
}