From 0ad7cabf1feaa6af32f127254453f6b37d9fecc6 Mon Sep 17 00:00:00 2001 From: Alexander Baldeck Date: Sun, 26 Dec 2004 14:46:39 +0000 Subject: - final touch to writing mechanism of the config system - tmw now can read and write its ini file - some fixes in login.cpp to get it to work properly - Init method -> init - Write method -> write - minor code "beautification" in several files --- src/configuration.cpp | 57 ++++++++++++-------- src/configuration.h | 5 +- src/game.cpp | 114 ++++++++++++++++++++------------------- src/gui/gui.cpp | 34 ++++++------ src/gui/login.cpp | 16 +++--- src/main.cpp | 94 ++++++++++++++++---------------- src/net/network.cpp | 146 +++++++++++++++++++++++++------------------------- 7 files changed, 239 insertions(+), 227 deletions(-) diff --git a/src/configuration.cpp b/src/configuration.cpp index dc6aa448..06296bf1 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -26,8 +26,8 @@ \brief read INI file and parse all options into memory \param filename full path to INI file (~/.manaworld/tmw.ini) */ -void Configuration::Init(std::string filename) { - inFile.open(filename.c_str(), std::ifstream::in); +void Configuration::init(std::string filename) { + std::ifstream inFile(filename.c_str(), std::ifstream::in); std::string inBuffer; int position; INI_OPTION optionTmp; @@ -54,12 +54,12 @@ void Configuration::Init(std::string filename) { #ifdef __DEBUG for (iter = iniOptions.begin(); iter != iniOptions.end(); iter++) { optionTmp = *iter; - std::cout << "Configuration::Init(" << optionTmp.key << ", \"" << optionTmp.stringValue << "\" / " << optionTmp.numericValue << ")\n"; + std::cout << "Configuration::init(" << optionTmp.key << ", \"" << optionTmp.stringValue << "\" / " << optionTmp.numericValue << ")\n"; } #endif } -bool Configuration::Write(std::string filename) { +bool Configuration::write(std::string filename) { std::ofstream out(filename.c_str(), std::ofstream::out | std::ofstream::trunc); char tmp[20]; @@ -76,7 +76,7 @@ bool Configuration::Write(std::string filename) { out.write(tmp, strlen(tmp)); strcpy(tmp, ""); } - + std::cout << "Configuration::write(" << optionTmp.key << ", \"" << optionTmp.stringValue << "\" / " << optionTmp.numericValue << ")\n"; out.write("\n", 1); } @@ -85,32 +85,49 @@ bool Configuration::Write(std::string filename) { } void Configuration::setValue(std::string key, std::string value) { - if(getValue(key, value) == value) { + INI_OPTION optionTmp; + if(getValue(key, "") == "") { #ifdef __DEBUG - std::cout << "Configuration::setValue(" << key << ", \"" << value << "\")\n"; + std::cout << "Configuration::setValue(" << key << ", \"" << value << "\") newly set\n"; #endif - INI_OPTION optionTmp; - optionTmp.key = key; optionTmp.stringValue = value; optionTmp.numericValue = 0; iniOptions.push_back(optionTmp); + } else { + for (iter = iniOptions.begin(); iter != iniOptions.end(); iter++) { + if(iter->key == key) { + #ifdef __DEBUG + std::cout << "Configuration::setValue(" << key << ", \"" << value << "\") reset\n"; + #endif + iter->stringValue = value; + iter->numericValue = 0; + } + } } } void Configuration::setValue(std::string key, float value) { - if(getValue(key, value) == value) { + INI_OPTION optionTmp; + if(getValue(key, 0) == 0) { #ifdef __DEBUG - std::cout << "Configuration::setValue(" << key << ", " << value << ")\n"; + std::cout << "Configuration::setValue(" << key << ", " << value << ") newly set\n"; #endif - - INI_OPTION optionTmp; - optionTmp.key = key; optionTmp.numericValue = value; iniOptions.push_back(optionTmp); + } else { + for (iter = iniOptions.begin(); iter != iniOptions.end(); iter++) { + if(iter->key == key) { + #ifdef __DEBUG + std::cout << "Configuration::setValue(" << key << ", " << value << ") reset\n"; + #endif + iter->stringValue = ""; + iter->numericValue = value; + } + } } } @@ -120,11 +137,9 @@ void Configuration::setValue(std::string key, float value) { \param deflt default option if not there or error */ std::string Configuration::getValue(std::string key, std::string deflt) { - INI_OPTION optionTmp; for (iter = iniOptions.begin(); iter != iniOptions.end(); iter++) { - optionTmp = *iter; - if(optionTmp.key == key) - return optionTmp.stringValue; + if(iter->key == key) + return iter->stringValue; } return deflt; @@ -136,11 +151,9 @@ std::string Configuration::getValue(std::string key, std::string deflt) { \param deflt default option if not there or error */ float Configuration::getValue(std::string key, float deflt) { - INI_OPTION optionTmp; for (iter = iniOptions.begin(); iter != iniOptions.end(); iter++) { - optionTmp = *iter; - if(optionTmp.key == key) - return optionTmp.numericValue; + if(iter->key == key) + return iter->numericValue; } return deflt; diff --git a/src/configuration.h b/src/configuration.h index 2bd8f83b..db99007c 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -37,9 +37,9 @@ */ class Configuration { public: - void Init(std::string); + void init(std::string); - bool Write(std::string); + bool write(std::string); void setValue(std::string, std::string); void setValue(std::string, float); @@ -47,7 +47,6 @@ class Configuration { std::string getValue(std::string, std::string); float getValue(std::string, float); private: - std::ifstream inFile; typedef struct INI_OPTION { std::string key; diff --git a/src/game.cpp b/src/game.cpp index b1449cb6..37162409 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -92,64 +92,67 @@ short get_elapsed_time(short start_time) { /** Main game loop */ void game() { - status("INIT"); - do_init(); - init_graphic(); - while(state!=EXIT) { - status("INPUT"); - do_input(); - status("GRAPHIC"); - do_graphic(); - status("PARSE"); - do_parse(); - status("FLUSH"); - flush(); - - //rest(1); // This one should work only in Win32 - } + status("INIT"); + do_init(); + init_graphic(); + + while(state!=EXIT) { + status("INPUT"); + do_input(); + status("GRAPHIC"); + do_graphic(); + status("PARSE"); + do_parse(); + status("FLUSH"); + flush(); + } - exit_graphic(); - close_session(); + exit_graphic(); + close_session(); } /** Initialize game engine */ void do_init() { - if(!load_map(map_path))error("Could not find map file"); - - sound.StartMOD("./data/sound/Mods/somemp.xm", -1); - - // Initialize timers - tick_time = 0; - refresh = false; - LOCK_VARIABLE(tick_time); - LOCK_VARIABLE(refresh); - install_int_ex(refresh_time, MSEC_TO_TIMER(1)); - install_int_ex(refresh_screen, /*MSEC_TO_TIMER(2000)*/BPS_TO_TIMER(200)); // Set max refresh rate to 75 fps - install_int_ex(second, BPS_TO_TIMER(1)); - - // Interrupt drawing while in background - #ifdef WIN32 - set_display_switch_mode(SWITCH_AMNESIA); - #else - set_display_switch_mode(SWITCH_PAUSE); - #endif - - // Initialize beings - empty(); - player_node = new NODE(); - player_node->id = account_ID; - player_node->type = ACTION_NODE; - set_coordinates(player_node->coordinates, x, y, 0); - player_node->speed = 150; - player_node->hair_color = char_info->hair_color; - player_node->hair_style = char_info->hair_style; - if(char_info->weapon==11)char_info->weapon = 1; - player_node->weapon = char_info->weapon; - add_node(player_node); - show_npc_dialog = 0; - - remove("./docs/packet.list"); + if(!load_map(map_path)) { + error("Could not find map file"); + } + + sound.StartMOD("./data/sound/Mods/somemp.xm", -1); + + // Initialize timers + tick_time = 0; + refresh = false; + LOCK_VARIABLE(tick_time); + LOCK_VARIABLE(refresh); + install_int_ex(refresh_time, MSEC_TO_TIMER(1)); + install_int_ex(refresh_screen, /*MSEC_TO_TIMER(2000)*/BPS_TO_TIMER(200)); // Set max refresh rate to 75 fps + install_int_ex(second, BPS_TO_TIMER(1)); + + // Interrupt drawing while in background + #ifdef WIN32 + set_display_switch_mode(SWITCH_AMNESIA); + #else + set_display_switch_mode(SWITCH_PAUSE); + #endif + + // Initialize beings + empty(); + player_node = new NODE(); + player_node->id = account_ID; + player_node->type = ACTION_NODE; + set_coordinates(player_node->coordinates, x, y, 0); + player_node->speed = 150; + player_node->hair_color = char_info->hair_color; + player_node->hair_style = char_info->hair_style; + if(char_info->weapon==11) { + char_info->weapon = 1; + } + player_node->weapon = char_info->weapon; + add_node(player_node); + show_npc_dialog = 0; + + remove("./docs/packet.list"); } /** @@ -306,8 +309,9 @@ void do_input() { //} } - if(key[KEY_ESC])state = EXIT; - + if(key[KEY_ESC]) { + state = EXIT; + } } /** Calculate packet length */ @@ -855,7 +859,7 @@ void do_parse() { for(int i=0;i<(RFIFOW(2)-4)/20;i++) inventory.add_item(RFIFOW(4+20*i), RFIFOW(6+20*i), 1); break; - + // Manage non implemented packets default: //printf("%x\n",id); diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 0c052fec..b6bfd72f 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -127,7 +127,7 @@ void loadButtonSkin() { int a = 0; int x,y,mode; - tokens = get_config_argv("button", "gridx", &tokenCount); + tokens = get_config_argv("button", "gridx", &tokenCount); for (a=0; a < 4; a++) { gridx[a] = atoi(tokens[a]); } @@ -438,22 +438,22 @@ void draw_skinned_rect(BITMAP*dst, LexSkinnedRect *skin, int x, int y,int w, int int gui_load_skin(const char* skinname) { - gui__external_slider_callback = NULL; - push_config_state(); - set_config_file(skinname); - gui_gfx = load_datafile(get_config_string("skin", "gfx", 0)); - loadButtonSkin(); - loadSliderSkin(); - loadCheckboxSkin(); - loadTextboxSkin(); - loadListboxSkin(); - loadDialogSkin(); - loadBarSkin(); - loadPlusSkin(); - pop_config_state(); - set_mouse_sprite((BITMAP *)gui_gfx[7].dat); - - return TRUE; + gui__external_slider_callback = NULL; + push_config_state(); + set_config_file(skinname); + gui_gfx = load_datafile(get_config_string("skin", "gfx", 0)); + loadButtonSkin(); + loadSliderSkin(); + loadCheckboxSkin(); + loadTextboxSkin(); + loadListboxSkin(); + loadDialogSkin(); + loadBarSkin(); + loadPlusSkin(); + pop_config_state(); + set_mouse_sprite((BITMAP *)gui_gfx[7].dat); + + return TRUE; } void gui_exit() { diff --git a/src/gui/login.cpp b/src/gui/login.cpp index 313ea516..1645a329 100644 --- a/src/gui/login.cpp +++ b/src/gui/login.cpp @@ -29,7 +29,6 @@ #include "passwordfield.h" #include "../graphic/graphic.h" - LoginDialog::LoginDialog(gcn::Container *parent): Window(parent, "Login") { @@ -49,7 +48,7 @@ LoginDialog::LoginDialog(gcn::Container *parent): userField->setWidth(130); passField->setWidth(130); keepCheck->setPosition(4, 52); - keepCheck->setMarked(get_config_int("login", "remember", 0)); + keepCheck->setMarked(config.getValue("remember", 0)); okButton->setPosition(120, 52); cancelButton->setPosition(146, 52); @@ -76,9 +75,9 @@ LoginDialog::LoginDialog(gcn::Container *parent): userField->requestFocus(); userField->setCaretPosition(userField->getText().length()); - if (get_config_int("login", "remember", 0)) { - if (get_config_string("login", "username", 0)) { - userField->setText(get_config_string("login", "username", "")); + if (config.getValue("remember", 0) != 0) { + if (config.getValue("username", "") != "") { + userField->setText(config.getValue("username", "")); passField->requestFocus(); } } @@ -102,11 +101,12 @@ void LoginDialog::action(const std::string& eventId) log("Network", "Username is %s", user.c_str()); // Store config settings - set_config_int("login", "remember", keepCheck->isMarked()); + config.setValue("remember", keepCheck->isMarked()); if (keepCheck->isMarked()) { - set_config_string("login", "username", user.c_str()); + std::cout << "blah\n"; + config.setValue("username", user); } else { - set_config_string("login", "username", ""); + config.setValue("username", ""); } // Check login diff --git a/src/main.cpp b/src/main.cpp index a73e04a8..8721da19 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -88,6 +88,7 @@ unsigned short x, y; unsigned char direction; //unsigned short job, hair, hair_color; unsigned char stretch_mode, screen_mode; +char *dir; // new sound-engine /- kth5 TmwSound sound; @@ -125,7 +126,7 @@ void init_engine() { init_log(); set_close_button_callback(request_exit); - char *dir = new char[400]; + dir = new char[400]; strcpy(dir, ""); #ifndef __USE_UNIX98 @@ -200,20 +201,17 @@ void init_engine() { config.setValue("remember", 1); config.setValue("username", "Player"); - config.Write(dir); + config.write(dir); } } - //set_config_file(dir); - config.Init(dir); + config.init(dir); #ifdef WIN32 if(code) { set_config_string("system", "keyboard", code); } #endif - delete dir; dir = 0; - // set_config_file("tmw.ini"); #ifdef MACOSX set_color_depth(32); @@ -269,6 +267,8 @@ void init_engine() { /** Clear the engine */ void exit_engine() { + config.write(dir); + delete dir; gui_exit(); destroy_bitmap(buffer); allegro_exit(); @@ -276,49 +276,45 @@ void exit_engine() { /** Main */ int main() { - init_engine(); - // initialize sound-engine and start playing intro-theme /-kth5 - try{ - if(config.getValue("sound", 0)==1) - sound.Init(32,20); // inits the sound-subsystem w/ 32 voices / 20 for mod - sound.SetVol(128,128,128); // sets intial volume parameters - //#ifdef WIN32 - //sound.StartMIDI("Sound/Midis/city.mid",-1); // play a midi file - //#endif - //sound.LoadItem("test.wav", TMWSOUND_SFX); - }catch(const char * err){ // catch errors and show appropriate messages on-screen (elven plz... ^^) - ok("Sound Engine", err); - warning(err); - } - - while(state!=EXIT) { - switch(state) { - case LOGIN: - status("LOGIN"); - login(); - break; - case CHAR_SERVER: - status("CHAR_SERVER"); - char_server(); - break; - case CHAR_SELECT: - status("CHAR_SELECT"); - charSelect(); - break; - case GAME: - sound.StopBGM(); - status("GAME"); - map_start(); - if( state==GAME ) - game(); - break; - default: - state = EXIT; - break; + init_engine(); + // initialize sound-engine and start playing intro-theme /-kth5 + try{ + if(config.getValue("sound", 0)==1) + sound.Init(32,20); + sound.SetVol(128,128,128); + }catch(const char * err){ + ok("Sound Engine", err); + warning(err); + } + + while(state!=EXIT) { + switch(state) { + case LOGIN: + status("LOGIN"); + login(); + break; + case CHAR_SERVER: + status("CHAR_SERVER"); + char_server(); + break; + case CHAR_SELECT: + status("CHAR_SELECT"); + charSelect(); + break; + case GAME: + sound.StopBGM(); + status("GAME"); + map_start(); + if( state==GAME ) + game(); + break; + default: + state = EXIT; + break; + } } - } - status("EXIT"); - exit_engine(); - return 0; + status("EXIT"); + exit_engine(); + return 0; } END_OF_MAIN(); diff --git a/src/net/network.cpp b/src/net/network.cpp index b030d92d..993abbc7 100644 --- a/src/net/network.cpp +++ b/src/net/network.cpp @@ -41,82 +41,86 @@ fd_set write_socket; /** Increase size of written data */ void WFIFOSET(int len) { - if(out_size+len>=buffer_size) - warning("Output buffer full"); - else out_size+=len; + if(out_size+len>=buffer_size) + warning("Output buffer full"); + else out_size+=len; } /** Convert an address from int format to string */ char *iptostring(int address) { - short temp1, temp2; - static char asciiIP[16]; + short temp1, temp2; + static char asciiIP[16]; - temp1 = LOWORD(address); - temp2 = HIWORD(address); - sprintf(asciiIP, "%i.%i.%i.%i", LOBYTE(temp1), HIBYTE(temp1), LOBYTE(temp2), HIBYTE(temp2)); - return asciiIP; + temp1 = LOWORD(address); + temp2 = HIWORD(address); + sprintf(asciiIP, "%i.%i.%i.%i", LOBYTE(temp1), HIBYTE(temp1), LOBYTE(temp2), HIBYTE(temp2)); + return asciiIP; } /** Open a session with a server */ SOCKET open_session(const char* address, short port) { - #ifdef WIN32 - WSADATA wsda; - #endif - struct hostent *server; - int ret; - - // Init WinSock and connect the socket - #ifdef WIN32 - WSAStartup(MAKEWORD(2,0), &wsda); - #endif - - sock = socket(PF_INET, SOCK_STREAM, 0); // Create socket for current session - if(sock==SOCKET_ERROR)return SOCKET_ERROR; - - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - addr.sin_addr.s_addr = inet_addr(address); - if(addr.sin_addr.s_addr == INADDR_NONE){ - server = NULL; - server = gethostbyname(address); - if(server == NULL)return SOCKET_ERROR; - memcpy(&addr.sin_addr, server->h_addr_list[0], server->h_length); - } - - ret = connect(sock, (struct sockaddr *) &addr, sizeof(addr)); - if(ret == SOCKET_ERROR)return SOCKET_ERROR; - - // Init buffers - in = (char *)malloc(buffer_size); - out = (char *)malloc(buffer_size); - memset(in, '\0', buffer_size); - memset(out, '\0', buffer_size); - in_size = 0; - out_size = 0; - FD_CLR(sock, &read_socket); - FD_CLR(sock, &write_socket); - - return sock; + #ifdef WIN32 + WSADATA wsda; + #endif + struct hostent *server; + int ret; + + // Init WinSock and connect the socket + #ifdef WIN32 + WSAStartup(MAKEWORD(2,0), &wsda); + #endif + + sock = socket(PF_INET, SOCK_STREAM, 0); // Create socket for current session + if(sock==SOCKET_ERROR)return SOCKET_ERROR; + + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = inet_addr(address); + if(addr.sin_addr.s_addr == INADDR_NONE){ + server = NULL; + server = gethostbyname(address); + if(server == NULL)return SOCKET_ERROR; + memcpy(&addr.sin_addr, server->h_addr_list[0], server->h_length); + } + + ret = connect(sock, (struct sockaddr *) &addr, sizeof(addr)); + if(ret == SOCKET_ERROR)return SOCKET_ERROR; + + // Init buffers + in = (char *)malloc(buffer_size); + out = (char *)malloc(buffer_size); + memset(in, '\0', buffer_size); + memset(out, '\0', buffer_size); + in_size = 0; + out_size = 0; + FD_CLR(sock, &read_socket); + FD_CLR(sock, &write_socket); + + return sock; } /** Close a session */ void close_session() { - FD_CLR(sock,&read_socket); - FD_CLR(sock,&write_socket); - closesocket(sock); - if(in!=NULL)free(in); - if(out!=NULL)free(out); - in = NULL; - out = NULL; - in_size = 0; - out_size = 0; - WSACleanup(); + FD_CLR(sock,&read_socket); + FD_CLR(sock,&write_socket); + closesocket(sock); + if(in!=NULL) { + free(in); + } + if(out!=NULL) { + free(out); + } + in = NULL; + out = NULL; + in_size = 0; + out_size = 0; + WSACleanup(); } /** Send and receive data waiting in the buffers */ void flush() { int ret = 0; - void *buf = out; //-kth5 + void *buf = out; timeval time_out; // Init the time_out struct to 0s so it won't block @@ -135,15 +139,11 @@ void flush() { // Send data if available if(FD_ISSET(sock, &write_socket)) { // While there wasn't a error or sent the whole data: handles partial packet send - while((ret!=SOCKET_ERROR)&&(out_size>0)) { + while((ret!=SOCKET_ERROR)&&(out_size>0)) { ret = send(sock, (char *)buf, out_size, 0); - /*FILE *file = fopen("log.log","wb"); - fprintf(file, "%s", out[0]); - fclose(file);*/ - // If not the whole data has been sent, empty the buffer from already sent bytes if(ret!=SOCKET_ERROR && ret>0) { - buf = (char*)buf+ret; //-kth5 + buf = (char*)buf+ret; out_size -= ret; } } @@ -151,7 +151,7 @@ void flush() { error("Socket Error"); #ifdef WIN32 log("Error", "Socket error: %i ", WSAGetLastError()); -#else +#else log("Error", "socket_error", "Undefined socket error"); #endif } @@ -159,16 +159,16 @@ void flush() { // Read data, if available if(FD_ISSET(sock, &read_socket)) { - /* There's no check for partial received packets because at this level - the app doesn't know packet length, but it will done when parsing received data */ - ret = recv(sock, in+in_size, RFIFOSPACE, 0); - if(ret==SOCKET_ERROR) { + /* There's no check for partial received packets because at this level + the app doesn't know packet length, but it will done when parsing received data */ + ret = recv(sock, in+in_size, RFIFOSPACE, 0); + if(ret==SOCKET_ERROR) { #ifdef WIN32 - log("Error", "Socket error: %i ", WSAGetLastError()); -#else - log("Error", "socket_error", "Undefined socket error"); + log("Error", "Socket error: %i ", WSAGetLastError()); +#else + log("Error", "socket_error", "Undefined socket error"); #endif - } else RFIFOSET(ret); // Set size of available data to read + } else RFIFOSET(ret); // Set size of available data to read } } -- cgit v1.2.3-70-g09d2