summaryrefslogtreecommitdiff
path: root/src/loadmechanism.cpp
blob: 475874672b5a44e0a9938f455a3d45318bcd1650 (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
/*
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 "loadmechanism.h"
#include "utility.h"
#include "util.h"
#include <iplugingame.h>
#include <scriptextender.h>
#include <appconfig.h>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QString>
#include <QMessageBox>
#include <QCryptographicHash>
#include <QCoreApplication>


using namespace MOBase;
using namespace MOShared;


LoadMechanism::LoadMechanism()
  : m_SelectedMechanism(LOAD_MODORGANIZER)
{
}

void LoadMechanism::writeHintFile(const QDir &targetDirectory)
{
  QString hintFilePath = targetDirectory.absoluteFilePath("mo_path.txt");
  QFile hintFile(hintFilePath);
  if (hintFile.exists()) {
    hintFile.remove();
  }
  if (!hintFile.open(QIODevice::WriteOnly)) {
    throw MyException(QObject::tr("failed to open %1: %2").arg(hintFilePath).arg(hintFile.errorString()));
  }
  hintFile.write(qApp->applicationDirPath().toUtf8().constData());
  hintFile.close();
}


void LoadMechanism::removeHintFile(QDir targetDirectory)
{
  targetDirectory.remove("mo_path.txt");
}


bool LoadMechanism::isDirectLoadingSupported()
{
  //FIXME: Seriously? isn't there a 'do i need steam' thing?
  IPluginGame const *game = qApp->property("managed_game").value<IPluginGame const *>();
  if (game->gameName().compare("oblivion", Qt::CaseInsensitive) == 0) {
    // oblivion can be loaded directly if it's not the steam variant
    return !game->gameDirectory().exists("steam_api.dll");
  } else {
    // all other games work afaik
    return true;
  }
}

bool LoadMechanism::isScriptExtenderSupported()
{
  IPluginGame const *game = qApp->property("managed_game").value<IPluginGame const *>();
  ScriptExtender *extender = game->feature<ScriptExtender>();

  // test if there even is an extender for the managed game and if so whether it's installed
  return extender != nullptr && extender->isInstalled();
}

bool LoadMechanism::isProxyDLLSupported()
{
  // using steam_api.dll as the proxy is way too game specific as many games will have different
  // versions of that game.
  // plus: the proxy dll hasn't been working for at least the whole 1.12.x versions of MO and
  // noone reported it so why maintain an unused feature?
  return false;
/*  IPluginGame const *game = qApp->property("managed_game").value<IPluginGame const *>();
  return game->gameDirectory().exists(QString::fromStdWString(AppConfig::proxyDLLTarget()));*/
}


bool LoadMechanism::hashIdentical(const QString &fileNameLHS, const QString &fileNameRHS)
{
  QFile fileLHS(fileNameLHS);
  if (!fileLHS.open(QIODevice::ReadOnly)) {
    throw MyException(QObject::tr("%1 not found").arg(fileNameLHS));
  }
  QByteArray dataLHS = fileLHS.readAll();
  QByteArray hashLHS = QCryptographicHash::hash(dataLHS, QCryptographicHash::Md5);

  fileLHS.close();

  QFile fileRHS(fileNameRHS);
  if (!fileRHS.open(QIODevice::ReadOnly)) {
    throw MyException(QObject::tr("%1 not found").arg(fileNameRHS));
  }
  QByteArray dataRHS = fileRHS.readAll();
  QByteArray hashRHS = QCryptographicHash::hash(dataRHS, QCryptographicHash::Md5);

  fileRHS.close();

  return hashLHS == hashRHS;
}


void LoadMechanism::deactivateScriptExtender()
{
  try {
    IPluginGame const *game = qApp->property("managed_game").value<IPluginGame const *>();
    ScriptExtender *extender = game->feature<ScriptExtender>();
    if (extender == nullptr) {
      throw MyException(QObject::tr("game doesn't support a script extender"));
    }

    QDir pluginsDir(game->gameDirectory().absolutePath() + "/data/" + extender->name() + "/plugins");

    QString hookDLLName = ToQString(AppConfig::hookDLLName());
    if (QFile(pluginsDir.absoluteFilePath(hookDLLName)).exists()) {
      // remove dll from SE plugins directory
      if (!pluginsDir.remove(hookDLLName)) {
        throw MyException(QObject::tr("Failed to delete %1").arg(pluginsDir.absoluteFilePath(hookDLLName)));
      }
    }

    removeHintFile(pluginsDir);
  } catch (const std::exception &e) {
    QMessageBox::critical(nullptr, QObject::tr("Failed to deactivate script extender loading"), e.what());
  }
}


void LoadMechanism::deactivateProxyDLL()
{
  try {
    IPluginGame const *game = qApp->property("managed_game").value<IPluginGame const *>();

    QString targetPath = game->gameDirectory().absoluteFilePath(QString::fromStdWString(AppConfig::proxyDLLTarget()));

    QFile targetDLL(targetPath);
    if (targetDLL.exists()) {
      QString origFile = game->gameDirectory().absoluteFilePath(QString::fromStdWString(AppConfig::proxyDLLOrig()));
      // determine if a proxy-dll is installed
      // this is a very crude way of making this decision but it should be good enough
      if ((targetDLL.size() < 24576) && (QFile(origFile).exists())) {
        // remove proxy-dll
        if (!targetDLL.remove()) {
          throw MyException(QObject::tr("Failed to remove %1: %2").arg(targetPath).arg(targetDLL.errorString()));
        } else if (!QFile::rename(origFile, targetPath)) {
          throw MyException(QObject::tr("Failed to rename %1 to %2").arg(origFile, targetPath));
        }
      }
    }

    removeHintFile(game->gameDirectory());
  } catch (const std::exception &e) {
    QMessageBox::critical(nullptr, QObject::tr("Failed to deactivate proxy-dll loading"), e.what());
  }
}


void LoadMechanism::activateScriptExtender()
{
  try {
    IPluginGame const *game = qApp->property("managed_game").value<IPluginGame const *>();
    ScriptExtender *extender = game->feature<ScriptExtender>();
    if (extender == nullptr) {
      throw MyException(QObject::tr("game doesn't support a script extender"));
    }

    QDir pluginsDir(game->gameDirectory().absolutePath() + "/data/" + extender->name() + "/plugins");

    if (!pluginsDir.exists()) {
      pluginsDir.mkpath(".");
    }

    QString targetPath = pluginsDir.absoluteFilePath(ToQString(AppConfig::hookDLLName()));
    QString hookDLLPath = qApp->applicationDirPath() + "/" + QString::fromStdWString(AppConfig::hookDLLName());

    QFile dllFile(targetPath);

    if (dllFile.exists()) {
      // may be outdated
      if (!hashIdentical(targetPath, hookDLLPath)) {
        dllFile.remove();
      }
    }

    if (!dllFile.exists()) {
      // install dll to SE plugins
      if (!QFile::copy(hookDLLPath, targetPath)) {
        throw MyException(QObject::tr("Failed to copy %1 to %2").arg(hookDLLPath, targetPath));
      }
    }
    writeHintFile(pluginsDir);
  } catch (const std::exception &e) {
    QMessageBox::critical(nullptr, QObject::tr("Failed to set up script extender loading"), e.what());
  }
}


void LoadMechanism::activateProxyDLL()
{
  try {
    IPluginGame const *game = qApp->property("managed_game").value<IPluginGame const *>();

    QString targetPath = game->gameDirectory().absoluteFilePath(QString::fromStdWString(AppConfig::proxyDLLTarget()));

    QFile targetDLL(targetPath);
    if (!targetDLL.exists()) {
      return;
    }

    QString sourcePath = qApp->applicationDirPath() + "/" + ToQString(AppConfig::proxyDLLSource());

    // this is a very crude way of making this decision but it should be good enough
    if (targetDLL.size() < 24576) {
      // determine if a proxy-dll is already installed and if so, if it's the right one
      if (!hashIdentical(targetPath, sourcePath)) {
        // wrong proxy dll, probably outdated. delete and install the new one
        if (!QFile::remove(targetPath)) {
          throw MyException(QObject::tr("Failed to delete old proxy-dll %1").arg(targetPath));
        }
        if (!QFile::copy(sourcePath, targetPath)) {
          throw MyException(QObject::tr("Failed to copy %1 to %2").arg(sourcePath).arg(targetPath));
        }
      } // otherwise the proxy-dll is already the right one
    } else {
      // no proxy dll installed yet. move the original and insert proxy-dll

      QString origFile = game->gameDirectory().absoluteFilePath(QString::fromStdWString(AppConfig::proxyDLLOrig()));

      if (QFile(origFile).exists()) {
        // orig-file exists. this may happen if the steam-api was updated or the user messed with the
        // dlls.
        if (!QFile::remove(origFile)) {
          throw MyException(QObject::tr("Failed to overwrite %1").arg(origFile));
        }
      }
      if (!QFile::rename(targetPath, origFile)) {
        throw MyException(QObject::tr("Failed to rename %1 to %2").arg(targetPath).arg(origFile));
      }
      if (!QFile::copy(sourcePath, targetPath)) {
        throw MyException(QObject::tr("Failed to copy %1 to %2").arg(sourcePath).arg(targetPath));
      }
    }
    writeHintFile(game->gameDirectory());
  } catch (const std::exception &e) {
    QMessageBox::critical(nullptr, QObject::tr("Failed to set up proxy-dll loading"), e.what());
  }
}


void LoadMechanism::activate(EMechanism mechanism)
{
  switch (mechanism) {
    case LOAD_MODORGANIZER: {
      deactivateProxyDLL();
      deactivateScriptExtender();
    } break;
    case LOAD_SCRIPTEXTENDER: {
      deactivateProxyDLL();
      activateScriptExtender();
    } break;
    case LOAD_PROXYDLL: {
      deactivateScriptExtender();
      activateProxyDLL();
    } break;
  }
}