aboutsummaryrefslogtreecommitdiff
path: root/src/qt/workers.cpp
blob: b0e18e8c7e96cdcdb77ca2b1ea10f8fb5e1171db (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
#include <iostream>
#include <limits.h>
#include <pthread.h>

#include "net.h"
#include "steam.h"
#include "toast.h"
#include "pool.h"

#include "./ui_mainwindow.h"
#include "workers.hpp"

struct thread_object_info {
    QString infoText;
    char* of_dir;
    char* remote;
    struct revision_t* rev;
    size_t index;

    Worker* worker;
};

static void* thread_download(void* pinfo)
{
    struct thread_object_info* info = (struct thread_object_info*)pinfo;
    Worker* worker = info->worker;
    int* do_work = &worker->do_work;

    if (info && *do_work)
    {
        char* of_dir = info->of_dir;
        char* remote = info->remote;
        struct revision_t* rev = info->rev;
        size_t i = info->index;

        struct file_info* file = &rev->files[i];
        if (file->type == TYPE_WRITE && *do_work)
        {
            info->infoText = QString("Verifying %1").arg(file->object);
            if (verifyFileHash(of_dir, file) && *do_work)
            {
                info->infoText = QString("Downloading %1").arg(file->object);
                downloadObject(of_dir, remote, file);
            }
        }

        QString* threadString = &info->infoText;
        if (!threadString->isEmpty() && *do_work)
        {
            int progress = (int)(((info->index * 100) + 1) / rev->file_count);
            if (progress > worker->progress)
                worker->progress = progress;

            worker->setInfoText(*threadString);
        }
    }
    return NULL;
}

Worker::Worker()
{
    net_init();
    of_dir = NULL;
    remote = NULL;
    textMutex = PTHREAD_MUTEX_INITIALIZER;
}

Worker::~Worker()
{
    net_deinit();

    if (of_dir)
        free(of_dir);

    if (remote)
        free(remote);
}

QString Worker::getOfDir()
{
    return QString(of_dir);
}

QString Worker::getRemote()
{
    return QString(remote);
}

void Worker::setRemote(QString remotestr)
{
    if (remote)
        free(remote);

    remote = strdup(remotestr.toStdString().c_str());
    setLocalRemote(of_dir, remote);
}

QString Worker::getArguments()
{
    return settings.value("launchArguments", QString()).toString();
}

void Worker::setInfoText(QString infoTextArg)
{
    pthread_mutex_lock(&this->textMutex);
    this->infoText = infoTextArg;
    emit this->resultReady(Worker::RESULT_UPDATE_TEXT);
    pthread_mutex_unlock(&this->textMutex);
}

void Worker::setArguments(QString argumentstr)
{
    settings.setValue("launchArguments", argumentstr);
}

int Worker::getRevision()
{
    return getLocalRevision(of_dir);
}

int Worker::getRemoteRevision()
{
    return getLatestRemoteRevision(remote);
}

bool Worker::isOutdated()
{
    return getRemoteRevision() > getRevision();
}

void Worker::stop_work()
{
    do_work = 0;
}

int Worker::update_setup(int local_rev, int remote_rev)
{
    if (!of_dir) return 1;
    else if (update_in_progress) return 0;

    update_in_progress = true;

    int retval = 0;
    struct revision_t* rev = fastFowardRevisions(remote, local_rev, remote_rev);

    if (rev)
    {
        for (size_t i = 0; i < rev->file_count && do_work; ++i)
        {
            struct file_info* file = &rev->files[i];

            if (leavesRelativePath(file->path))
            {
                infoText = QString("Revision contains invalid path '%1'").arg(file->path);
                emit resultReady(RESULT_UPDATE_TEXT);

                update_in_progress = false;
                freeRevision(rev);
                return 1;
            }
        }

        struct thread_object_info* thread_info = new struct thread_object_info[rev->file_count];
        struct pool_t* pool = pool_init();
        pool->condition = &do_work;

        for (size_t i = 0; i < rev->file_count && do_work; ++i)
        {
            struct thread_object_info* info = &thread_info[i];

            info->of_dir = of_dir;
            info->remote = remote;
            info->rev = rev;
            info->index = i;
            info->worker = this;

            pool_submit(pool, thread_download, info);
        }

        pool_complete(pool);
        pool_free(pool);
        delete[] thread_info;

        progress = 0;
        this->setInfoText("Processing");

        for (size_t i = 0; i < rev->file_count && do_work; ++i)
        {
            struct file_info* file = &rev->files[i];

            if (file->type != TYPE_DELETE) continue;
            size_t len = strlen(of_dir) + strlen(OS_PATH_SEP) + strlen(file->path) + 1;
            char* buf = new char[len];
            snprintf(buf, len, "%s%s%s", of_dir, OS_PATH_SEP, file->path);
            if (isFile(buf))
            {
                remove(buf);
            }
            delete[] buf;

        }

        for (size_t i = 0; i < rev->file_count && do_work; ++i)
        {
            struct file_info* file = &rev->files[i];

            if (file->type != TYPE_MKDIR) continue;
            size_t len = strlen(of_dir) + strlen(OS_PATH_SEP) + strlen(file->path) + 1;
            char* buf = new char[len];
            snprintf(buf, len, "%s%s%s", of_dir, OS_PATH_SEP, file->path);
            makeDir(buf);
            delete[] buf;
        }

        for (size_t i = 0; i < rev->file_count && do_work; ++i)
        {
            struct file_info* file = &rev->files[i];

            if (file->type != TYPE_WRITE) continue;
            applyObject(of_dir, file);
        }

        if (do_work)
        {
            removeObjects(of_dir);
            setLocalRemote(of_dir, remote);
            setLocalRevision(of_dir, remote_rev);
        }

        freeRevision(rev);
    }

    this->setInfoText("");
    update_in_progress = false;

    return retval;
}


void Worker::doWork(const enum Worker::Tasks_t &parameter) {
    Results_t result = RESULT_NONE;


    switch (parameter)
    {
        case TASK_INVALID:
            break;

        case TASK_INIT:
            result = RESULT_INIT_FAILURE;
            of_dir = getOpenFortressDir();

            if (of_dir)
            {
                of_dir_len = strlen(of_dir);
                remote = getLocalRemote(of_dir);

                if (remote)
                {
                    remote_len = strlen(remote);
                    result = RESULT_INIT_COMPLETE;
                }
                else
                {
                    free(of_dir);
                    of_dir = NULL;
                    remote = NULL;
                }
            }
            else
            {
                of_dir = NULL;
                remote = NULL;
            }

            break;

        case TASK_IS_INSTALLED:
            result = getRevision() > -1 ? RESULT_IS_INSTALLED : RESULT_IS_NOT_INSTALLED;
            break;

        case TASK_IS_UPTODATE:
            result = isOutdated() ? RESULT_IS_OUTDATED : RESULT_IS_UPTODATE;
            break;

        case TASK_UNINSTALL:
            result = RESULT_UNINSTALL_FAILURE;
            //if (direxists) result = svn_delete(mod) ? RESULT_UNINSTALL_FAILURE : RESULT_UNINSTALL_COMPLETE;
            break;

        case TASK_INSTALL:
            result = update_setup(0, getRemoteRevision()) > 0 ? RESULT_INSTALL_FAILURE : RESULT_INSTALL_COMPLETE;
            break;

        case TASK_UPDATE:
            result = update_setup(getRevision(), getRemoteRevision()) > 0 ? RESULT_UPDATE_FAILURE : RESULT_UPDATE_COMPLETE;
            break;

        case TASK_UPDATE_RUN:
            result = update_setup(getRevision(), getRemoteRevision()) > 0 ? RESULT_UPDATE_FAILURE : RESULT_UPDATE_COMPLETE;
            break;

        case TASK_RUN:
            result = getSteamPID() > -1 ? RESULT_EXIT : RESULT_NO_STEAM;
            if (result == RESULT_EXIT)
            {
                auto arg_list = getArguments().split(' ');

                auto argv = (char**)malloc(sizeof(char*) * arg_list.size());
                for (int i = 0; i < arg_list.size(); ++i)
                    argv[i] = strdup(arg_list[i].toStdString().c_str());

                runOpenFortress(argv, arg_list.size());

                for (int i = 0; i < arg_list.size(); ++i)
                    free(argv[i]);
                free(argv);
            }
            break;

        default:
            assert(0);
            break;
    }

    emit resultReady(result);
}