zaela-Server/common/misc_functions.cpp

299 lines
6.4 KiB
C++
Raw Permalink Normal View History

2013-05-09 10:44:08 -04:00
/* EQEMu: Everquest Server Emulator
Copyright (C) 2001-2002 EQEMu Development Team (http://eqemu.org)
2013-02-16 16:14:39 -08:00
2013-05-09 10:44:08 -04:00
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; version 2 of the License.
2013-02-16 16:14:39 -08:00
2013-05-09 10:44:08 -04:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY except by those people which sell it, which
2013-02-16 16:14:39 -08:00
are required to give you total support for your newly bought product;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR
2013-05-09 10:44:08 -04:00
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
2013-02-16 16:14:39 -08:00
2013-05-09 10:44:08 -04:00
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
2013-02-16 16:14:39 -08:00
*/
2014-12-15 22:21:44 -06:00
#include "../common/global_define.h"
2014-08-21 19:33:02 -07:00
#include "misc_functions.h"
2013-02-16 16:14:39 -08:00
#include <string.h>
#include <time.h>
2014-12-15 22:21:44 -06:00
2013-02-16 16:14:39 -08:00
#ifndef WIN32
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include <iostream>
#include <iomanip>
#ifdef _WINDOWS
#include <io.h>
#endif
#include "../common/timer.h"
#include "../common/seperator.h"
#ifdef _WINDOWS
#include <windows.h>
#define snprintf _snprintf
#define strncasecmp _strnicmp
2013-05-09 10:44:08 -04:00
#define strcasecmp _stricmp
2013-02-16 16:14:39 -08:00
#else
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/time.h>
#ifdef FREEBSD //Timothy Whitman - January 7, 2003
2013-05-09 10:44:08 -04:00
#include <sys/socket.h>
#include <netinet/in.h>
#endif
2013-02-16 16:14:39 -08:00
#include <sys/stat.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#endif
void CoutTimestamp(bool ms) {
time_t rawtime;
struct tm* gmt_t;
time(&rawtime);
gmt_t = gmtime(&rawtime);
2013-05-09 10:44:08 -04:00
struct timeval read_time;
gettimeofday(&read_time,0);
2013-02-16 16:14:39 -08:00
std::cout << (gmt_t->tm_year + 1900) << "/" << std::setw(2) << std::setfill('0') << (gmt_t->tm_mon + 1) << "/" << std::setw(2) << std::setfill('0') << gmt_t->tm_mday << " " << std::setw(2) << std::setfill('0') << gmt_t->tm_hour << ":" << std::setw(2) << std::setfill('0') << gmt_t->tm_min << ":" << std::setw(2) << std::setfill('0') << gmt_t->tm_sec;
2013-02-16 16:14:39 -08:00
if (ms)
std::cout << "." << std::setw(3) << std::setfill('0') << (read_time.tv_usec / 1000);
std::cout << " GMT";
2013-02-16 16:14:39 -08:00
}
int32 filesize(FILE* fp) {
#ifdef _WINDOWS
return _filelength(_fileno(fp));
#else
struct stat file_stat;
fstat(fileno(fp), &file_stat);
return (int32) file_stat.st_size;
/* uint32 tmp = 0;
while (!feof(fp)) {
fseek(fp, tmp++, SEEK_SET);
}
return tmp;*/
#endif
}
uint32 ResolveIP(const char* hostname, char* errbuf) {
#ifdef _WINDOWS
static InitWinsock ws;
#endif
if (errbuf)
errbuf[0] = 0;
if (hostname == 0) {
if (errbuf)
snprintf(errbuf, ERRBUF_SIZE, "ResolveIP(): hostname == 0");
return 0;
}
2013-05-09 10:44:08 -04:00
struct sockaddr_in server_sin;
2013-02-16 16:14:39 -08:00
#ifdef _WINDOWS
2013-05-04 18:06:58 -07:00
PHOSTENT phostent = nullptr;
2013-02-16 16:14:39 -08:00
#else
2013-05-04 18:06:58 -07:00
struct hostent *phostent = nullptr;
2013-02-16 16:14:39 -08:00
#endif
server_sin.sin_family = AF_INET;
2013-05-04 18:06:58 -07:00
if ((phostent = gethostbyname(hostname)) == nullptr) {
2013-02-16 16:14:39 -08:00
#ifdef _WINDOWS
if (errbuf)
snprintf(errbuf, ERRBUF_SIZE, "Unable to get the host name. Error: %i", WSAGetLastError());
#else
if (errbuf)
snprintf(errbuf, ERRBUF_SIZE, "Unable to get the host name. Error: %s", strerror(errno));
#endif
return 0;
}
#ifdef _WINDOWS
memcpy ((char FAR *)&(server_sin.sin_addr), phostent->h_addr, phostent->h_length);
#else
memcpy ((char*)&(server_sin.sin_addr), phostent->h_addr, phostent->h_length);
#endif
return server_sin.sin_addr.s_addr;
}
bool ParseAddress(const char* iAddress, uint32* oIP, uint16* oPort, char* errbuf) {
Seperator sep(iAddress, ':', 2, 250, false, 0, 0);
if (sep.argnum == 1 && sep.IsNumber(1)) {
*oIP = ResolveIP(sep.arg[0], errbuf);
if (*oIP == 0)
return false;
if (oPort)
*oPort = atoi(sep.arg[1]);
return true;
}
return false;
}
#ifdef _WINDOWS
InitWinsock::InitWinsock() {
WORD version = MAKEWORD (1,1);
WSADATA wsadata;
WSAStartup (version, &wsadata);
}
InitWinsock::~InitWinsock() {
WSACleanup();
}
#endif
const char * itoa(int num) {
static char temp[_ITOA_BUFLEN];
memset(temp,0,_ITOA_BUFLEN);
snprintf(temp,_ITOA_BUFLEN,"%d",num);
return temp;
}
#ifndef WIN32
const char * itoa(int num, char* a,int b) {
static char temp[_ITOA_BUFLEN];
memset(temp,0,_ITOA_BUFLEN);
snprintf(temp,_ITOA_BUFLEN,"%d",num);
return temp;
return temp;
}
#endif
uint32 rnd_hash( time_t t, clock_t c )
{
2013-05-09 10:44:08 -04:00
// Get a uint32 from t and c
// Better than uint32(x) in case x is floating point in [0,1]
// Based on code by Lawrence Kirby (fred@genesis.demon.co.uk)
static uint32 differ = 0; // guarantee time-based seeds will change
uint32 h1 = 0;
unsigned char *p = (unsigned char *) &t;
for( size_t i = 0; i < sizeof(t); ++i )
{
h1 *= 255 + 2U;
h1 += p[i];
}
uint32 h2 = 0;
p = (unsigned char *) &c;
for( size_t j = 0; j < sizeof(c); ++j )
{
h2 *= 255 + 2U;
h2 += p[j];
}
return ( h1 + differ++ ) ^ h2;
2013-02-16 16:14:39 -08:00
}
float EQ13toFloat(int d)
{
return static_cast<float>(d) / 64.0f;
2013-02-16 16:14:39 -08:00
}
float EQ19toFloat(int d)
2013-02-16 16:14:39 -08:00
{
return static_cast<float>(d) / 8.0f;
2013-02-16 16:14:39 -08:00
}
int FloatToEQ13(float d)
2013-02-16 16:14:39 -08:00
{
return static_cast<int>(d * 64.0f);
2013-02-16 16:14:39 -08:00
}
int FloatToEQ19(float d)
2013-02-16 16:14:39 -08:00
{
return static_cast<int>(d * 8.0f);
2013-02-16 16:14:39 -08:00
}
float EQ12toFloat(int d)
2013-02-16 16:14:39 -08:00
{
return static_cast<float>(d) / 4.0f;
2013-02-16 16:14:39 -08:00
}
int FloatToEQ12(float d)
{
return static_cast<int>((d + 2048.0f) * 4.0f) % 2048;
}
float EQ10toFloat(int d)
{
return static_cast<float>(d) / 20.0f;
}
int FloatToEQ10(float d)
{
return static_cast<int>(d * 20.0f);
}
float EQSpeedRunToFloat(int d)
{
return static_cast<float>(d) / 40.0f;
}
int FloatToEQSpeedRun(float d)
2013-02-16 16:14:39 -08:00
{
return static_cast<int>(d * 40.0f);
2013-02-16 16:14:39 -08:00
}
float FixHeading(float in)
{
int i = 0;
if (in >= 512.0f) {
do {
in -= 512.0f;
} while (in >= 512.0f && i++ <= 5);
}
i = 0;
if (in < 0.0f) {
do {
in += 512.0f;
} while (in < 0.0f && i++ <= 5);
}
return in;
}
2013-02-16 16:14:39 -08:00
/*
Heading of 0 points in the pure positive Y direction
*/
int FloatToEQH(float d)
{
return(int((360.0f - d) * float(1<<11)) / 360);
}
float EQHtoFloat(int d)
{
return(360.0f - float((d * 360) >> 11));
}
2014-09-25 18:48:58 -04:00
// returns a swapped-bit value for use in client translator and inventory functions
uint32 SwapBits21And22(uint32 mask)
2014-09-25 18:48:58 -04:00
{
static const uint32 BIT21 = 1 << 21;
static const uint32 BIT22 = 1 << 22;
static const uint32 SWAPBITS = (BIT21 | BIT22);
if ((bool)(mask & BIT21) != (bool)(mask & BIT22))
mask ^= SWAPBITS;
return mask;
}
// returns an unset bit 22 value for use in client translators
uint32 Catch22(uint32 mask)
{
static const uint32 KEEPBITS = ~(1 << 22);
mask &= KEEPBITS;
return mask;
}