aboutsummaryrefslogtreecommitdiff
path: root/src/steam.c
blob: 6bcf11a665f7b978f91939de5c07f29332dc8001 (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
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>

#include "vdf.h"
#include "proc.h"
#include "steam.h"

#ifdef HAS_HOOKS
#include "hook.h"
#endif

/**
 * Returns a heap allocated path to the main steam directory
 * If a problem occurs returns NULL
 */
char* getSteamDir(void)
{
#if defined(__linux__) || defined(__FreeBSD__)
    char* home = getenv("HOME");

    if (!home || !isDir(home))
        return NULL;

    size_t len = strlen(home);
#ifdef FLATPAK_DIR
    len += strlen(FLATPAK_DIR);
#else
    len += strlen(STEAM_DIR);
#endif
    char* path = malloc(len+1);

    strncpy(path, home, len);
    strncat(path, STEAM_DIR, len-strlen(path));

    if (isDir(path))
        return path;

#ifdef FLATPAK_DIR
    strncpy(path, home, len);
    strncat(path, FLATPAK_DIR, len-strlen(path));

    if (isDir(path))
        return path;
#endif

    free(path);
#elif defined(_WIN32)
    size_t size = PATH_MAX;
    char* path = malloc(size+1);
    if (!path)
        return NULL;

    LSTATUS res = RegGetValueA(HKEY_LOCAL_MACHINE, REG_PATH, "InstallPath", RRF_RT_REG_SZ, NULL, path, (LPDWORD)&size);

    if (res == ERROR_SUCCESS && isDir(path))
        return path;
    
    strncpy(path, STEAM_PGRM_64, size);
    path[size] = '\0';

    if (isDir(path))
        return path;

    strncpy(path, STEAM_PGRM_86, size);
    path[size] = '\0';

    if (isDir(path))
        return path;

    free(path);

#else
    #error No Implementation
#endif
    return NULL;
}

/**
 * Returns a heap allocated path to the sourcemod dirctory
 * If a problem occurs returns NULL
 */
char* getSourcemodDir(void)
{
    char* steam = getSteamDir();
    if (!steam)
        return NULL;

    steam = realloc(steam, strlen(steam) + strlen(OS_PATH_SEP) + strlen(SOURCEMOD_DIR) + 1);
    strcat(steam, OS_PATH_SEP);
    strcat(steam, SOURCEMOD_DIR);

    return steam;
}

char* getOpenFortressDir(void)
{
    char* sm_dir = getSourcemodDir();
    if (!sm_dir)
        return NULL;

    sm_dir = realloc(sm_dir, strlen(sm_dir) + strlen(OS_PATH_SEP) + strlen(OPEN_FORTRESS_DIR) + 1);
    strcat(sm_dir, OS_PATH_SEP);
    strcat(sm_dir, OPEN_FORTRESS_DIR);

    return sm_dir;
}

char* getAppInstallDir(const char* appid)
{
    char* librayfolders = getSteamDir();
    if (!librayfolders)
        return NULL;

    librayfolders = realloc(librayfolders, strlen(librayfolders) + strlen(OS_PATH_SEP) + strlen(STEAMAPPS) + strlen(OS_PATH_SEP) + strlen(LIBARYFOLDERS_VDF) + 1);

    strcat(librayfolders, OS_PATH_SEP);
    strcat(librayfolders, STEAMAPPS);
    strcat(librayfolders, OS_PATH_SEP);
    strcat(librayfolders, LIBARYFOLDERS_VDF);

    struct vdf_object* o = vdf_parse_file(librayfolders);
    free(librayfolders);

    char* sdkdir = NULL;

    if (o)
    {
        for (size_t i = 0; i < vdf_object_get_array_length(o); ++i)
        {
            struct vdf_object* library = vdf_object_index_array(o, i);
            struct vdf_object* apps = vdf_object_index_array_str(library, "apps");

            if (vdf_object_index_array_str(apps, appid))
            {
                char* manifest = malloc(strlen(appid) + strlen(STEAM_MANIFEST) + 1);
                sprintf(manifest, STEAM_MANIFEST, appid);

                struct vdf_object* path = vdf_object_index_array_str(library, "path");

                size_t path_len = path->data.data_string.len + strlen(OS_PATH_SEP) + strlen(STEAMAPPS) + strlen(OS_PATH_SEP) + strlen(manifest) + 1;
                char* path_str = malloc(path_len);

                snprintf(path_str, path_len, "%s%s%s%s%s", vdf_object_get_string(path), OS_PATH_SEP, STEAMAPPS, OS_PATH_SEP, manifest);

                struct vdf_object* k = vdf_parse_file(path_str);
                free(path_str);

                if (k)
                {
                    struct vdf_object* installdir = vdf_object_index_array_str(k, "installdir");

                    path_len = path->data.data_string.len + strlen(OS_PATH_SEP) + strlen(STEAMAPPS_COMMON) + strlen(OS_PATH_SEP) + installdir->data.data_string.len + 1;
                    path_str = malloc(path_len);
                    snprintf(path_str, path_len, "%s%s%s%s%s", vdf_object_get_string(path), OS_PATH_SEP, STEAMAPPS_COMMON, OS_PATH_SEP, vdf_object_get_string(installdir));

                    sdkdir = path_str;

                    vdf_free_object(k);
                }

                free(manifest);
            }
        }

        vdf_free_object(o);
    }

    return sdkdir;
}

char* getSourceSDK2013MpDir(void)
{
    return getAppInstallDir(SOURCESDK_APPID);
}

/**
 * function to fetch the PID of a running Steam process.
 * If none were found returns -1
 */
pid_t getSteamPID(void)
{
    return getPid(STEAM_PROC);
}

int runOpenFortressDirect(char** args, size_t arg_count)
{
    pid_t pid;

    if ((pid = fork()))
    {
        // parent
#ifdef HAS_HOOKS
        //fix_SysLoadLibary(pid);
#endif
        return 0;
    }
    else
    {
        // child
        char* game = getSourceSDK2013MpDir();
        if (!game)
        {
            exit(1);
        }

        char* of_dir = getOpenFortressDir();
        if (!of_dir)
        {
            free(game);
            exit(1);
        }

        game = realloc(game, strlen(game) + strlen(OS_PATH_SEP) + strlen(HL2_EXE) + 1);
        strcat(game, OS_PATH_SEP);
        strcat(game, HL2_EXE);

#if defined(__linux__) || defined(__FreeBSD__)
        // we need to be in the steam environment to get the right locales 
        setenv("SteamEnv", "1", 1);
#endif

        char** argv = malloc(sizeof(char*) * (arg_count + 6));

#ifdef _WIN32
        size_t of_dir_len = strlen(of_dir); 
        of_dir = realloc(of_dir, of_dir_len + 3);
        memmove(of_dir+1, of_dir, of_dir_len);
        of_dir[0] = '"';
        of_dir[of_dir_len+1] = '"';
        of_dir[of_dir_len+2] = '\0';
#endif

        argv[0] = game;
        argv[1] = "-game";
        argv[2] = of_dir;
        argv[3] = "-secure";
        argv[4] = "-steam";
        for (size_t i = 0; i < arg_count; ++i)
            argv[5+i] = args[i];
        argv[5+arg_count] = NULL;

        execv(game, argv);

        free(game);
        free(of_dir);

        exit(0);
    }
}

int runOpenFortressNaive(char** args, size_t arg_count)
{
    #ifdef _WIN32
        return system("start steam://rungameid/11677091221058336806");
    #else
        return system("xdg-open steam://rungameid/11677091221058336806");
    #endif
}

int runOpenFortressSteam(char** args, size_t arg_count)
{
    char* of_dir = getOpenFortressDir();
    char* steam = getSteamDir();
    steam = realloc(steam, strlen(steam) + strlen(OS_PATH_SEP) + strlen(STEAM_BIN) + 1);
    strcat(steam, OS_PATH_SEP);
    strcat(steam, STEAM_BIN);

    char** argv = malloc(sizeof(char*) * (arg_count + 8));

#ifdef _WIN32
    size_t of_dir_len = strlen(of_dir); 
    of_dir = realloc(of_dir, of_dir_len + 3);
    memmove(of_dir+1, of_dir, of_dir_len);
    of_dir[0] = '"';
    of_dir[of_dir_len+1] = '"';
    of_dir[of_dir_len+2] = '\0';
#endif

    argv[0] = steam;
    argv[1] = "-applaunch";
    argv[2] = "243750";
    argv[3] = "-game";
    argv[4] = of_dir;
    argv[5] = "-secure";
    argv[6] = "-steam";
    for (size_t i = 0; i < arg_count; ++i)
        argv[7+i] = args[i];
    argv[7+arg_count] = NULL;

    return execv(steam, argv);
}

int runOpenFortress(char** args, size_t arg_count)
{
#ifdef STEAM_DIRECT_LAUNCH
    return runOpenFortressDirect(args, arg_count);
#else
    #ifdef STEAM_NAIVE_LAUNCH
        return runOpenFortressNaive(args, arg_count);
    #else
        return runOpenFortressSteam(args, arg_count);
    #endif
#endif
}