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

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

const static struct Command dxvk_commands[] = {
    { .name = "download",       .func = dxvk_download,   .description = "download and install 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" },
};

int dxvk(int argc, char** argv)
{
    if (argc > 1)
    {
        for (int i = 0; i < ARRAY_LEN(dxvk_commands); ++i)
        {
            if (!strcmp(dxvk_commands[i].name, argv[1])) return dxvk_commands[i].func(argc-1, argv+1);
        }
    } 

    return dxvk_help(argc-1, argv+1);
}


int dxvk_download(int argc, char** argv)
{
    if (argc == 2)
    {
        struct json_object* runner = fetchJSON(DXVK_API);

        if (runner)
        {

            int choice = atoi(argv[1]);

            if (choice > json_object_array_length(runner) - 1 || choice < 0)
            {
                printf("`%i' is not a valid ID\n\nrun `" NAME " dxvk list' to get a valid ID", choice);
            }
            else
            {
                struct json_object* version = json_object_array_get_idx(runner, choice);
                struct json_object* assets;

                json_object_object_get_ex(version, "assets", &assets);
                version =json_object_array_get_idx(assets, 0);

                json_object_object_get_ex(version, "browser_download_url", &assets);

                char* name = basename((char*)json_object_get_string(assets));
                struct MemoryStruct* archive;

                char dxvkdir[PATH_MAX];
                getDXVKDir(dxvkdir, sizeof(dxvk));
                makeDir(dxvkdir);
                
                printf("Downloading %s\n", name);

                archive = downloadToRam(json_object_get_string(assets));
                if (archive)
                {
                    printf("Extracting %s\n", name);
                    extract(archive, dxvkdir);
                }
                else
                {
                    puts("Something went wrong. The archive went missing");
                }

                free(archive->memory);
                free(archive);
            }

            json_object_put(runner);
        }
    }
    else
    {
        puts(USAGE_STR " dxvk download <ID>\n\nIDs are obtained via `" NAME " dxvk list' ");
    }
    return 0;
}

int dxvk_install(int argc, char** argv)
{
    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))
        {
            printf("`%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
        {
            printf("cannot find the setup script for `%s'\n", dxvkver);
        }

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

        
    return 0;
}

int dxvk_installed(int argc, char** argv)
{
    char dxvkdir[PATH_MAX];
    getDXVKDir(dxvkdir, sizeof(dxvkdir));

    DIR *dir;
    struct dirent *ent;

    printf("Installed DXVK versions:\n");
    if ((dir = opendir(dxvkdir)) != NULL)
    {
        while ((ent = readdir(dir)) != NULL)
        {
            /*
             * WARNING: crusty
             * d_type is only specified on glibc (including musl) and BSD
             */
            if (ent->d_name[0] != '.' && ent->d_type == DT_DIR)
            {
                printf(" - %s\n", ent->d_name);
            }
        }
        closedir (dir);
    } 

    return 0;
}


int dxvk_list(int argc, char** argv)
{
    struct json_object* runner = fetchJSON(DXVK_API);

    if (runner)
    {
        puts("Installable DXVK versions:");

        for (size_t 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, "name", &name);
            printf(" [%zu]\t%s\n", i, json_object_get_string(name));
        }
    }

    return 0;
}

int dxvk_help(int argc, char** argv)
{
    puts(USAGE_STR " dxvk <command>\n\nList of commands:");

    print_help(dxvk_commands, ARRAY_LEN(dxvk_commands));

    return 0;
}