2016-01-12 03:41:44 +00:00
|
|
|
#include "TimerAPI.hpp"
|
|
|
|
|
|
|
|
#include <chrono>
|
2019-01-16 15:52:22 +00:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstring>
|
2016-01-12 03:41:44 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
using namespace mwmp;
|
|
|
|
using namespace std;
|
|
|
|
|
2019-01-16 15:52:22 +00:00
|
|
|
static int timerId = -1;
|
|
|
|
|
|
|
|
Timer::Timer(ScriptFunc callback, long msec, const std::string& def, va_list args)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2019-01-16 15:52:22 +00:00
|
|
|
this->def = strdup(def.c_str());
|
|
|
|
setRetType('v');
|
|
|
|
setFunctionPtr(callback);
|
|
|
|
setArguments(def, args);
|
2019-01-15 10:45:31 +00:00
|
|
|
startTime = 0;
|
2016-01-12 03:41:44 +00:00
|
|
|
targetMsec = msec;
|
2018-12-30 02:15:53 +00:00
|
|
|
isEnded = true;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 15:52:22 +00:00
|
|
|
Timer::~Timer()
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2019-01-16 15:52:22 +00:00
|
|
|
free(def);
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 15:52:22 +00:00
|
|
|
void Timer::Tick(int timerid)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2018-12-30 02:15:53 +00:00
|
|
|
if (isEnded)
|
2016-01-12 03:41:44 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
const auto duration = chrono::system_clock::now().time_since_epoch();
|
|
|
|
const auto time = chrono::duration_cast<chrono::milliseconds>(duration).count();
|
|
|
|
|
|
|
|
if (time - startTime >= targetMsec)
|
|
|
|
{
|
2018-12-30 02:15:53 +00:00
|
|
|
isEnded = true;
|
2019-01-16 15:52:22 +00:00
|
|
|
timerId = timerid;
|
|
|
|
call();
|
|
|
|
timerId = -1;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 02:15:53 +00:00
|
|
|
bool Timer::IsEnded()
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2018-12-30 02:15:53 +00:00
|
|
|
return isEnded;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::Stop()
|
|
|
|
{
|
2018-12-30 02:15:53 +00:00
|
|
|
isEnded = true;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::Restart(int msec)
|
|
|
|
{
|
|
|
|
targetMsec = msec;
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::Start()
|
|
|
|
{
|
2018-12-30 02:15:53 +00:00
|
|
|
isEnded = false;
|
2016-01-12 03:41:44 +00:00
|
|
|
|
|
|
|
const auto duration = chrono::system_clock::now().time_since_epoch();
|
|
|
|
const auto msec = chrono::duration_cast<chrono::milliseconds>(duration).count();
|
|
|
|
startTime = msec;
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:52:22 +00:00
|
|
|
const char *Timer::GetDefinition()
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2019-01-16 15:52:22 +00:00
|
|
|
return def;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
2019-01-16 15:52:22 +00:00
|
|
|
|
|
|
|
int TimerAPI::pointer = 0;
|
|
|
|
std::unordered_map<int, Timer* > TimerAPI::timers;
|
2016-01-12 03:41:44 +00:00
|
|
|
|
|
|
|
|
2019-01-16 15:52:22 +00:00
|
|
|
int TimerAPI::CreateTimer(ScriptFunc callback, long msec, const std::string &def, va_list args)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
int id = -1;
|
|
|
|
|
|
|
|
for (auto timer : timers)
|
|
|
|
{
|
|
|
|
if (timer.second != nullptr)
|
|
|
|
continue;
|
|
|
|
timer.second = new Timer(callback, msec, def, args);
|
|
|
|
id = timer.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == -1)
|
|
|
|
{
|
|
|
|
timers[pointer] = new Timer(callback, msec, def, args);
|
|
|
|
id = pointer;
|
|
|
|
pointer++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2016-11-16 17:27:46 +00:00
|
|
|
void TimerAPI::FreeTimer(int timerid)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-08-17 15:04:35 +00:00
|
|
|
if (timers.at(timerid) != nullptr)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
delete timers[timerid];
|
|
|
|
timers[timerid] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
std::cerr << "Timer " << timerid << " not found!" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 17:27:46 +00:00
|
|
|
void TimerAPI::ResetTimer(int timerid, long msec)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
timers.at(timerid)->Restart(msec);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
std::cerr << "Timer " << timerid << " not found!" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 17:27:46 +00:00
|
|
|
void TimerAPI::StartTimer(int timerid)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Timer *timer = timers.at(timerid);
|
2016-08-17 15:04:35 +00:00
|
|
|
if (timer == nullptr)
|
2016-01-12 03:41:44 +00:00
|
|
|
throw 1;
|
|
|
|
timer->Start();
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
std::cerr << "Timer " << timerid << " not found!" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 17:27:46 +00:00
|
|
|
void TimerAPI::StopTimer(int timerid)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
timers.at(timerid)->Stop();
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
std::cerr << "Timer " << timerid << " not found!" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 02:15:53 +00:00
|
|
|
bool TimerAPI::IsTimerElapsed(int timerid)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
try
|
|
|
|
{
|
2018-12-30 02:15:53 +00:00
|
|
|
ret = timers.at(timerid)->IsEnded();
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
std::cerr << "Timer " << timerid << " not found!" << endl;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:52:22 +00:00
|
|
|
int TimerAPI::GetTimerId()
|
|
|
|
{
|
|
|
|
return timerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *TimerAPI::GetDefinition(int timerid)
|
|
|
|
{
|
|
|
|
const char *ret = nullptr;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ret = timers.at(timerid)->GetDefinition();
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
std::cerr << "Timer " << timerid << " not found!" << endl;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-12 03:41:44 +00:00
|
|
|
void TimerAPI::Terminate()
|
|
|
|
{
|
2016-08-17 15:20:36 +00:00
|
|
|
for (auto timer : timers)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-08-17 15:04:35 +00:00
|
|
|
if (timer.second != nullptr)
|
2016-01-12 03:41:44 +00:00
|
|
|
delete timer.second;
|
|
|
|
timer.second = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimerAPI::Tick()
|
|
|
|
{
|
|
|
|
for (auto timer : timers)
|
|
|
|
{
|
2016-08-17 15:04:35 +00:00
|
|
|
if (timer.second != nullptr)
|
2019-01-16 15:52:22 +00:00
|
|
|
timer.second->Tick(timer.first);
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
2017-01-26 04:17:29 +00:00
|
|
|
}
|