summaryrefslogtreecommitdiff
path: root/src/usvfsconnector.cpp
blob: f3a45dfe681706dd412ceed85dd63ea53b34f0b8 (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
/*
Copyright (C) 2015 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 "usvfsconnector.h"
#include "settings.h"
#include <memory>
#include <QTemporaryFile>
#include <QProgressDialog>
#include <QDateTime>
#include <QCoreApplication>


static const char SHMID[] = "mod_organizer_instance";


/*
extern "C" DLLEXPORT BOOL WINAPI VirtualLinkFile(LPCWSTR source, LPCWSTR destination, BOOL failIfExists);
extern "C" DLLEXPORT BOOL WINAPI VirtualLinkDirectoryStatic(LPCWSTR source, LPCWSTR destination, unsigned int flags);
extern "C" DLLEXPORT BOOL WINAPI ConnectVFS(const usvfs::Parameters *parameters);
extern "C" DLLEXPORT void WINAPI DisconnectVFS();
extern "C" DLLEXPORT void WINAPI GetCurrentVFSName(char *buffer, size_t size);
extern "C" DLLEXPORT BOOL WINAPI CreateProcessHooked(LPCWSTR lpApplicationName
                                                     , LPWSTR lpCommandLine
                                                     , LPSECURITY_ATTRIBUTES lpProcessAttributes
                                                     , LPSECURITY_ATTRIBUTES lpThreadAttributes
                                                     , BOOL bInheritHandles
                                                     , DWORD dwCreationFlags
                                                     , LPVOID lpEnvironment
                                                     , LPCWSTR lpCurrentDirectory
                                                     , LPSTARTUPINFOW lpStartupInfo
                                                     , LPPROCESS_INFORMATION lpProcessInformation
                                                     );
extern "C" DLLEXPORT void __cdecl InitLogging(bool toLocal = false);
extern "C" DLLEXPORT void __cdecl InitHooks(LPVOID userData, size_t userDataSize);
*/


LogWorker::LogWorker()
  : m_Buffer(1024, '\0')
  , m_QuitRequested(false)
  , m_LogFile(qApp->property("dataPath").toString()
              + QString("/logs/usvfs-%1.log").arg(
                QDateTime::currentDateTimeUtc().toString("yyyy-MM-dd_hh-mm-ss")))
{
  m_LogFile.open(QIODevice::WriteOnly);
  qDebug("usvfs log messages are written to %s", qPrintable(m_LogFile.fileName()));
}

LogWorker::~LogWorker()
{
}

void LogWorker::process()
{
  while (!m_QuitRequested) {
    if (GetLogMessages(&m_Buffer[0], m_Buffer.size(), false)) {
      m_LogFile.write(m_Buffer.c_str());
      m_LogFile.write("\n");
      m_LogFile.flush();
    } else {
      QThread::sleep(1);
    }
  }
  emit finished();
}

void LogWorker::exit()
{
  m_QuitRequested = true;
}

LogLevel logLevel(int level)
{
  switch (level) {
    case LogLevel::Info:    return LogLevel::Info;
    case LogLevel::Warning: return LogLevel::Warning;
    case LogLevel::Error:   return LogLevel::Error;
    default:                return LogLevel::Debug;
  }
}

UsvfsConnector::UsvfsConnector()
{
  usvfs::Parameters params(SHMID, false, logLevel(Settings::instance().logLevel()));
  InitLogging(false);
  ConnectVFS(&params);

  m_LogWorker.moveToThread(&m_WorkerThread);

  connect(&m_WorkerThread, SIGNAL(started()), &m_LogWorker, SLOT(process()));
  connect(&m_LogWorker, SIGNAL(finished()), &m_WorkerThread, SLOT(quit()));

  m_WorkerThread.start(QThread::LowestPriority);
}

UsvfsConnector::~UsvfsConnector()
{
  DisconnectVFS();
  m_LogWorker.exit();
  m_WorkerThread.quit();
  //m_WorkerThread.wait();
}

void UsvfsConnector::updateMapping(const MappingType &mapping)
{
  QProgressDialog progress;
  progress.setLabelText(tr("Preparing vfs"));
  progress.setMaximum(mapping.size());
  progress.show();
  int value = 0;
  for (auto map : mapping) {
    progress.setValue(value++);
    if (value % 10 == 0) {
      QCoreApplication::processEvents();
    }
    if (map.isDirectory) {
      VirtualLinkDirectoryStatic(map.source.toStdWString().c_str()
                                 , map.destination.toStdWString().c_str()
                                 , 0);
    } else {
      VirtualLinkFile(map.source.toStdWString().c_str()
                      , map.destination.toStdWString().c_str()
                      , 0);
    }
  }
/*
  size_t dumpSize = 0;
  CreateVFSDump(nullptr, &dumpSize);
  std::unique_ptr<char[]> buffer(new char[dumpSize]);
  CreateVFSDump(buffer.get(), &dumpSize);
  qDebug(buffer.get());
*/
}