summaryrefslogtreecommitdiff
path: root/src/savegamegamebryo.cpp
blob: 06a72d2b1728bfe37396556af240171d5aacca0d (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
/*
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 "isavegame.h"
#include "savegameinfo.h"
#include "iplugingame.h"
#include <gamebryosavegame.h>
#include <scopeguard.h>
#include <limits>
#include <set>

using namespace MOBase;


SaveGameGamebryo::SaveGameGamebryo(QObject *parent, const QString &fileName, IPluginGame const *game)
  : SaveGame(parent, fileName, game)
  , m_Plugins()
{
  // ensure creation time is set to a valid, preferrably even a reasonable value
  memset(&m_CreationTime, 0, sizeof(SYSTEMTIME));
  HANDLE fileHandle = CreateFileW(fileName.toStdWString().c_str(),
                                  GENERIC_READ,
                                  FILE_SHARE_READ | FILE_SHARE_WRITE,
                                  nullptr,
                                  OPEN_EXISTING,
                                  FILE_ATTRIBUTE_NORMAL,
                                  nullptr);
  ON_BLOCK_EXIT([fileHandle] () {
    CloseHandle(fileHandle);
  });
  if (fileHandle != INVALID_HANDLE_VALUE) {
    FILETIME creationTime;
    if (GetFileTime(fileHandle, &creationTime, nullptr, nullptr)) {
      FileTimeToSystemTime(&creationTime, &m_CreationTime);
    }
  }

  const SaveGameInfo *info = game->feature<SaveGameInfo>();
  if (info != nullptr) {
    const ISaveGame *save = info->getSaveGameInfo(fileName);
    m_Save = save;

    //Kludgery
    const GamebryoSaveGame *s = dynamic_cast<const GamebryoSaveGame *>(save);
    m_PCName = s->getPCName();
    m_PCLevel = s->getPCLevel();
    m_PCLocation = s->getPCLocation();
    m_SaveNumber = s->getSaveNumber();

    QDateTime modified = s->getCreationTime();
    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());

    m_Screenshot = s->getScreenshot();

    m_Plugins = s->getPlugins();
  }
}