#include "main.h" int Initialize(); int PopulateOpcodeTable(); void OpcodeProcessing(); int RegisterFiles(); void StartFileTransfer(LocalData* pLir); FsFile* LoadFile(unsigned long hash); bool PopulateAnetIps(); int HandleOpcode_FS_REQUEST_MORE(NetMessage* pData, LocalData* pLir); unsigned char *SaveHOSTS(const char *filestr, const char *authstr); void RestoreHOSTS(unsigned char *oldhosts); int main() { //Initializes the server if (Initialize() < 0) { return -1; } //Populate the opcode table if (PopulateOpcodeTable() < 0) { return -2; } //Load the build version information gCoreLog.Section("Loading build version info..."); ConfigLoader con; if (con.Load("BuildVersion.ini", &gCoreLog) != 0) { gCoreLog.error("Failed to load BuildVersion.ini"); gCoreLog.SectionFail(); return -1; } con.GetDataAsUInt("Unknown1", (unsigned int*)&gFsConfig.unknown1); con.GetDataAsUInt("DepFileHash", (unsigned int*)&gFsConfig.depFile); con.GetDataAsUInt("ExecutableHash", (unsigned int*)&gFsConfig.exeFile); con.GetDataAsUInt("Unknown2", (unsigned int*)&gFsConfig.unknown2); con.GetDataAsUInt("Unknown3", (unsigned int*)&gFsConfig.unknown3); con.CleanUp(); if (con.ErrorOccurred()) { gCoreLog.SectionFail(); return -1; } gCoreLog.SectionOk(); //Register files in the file cache gCoreLog.Section("Loading file cache..."); ifstream fin("Files/CacheList.txt"); if(!fin.fail()) { gFsConfig.disableFileLog=true; gIgnoreFileWarning=true; while(!fin.eof()) { char file_name[150]; unsigned long decomp_size=0; unsigned long hash=0; fin >> hash; fin >> file_name; fin >> decomp_size; RegisterFile(file_name, hash, decomp_size); } fin.close(); gIgnoreFileWarning=false; } gCoreLog.SectionOk(); gFsConfig.disableFileLog=false; //Populate the ANet IP table if (gFsConfig.useArenaNet) { unsigned char* pOld_hosts=SaveHOSTS(NULL, NULL); gCoreLog.Section("Populating ANet ips..."); if (!PopulateAnetIps()) { gFsConfig.useArenaNet=false; gCoreLog.SectionFail(); } gCoreLog.SectionOk(); RestoreHOSTS(pOld_hosts); } else { gCoreLog.Section("Not using ArenaNet file server backend..."); gCoreLog.SectionOk(); } if (gFsConfig.useArchive) { //Open the Guild Wars archive file if one is specified. gCoreLog.Section("Opening Guild Wars Archive File..."); if (gGwArchive.Open(gFsConfig.archivePath) <= 0) { gFsConfig.useArchive=false; gCoreLog.SectionFail(); } gCoreLog.SectionOk(); } else { gCoreLog.Section("Not using Guild Wars archive file..."); gCoreLog.SectionOk(); } //Load files if (RegisterFiles() < 0) { gCoreLog.error("Could not open file list!"); return -3; } gCoreLog.WriteCategory("[Server Ready for Connections]"); while (true) { //Update the network handler gpFileServer->Update(); //Process incoming packets OpcodeProcessing(); bool active=false; //Process queued file send hash_map::iterator itr; for (itr=gData.begin(); itr!=gData.end(); itr++) { //Start any file transfers that need to be started if (!itr->second.sendingData && itr->second.hashes.size() > 0) { StartFileTransfer(&itr->second); } //If we are transferring a file and we have waited the timeout if(itr->second.sendingData) { //If we haven't finished sending our current data chunk if(itr->second.bytesRemainingInSegment > 0 ) { SendData(&itr->second); } //If we have segment sends queued then process them else if(itr->second.queuedSends > 0) { SendDataHeader(&itr->second, itr->second.segmentLength); itr->second.queuedSends--; } } //See if all the data has been sent. if(itr->second.sendingData) { FsFile* pFile=LoadFile(itr->second.hashes.front()); if(itr->second.filePos==pFile->compressedSize) { //Show completion notice and remove that hash from the processing list gCoreLog.debug("%u - Transfer of 0x%X complete.", itr->second.networkId, itr->second.hashes.front()); itr->second.sendingData=false; FreeFile(itr->second.hashes.front()); itr->second.hashes.erase(itr->second.hashes.begin()); } } if(itr->second.sendingData) { active=true; } } if(!active) { Sleep(1); } } //Close the log file gCoreLog.Close(); } char hostfile[150]; int hostlength=0; // save old hosts file and write new entries to the file unsigned char *SaveHOSTS(const char *filestr, const char *authstr) { #ifdef WIN32 FILE *file; char *windir = getenv("windir"); strcpy(hostfile,windir); strcat(hostfile,"\\System32\\drivers\\etc\\HOSTS"); printf("Windows directory: %s\n",hostfile); if ((file = fopen(hostfile,"rb")) == NULL) { return 0; } fseek(file,0,SEEK_END); hostlength = ftell(file); unsigned char *oldhosts = new unsigned char[hostlength+1]; rewind(file); fread(oldhosts,hostlength,1,file); fclose(file); file = fopen(hostfile,"wb"); // just in case fprintf(file,"\r\n"); fclose(file); return oldhosts; #endif } // restore old hosts file void RestoreHOSTS(unsigned char *oldhosts) { #ifdef WIN32 FILE *file = fopen(hostfile,"wb"); rewind(file); fwrite(oldhosts,hostlength,1,file); fclose(file); delete[] oldhosts; #endif }