From 19004e830980892c3abc3021bd1b868861836bb8 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Mon, 23 Nov 1998 21:05:35 +0100 Subject: [PATCH] rnd-19981123-4 --- src/files.c | 94 +++++++++++++++++++++++++++++++-------------------- src/misc.c | 13 ++++++- src/misc.h | 1 + src/netserv.c | 34 +++++++++---------- src/netserv.h | 6 ++-- src/network.c | 9 ++--- 6 files changed, 95 insertions(+), 62 deletions(-) diff --git a/src/files.c b/src/files.c index 81838cf4..b3a37e61 100644 --- a/src/files.c +++ b/src/files.c @@ -49,6 +49,7 @@ #define SETUP_FILENAME "setup.conf" #define LEVELSETUP_FILENAME "levelsetup.conf" #define LEVELINFO_FILENAME "levelinfo.conf" +#define LEVELFILE_EXTENSION "level" #define TAPEFILE_EXTENSION "tape" #define SCOREFILE_EXTENSION "score" #else @@ -56,6 +57,7 @@ #define SETUP_FILENAME "setup.cnf" #define LEVELSETUP_FILENAME "lvlsetup.cnf" #define LEVELINFO_FILENAME "lvlinfo.cnf" +#define LEVELFILE_EXTENSION "lvl" #define TAPEFILE_EXTENSION "rec" #define SCOREFILE_EXTENSION "sco" #endif @@ -150,6 +152,52 @@ static char *getScoreDir(char *level_subdir) return score_dir; } +static char *getLevelFilename(int nr) +{ + static char *filename = NULL; + char basename[20 + strlen(LEVELFILE_EXTENSION)]; + + if (filename != NULL) + free(filename); + + sprintf(basename, "%03d.%s", nr, LEVELFILE_EXTENSION); + filename = getPath3((leveldir[leveldir_nr].user_defined ? + getUserLevelDir("") : + options.level_directory), + leveldir[leveldir_nr].filename, + basename); + + return filename; +} + +static char *getTapeFilename(int nr) +{ + static char *filename = NULL; + char basename[20 + strlen(LEVELFILE_EXTENSION)]; + + if (filename != NULL) + free(filename); + + sprintf(basename, "%03d.%s", nr, TAPEFILE_EXTENSION); + filename = getPath2(getTapeDir(leveldir[leveldir_nr].filename), basename); + + return filename; +} + +static char *getScoreFilename(int nr) +{ + static char *filename = NULL; + char basename[20 + strlen(LEVELFILE_EXTENSION)]; + + if (filename != NULL) + free(filename); + + sprintf(basename, "%03d.%s", nr, SCOREFILE_EXTENSION); + filename = getPath2(getScoreDir(leveldir[leveldir_nr].filename), basename); + + return filename; +} + static void createDirectory(char *dir, char *text) { if (access(dir, F_OK) != 0) @@ -221,7 +269,7 @@ static void setLevelInfoToDefaults() void LoadLevel(int level_nr) { int i, x, y; - char filename[MAX_FILENAME_LEN]; + char *filename = getLevelFilename(level_nr); char cookie[MAX_LINE_LEN]; char chunk[CHUNK_ID_LEN + 1]; int file_version = FILE_VERSION_1_2; /* last version of level files */ @@ -231,13 +279,6 @@ void LoadLevel(int level_nr) /* always start with reliable default values */ setLevelInfoToDefaults(); - if (leveldir[leveldir_nr].user_defined) - sprintf(filename, "%s/%s/%d", - getUserLevelDir(""), leveldir[leveldir_nr].filename, level_nr); - else - sprintf(filename, "%s/%s/%d", - options.level_directory, leveldir[leveldir_nr].filename, level_nr); - if (!(file = fopen(filename, "r"))) { Error(ERR_WARN, "cannot read level '%s' - creating new level", filename); @@ -327,17 +368,10 @@ void LoadLevel(int level_nr) void SaveLevel(int level_nr) { int i, x, y; - char filename[MAX_FILENAME_LEN]; + char *filename = getLevelFilename(level_nr); FILE *file; int chunk_length; - if (leveldir[leveldir_nr].user_defined) - sprintf(filename, "%s/%s/%d", - getUserLevelDir(""), leveldir[leveldir_nr].filename, level_nr); - else - sprintf(filename, "%s/%s/%d", - options.level_directory, leveldir[leveldir_nr].filename, level_nr); - if (!(file = fopen(filename, "w"))) { Error(ERR_WARN, "cannot save level file '%s'", filename); @@ -399,7 +433,7 @@ void SaveLevel(int level_nr) void LoadTape(int level_nr) { int i, j; - char filename[MAX_FILENAME_LEN]; + char *filename = getTapeFilename(level_nr); char cookie[MAX_LINE_LEN]; char chunk[CHUNK_ID_LEN + 1]; FILE *file; @@ -407,10 +441,6 @@ void LoadTape(int level_nr) int file_version = FILE_VERSION_1_2; /* last version of tape files */ int chunk_length; - sprintf(filename, "%s/%d.%s", - getTapeDir(leveldir[leveldir_nr].filename), - level_nr, TAPEFILE_EXTENSION); - if (!(file = fopen(filename, "r"))) return; @@ -559,7 +589,7 @@ void LoadTape(int level_nr) void SaveTape(int level_nr) { int i; - char filename[MAX_FILENAME_LEN]; + char *filename = getTapeFilename(level_nr); FILE *file; boolean new_tape = TRUE; byte store_participating_players; @@ -568,10 +598,6 @@ void SaveTape(int level_nr) InitTapeDirectory(leveldir[leveldir_nr].filename); - sprintf(filename, "%s/%d.%s", - getTapeDir(leveldir[leveldir_nr].filename), - level_nr, TAPEFILE_EXTENSION); - /* if a tape still exists, ask to overwrite it */ if ((file = fopen(filename, "r"))) { @@ -664,28 +690,26 @@ void SaveTape(int level_nr) void LoadScore(int level_nr) { int i; - char filename[MAX_FILENAME_LEN]; + char *filename = getScoreFilename(level_nr); char cookie[MAX_LINE_LEN]; char line[MAX_LINE_LEN]; char *line_ptr; FILE *file; - /* start with empty score table */ + /* always start with reliable default values */ for(i=0; i 0 && cookie[strlen(cookie) - 1] == '\n') + cookie[strlen(cookie) - 1] = '\0'; if (strcmp(cookie, SCORE_COOKIE) != 0) { @@ -719,15 +743,11 @@ void LoadScore(int level_nr) void SaveScore(int level_nr) { int i; - char filename[MAX_FILENAME_LEN]; + char *filename = getScoreFilename(level_nr); FILE *file; InitScoreDirectory(leveldir[leveldir_nr].filename); - sprintf(filename, "%s/%d.%s", - getScoreDir(leveldir[leveldir_nr].filename), - level_nr, SCOREFILE_EXTENSION); - if (!(file = fopen(filename, "w"))) { Error(ERR_WARN, "cannot save score for level %d", level_nr); diff --git a/src/misc.c b/src/misc.c index 88f1cd77..c2c85e40 100644 --- a/src/misc.c +++ b/src/misc.c @@ -229,12 +229,23 @@ char *getHomeDir() char *getPath2(char *path1, char *path2) { - char *complete_path = checked_malloc(strlen(path1) + strlen(path2) + 2); + char *complete_path = checked_malloc(strlen(path1) + 1 + + strlen(path2) + 1); sprintf(complete_path, "%s/%s", path1, path2); return complete_path; } +char *getPath3(char *path1, char *path2, char *path3) +{ + char *complete_path = checked_malloc(strlen(path1) + 1 + + strlen(path2) + 1 + + strlen(path3) + 1); + + sprintf(complete_path, "%s/%s/%s", path1, path2, path3); + return complete_path; +} + char *getStringCopy(char *s) { char *s_copy = checked_malloc(strlen(s) + 1); diff --git a/src/misc.h b/src/misc.h index 182e5ff3..83aa074f 100644 --- a/src/misc.h +++ b/src/misc.h @@ -42,6 +42,7 @@ unsigned int InitRND(long); char *getLoginName(void); char *getHomeDir(void); char *getPath2(char *, char *); +char *getPath3(char *, char *, char*); char *getStringCopy(char *); char *getStringToLower(char *); void MarkTileDirty(int, int); diff --git a/src/netserv.c b/src/netserv.c index 6ff6e52e..0417b80f 100644 --- a/src/netserv.c +++ b/src/netserv.c @@ -1,15 +1,15 @@ -/* - * A server for a multi-player version of Tetris - * - * Copyright (C) 1996 Roger Espel Llima - * - * Started: 10 Oct 1996 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation. See the file COPYING for details. - * - */ +/*********************************************************** +* Rocks'n'Diamonds -- McDuffin Strikes Back! * +*----------------------------------------------------------* +* (c) 1995-98 Artsoft Entertainment * +* Holger Schemel * +* Oststrasse 11a * +* 33604 Bielefeld * +* phone: ++49 +521 290471 * +* email: aeglos@valinor.owl.de * +*----------------------------------------------------------* +* network.c * +***********************************************************/ #include #include @@ -236,16 +236,16 @@ static void new_connect(int fd) static void Handle_OP_PROTOCOL_VERSION(struct user *u, unsigned int len) { - if (len != 5 || buf[2] != PROT_VERS_1 || buf[3] != PROT_VERS_2) + if (len != 5 || buf[2] != PROTOCOL_VERSION_1 || buf[3] != PROTOCOL_VERSION_2) { if (options.verbose) printf("RND_SERVER: client %d (%s) has wrong protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]); buf[0] = 0; buf[1] = OP_BADVERS; - buf[2] = PROT_VERS_1; - buf[3] = PROT_VERS_2; - buf[4] = PROT_VERS_3; + buf[2] = PROTOCOL_VERSION_1; + buf[3] = PROTOCOL_VERSION_2; + buf[4] = PROTOCOL_VERSION_3; sendtoone(u, 5); flushuser(u); @@ -582,7 +582,7 @@ void NetworkServer(int port, int serveronly) printf("rocksndiamonds network server: started up, listening on port %d\n", port); printf("rocksndiamonds network server: using protocol version %d.%d.%d\n", - PROT_VERS_1, PROT_VERS_2, PROT_VERS_3); + PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_3); } while(1) diff --git a/src/netserv.h b/src/netserv.h index 55ebf8e9..69d24202 100644 --- a/src/netserv.h +++ b/src/netserv.h @@ -16,9 +16,9 @@ #define DEFAULTPORT 19504 -#define PROT_VERS_1 1 -#define PROT_VERS_2 2 -#define PROT_VERS_3 0 +#define PROTOCOL_VERSION_1 1 +#define PROTOCOL_VERSION_2 2 +#define PROTOCOL_VERSION_3 0 #define OP_PROTOCOL_VERSION 1 #define OP_BAD_PROTOCOL_VERSION 2 diff --git a/src/network.c b/src/network.c index 76b4e66f..8af73075 100644 --- a/src/network.c +++ b/src/network.c @@ -238,9 +238,9 @@ void SendToServer_Nickname(char *nickname) void SendToServer_ProtocolVersion() { buf[1] = OP_PROTOCOL_VERSION; - buf[2] = PROT_VERS_1; - buf[3] = PROT_VERS_2; - buf[4] = PROT_VERS_3; + buf[2] = PROTOCOL_VERSION_1; + buf[3] = PROTOCOL_VERSION_2; + buf[4] = PROTOCOL_VERSION_3; sendbuf(5); } @@ -306,7 +306,8 @@ static void Handle_OP_BAD_PROTOCOL_VERSION() { Error(ERR_WARN, "protocol version mismatch"); Error(ERR_EXIT, "server expects %d.%d.x instead of %d.%d.%d", - buf[2], buf[3], PROT_VERS_1, PROT_VERS_2, PROT_VERS_3); + buf[2], buf[3], + PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_3); } static void Handle_OP_YOUR_NUMBER() -- 2.34.1