aboutsummaryrefslogtreecommitdiff
path: root/src/dxvk.c
blob: ffe92622fe4530a94f644264e2b58c83ad753d9d (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
#include <stdio.h>
#include <string.h>
#include <json.h>
#include <libgen.h>
#include <unistd.h>
#include <linux/limits.h>
#include <dirent.h>

#include "dxvk.h"
#include "net.h"
#include "tar.h"
#include "config.h"

static const struct Command dxvk_commands[] = {
    { .name = "download",       .func = dxvk_download,   .description = "download and install a dxvk version" },
    { .name = "remove",         .func = dxvk_remove,     .description = "remove a dxvk version" },
    { .name = "list",           .func = dxvk_list,       .description = "list available dxvk versions" },
    { .name = "install",        .func = dxvk_install,    .description = "run the DXVK installer" },
    { .name = "list-installed", .func = dxvk_installed,  .description = "list installed dxvk versions" },
};

COMMAND_GROUP_FUNC(dxvk)

COMMAND(dxvk, download)
{
    if (argc == 2)
    {
        struct json_object* runner = fetchJSON(DXVK_API);

        if (runner)
        {

            struct json_object* value, *temp, *temp2;
            uint8_t found = 0;

            char* choice = argv[1];

            for (JSON_LENGTH_TYPE i = 0; i < json_object_array_length(runner); ++i)
            {
                value = json_object_array_get_idx(runner, i);
                json_object_object_get_ex(value, "tag_name", &temp);
                if (strcmp(json_object_get_string(temp), choice) == 0)
                {
                    found = 1;
                    break;
                }
            }

            if (found)
            {
                json_object_object_get_ex(value, "assets", &temp);
                temp2 = json_object_array_get_idx(temp, 0);

                json_object_object_get_ex(temp2, "browser_download_url", &temp);

                char* name = basename((char*)json_object_get_string(temp));

                char dxvkdir[PATH_MAX];
                struct MemoryStruct* archive;

                getDXVKDir(dxvkdir, sizeof(dxvkdir));
                makeDir(dxvkdir);
                
                fprintf(stderr, "Downloading %s...\n", name);

                archive = downloadToRam(json_object_get_string(temp), 0L);
                if (archive)
                {
                    fprintf(stderr, "Extracting %s\n", name);
                    extract(archive, dxvkdir);
                    fprintf(stderr, "Done\n");
                }
                else
                {
                    fprintf(stderr, "Something went wrong. The archive went missing\n");
                }

                free(archive->memory);
                free(archive);
            }
            else
            {
                fprintf(stderr, "Could not find '%s'\n", choice);
            }

            json_object_put(runner);
        }
    }
    else
    {
        fprintf(stderr, USAGE_STR " dxvk download <version>\n\nversions are obtained via '" NAME " dxvk list'\n");
    }
    return 0;
}

COMMAND(dxvk, remove)
{
    if (argc == 2)
    {
        char* dxvkver = argv[1];

        char dxvkpath[PATH_MAX];
        getDXVKDir(dxvkpath, sizeof(dxvkpath));

        strncat(dxvkpath, "/", sizeof(dxvkpath) - strlen(dxvkpath) - 1);
        strncat(dxvkpath, dxvkver, sizeof(dxvkpath) - strlen(dxvkpath) - 1);


        if (!isDir(dxvkpath))
        {
            fprintf(stderr, "'%s' is not an downloaded DXVK version\n", dxvkver);
            return 0;
        }

        int retval = removeDir(dxvkpath);
        if (!retval)
        {
            fprintf(stderr, "Done\n");
        }
        else
        {
            fprintf(stderr, "Something might have gone wrong. Manual cleanup might be required\n");
        }

        return retval;
    }

    fprintf(stderr, USAGE_STR " dxvk remove <version>\n\nInstalled dxvk versions can be obtained by using '" NAME " dxvk list-installed\n");

    return 0;
}

COMMAND(dxvk, list)
{
    struct json_object* runner = fetchJSON(DXVK_API);

    if (runner)
    {
        int istty = isatty(STDOUT_FILENO); 
        if (istty) puts("Installable DXVK versions:");

        for (JSON_LENGTH_TYPE i = 0; i < json_object_array_length(runner); ++i)
        {
            struct json_object* version = json_object_array_get_idx(runner, i);
            struct json_object* name;

            json_object_object_get_ex(version, "tag_name", &name);
            if (istty) printf(" - ");
            printf("%s\n", json_object_get_string(name));
        }
    }

    return 0;
}

COMMAND(dxvk, install)
{
    if (argc > 1)
    {
        char dxvkpath[PATH_MAX];
        getDXVKDir(dxvkpath, sizeof(dxvkpath));
        char* dxvkver = argv[1];

        strncat(dxvkpath, "/", sizeof(dxvkpath) - strlen(dxvkpath) - 1);
        strncat(dxvkpath, dxvkver, sizeof(dxvkpath) - strlen(dxvkpath) - 1);

        if (!isDir(dxvkpath))
        {
            fprintf(stderr, "'%s' is not an downloaded DXVK version\n", dxvkver);
            return 0;
        }

        strncat(dxvkpath, DXVKSETUP, sizeof(dxvkpath) - strlen(dxvkpath) - 1);

        if (isFile(dxvkpath))
        {
            strncat(dxvkpath, " install", sizeof(dxvkpath) - strlen(dxvkpath) - 1);

            return system(dxvkpath);
        }
        else
        {
            fprintf(stderr, "cannot find the setup script for '%s'\n", dxvkver);
        }

    }
    else
    {
        fprintf(stderr, "Specify a what DXVK version to install.\nUse '" NAME " dxvk list-installed' to list available versions\n");
    }

        
    return 0;
}

COMMAND(dxvk, installed)
{
    char dxvkdir[PATH_MAX];
    getDXVKDir(dxvkdir, sizeof(dxvkdir));

    DIR *dir;
    struct dirent *ent;

    int intty = isatty(STDOUT_FILENO);

    if (intty) puts("Installed DXVK versions:");
    if ((dir = opendir(dxvkdir)) != NULL)
    {
        while ((ent = readdir(dir)) != NULL)
        {
            if (ent->d_name[0] != '.' && ent->d_type == DT_DIR)
            {
                if (intty) printf(" - ");
                printf("%s\n", ent->d_name);
            }
        }
        closedir (dir);
    } 

    return 0;
}

COMMAND_HELP(dxvk, " dxvk")