Skip to content

Commit

Permalink
Partially sync engine code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaffeine committed Jul 9, 2024
1 parent 649e2e5 commit 5aca628
Show file tree
Hide file tree
Showing 22 changed files with 739 additions and 550 deletions.
18 changes: 18 additions & 0 deletions src/base/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <iomanip> // std::get_time
#include <iterator> // std::size
#include <sstream> // std::istringstream
#include <string_view>

#include "lock.h"
Expand Down Expand Up @@ -3429,6 +3431,22 @@ void str_timestamp(char *buffer, int buffer_size)
{
str_timestamp_format(buffer, buffer_size, FORMAT_NOSPACE);
}

bool timestamp_from_str(const char *string, const char *format, time_t *timestamp)
{
std::tm tm{};
std::istringstream ss(string);
ss >> std::get_time(&tm, format);
if(ss.fail() || !ss.eof())
return false;

time_t result = mktime(&tm);
if(result < 0)
return false;

*timestamp = result;
return true;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
Expand Down
15 changes: 15 additions & 0 deletions src/base/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,21 @@ void str_timestamp_format(char *buffer, int buffer_size, const char *format)
void str_timestamp_ex(time_t time, char *buffer, int buffer_size, const char *format)
GNUC_ATTRIBUTE((format(strftime, 4, 0)));

/**
* Parses a string into a timestamp following a specified format.
*
* @ingroup Timestamp
*
* @param string Pointer to the string to parse
* @param format The time format to use (for example FORMAT_NOSPACE below)
* @param timestamp Pointer to the timestamp result
*
* @return true on success, false if the string could not be parsed with the specified format
*
*/
bool timestamp_from_str(const char *string, const char *format, time_t *timestamp)
GNUC_ATTRIBUTE((format(strftime, 2, 0)));

#define FORMAT_TIME "%H:%M:%S"
#define FORMAT_SPACE "%Y-%m-%d %H:%M:%S"
#define FORMAT_NOSPACE "%Y-%m-%d_%H-%M-%S"
Expand Down
26 changes: 5 additions & 21 deletions src/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,24 @@
#define ENGINE_ENGINE_H

#include "kernel.h"
#include <engine/shared/jobs.h>

#include <memory>

class CFutureLogger;
class IJob;
class ILogger;

class CHostLookup : public IJob
{
private:
void Run() override;

public:
CHostLookup();
CHostLookup(const char *pHostname, int Nettype);

int m_Result;
char m_aHostname[128];
int m_Nettype;
NETADDR m_Addr;
};

class IEngine : public IInterface
{
MACRO_INTERFACE("engine")

protected:
class CJobPool m_JobPool;

public:
virtual ~IEngine() = default;

virtual void Init() = 0;
virtual void AddJob(std::shared_ptr<IJob> pJob) = 0;
virtual void SetAdditionalLogger(std::unique_ptr<ILogger> &&pLogger) = 0;
static void RunJobBlocking(IJob *pJob);
virtual void ShutdownJobs() = 0;
virtual void SetAdditionalLogger(std::shared_ptr<ILogger> &&pLogger) = 0;
};

extern IEngine *CreateEngine(const char *pAppname, std::shared_ptr<CFutureLogger> pFutureLogger, int Jobs);
Expand Down
16 changes: 7 additions & 9 deletions src/engine/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#ifndef ENGINE_KERNEL_H
#define ENGINE_KERNEL_H

#include <base/system.h>

class IKernel;
class IInterface;

Expand All @@ -30,13 +28,13 @@ public: \
\
private:

// This kernel thingie makes the structure very flat and basiclly singletons.
// This kernel thingie makes the structure very flat and basically singletons.
// I'm not sure if this is a good idea but it works for now.
class IKernel
{
// hide the implementation
virtual bool RegisterInterfaceImpl(const char *pInterfaceName, IInterface *pInterface, bool Destroy) = 0;
virtual bool ReregisterInterfaceImpl(const char *pInterfaceName, IInterface *pInterface) = 0;
virtual void RegisterInterfaceImpl(const char *pInterfaceName, IInterface *pInterface, bool Destroy) = 0;
virtual void ReregisterInterfaceImpl(const char *pInterfaceName, IInterface *pInterface) = 0;
virtual IInterface *RequestInterfaceImpl(const char *pInterfaceName) = 0;

public:
Expand All @@ -46,14 +44,14 @@ class IKernel

// templated access to handle pointer conversions and interface names
template<class TINTERFACE>
bool RegisterInterface(TINTERFACE *pInterface, bool Destroy = true)
void RegisterInterface(TINTERFACE *pInterface, bool Destroy = true)
{
return RegisterInterfaceImpl(TINTERFACE::InterfaceName(), pInterface, Destroy);
RegisterInterfaceImpl(TINTERFACE::InterfaceName(), pInterface, Destroy);
}
template<class TINTERFACE>
bool ReregisterInterface(TINTERFACE *pInterface)
void ReregisterInterface(TINTERFACE *pInterface)
{
return ReregisterInterfaceImpl(TINTERFACE::InterfaceName(), pInterface);
ReregisterInterfaceImpl(TINTERFACE::InterfaceName(), pInterface);
}

// Usage example:
Expand Down
2 changes: 2 additions & 0 deletions src/engine/server/roundstatistics.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "roundstatistics.h"

#include <base/system.h>

#include <game/infclass/classes.h>

int CRoundStatistics::CPlayerStats::OnScoreEvent(int EventType, EPlayerClass Class)
Expand Down
18 changes: 9 additions & 9 deletions src/engine/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4068,18 +4068,18 @@ const char *CServer::GetAnnouncementLine(char const *pFileName)
str_copy(m_aAnnouncementFile, pFileName);
m_vAnnouncements.clear();

IOHANDLE File = m_pStorage->OpenFile(pFileName, IOFLAG_READ | IOFLAG_SKIP_BOM, IStorage::TYPE_ALL);
if(!File)
CLineReader LineReader;
if(!LineReader.OpenFile(m_pStorage->OpenFile(pFileName, IOFLAG_READ, IStorage::TYPE_ALL)))
{
return 0;

char *pLine;
CLineReader Reader;
Reader.Init(File);
while((pLine = Reader.Get()))
}
while(const char *pLine = LineReader.Get())
{
if(str_length(pLine) && pLine[0] != '#')
{
m_vAnnouncements.emplace_back(pLine);

io_close(File);
}
}
}

if(m_vAnnouncements.empty())
Expand Down
7 changes: 5 additions & 2 deletions src/engine/shared/config.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include <engine/config.h>
#include <engine/shared/config.h>

#include <base/system.h>

#include <engine/shared/protocol.h>
#include <engine/storage.h>

#include "config.h"

CConfig g_Config;

void EscapeParam(char *pDst, const char *pSrc, int Size)
Expand Down
2 changes: 2 additions & 0 deletions src/engine/shared/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#define ENGINE_SHARED_CONFIG_H

#include <base/detect.h>

#include <engine/config.h>
#include <engine/storage.h>

#define CONFIG_FILE "settings_ddnet.cfg"
#define AUTOEXEC_FILE "autoexec.cfg"
Expand Down
18 changes: 8 additions & 10 deletions src/engine/shared/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,22 +627,20 @@ void CConsole::ExecuteFile(const char *pFilename, int ClientID, bool LogFailure,
m_pFirstExec = &ThisFile;

// exec the file
IOHANDLE File = m_pStorage->OpenFile(pFilename, IOFLAG_READ | IOFLAG_SKIP_BOM, StorageType);

char aBuf[128];
if(File)
CLineReader LineReader;
bool Success = false;
char aBuf[32 + IO_MAX_PATH_LENGTH];
if(LineReader.OpenFile(m_pStorage->OpenFile(pFilename, IOFLAG_READ, StorageType)))
{
char *pLine;
CLineReader Reader;

str_format(aBuf, sizeof(aBuf), "executing '%s'", pFilename);
Print(IConsole::OUTPUT_LEVEL_STANDARD, "console", aBuf);
Reader.Init(File);

while((pLine = Reader.Get()))
while(const char *pLine = LineReader.Get())
{
ExecuteLine(pLine, ClientID);
}

io_close(File);
Success = true;
}
else if(LogFailure)
{
Expand Down
43 changes: 15 additions & 28 deletions src/engine/shared/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,22 @@
#include <engine/console.h>
#include <engine/engine.h>
#include <engine/shared/config.h>
#include <engine/shared/jobs.h>
#include <engine/shared/network.h>
#include <engine/storage.h>

CHostLookup::CHostLookup() = default;

CHostLookup::CHostLookup(const char *pHostname, int Nettype)
{
str_copy(m_aHostname, pHostname);
m_Nettype = Nettype;
}

void CHostLookup::Run()
{
m_Result = net_host_lookup(m_aHostname, &m_Addr, m_Nettype);
}

class CEngine : public IEngine
{
public:
IConsole *m_pConsole;
IStorage *m_pStorage;
bool m_Logging;

bool m_Logging;
std::shared_ptr<CFutureLogger> m_pFutureLogger;

char m_aAppName[256];

CJobPool m_JobPool;

static void Con_DbgLognetwork(IConsole::IResult *pResult, void *pUserData)
{
CEngine *pEngine = static_cast<CEngine *>(pUserData);
Expand All @@ -56,13 +45,13 @@ class CEngine : public IEngine
}
}

public:
CEngine(bool Test, const char *pAppname, std::shared_ptr<CFutureLogger> pFutureLogger, int Jobs) :
m_pFutureLogger(std::move(pFutureLogger))
{
str_copy(m_aAppName, pAppname);
if(!Test)
{
//
dbg_msg("engine", "running on %s-%s-%s", CONF_FAMILY_STRING, CONF_PLATFORM_STRING, CONF_ARCH_STRING);
#ifdef CONF_ARCH_ENDIAN_LITTLE
dbg_msg("engine", "arch is little endian");
Expand All @@ -73,9 +62,9 @@ class CEngine : public IEngine
#endif

char aVersionStr[128];
if(!os_version_str(aVersionStr, sizeof(aVersionStr)))
if(os_version_str(aVersionStr, sizeof(aVersionStr)))
{
dbg_msg("engine", "operation system version: %s", aVersionStr);
dbg_msg("engine", "operating system version: %s", aVersionStr);
}

// init the network
Expand All @@ -90,7 +79,7 @@ class CEngine : public IEngine

~CEngine() override
{
m_JobPool.Destroy();
CNetBase::CloseLog();
}

void Init() override
Expand All @@ -101,8 +90,6 @@ class CEngine : public IEngine
if(!m_pConsole || !m_pStorage)
return;

char aFullPath[IO_MAX_PATH_LENGTH];
m_pStorage->GetCompletePath(IStorage::TYPE_SAVE, "dumps/", aFullPath, sizeof(aFullPath));
m_pConsole->Register("dbg_lognetwork", "", CFGFLAG_SERVER | CFGFLAG_CLIENT, Con_DbgLognetwork, this, "Log the network");
}

Expand All @@ -113,16 +100,16 @@ class CEngine : public IEngine
m_JobPool.Add(std::move(pJob));
}

void SetAdditionalLogger(std::unique_ptr<ILogger> &&pLogger) override
void ShutdownJobs() override
{
m_pFutureLogger->Set(std::move(pLogger));
m_JobPool.Shutdown();
}
};

void IEngine::RunJobBlocking(IJob *pJob)
{
CJobPool::RunBlocking(pJob);
}
void SetAdditionalLogger(std::shared_ptr<ILogger> &&pLogger) override
{
m_pFutureLogger->Set(pLogger);
}
};

IEngine *CreateEngine(const char *pAppname, std::shared_ptr<CFutureLogger> pFutureLogger, int Jobs) { return new CEngine(false, pAppname, std::move(pFutureLogger), Jobs); }
IEngine *CreateTestEngine(const char *pAppname, int Jobs) { return new CEngine(true, pAppname, nullptr, Jobs); }
Loading

0 comments on commit 5aca628

Please sign in to comment.