summaryrefslogtreecommitdiff
path: root/src/client/cl_rest.cpp
blob: c1ff2fcf1af277666c4c80a741c24d3e7b558273 (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
//
// Restclient wrapper abusively modified to be a one-off donwloader.
// -wtfbbqhax
//
// TODO Verify sha256 of file if exists

#include "cl_rest.h"

#include <unistd.h>

#include <cerrno>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>

#include "qcommon/files.h"
#include "restclient/restclient.h"

bool is_good(std::string filename, int permissions = (R_OK|W_OK))
{
    int ret = access(filename.c_str(), permissions);
    if (ret)
        std::cerr << filename << ": " << strerror(errno) << std::endl;

    return !ret;
}

bool MakeDir(std::string destdir, std::string basegame)
{
    std::string destpath(destdir);

    if ( basegame != "" )
    {
        destpath += '/';
        destpath += basegame;
    }

    if ( *destpath.rbegin() != '/' )
        destpath += '/'; // XXX FS_CreatePath requires a trailing slash. 
        // Maybe the assumption is that a file listing might be included?

    FS_CreatePath(destpath.c_str());
    return true;
}

#include "sys/dialog.h"
static bool PromptDownloadPk3s(std::string basegame, const std::vector<std::string>& missing)
{
    std::string msg;
        
    msg = "The following files must be downloaded to complete the installation.\n\n";
    for ( auto f : missing )
        msg += "\t" + basegame + "/" + f + "\n";

    msg += "\n";
    msg += "Yes to continue, No to quit the game.";

    if( Sys_Dialog( DT_YES_NO, msg.c_str(), "You're almost ready!" ) == DR_YES )
        return true;

    return false;
}

bool GetTremulousPk3s(const char* destdir, const char* basegame)
{
    std::string baseuri = "https://github.com/wtfbbqhax/tremulous-data/raw/master/";
    std::vector<std::string> files = { 
        "data-gpp1.pk3",
        "data-1.1.0.pk3",
        "map-arachnid2-1.1.0.pk3",
        "map-atcs-1.1.0.pk3",
        "map-karith-1.1.0.pk3",
        "map-nexus6-1.1.0.pk3",
        "map-niveus-1.1.0.pk3",
        "map-transit-1.1.0.pk3",
        "map-tremor-1.1.0.pk3",
        "map-uncreation-1.1.0.pk3"
    };

    RestClient::init();

    MakeDir(destdir, basegame);

    if (!PromptDownloadPk3s(basegame, files))
        return false;

    for (auto f : files )
    {
        std::string destpath(destdir);
        destpath += "/";
        destpath += basegame;
        destpath += "/";
        destpath += f;

        if ( is_good(destpath) )
        {
            return false;
        }

        std::cout << "Downloading " << baseuri << f << std::endl;
        std::ofstream dl(destpath);
        //dl.open(destpath);
        if ( dl.fail() )
        {
            std::cerr << "Error " << strerror(errno) << "\n";
            continue;
        }

        RestClient::Response resp = RestClient::get(baseuri + f);

        dl << resp.body;
        dl.close();
    }

    return true;
}