Skip to content

Commit

Permalink
Merge branch 'master' into feature/sdl3
Browse files Browse the repository at this point in the history
  • Loading branch information
asiekierka committed Oct 15, 2024
2 parents 1a65a8a + ecca7b1 commit d9a79bc
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ CC = gcc
endif
endif

VERSION ?= 1.0.6
VERSION ?= 1.0.8

RESDIR = res
SRCDIR = src
Expand Down
116 changes: 77 additions & 39 deletions src/frontend_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,26 @@
#include <unistd.h>
#include <pwd.h>
#endif
#include <limits.h>

double posix_zzt_arg_note_delay = -1.0;

static void posix_vfs_chdir_arg0(char *argv0) {
if (argv0 == NULL || argv0[0] == 0) return;

char *path = strdup(argv0);
if (path == NULL) return;
char *final_path = strrchr(path, PATH_SEP);
if (final_path == NULL) final_path = strrchr(path, '/');
if (final_path == NULL) {
free(path);
return;
}
*final_path = 0;
chdir(path);
free(path);
}

static int posix_vfs_exists(const char *filename) {
int h = vfs_open(filename, 0);
if (h >= 0) { vfs_close(h); return 1; }
Expand Down Expand Up @@ -72,7 +89,56 @@ static void posix_zzt_help(int argc, char **argv) {
#define INIT_ERR_NO_EXECUTABLE -2
#define INIT_ERR_NO_EXECUTABLE_ZZT -3

static int posix_try_run_zzt(int exec_count, char **execs, char *arg_name, bool is_final_attempt) {
if (exec_count > 0) {
int exeh = 0;
for (int i = 0; i < exec_count; i++) {
char *space_ptr = strchr(execs[i], ' ');
if (space_ptr != NULL) {
space_ptr[0] = '\0';
exeh = vfs_open(execs[i], 0);
if (exeh < 0) {
if (is_final_attempt) {
fprintf(stderr, "Could not load %s!\n", execs[i]);
}
space_ptr[0] = ' ';
return INIT_ERR_NO_EXECUTABLE;
}
space_ptr[0] = ' ';
fprintf(stderr, "'%s'\n", space_ptr + 1);
zzt_load_binary(exeh, space_ptr + 1);
vfs_close(exeh);
} else {
exeh = vfs_open(execs[i], 0);
if (exeh < 0) {
if (is_final_attempt) {
fprintf(stderr, "Could not load %s!\n", execs[i]);
}
return INIT_ERR_NO_EXECUTABLE;
}
zzt_load_binary(exeh, (i == exec_count - 1) ? arg_name : NULL);
vfs_close(exeh);
}

// last binary is engine
if (i == exec_count - 1) break;
while (zzt_execute(10000) != STATE_END) { }
}
} else {
int exeh = vfs_open("zzt.exe", 0);
if (exeh < 0)
exeh = vfs_open("superz.exe", 0);
if (exeh < 0)
return INIT_ERR_NO_EXECUTABLE_ZZT;
zzt_load_binary(exeh, arg_name);
vfs_close(exeh);
}

return 0;
}

static int posix_zzt_init(int argc, char **argv) {
char cwd[PATH_MAX+1];
char arg_name[257];
char *execs[16];
char *loads[16];
Expand All @@ -85,13 +151,13 @@ static int posix_zzt_init(int argc, char **argv) {
int video_blink = 1;
int starting_volume = 20;

getcwd(cwd, PATH_MAX);

#ifdef __APPLE__
if (access("../Info.plist", F_OK) == 0) {
// We're in a relative path inside the .app.
chdir("../../..");
} else {
char cwd[1025];
getcwd(cwd, 1024);
if (!strcmp(cwd, "/") || !strncmp(cwd, "/Applications", 13)) {
// We're in no reasonable path. Assume the user's root directory.
struct passwd *pw = getpwuid(geteuid());
Expand Down Expand Up @@ -271,44 +337,16 @@ static int posix_zzt_init(int argc, char **argv) {
free(buffer);
}

if (exec_count > 0) {
int exeh = 0;
for (int i = 0; i < exec_count; i++) {
char *space_ptr = strchr(execs[i], ' ');
if (space_ptr != NULL) {
space_ptr[0] = '\0';
exeh = vfs_open(execs[i], 0);
if (exeh < 0) {
fprintf(stderr, "Could not load %s!\n", execs[i]);
space_ptr[0] = ' ';
return INIT_ERR_NO_EXECUTABLE;
}
space_ptr[0] = ' ';
fprintf(stderr, "'%s'\n", space_ptr + 1);
zzt_load_binary(exeh, space_ptr + 1);
vfs_close(exeh);
} else {
exeh = vfs_open(execs[i], 0);
if (exeh < 0) {
fprintf(stderr, "Could not load %s!\n", execs[i]);
return INIT_ERR_NO_EXECUTABLE;
}
zzt_load_binary(exeh, (i == exec_count - 1) ? arg_name : NULL);
vfs_close(exeh);
}

// last binary is engine
if (i == exec_count - 1) break;
while (zzt_execute(10000) != STATE_END) { }
int result = posix_try_run_zzt(exec_count, execs, arg_name, false);
if (result != 0) {
if (argv != NULL) {
posix_vfs_chdir_arg0(argv[0]);
}
result = posix_try_run_zzt(exec_count, execs, arg_name, true);
if (result != 0) {
chdir(cwd);
return result;
}
} else {
int exeh = vfs_open("zzt.exe", 0);
if (exeh < 0)
exeh = vfs_open("superz.exe", 0);
if (exeh < 0)
return INIT_ERR_NO_EXECUTABLE_ZZT;
zzt_load_binary(exeh, arg_name);
vfs_close(exeh);
}

zzt_set_timer_offset((time(NULL) % 86400) * 1000L);
Expand Down
6 changes: 0 additions & 6 deletions src/posix_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@
#include <sys/stat.h>
#endif

#ifdef _WIN32
#define PATH_SEP '\\'
#else
#define PATH_SEP '/'
#endif

#define MAX_FNLEN 259
#define MAX_SPECLEN 16
#define MAX_VFS_FNLEN 12
Expand Down
6 changes: 6 additions & 0 deletions src/posix_vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@

#include "types.h"

#ifdef _WIN32
#define PATH_SEP '\\'
#else
#define PATH_SEP '/'
#endif

USER_FUNCTION
void init_posix_vfs(const char* path, bool debug_enabled);
USER_FUNCTION
Expand Down
6 changes: 3 additions & 3 deletions src/zzt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1386,9 +1386,9 @@ int zzt_load_palette(u32 *colors) {

for (int c = 0; c < PALETTE_COLOR_COUNT; c++) {
int i = zzt.palette_lut[c];
zzt.palette_dac[i * 3 + 0] = ((colors[i] >> 16) & 0xFF);
zzt.palette_dac[i * 3 + 1] = ((colors[i] >> 8) & 0xFF);
zzt.palette_dac[i * 3 + 2] = (colors[i] & 0xFF);
zzt.palette_dac[i * 3 + 0] = ((colors[c] >> 16) & 0xFF);
zzt.palette_dac[i * 3 + 1] = ((colors[c] >> 8) & 0xFF);
zzt.palette_dac[i * 3 + 2] = (colors[c] & 0xFF);
}

zzt_refresh_palette();
Expand Down

0 comments on commit d9a79bc

Please sign in to comment.