?? d2charfile.c
字號:
/* * Copyright (C) 2000,2001 Onlyer (onlyer@263.net) * * 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; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include "common/setup_before.h"#include "setup.h"#ifdef HAVE_STDDEF_H# include <stddef.h>#else# ifndef NULL# define NULL ((void *)0)# endif#endif#include <stdio.h>#include <ctype.h>#ifdef HAVE_STRING_H# include <string.h>#else# ifdef HAVE_STRINGS_H# include <strings.h># endif# ifdef HAVE_MEMORY_H# include <memory.h># endif#endif#ifdef STDC_HEADERS# include <stdlib.h>#else# ifdef HAVE_MALLOC_H# include <malloc.h># endif#endif#include "compat/memset.h"#include "compat/memcpy.h"#include <errno.h>#include "compat/strerror.h"#ifdef HAVE_UNISTD_H# include <unistd.h>#endif#include "compat/access.h"#ifdef TIME_WITH_SYS_TIME# include <time.h># include <sys/time.h>#else# ifdef HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#ifdef HAVE_SYS_STAT_H# include <sys/stat.h>#endif#include "d2charfile.h"#include "prefs.h"#include "common/xstring.h"#include "common/bn_type.h"#include "common/eventlog.h"#include "common/d2char_checksum.h"#include "common/xalloc.h"#include "common/setup_after.h"static int d2charsave_init(void * buffer,char const * charname,unsigned char class,unsigned short status);static int d2charinfo_init(t_d2charinfo_file * chardata, char const * account, char const * charname, unsigned char class, unsigned short status);static int d2charsave_init(void * buffer,char const * charname,unsigned char class,unsigned short status){ ASSERT(buffer,-1); ASSERT(charname,-1); bn_byte_set((bn_byte *)((char *)buffer+D2CHARSAVE_CLASS_OFFSET), class); bn_short_set((bn_short *)((char *)buffer+D2CHARSAVE_STATUS_OFFSET),status); strncpy((char *)buffer+D2CHARSAVE_CHARNAME_OFFSET,charname,MAX_CHARNAME_LEN); return 0;}static int d2charinfo_init(t_d2charinfo_file * chardata, char const * account, char const * charname, unsigned char class, unsigned short status){ unsigned int i; time_t now; now=time(NULL); bn_int_set(&chardata->header.magicword,D2CHARINFO_MAGICWORD); bn_int_set(&chardata->header.version,D2CHARINFO_VERSION); bn_int_set(&chardata->header.create_time,now); bn_int_set(&chardata->header.last_time,now); bn_int_set(&chardata->header.total_play_time,0); memset(chardata->header.charname, 0,MAX_CHARNAME_LEN); strncpy(chardata->header.charname,charname,MAX_CHARNAME_LEN); memset(chardata->header.account, 0,MAX_ACCTNAME_LEN); strncpy(chardata->header.account,account,MAX_ACCTNAME_LEN); memset(chardata->header.realmname, 0,MAX_REALMNAME_LEN); strncpy(chardata->header.realmname,prefs_get_realmname(),MAX_REALMNAME_LEN); bn_int_set(&chardata->header.checksum,0); for (i=0; i<NELEMS(chardata->header.reserved); i++) { bn_int_set(&chardata->header.reserved[i],0); } bn_int_set(&chardata->summary.charlevel,1); bn_int_set(&chardata->summary.experience,0); bn_int_set(&chardata->summary.charclass,class); bn_int_set(&chardata->summary.charstatus,status); memset(chardata->portrait.gfx,D2CHARINFO_PORTRAIT_PADBYTE,sizeof(chardata->portrait.gfx)); memset(chardata->portrait.color,D2CHARINFO_PORTRAIT_PADBYTE,sizeof(chardata->portrait.color)); memset(chardata->portrait.u2,D2CHARINFO_PORTRAIT_PADBYTE,sizeof(chardata->portrait.u2)); memset(chardata->portrait.u1,D2CHARINFO_PORTRAIT_MASK,sizeof(chardata->portrait.u1)); memset(chardata->pad,0,sizeof(chardata->pad)); bn_short_set(&chardata->portrait.header,D2CHARINFO_PORTRAIT_HEADER); bn_byte_set(&chardata->portrait.status,status|D2CHARINFO_PORTRAIT_MASK); bn_byte_set(&chardata->portrait.class,class+1); bn_byte_set(&chardata->portrait.level,1); if (charstatus_get_ladder(status)) bn_byte_set(&chardata->portrait.ladder, 1); else bn_byte_set(&chardata->portrait.ladder, D2CHARINFO_PORTRAIT_PADBYTE); bn_byte_set(&chardata->portrait.end,'\0'); memset(chardata->pad,0,sizeof(chardata->pad)); return 0;}extern int d2char_create(char const * account, char const * charname, unsigned char class, unsigned short status){ t_d2charinfo_file chardata; char * savefile, * infofile; char buffer[1024]; unsigned int size; int ladder_time, now; FILE * fp; ASSERT(account,-1); ASSERT(charname,-1); if (class>D2CHAR_MAX_CLASS) class=0; status &= D2CHARINFO_STATUS_FLAG_INIT_MASK; charstatus_set_init(status,1); /* We need to make sure we are creating the correct character (Classic or Expansion) for the type of game server we are running. If lod_realm = 1 then only Expansion characters can be created and if set to 0 then only Classic character can be created */ if (!(prefs_get_lod_realm() == 2)) { if (prefs_get_lod_realm() && ((status & 0x20) != 0x20)) { eventlog(eventlog_level_warn,__FUNCTION__,"This Realm is for LOD Characters Only"); return -1; } if (!prefs_get_lod_realm() && ((status & 0x20) != 0x0)) { eventlog(eventlog_level_warn,__FUNCTION__,"This Realm is for Classic Characters Only"); return -1; } } /* Once correct type of character is varified then continue with creation of character */ if (!prefs_allow_newchar()) { eventlog(eventlog_level_warn,__FUNCTION__,"creation of new character is disabled"); return -1; } if (d2char_check_charname(charname)<0) { eventlog(eventlog_level_error,__FUNCTION__,"got bad character name \"%s\"",charname); return -1; } if (d2char_check_acctname(account)<0) { eventlog(eventlog_level_error,__FUNCTION__,"got bad account name \"%s\"",account); return -1; } size=sizeof(buffer); if (file_read(prefs_get_charsave_newbie(), buffer, &size)<0) { eventlog(eventlog_level_error,__FUNCTION__,"error loading newbie save file"); return -1; } if (size>=sizeof(buffer)) { eventlog(eventlog_level_error,__FUNCTION__,"newbie save file \"%s\" is corrupt (length %lu, expected <%lu)",prefs_get_charsave_newbie(),(unsigned long)size,(unsigned long)sizeof(buffer)); return -1; } savefile=xmalloc(strlen(prefs_get_charsave_dir())+1+strlen(charname)+1); d2char_get_savefile_name(savefile,charname); if ((fp=fopen(savefile,"rb"))) { eventlog(eventlog_level_warn,__FUNCTION__,"character save file \"%s\" for \"%s\" already exist",savefile,charname); fclose(fp); xfree(savefile); return -1; } infofile=xmalloc(strlen(prefs_get_charinfo_dir())+1+strlen(account)+1+strlen(charname)+1); d2char_get_infofile_name(infofile,account,charname); now = time(NULL); ladder_time = prefs_get_ladder_start_time(); if ((ladder_time > 0) && (now < ladder_time)) charstatus_set_ladder(status, 0); d2charsave_init(buffer,charname,class,status); d2charinfo_init(&chardata,account,charname,class,status); if (file_write(infofile,&chardata,sizeof(chardata))<0) { eventlog(eventlog_level_error,__FUNCTION__,"error writing info file \"%s\"",infofile); remove(infofile); xfree(infofile); xfree(savefile); return -1; } if (file_write(savefile,buffer,size)<0) { eventlog(eventlog_level_error,__FUNCTION__,"error writing save file \"%s\"",savefile); remove(infofile); remove(savefile); xfree(savefile); xfree(infofile); return -1; } xfree(savefile); xfree(infofile); eventlog(eventlog_level_info,__FUNCTION__,"character %s(*%s) class %d status 0x%X created",charname,account,class,status); return 0;}extern int d2char_find(char const * account, char const * charname){ char * file; FILE * fp; ASSERT(account,-1); ASSERT(charname,-1); file=xmalloc(strlen(prefs_get_charinfo_dir())+1+strlen(account)+1+strlen(charname)+1); d2char_get_infofile_name(file,account,charname); fp=fopen(file,"rb"); xfree(file); if (fp) { fclose(fp); return 0; } return -1;}extern int d2char_convert(char const * account, char const * charname){ FILE * fp; char * file; unsigned char buffer[MAX_SAVEFILE_SIZE]; unsigned int status_offset; unsigned char status; unsigned int charstatus; t_d2charinfo_file charinfo; unsigned int size; unsigned int version; unsigned int checksum; ASSERT(account,-1); ASSERT(charname,-1);/* Playing with a expanstion char on a classic realm will cause the game server to crash, therefore I recommed setting allow_convert = 0 in the d2cs.conf We need to do this to prevent creating classic char and converting to expantion on a classic realm. LOD Char must be created on LOD realm */ if (!prefs_get_allow_convert()) { eventlog(eventlog_level_info,__FUNCTION__,"Convert char has been disabled"); return -1; }/* Procedure is stopped here and returned if allow_convet = 0 in d2cs.conf */ if (d2char_check_charname(charname)<0) { eventlog(eventlog_level_error,__FUNCTION__,"got bad character name \"%s\"",charname); return -1; } if (d2char_check_acctname(account)<0) { eventlog(eventlog_level_error,__FUNCTION__,"got bad account name \"%s\"",account); return -1; } file=xmalloc(strlen(prefs_get_charinfo_dir())+1+strlen(account)+1+strlen(charname)+1); d2char_get_infofile_name(file,account,charname); if (!(fp=fopen(file,"rb+"))) { eventlog(eventlog_level_error,__FUNCTION__,"unable to open charinfo file \"%s\" for reading and writing (fopen: %s)",file,pstrerror(errno)); xfree(file); return -1; } xfree(file); if (fread(&charinfo,1,sizeof(charinfo),fp)!=sizeof(charinfo)) { eventlog(eventlog_level_error,__FUNCTION__,"error reading charinfo file for character \"%s\" (fread: %s)",charname,pstrerror(errno)); fclose(fp); return -1; } charstatus=bn_int_get(charinfo.summary.charstatus); charstatus_set_expansion(charstatus,1); bn_int_set(&charinfo.summary.charstatus,charstatus); status=bn_byte_get(charinfo.portrait.status); charstatus_set_expansion(status,1); bn_byte_set(&charinfo.portrait.status,status); fseek(fp,0,SEEK_SET); /* FIXME: check return */ if (fwrite(&charinfo,1,sizeof(charinfo),fp)!=sizeof(charinfo)) { eventlog(eventlog_level_error,__FUNCTION__,"error writing charinfo file for character \"%s\" (fwrite: %s)",charname,pstrerror(errno)); fclose(fp); return -1; } if (fclose(fp)<0) { eventlog(eventlog_level_error,__FUNCTION__,"could not close charinfo file for character \"%s\" after writing (fclose: %s)",charname,pstrerror(errno)); return -1; } file=xmalloc(strlen(prefs_get_charsave_dir())+1+strlen(charname)+1); d2char_get_savefile_name(file,charname); if (!(fp=fopen(file,"rb+"))) { eventlog(eventlog_level_error,__FUNCTION__,"could not open charsave file \"%s\" for reading and writing (fopen: %s)",file,pstrerror(errno)); xfree(file); return -1; } xfree(file); size=fread(buffer,1,sizeof(buffer),fp); if (!feof(fp)) { eventlog(eventlog_level_error,__FUNCTION__,"error reading charsave file for character \"%s\" (fread: %s)",charname,pstrerror(errno)); fclose(fp); return -1; } version=bn_int_get(buffer+D2CHARSAVE_VERSION_OFFSET); if (version>=0x0000005C) { status_offset=D2CHARSAVE_STATUS_OFFSET_109; } else { status_offset=D2CHARSAVE_STATUS_OFFSET; } status=bn_byte_get(buffer+status_offset); charstatus_set_expansion(status,1); bn_byte_set((bn_byte *)(buffer+status_offset),status); /* FIXME: shouldn't abuse bn_*_set()... what's the best way to do this? */ if (version>=0x0000005C) { checksum=d2charsave_checksum(buffer,size,D2CHARSAVE_CHECKSUM_OFFSET); bn_int_set((bn_int *)(buffer+D2CHARSAVE_CHECKSUM_OFFSET),checksum); /* FIXME: shouldn't abuse bn_*_set()... what's the best way to do this? */ } fseek(fp,0,SEEK_SET); /* FIXME: check return */ if (fwrite(buffer,1,size,fp)!=size) { eventlog(eventlog_level_error,__FUNCTION__,"error writing charsave file for character %s (fwrite: %s)",charname,pstrerror(errno)); fclose(fp); return -1; } if (fclose(fp)<0) { eventlog(eventlog_level_error,__FUNCTION__,"could not close charsave file for character \"%s\" after writing (fclose: %s)",charname,pstrerror(errno)); return -1; } eventlog(eventlog_level_info,__FUNCTION__,"character %s(*%s) converted to expansion",charname,account); return 0;}extern int d2char_delete(char const * account, char const * charname){ char * file; ASSERT(account,-1); ASSERT(charname,-1); if (d2char_check_charname(charname)<0) { eventlog(eventlog_level_error,__FUNCTION__,"got bad character name \"%s\"",charname); return -1; } if (d2char_check_acctname(account)<0) { eventlog(eventlog_level_error,__FUNCTION__,"got bad account name \"%s\"",account); return -1; } /* charsave file */ file=xmalloc(strlen(prefs_get_charinfo_dir())+1+strlen(account)+1+strlen(charname)+1); d2char_get_infofile_name(file,account,charname); if (remove(file)<0) { eventlog(eventlog_level_error,__FUNCTION__,"failed to delete charinfo file \"%s\" (remove: %s)",file,pstrerror(errno)); xfree(file); return -1; } xfree(file); /* charinfo file */ file=xmalloc(strlen(prefs_get_charsave_dir())+1+strlen(charname)+1); d2char_get_savefile_name(file,charname); if (remove(file)<0) { eventlog(eventlog_level_error,__FUNCTION__,"failed to delete charsave file \"%s\" (remove: %s)",file,pstrerror(errno)); } xfree(file); /* bak charsave file */ file=xmalloc(strlen(prefs_get_bak_charinfo_dir())+1+strlen(account)+1+strlen(charname)+1); d2char_get_bak_infofile_name(file,account,charname); if (access(file, F_OK) == 0) { if (remove(file)<0) { eventlog(eventlog_level_error,__FUNCTION__,"failed to delete bak charinfo file \"%s\" (remove: %s)",file,pstrerror(errno)); } } xfree(file); /* bak charinfo file */ file=xmalloc(strlen(prefs_get_bak_charsave_dir())+1+strlen(charname)+1); d2char_get_bak_savefile_name(file,charname); if (access(file, F_OK) == 0) { if (remove(file)<0) { eventlog(eventlog_level_error,__FUNCTION__,"failed to delete bak charsave file \"%s\" (remove: %s)",file,pstrerror(errno));
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -