Advertisement
Mellnik

mtime Plugin by Mellnik

Nov 24th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2012 Mellnik
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. #if defined(_WIN32)
  18. #pragma warning(disable:4996)
  19. #define formats sprintf_s
  20. #else
  21. #define formats sprintf
  22. #endif
  23.  
  24. #include "../SDK/plugin.h"
  25. #include "../SDK/amx/amx.h"
  26. #include <stdio.h>
  27. #include <time.h>
  28.  
  29. typedef void (*logprintf_t)(char* format, ...);
  30. logprintf_t logprintf;
  31. void **ppPluginData;
  32. extern void *pAMXFunctions;
  33. clock_t startup_time;
  34.  
  35. PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData)
  36. {
  37.    pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
  38.    logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];
  39.  
  40.    startup_time = clock();
  41.  
  42.    logprintf("** mtime v1.0 loaded");
  43.    return 1;
  44. }
  45.  
  46. PLUGIN_EXPORT void PLUGIN_CALL Unload()
  47. {
  48.     logprintf("** mtime v1.0 unloaded");
  49. }
  50.  
  51. static cell AMX_NATIVE_CALL _getlocaltime(AMX *amx, cell *params)
  52. {  
  53.     if(params[2] < 40)
  54.     {
  55.         logprintf("[mtime] string is too small (localtime)");
  56.         return 0;
  57.     }
  58.  
  59.     time_t unixt = time(NULL);
  60.     struct tm *tm = localtime(&unixt);
  61.     char date[25];
  62.     char clock[15];
  63.     char string[sizeof(date) + sizeof(clock)];
  64.  
  65.     cell *addr;
  66.  
  67.     switch(params[3])
  68.     {
  69.     case 0 :
  70.         strftime(date, sizeof(date), "%d.%m.%Y", tm);
  71.         break;
  72.     case 1 :
  73.         strftime(date, sizeof(date), "%Y.%m.%d", tm);
  74.         break;
  75.     case 2 :
  76.         strftime(date, sizeof(date), "%m.%d.%Y", tm);
  77.         break;
  78.     default :
  79.         logprintf("[mtime] invalid time format (localtime)");
  80.         return 0;
  81.     }
  82.  
  83.     switch(params[4])
  84.     {
  85.     case 0 :
  86.         strftime(clock, sizeof(clock), "%H:%M:%S", tm);
  87.         break;
  88.     case 1 :
  89.         strftime(clock, sizeof(clock), "%I:%M:%S %p", tm);
  90.         break;
  91.     default :
  92.         logprintf("[mtime] invalid time format (localtime)");
  93.         return 0;
  94.     }
  95.  
  96.     formats(string, "%s - %s", date, clock);
  97.  
  98.     amx_GetAddr(amx, params[1], &addr);
  99.     amx_SetString(addr, string, 0, 0, sizeof(string));
  100.     return 1;
  101. }
  102.  
  103. static cell AMX_NATIVE_CALL _svruptime(AMX *amx, cell *params)
  104. {  
  105.     switch(params[1])
  106.     {
  107.     case 0 : return (static_cast<int>(clock() - startup_time));
  108.     case 1 : return (static_cast<int>(clock() - startup_time) / 1000);
  109.     case 2 : return (static_cast<int>(clock() - startup_time) / 60000);
  110.     default :
  111.         logprintf("[mtime] invalid time format (unixtodate)");
  112.         break;
  113.     }
  114.     return 0;
  115. }
  116.  
  117. static cell AMX_NATIVE_CALL _unixtodate(AMX *amx, cell *params)
  118. {
  119.     if(params[2] < 40)
  120.     {
  121.         logprintf("[mtime] string is too small (unixtodate)");
  122.         return 0;
  123.     }
  124.     // 2100000000
  125.     time_t unixt = params[3];
  126.     struct tm *tm = localtime(&unixt);
  127.  
  128.     char date[25];
  129.     char clock[15];
  130.     char string[sizeof(clock) + sizeof(date)];
  131.     cell *addr;
  132.  
  133.     switch(params[4])
  134.     {
  135.     case 0 :
  136.         strftime(date, sizeof(date), "%d.%m.%Y", tm);
  137.         break;
  138.     case 1 :
  139.         strftime(date, sizeof(date), "%Y.%m.%d", tm);
  140.         break;
  141.     case 2 :
  142.         strftime(date, sizeof(date), "%m.%d.%Y", tm);
  143.         break;
  144.     default :
  145.         logprintf("[mtime] invalid time format (unixtodate)");
  146.         return 0;
  147.     }
  148.  
  149.     switch(params[5])
  150.     {
  151.     case 0 :
  152.         strftime(clock, sizeof(clock), "%H:%M:%S", tm);
  153.         break;
  154.     case 1 :
  155.         strftime(clock, sizeof(clock), "%I:%M:%S %p", tm);
  156.         break;
  157.     default :
  158.         logprintf("[mtime] invalid time format (unixtodate)");
  159.         return 0;
  160.     }
  161.  
  162.     formats(string, "%s - %s", date, clock);
  163.  
  164.     amx_GetAddr(amx, params[1], &addr);
  165.     amx_SetString(addr, string, 0, 0, sizeof(string));
  166.     return 1;
  167. }
  168.  
  169. static cell AMX_NATIVE_CALL _timeconvert(AMX *amx, cell *params)
  170. {  
  171.     if(params[2] < 20)
  172.     {
  173.         logprintf("[mtime] string is too small (regular time)");
  174.         return 0;
  175.     }
  176.  
  177.     int msecs = params[3];
  178.  
  179.     int hrs = msecs / (1000 * 60 * 60);
  180.     int mins = (msecs % (1000 * 60 * 60)) / (1000 * 60);
  181.     int secs = ((msecs % (1000 * 60 * 60)) % (1000 * 60)) / 1000;
  182.     msecs = msecs - (hrs * 60 * 60 * 1000) - (mins * 60 * 1000) - (secs * 1000);
  183.  
  184.     char format[20];
  185.  
  186.     if(params[4])
  187.     {
  188.         formats(format, "%02i:%02i:%02i.%03i", hrs, mins, secs, msecs);
  189.     }
  190.     else formats(format, "%02i:%02i.%03i", mins, secs, msecs);
  191.  
  192.     cell *addr;
  193.  
  194.     amx_GetAddr(amx, params[1], &addr);
  195.     amx_SetString(addr, format, 0, 0, 20);
  196.     return 1;
  197. }
  198.  
  199. AMX_NATIVE_INFO mtime_natives[] =
  200. {
  201.     {"mtime_GetServerUptime",   _svruptime},
  202.     {"mtime_UnixToDate",        _unixtodate},
  203.     {"mtime_ToRegularTime",     _timeconvert},
  204.     {"mtime_GetLocalTime",      _getlocaltime},
  205.     {0, 0}
  206. };
  207.  
  208. PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports()
  209. {
  210.    return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
  211. }
  212.  
  213. PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX *amx)
  214. {
  215.    return amx_Register(amx, mtime_natives, -1);
  216. }
  217.  
  218. PLUGIN_EXPORT int PLUGIN_CALL AmxUnload(AMX *amx)
  219. {
  220.    return AMX_ERR_NONE;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement