#include <windows.h>
#include <string>
#include <assert.h>
#include "CSAMPFUNCS.h"
#include <time.h>
#include <stdio.h>
CSAMPFUNCS *SF = new CSAMPFUNCS();
stFontInfo *pFont = nullptr;
int coordX;
int coordY;
int textColor;
int fontFlags;
int fontSize;
bool rState = true;
std::string fontName;
bool ini_file_exist(std::string ini_file_name)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFindIni;
hFindIni = FindFirstFile(ini_file_name.c_str(), &FindFileData);
if (hFindIni != INVALID_HANDLE_VALUE)
{
return true;
}
return false;
}
std::string get_path(std::string &path, std::string &ini_file_name)
{
char ppath[MAX_PATH];
GetCurrentDirectory(MAX_PATH, ppath);
strcat(ppath, "\\");
path = ppath + ini_file_name;
return path;
}
bool create_ini_file(std::string new_ini_file_name)
{
if (!ini_file_exist(new_ini_file_name))
{
FILE *fp = fopen(new_ini_file_name.c_str(), "w");
if (fp != NULL)
fclose(fp);
return true;
}
return false;
}
bool delete_ini_file(std::string ini_file_name)
{
if (!ini_file_exist(ini_file_name)) return true;
return remove(ini_file_name.c_str());
}
int get_int_from_ini(std::string ini_file_name, std::string section, std::string key)
{
if (!ini_file_exist(ini_file_name)) return 0x80000000;
std::string ini_path;
get_path(ini_path, ini_file_name);
return GetPrivateProfileInt(section.c_str(), key.c_str(), 0, ini_path.c_str());
}
float get_float_from_ini(std::string ini_file_name, std::string section, std::string key)
{
if (!ini_file_exist(ini_file_name)) return 0.0f;
char str_value[100];
std::string ini_path;
get_path(ini_path, ini_file_name);
if (GetPrivateProfileString(section.c_str(), key.c_str(), NULL, str_value, sizeof(str_value), ini_path.c_str()))
return std::stof(str_value);
return 0.0f;
}
std::string get_string_from_ini(std::string ini_file_name, std::string section, std::string key)
{
if (!ini_file_exist(ini_file_name)) return " ";
char buff[1024];
std::string ini_path;
get_path(ini_path, ini_file_name);
if (GetPrivateProfileString(section.c_str(), key.c_str(), NULL, buff, sizeof(buff), ini_path.c_str()))
return buff;
return " ";
}
void set_int_to_ini(std::string ini_file_name, std::string section, std::string key, DWORD value)
{
if (!ini_file_exist(ini_file_name)) return;
std::string ini_path;
std::string str_value = std::to_string(value);
get_path(ini_path, ini_file_name);
WritePrivateProfileString(section.c_str(), key.c_str(), str_value.c_str(), ini_path.c_str());
}
void set_float_to_ini(std::string ini_file_name, std::string section, std::string key, float value)
{
if (!ini_file_exist(ini_file_name)) return;
std::string ini_path;
std::string str_value = std::to_string(value);
get_path(ini_path, ini_file_name);
WritePrivateProfileString(section.c_str(), key.c_str(), str_value.c_str(), ini_path.c_str());
}
void set_string_to_ini(std::string ini_file_name, std::string section, std::string key, std::string text)
{
if (!ini_file_exist(ini_file_name)) return;
std::string ini_path;
get_path(ini_path, ini_file_name);
WritePrivateProfileString(section.c_str(), key.c_str(), text.c_str(), ini_path.c_str());
}
void _stdcall cmd_datetime_set(std::string param)
{
if (param.empty())
{
SF->getSAMP()->getChat()->AddChatMessage(0xFFCC00, "[BLASTHACK]: {EAEAEA}Допустимые параметры: {FFCC00}color | posx | posy | font | flags | size" );
return;
};
if (param.find("color") != std::string::npos)
{
param.erase(0, 6);
sscanf(param.c_str(), "%x", &textColor);
set_int_to_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "COLOR", textColor);
};
if (param.find("posx") != std::string::npos)
{
param.erase(0, 5);
sscanf(param.c_str(), "%d", &coordX);
set_int_to_ini("SAMPFUNCS\\Date&Time.ini", "POSITION", "POSX", coordX);
};
if (param.find("posy") != std::string::npos)
{
param.erase(0, 5);
sscanf(param.c_str(), "%d", &coordY);
set_int_to_ini("SAMPFUNCS\\Date&Time.ini", "POSITION", "POSY", coordY);
};
if (param.find("font") != std::string::npos)
{
param.erase(0, 5);
fontName = param;
set_string_to_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "NAME", fontName.c_str());
SF->getRender()->ReleaseFont(pFont);
int fontFlags = get_int_from_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "FLAGS");
char fontNameBuf[64];
sprintf(fontNameBuf, "%s", fontName.c_str());
pFont = SF->getRender()->CreateNewFont(fontNameBuf, 8, fontFlags);
};
if (param.find("flags") != std::string::npos)
{
param.erase(0, 6);
sscanf(param.c_str(), "%d", &fontFlags);
set_int_to_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "FLAGS", fontFlags);
SF->getRender()->ReleaseFont(pFont);
std::string fontName = get_string_from_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "NAME");
int fontSize = get_int_from_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "Size");
int fontFlags = get_int_from_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "FLAGS");
char fontNameBuf[64];
sprintf(fontNameBuf, "%s", fontName.c_str());
pFont = SF->getRender()->CreateNewFont(fontNameBuf, 8, fontFlags);
};
if (param.find("size") != std::string::npos)
{
param.erase(0, 5);
sscanf(param.c_str(), "%d", &fontSize);
set_int_to_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "SIZE", fontSize);
SF->getRender()->ReleaseFont(pFont);
std::string fontName = get_string_from_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "NAME");
int fontSize = get_int_from_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "Size");
int fontFlags = get_int_from_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "FLAGS");
char fontNameBuf[64];
sprintf(fontNameBuf, "%s", fontName.c_str());
pFont = SF->getRender()->CreateNewFont(fontNameBuf, fontSize, fontFlags);
};
//iState ^= true;
};
bool CALLBACK Present(CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride,
CONST RGNDATA *pDirtyRegion)
{
if (SUCCEEDED(SF->getRender()->BeginRender()) && rState == true) // если девайс готов к рисованию
{
// рисуем
SYSTEMTIME time;
textColor = get_int_from_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "COLOR");
coordX = get_int_from_ini("SAMPFUNCS\\Date&Time.ini", "POSITION", "POSX");
coordY = get_int_from_ini("SAMPFUNCS\\Date&Time.ini", "POSITION", "POSY");
GetLocalTime(&time);
char buf[128];
sprintf(buf, "[%d.%d.%d %.2d:%.2d:%.2d]", time.wDay, time.wMonth, time.wYear, time.wHour, time.wMinute, time.wSecond);
pFont->Print(buf, textColor, coordX, coordY, false);
SF->getRender()->EndRender(); // завершаем рисование
};
return true; // возвращаем положительный результат
};
void CALLBACK mainloop( void )
{
static bool init = false;
if( !init )
{
if( !SF->getSAMP()->IsInitialized() )
return;
SF->getSAMP()->getChat()->AddChatMessage(0xFFCC00, "[BLASTHACK] {EAEAEA}Date&Time plugin loaded // by hnnssy.");
SF->getSAMP()->registerChatCommand("dtset", cmd_datetime_set);
bool configExist = ini_file_exist("SAMPFUNCS\\Date&Time.ini");
if (configExist == 0)
{
create_ini_file("SAMPFUNCS\\Date&Time.ini");
set_string_to_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "NAME", "Comic Sans MS");
set_int_to_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "FLAGS", 5);
set_int_to_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "COLOR", 0xFFFFCC00);
set_int_to_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "SIZE", 8);
//SF->getGame()->convertGameCoordsToWindow(20.0, 160.0, &coordX, &coordY);
set_int_to_ini("SAMPFUNCS\\Date&Time.ini", "POSITION", "POSX", 0);
set_int_to_ini("SAMPFUNCS\\Date&Time.ini", "POSITION", "POSY", 0);
}
std::string fontName = get_string_from_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "NAME");
int fontFlags = get_int_from_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "FLAGS");
int fontSize = get_int_from_ini("SAMPFUNCS\\Date&Time.ini", "FONT", "SIZE");
char fontNameBuf[64];
sprintf(fontNameBuf, "%s", fontName.c_str());
pFont = SF->getRender()->CreateNewFont(fontNameBuf, fontSize, fontFlags);
SF->getRender()->registerD3DCallback(eDirect3DDeviceMethods::D3DMETHOD_PRESENT, Present);
init = true;
}
}
BOOL APIENTRY DllMain( HMODULE hModule, DWORD dwReasonForCall, LPVOID lpReserved )
{
switch ( dwReasonForCall )
{
case DLL_PROCESS_ATTACH:
SF->initPlugin( mainloop, hModule );
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}