#include ".\global.h"
#using <mscorlib.dll>
#include <stdio.h>
#include "md5/Mymd5.h"

RoleValueData               Global::ValueCal;
BaseAbilityStruct           Global::Ability;
AccountInfoStruct           Global::AccountInfo;
vector< string >            Global::RaceName;
vector< string >            Global::JobName;
vector< string >            Global::SexName;
vector< string >            Global::ZoneName;

vector< string >            Global::PointEnumName;      //法術參考點
vector< string >            Global::MotionEnumName;     //動作
MutilThread_CritSect        Global::CritSect;
int							Global::MonsterMoney[ 200 ][ 3 ];//0 Base 1 一般怪Rand  2精英怪Rand
string						Global::IniRootPath;

vector<float>				Global::BaseMagicPower;
vector<float>				Global::MagicPowerGrowRate;
vector< NPCStrongArgStruct >	Global::NpcStrongArg;

vector<int>					Global::AbilityPoint;				//參考數值表
vector<int>					Global::DefPoint;					//參考數值表
vector<int>					Global::HitPoint;					//參考數值表

string						Global::DBSource;
string						Global::DataBase;


string Global::GetDataPath()
{
	string	DataPathStr = Global::GetIniString( "DataPath" );
	DataPathStr  = DataPathStr + "\\Data";

	return DataPathStr;
}

char* Global::GetIniString( char* Key )
{
	static char    Buf[256];
	int     Ret;
	char*	Path = (char*)Global::IniRootPath.c_str(); 
	Ret = GetPrivateProfileString("BOOT", Key , NULL , Buf , sizeof(Buf) , Path );
	if( Ret == 0 )
		return "";

	return Buf;
}

int Global::GetIniValue( char* Key, int iDefaultVal )
{
	static char    Buf[256];
	int     Ret;
	char*	Path = (char*)Global::IniRootPath.c_str(); 
	Ret = GetPrivateProfileString("BOOT", Key , NULL , Buf , sizeof(Buf) , Path );
	if( Ret == 0 )
		return iDefaultVal;

	else
		return atoi( Buf );
}

int	Global::GetDirVersionID()
{
	static int	DirVersionID = 0;
	static bool	bSet		= false;

	if( bSet == false )
	{
		bSet = true;
		DirVersionID = Global::GetIniValue( "DirVersionID", 1 );
	}
	return DirVersionID;
}

int	Global::GetStringDirTagID()
{
	static int	StringDirTagID = 0;
	static bool	bSet		= false;

	if( bSet == false )
	{
		bSet = true;
		StringDirTagID = Global::GetIniValue( "StringDirTagID", 1 );
	}
	return StringDirTagID;
}

int	Global::GetDirTagID()
{
	static int	DirTagID	= 0;
	static bool bSet		= false;

	if( bSet == false )
	{
		bSet = true;
		DirTagID = Global::GetIniValue( "DirTagID", 1 );
	}

	return DirTagID;
}


char*	Global::GetDBSource( )
{
	static string DBSourceInfo;
	if( DBSourceInfo.size() != 0 )
		return (char*)DBSourceInfo.c_str();


	DBSource = Global::GetIniString( "DBSource" );
	DataBase = Global::GetIniString( "Database" );

	if( DBSource.empty() == true )
		DBSource = "GS1";

	if( DataBase.empty() == true )
		DataBase = "ObjectData";

	char szConnectStr[1024];
	sprintf( szConnectStr, "workstation id=HSIANG;packet size=4096;user id=hsiangren;persist security info=True;initial catalog=%s;password=2ddilxxl;data source=%s", DataBase.c_str(), DBSource.c_str() );

	DBSourceInfo = szConnectStr;
	//DBSourceInfo = "Data Source=db1;Initial Catalog=ObjectData;Persist Security Info=True;User ID=sa;Password=rw20040813";
	
	return (char*)DBSourceInfo.c_str();
}

char*	Global::GetGlobalDBSource( )
{
	static string DBSourceInfo;
	if( DBSourceInfo.size() != 0 )
		return (char*)DBSourceInfo.c_str();


	char* DBSource = Global::GetIniString( "DBSource" );

	if( strlen( DBSource ) == 0 )
		DBSource = "GS1";

	DBSourceInfo = "workstation id=HSIANG;packet size=4096;user id=hsiangren;persist security info=True;initial catalog=GlobalDB;password=2ddilxxl;data source=";
	DBSourceInfo = DBSourceInfo + DBSource;

	return (char*)DBSourceInfo.c_str();
}


int		Global::LanguageSetting( )
{
	int LanguageSetting = atol( Global::GetIniString( "LanguageSetting" ) );
	if( LanguageSetting == 0 )
		LanguageSetting = -1;
	return LanguageSetting;
}

//編碼
char* Global::EncodePassword		( const char* Password , const char* Key )
{
	return ::EncodePassword( Password , Key );
}
//解碼
char* Global::DecodePassword		( const char* Password , const char* Key )
{
	return ::DecodePassword( Password , Key );
}
//MD5編碼
char* Global::MD5EncodePassword	( const char* Password , const char* Key )
{
	static MyMD5Class Md5;
	return (char*)Md5.ComputeStringHash( Password , Key );
}