From af4a59c071ca71ab95c496cfa8046cefa771df49 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 26 Aug 2007 18:41:57 +0000 Subject: [PATCH] added amb727 (sm_time_adjustment) --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401377 --- configs/cfg/sourcemod.cfg | 7 +++++++ core/Logger.cpp | 9 +++++---- core/TimerSys.cpp | 14 ++++++++++++++ core/TimerSys.h | 2 ++ core/smn_core.cpp | 5 +++-- core/sourcemod.cpp | 5 +++++ core/sourcemod.h | 1 + public/ISourceMod.h | 7 +++++++ 8 files changed, 44 insertions(+), 6 deletions(-) diff --git a/configs/cfg/sourcemod.cfg b/configs/cfg/sourcemod.cfg index ba55b366..a24edc86 100644 --- a/configs/cfg/sourcemod.cfg +++ b/configs/cfg/sourcemod.cfg @@ -33,6 +33,13 @@ sm_vote_delay 30 // 12 hour format: %m/%d/%Y - %I:%M:%S %p sm_datetime_format "%m/%d/%Y - %H:%M:%S" +// Sets how many seconds SourceMod should adjust time values for incorrect +// server clocks. This can be positive or negative and will affect every +// system time in SourceMod, including logging stamps. +// -- +// Default: 0 +sm_time_adjustment 0 + // Specifies the amount of time that is allowed between chat messages. This // includes the say and say_team commands. If a client sends a message faster // than this time, they receive a flood token. When the client has accumulated diff --git a/core/Logger.cpp b/core/Logger.cpp index 074daaba..d1b16f3d 100644 --- a/core/Logger.cpp +++ b/core/Logger.cpp @@ -35,6 +35,7 @@ #include "sm_stringutil.h" #include "Logger.h" #include "systems/LibrarySys.h" +#include "TimerSys.h" #include "sm_version.h" Logger g_Logger; @@ -119,7 +120,7 @@ void Logger::_NewMapFile() int i = 0; time_t t; - time(&t); + GetAdjustedTime(&t); tm *curtime = localtime(&t); while (true) @@ -189,7 +190,7 @@ void Logger::InitLogger(LoggingMode mode) m_Active = m_InitialState; time_t t; - time(&t); + GetAdjustedTime(&t); tm *curtime = localtime(&t); m_CurDay = curtime->tm_mday; @@ -257,7 +258,7 @@ void Logger::LogMessage(const char *vafmt, ...) char date[32]; time_t t; - time(&t); + GetAdjustedTime(&t); tm *curtime = localtime(&t); strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime); @@ -314,7 +315,7 @@ void Logger::LogError(const char *vafmt, ...) } time_t t; - time(&t); + GetAdjustedTime(&t); tm *curtime = localtime(&t); char date[32]; diff --git a/core/TimerSys.cpp b/core/TimerSys.cpp index 35454a3a..bbe7d4d0 100644 --- a/core/TimerSys.cpp +++ b/core/TimerSys.cpp @@ -29,11 +29,25 @@ * Version: $Id$ */ +#include #include "TimerSys.h" +#include "sourcemm_api.h" TimerSystem g_Timers; TickInfo g_SimTicks; +ConVar sm_time_adjustment("sm_time_adjustment", "0", 0, "Adjusts the server time in seconds"); + +time_t GetAdjustedTime(time_t *buf) +{ + time_t val = time(NULL) + sm_time_adjustment.GetInt(); + if (buf) + { + *buf = val; + } + return val; +} + inline float GetSimulatedTime() { if (g_SimTicks.ticking) diff --git a/core/TimerSys.h b/core/TimerSys.h index 2ffcb705..41bf9527 100644 --- a/core/TimerSys.h +++ b/core/TimerSys.h @@ -87,6 +87,8 @@ private: float m_LastExecTime; }; +time_t GetAdjustedTime(time_t *buf = NULL); + extern TimerSystem g_Timers; extern TickInfo g_SimTicks; diff --git a/core/smn_core.cpp b/core/smn_core.cpp index 440a0de5..ad988d6d 100644 --- a/core/smn_core.cpp +++ b/core/smn_core.cpp @@ -38,6 +38,7 @@ #include "PluginSys.h" #include "HandleSys.h" #include "LibrarySys.h" +#include "TimerSys.h" #if defined PLATFORM_WINDOWS #include @@ -93,7 +94,7 @@ static cell_t ThrowError(IPluginContext *pContext, const cell_t *params) static cell_t GetTime(IPluginContext *pContext, const cell_t *params) { - time_t t = time(NULL); + time_t t = GetAdjustedTime(); cell_t *addr; pContext->LocalToPhysAddr(params[1], &addr); @@ -130,7 +131,7 @@ static cell_t FormatTime(IPluginContext *pContext, const cell_t *params) _invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter); #endif - time_t t = (params[4] == -1) ? time(NULL) : (time_t)params[4]; + time_t t = (params[4] == -1) ? GetAdjustedTime() : (time_t)params[4]; size_t written = strftime(buffer, params[2], format, localtime(&t)); #if defined SUBPLATFORM_SECURECRT diff --git a/core/sourcemod.cpp b/core/sourcemod.cpp index 2c65dd39..5ef27d6c 100644 --- a/core/sourcemod.cpp +++ b/core/sourcemod.cpp @@ -756,6 +756,11 @@ void SourceModBase::AllPluginsLoaded() } } +time_t SourceModBase::GetAdjustedTime() +{ + return GetAdjustedTime(); +} + SMGlobalClass *SMGlobalClass::head = NULL; SMGlobalClass::SMGlobalClass() diff --git a/core/sourcemod.h b/core/sourcemod.h index f8698abe..f00e7068 100644 --- a/core/sourcemod.h +++ b/core/sourcemod.h @@ -115,6 +115,7 @@ public: // ISourceMod ISourcePawnEngine *GetScriptingEngine(); IVirtualMachine *GetScriptingVM(); void AllPluginsLoaded(); + time_t GetAdjustedTime(); private: /** * @brief Loading plugins diff --git a/public/ISourceMod.h b/public/ISourceMod.h index 61e8e347..38cabf0c 100644 --- a/public/ISourceMod.h +++ b/public/ISourceMod.h @@ -191,6 +191,13 @@ namespace SourceMod * @return A pointer to the JIT interface. */ virtual SourcePawn::IVirtualMachine *GetScriptingVM() =0; + + /** + * @brief Returns the adjusted server time. + * + * @return Adjusted server time. + */ + virtual time_t GetAdjustedTime() =0; }; }