~melchizedek6809/wolkenwelten-devel

WolkenWelten: Fixed server loading issues and added modifiable game data dir path v1 APPLIED

~matthilde: 1
 Fixed server loading issues and added modifiable game data dir path

 2 files changed, 91 insertions(+), 17 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~melchizedek6809/wolkenwelten-devel/patches/20143/mbox | git am -3
Learn more about email & git

[PATCH WolkenWelten] Fixed server loading issues and added modifiable game data dir path Export this patch

From: Matthilde <matthilde@cutebunni.es>

---
 client/src/main.c               | 57 +++++++++++++++++++++++++++++++++
 client/src/network/client_bsd.h | 51 +++++++++++++++++++----------
 2 files changed, 91 insertions(+), 17 deletions(-)

diff --git a/client/src/main.c b/client/src/main.c
index f0b8b3a2..53cc5d85 100644
--- a/client/src/main.c
+++ b/client/src/main.c
@@ -64,10 +64,15 @@

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#ifdef __EMSCRIPTEN__
	#include <emscripten.h>
#endif
@@ -191,7 +196,59 @@ void checkAutostart(){
	}
}

// Sets current working directory for game files.
// Create directories if needed.
// If the dir argument points to NULL,
// it will set the CWD as ~/.wolkenwelten/
static char* getDefaultPath(){
	// [!!] Uses malloc, must be freed.
	const char* dir  = "/.wolkenwelten";
	const char* home = getenv("HOME");
	if (!home) return NULL;
	
	char* result = (char*)malloc(sizeof(char) * 
			(strlen(home) + strlen(dir) + 1));
	strcpy(result, home);
	strcat(result, dir);

	return result;
}

static void setGameDir( const char* dir ){
	const char* dirname;
	char* alloc = NULL;
	if (!dir){
		dirname = alloc = getDefaultPath();
		if (!dirname){
			printf("[CLI] HOME environment variable not found.\n");
			return;
		}
	} else
		dirname = dir;

	DIR* gamedir = opendir(dirname);

	if (!gamedir && errno == ENOENT && mkdir(dirname, 0700) == -1){
		perror("mkdir");
		return;
	} else if (!gamedir && errno != ENOENT) {
		perror("opendir");
		return;
	}
	closedir(gamedir);

	if (chdir(dirname) == -1) {
		perror("chdir");
		return;
	}
	if (alloc)
		free(alloc);
}

int main( int argc, char* argv[] ){
	// Setting the dir
	setGameDir(getenv("WOLKENWELTEN_DIR"));

	clientGetName();
	lispInit();
	initOptions(argc,argv);
diff --git a/client/src/network/client_bsd.h b/client/src/network/client_bsd.h
index 21e01e13..5f1cb4eb 100644
--- a/client/src/network/client_bsd.h
+++ b/client/src/network/client_bsd.h
@@ -16,7 +16,6 @@
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/wait.h>
@@ -28,6 +27,7 @@
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>

bool signalHandlerBound = false;
int serverSocket        = 0;
@@ -58,22 +58,32 @@ bool fileExists(const char *fn){
		return NULL;
	}
#else
	const char *getServerExecutablePath(){
		static char *serverPath = NULL;
		char *serverPaths[4] = {
			"wolkenwelten-server",
			"/bin/wolkenwelten-server",
			"/usr/bin/wolkenwelten-server",
			"/usr/local/bin/wolkenwelten-server",
		};
		if(serverPath == NULL){
			for(int i=0;i<4;++i){
				if(fileExists(serverPaths[i])){
					serverPath = serverPaths[i];
					break;
				}
			}
	char* serverExecName = "wolkenwelten-server";
	char *getServerExecutablePath(){
		char* serverPath = NULL;
		if (access(serverExecName, F_OK) != -1)
			return serverExecName;
		
		char* path = getenv("PATH");
		if (!path)	// How is this even possible
			return NULL;
		
		char* token = strtok(path, ":");
		char* tmp = malloc(0);
		while (token)
		{
			tmp = (char*)realloc(tmp, sizeof(char) + 
								(strlen(token) + strlen(serverExecName) + 2));
			strcpy(tmp, token);
			strcat(tmp, "/");
			strcat(tmp, serverExecName);
			
			if (access(tmp, R_OK|X_OK) != -1)
				break;
			token = strtok(NULL, ":");
		}
		
		serverPath = tmp;
		return serverPath;
	}
#endif
@@ -81,7 +91,12 @@ bool fileExists(const char *fn){
void startSingleplayerServer(){
	char seed[64];
	char save[64];
	const char *wolkenweltenServer = getServerExecutablePath();
	char *wolkenweltenServer = getServerExecutablePath();
	if (!wolkenweltenServer)
	{
		printf("[CLI] Server exectuable not found\n");
		return;
	}
	if(optionWorldSeed == 0){
		optionWorldSeed = (int)(time(NULL)&0xFFFF);
	}
@@ -95,6 +110,8 @@ void startSingleplayerServer(){
	strncpy(serverName,"localhost",sizeof(serverName)-1);
	serverName[sizeof(serverName)-1]=0;
	usleep(1000);

	free(wolkenweltenServer);
}

void closeSingleplayerServer(){
-- 
2.26.2