aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2020-10-20 23:37:13 +0200
committerJan200101 <sentrycraft123@gmail.com>2020-10-20 23:37:13 +0200
commit7b3ee49b6aa5bcff2b46987ebc8d9cd46b9e5ac1 (patch)
tree7ce2fc46c98cd052893f01feda8b8f212f4c8f70 /src
parentdb8ae62fb64fa33657f598a62045f17fc779ef8b (diff)
downloadpolecat-7b3ee49b6aa5bcff2b46987ebc8d9cd46b9e5ac1.tar.gz
polecat-7b3ee49b6aa5bcff2b46987ebc8d9cd46b9e5ac1.zip
improve codebase quality
- improve unreachable macro to print a message with a minor traceback then exit(0) instead of assert hackery - add specific wine and dxvk directory methods - rename dxvk install to download - remove sanity asserts from lutris, caught by unreachable - improve comments - rename info to env - only use a single buffer for information fetching - fix formatting on code I had help on - purge unused code - if downloadToRam returns null pointer report that the archive went missing not that its invalid - rename undescriptive "datadir" to be more specific - remove unused code from when we stored the archive on disk
Diffstat (limited to 'src')
-rw-r--r--src/common.h9
-rw-r--r--src/config.c11
-rw-r--r--src/config.h3
-rw-r--r--src/dxvk.c39
-rw-r--r--src/dxvk.h2
-rw-r--r--src/lutris.c2
-rw-r--r--src/lutris.h6
-rw-r--r--src/main.c24
-rw-r--r--src/main.h2
-rw-r--r--src/net.c54
-rw-r--r--src/net.h1
-rw-r--r--src/tar.c36
-rw-r--r--src/wine.c36
13 files changed, 112 insertions, 113 deletions
diff --git a/src/common.h b/src/common.h
index 03b103f..1d4c70b 100644
--- a/src/common.h
+++ b/src/common.h
@@ -1,6 +1,7 @@
#ifndef COMMON_H
#define COMMON_H
+#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
@@ -26,9 +27,11 @@
#define USAGE_STR "Usage: " NAME
-// inspired by Zigs unreachable
-#include <assert.h>
-#define unreachable assert(0 && "unreachable code reached")
+#ifdef DEBUG
+#define unreachable printf("unreachable code reached\n" __FILE__ ":L%i\n", __LINE__); exit(0);
+#else
+#define unreachable
+#endif
struct MemoryStruct {
uint8_t* memory;
diff --git a/src/config.c b/src/config.c
index 0126646..318b196 100644
--- a/src/config.c
+++ b/src/config.c
@@ -39,3 +39,14 @@ void getCacheDir(char* config, const size_t size)
getXDGDir("XDG_CACHE_HOME", "/.cache/" NAME, config, size);
}
+void getWineDir(char* config, const size_t size)
+{
+ getDataDir(config, size);
+ strncat(config, "/wine", size - strlen(config) - 1);
+}
+
+void getDXVKDir(char* config, const size_t size)
+{
+ getDataDir(config, size);
+ strncat(config, "/dxvk", size - strlen(config) - 1);
+} \ No newline at end of file
diff --git a/src/config.h b/src/config.h
index 84c3fd0..39fb4d8 100644
--- a/src/config.h
+++ b/src/config.h
@@ -7,4 +7,7 @@ void getConfigDir(char*, const size_t);
void getDataDir(char*, const size_t);
void getCacheDir(char*, const size_t);
+void getWineDir(char*, const size_t);
+void getDXVKDir(char*, const size_t);
+
#endif
diff --git a/src/dxvk.c b/src/dxvk.c
index f219455..f8d4cc3 100644
--- a/src/dxvk.c
+++ b/src/dxvk.c
@@ -12,7 +12,7 @@
#include "config.h"
const static struct Command dxvk_commands[] = {
- { .name = "install", .func = dxvk_install, .description = "download and install a dxvk version" },
+ { .name = "download", .func = dxvk_download, .description = "download and install a dxvk version" },
{ .name = "list", .func = dxvk_list, .description = "list available dxvk versions" },
};
@@ -30,7 +30,7 @@ int dxvk(int argc, char** argv)
}
-int dxvk_install(int argc, char** argv)
+int dxvk_download(int argc, char** argv)
{
if (argc == 2)
{
@@ -56,24 +56,27 @@ int dxvk_install(int argc, char** argv)
json_object_object_get_ex(version, "browser_download_url", &assets);
char* name = basename((char*)json_object_get_string(assets));
- struct MemoryStruct* tar;
+ struct MemoryStruct* archive;
- char datadir[PATH_MAX];
- getDataDir(datadir, sizeof(datadir));
- makeDir(datadir);
+ char winedir[PATH_MAX];
+ getDXVKDir(winedir, sizeof(winedir));
+ makeDir(winedir);
- printf("Downloading %s", json_object_get_string(assets));
- //downloadFile(json_object_get_string(assets), name);
- tar = downloadToRam(json_object_get_string(assets));
-
- printf("Extracting %s\n", name);
-
- extract(tar, datadir);
-
- printf("Done\n");
-
- free(tar->memory);
- free(tar);
+ printf("Downloading %s\n", name);
+
+ archive = downloadToRam(json_object_get_string(assets));
+ if (archive)
+ {
+ printf("Extracting %s\n", name);
+ extract(archive, winedir);
+ }
+ else
+ {
+ puts("Something went wrong. The archive went missing");
+ }
+
+ free(archive->memory);
+ free(archive);
}
json_object_put(runner);
diff --git a/src/dxvk.h b/src/dxvk.h
index ef423e2..3829abf 100644
--- a/src/dxvk.h
+++ b/src/dxvk.h
@@ -2,7 +2,7 @@
#define DXVK_H
int dxvk(int, char**);
-int dxvk_install(int, char**);
+int dxvk_download(int, char**);
int dxvk_list(int, char**);
int dxvk_help(int, char**);
diff --git a/src/lutris.c b/src/lutris.c
index db4da4c..a3caa3d 100644
--- a/src/lutris.c
+++ b/src/lutris.c
@@ -57,7 +57,6 @@ int lutris_install(int argc, char** argv)
for (size_t i = 0; i < installer.directivecount; ++i)
{
- assert(installer.directives[i]->command < UNKNOWN_DIRECTIVE);
switch(installer.directives[i]->command)
{
@@ -123,7 +122,6 @@ int lutris_install(int argc, char** argv)
}
else
{
- assert(installer.error < NO_INSTALLER);
switch(installer.error)
{
case NO_JSON:
diff --git a/src/lutris.h b/src/lutris.h
index f3f648d..2142303 100644
--- a/src/lutris.h
+++ b/src/lutris.h
@@ -65,6 +65,11 @@ enum runner_t {
RUNNERMAX
};
+/*
+ * a list of all available runners could be fetched from lutris
+ * but we keep a local copy of all supported runners
+ * to reduce the ammount of API calls needed
+ */
static const char runnerStr[RUNNERMAX][0xF] =
{
"unknown",
@@ -93,6 +98,7 @@ struct file_t {
char* url;
};
+// my best attempt at representing a Lutris installer as a struct
struct script_t {
char* name;
char* version;
diff --git a/src/main.c b/src/main.c
index 6268543..330267c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -16,7 +16,7 @@ const static struct Command main_commands[] = {
{ .name = "dxvk", .func = dxvk, .description = "manage dxvk versions" },
#endif
{ .name = "lutris", .func = lutris, .description = "run lutris instraller"},
- { .name = "info", .func = main_info, .description = "show some information about polecat" },
+ { .name = "env", .func = main_env, .description = "show some information about polecat" },
};
@@ -33,22 +33,22 @@ int main(int argc, char** argv)
return main_help(argc-1, argv+1);
}
-int main_info(int argc, char** argv)
+int main_env(int argc, char** argv)
{
- char cfgdir[PATH_MAX], datadir[PATH_MAX], cachedir[PATH_MAX];
+ char buffer[PATH_MAX];
- getConfigDir(cfgdir, sizeof(cfgdir));
- getDataDir(datadir, sizeof(datadir));
- getCacheDir(cachedir, sizeof(cachedir));
printf("version:\t\t%s\n"
- "user-Agent:\t\t%s/%s\n"
- "config dir\t\t%s\n"
- "data dir\t\t%s\n"
- "cache dir\t\t%s\n",
+ "user-Agent:\t\t%s\n",
VERSION,
- NAME, VERSION,
- cfgdir, datadir, cachedir);
+ USER_AGENT);
+
+ getConfigDir(buffer, sizeof(buffer));
+ printf("config dir\t\t%s\n", buffer);
+
+ getDataDir(buffer, sizeof(buffer));
+ printf("data dir\t\t%s\n", buffer);
+
return 0;
}
diff --git a/src/main.h b/src/main.h
index e64eb9a..ef535fd 100644
--- a/src/main.h
+++ b/src/main.h
@@ -2,6 +2,6 @@
#define MAIN_H
int main_help(int, char**);
-int main_info(int, char**);
+int main_env(int, char**);
#endif \ No newline at end of file
diff --git a/src/net.c b/src/net.c
index bacfc8b..b435f4d 100644
--- a/src/net.c
+++ b/src/net.c
@@ -8,25 +8,25 @@
#include "net.h"
#include "common.h"
-
+
size_t memoryCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
- size_t realsize = size * nmemb;
- struct MemoryStruct* mem = (struct MemoryStruct*)userp;
-
- uint8_t* ptr = realloc(mem->memory, mem->size + realsize + 1);
- if(ptr == NULL) {
- /* out of memory! */
- puts("out of memory");
- return 0;
- }
-
- mem->memory = ptr;
- memcpy(&(mem->memory[mem->size]), contents, realsize);
- mem->size += realsize;
- mem->memory[mem->size] = 0;
-
- return realsize;
+ size_t realsize = size * nmemb;
+ struct MemoryStruct* mem = (struct MemoryStruct*)userp;
+
+ uint8_t* ptr = realloc(mem->memory, mem->size + realsize + 1);
+ if(ptr == NULL)
+ {
+ puts("out of memory");
+ return 0;
+ }
+
+ mem->memory = ptr;
+ memcpy(&(mem->memory[mem->size]), contents, realsize);
+ mem->size += realsize;
+ mem->memory[mem->size] = 0;
+
+ return realsize;
}
struct MemoryStruct* downloadToRam(const char* URL)
@@ -75,24 +75,6 @@ struct MemoryStruct* downloadToRam(const char* URL)
return chunk;
}
-void downloadFile(const char* URL, const char* path)
-{
- struct MemoryStruct* chunk = downloadToRam(URL);
-
- if (chunk)
- {
- FILE* file = fopen(path, "wb");
- if (file)
- {
- fwrite(chunk->memory, chunk->size, 1, file);
- fclose(file);
- }
-
- free(chunk->memory);
- free(chunk);
- }
-}
-
struct json_object* fetchJSON(const char* URL)
{
struct MemoryStruct* chunk = downloadToRam(URL);
@@ -109,4 +91,4 @@ struct json_object* fetchJSON(const char* URL)
}
return json;
-} \ No newline at end of file
+}
diff --git a/src/net.h b/src/net.h
index 91f6828..f3c92c3 100644
--- a/src/net.h
+++ b/src/net.h
@@ -5,7 +5,6 @@
size_t WriteMemoryCallback(void*, size_t, size_t, void*);
struct MemoryStruct* downloadToRam(const char* URL);
-void downloadFile(const char*, const char*);
struct json_object* fetchJSON(const char*);
#endif \ No newline at end of file
diff --git a/src/tar.c b/src/tar.c
index 1ffdbe8..6e90aa5 100644
--- a/src/tar.c
+++ b/src/tar.c
@@ -3,7 +3,7 @@
#include <stdlib.h>
#include <archive.h>
#include <archive_entry.h>
-#include <fcntl.h>
+#include <fcntl.h>
#include <linux/limits.h>
#include "common.h"
@@ -11,23 +11,25 @@
static int copy_data(struct archive* ar, struct archive* aw)
{
- int r;
- const void *buff;
- size_t size;
- la_int64_t offset;
-
- for (;;) {
- r = archive_read_data_block(ar, &buff, &size, &offset);
- if (r == ARCHIVE_EOF)
- return (ARCHIVE_OK);
- if (r < ARCHIVE_OK)
- return (r);
- r = archive_write_data_block(aw, buff, size, offset);
- if (r < ARCHIVE_OK) {
- printf("%s\n", archive_error_string(aw));
- return (r);
+ int r;
+ const void *buff;
+ size_t size;
+ la_int64_t offset;
+
+ for (;;)
+ {
+ r = archive_read_data_block(ar, &buff, &size, &offset);
+ if (r == ARCHIVE_EOF) return (ARCHIVE_OK);
+ if (r < ARCHIVE_OK) return (r);
+
+ r = archive_write_data_block(aw, buff, size, offset);
+
+ if (r < ARCHIVE_OK)
+ {
+ printf("%s\n", archive_error_string(aw));
+ return (r);
+ }
}
- }
}
void extract(const struct MemoryStruct* tar, const char* outputdir)
diff --git a/src/wine.c b/src/wine.c
index aaaeef9..089a0e9 100644
--- a/src/wine.c
+++ b/src/wine.c
@@ -64,34 +64,27 @@ int wine_download(int argc, char** argv)
json_object_object_get_ex(value, "url", &url);
char* name = basename((char*)json_object_get_string(url));
- char datadir[PATH_MAX];
- char downloadpath[PATH_MAX];
- struct MemoryStruct* tar;
+ char winedir[PATH_MAX];
+ struct MemoryStruct* archive;
- getDataDir(datadir, sizeof(datadir));
- makeDir(datadir);
-
- strncpy(downloadpath, datadir, sizeof(downloadpath));
- strncat(downloadpath, "/", sizeof(downloadpath) - strlen(downloadpath) - 1);
- strncat(downloadpath, name, sizeof(downloadpath) - strlen(downloadpath) - 1);
+ getWineDir(winedir, sizeof(winedir));
+ makeDir(winedir);
printf("Downloading %s\n", name);
- tar = downloadToRam(json_object_get_string(url));
- if (tar)
+ archive = downloadToRam(json_object_get_string(url));
+ if (archive)
{
printf("Extracting %s\n", name);
- extract(tar, datadir);
- puts("Done");
+ extract(archive, winedir);
}
else
{
- puts("Something went wrong. The tar is not valid");
+ puts("Something went wrong. The archive went missing");
}
-
- free(tar->memory);
- free(tar);
+ free(archive->memory);
+ free(archive);
}
json_object_put(runner);
@@ -133,7 +126,7 @@ int wine_run(int argc, char** argv)
if (argc > 1)
{
char winepath[PATH_MAX];
- getDataDir(winepath, sizeof(winepath));
+ getWineDir(winepath, sizeof(winepath));
char* winever = argv[1];
strncat(winepath, "/", sizeof(winepath) - strlen(winepath) - 1);
@@ -162,7 +155,6 @@ int wine_run(int argc, char** argv)
strncat(winepath, "\"", sizeof(winepath) - strlen(winepath) - 1);
}
- puts(winepath);
return system(winepath);
}
else
@@ -182,14 +174,14 @@ int wine_run(int argc, char** argv)
int wine_installed(int argc, char** argv)
{
- char datadir[PATH_MAX];
- getDataDir(datadir, sizeof(datadir));
+ char winedir[PATH_MAX];
+ getWineDir(winedir, sizeof(winedir));
DIR *dir;
struct dirent *ent;
printf("Installed wine versions:\n");
- if ((dir = opendir(datadir)) != NULL)
+ if ((dir = opendir(winedir)) != NULL)
{
while ((ent = readdir (dir)) != NULL)
{