1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-15 22:57:12 +02:00

Implemented a simple command system. Added a custom buffer class. Implemented reconnection for IRC sessions. Various other adjustments and additions.

This commit is contained in:
Sandu Liviu Catalin
2015-11-07 12:17:39 +02:00
parent 878a35eb2a
commit 3206d49404
16 changed files with 2792 additions and 305 deletions

122
source/Base/Buffer.cpp Normal file
View File

@ -0,0 +1,122 @@
#include "Base/Buffer.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
Buffer::Buffer()
: m_Data(nullptr), m_Size(0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Buffer::Buffer(SzType sz)
: m_Data(Alloc(sz)), m_Size(m_Data == nullptr ? 0 : sz)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Buffer::Buffer(Buffer && o)
: m_Data(o.m_Data), m_Size(o.m_Size)
{
o.m_Data = nullptr;
}
// ------------------------------------------------------------------------------------------------
Buffer::~Buffer()
{
Free(m_Data);
}
// ------------------------------------------------------------------------------------------------
Buffer & Buffer::operator = (Buffer && o)
{
if (m_Data != o.m_Data)
{
m_Data = o.m_Data;
m_Size = o.m_Size;
o.m_Data = nullptr;
}
return *this;
}
// ------------------------------------------------------------------------------------------------
void Buffer::Resize(SzType sz)
{
if (!sz)
{
Free(m_Data);
}
else if (sz != m_Size)
{
Pointer data = m_Data;
m_Data = Alloc(sz);
if (sz > m_Size)
{
Copy(data, m_Size);
}
else
{
Copy(data, sz);
}
m_Size = sz;
Free(data);
}
}
// ------------------------------------------------------------------------------------------------
void Buffer::Reserve(SzType sz)
{
if (!sz)
{
return;
}
else if (sz > m_Size)
{
Pointer data = m_Data;
m_Data = Alloc(sz);
Copy(data, m_Size);
m_Size = sz;
Free(data);
}
}
// ------------------------------------------------------------------------------------------------
void Buffer::Copy(ConstPtr buf, SzType sz)
{
memcpy(m_Data, buf, sz * sizeof(Value));
}
// ------------------------------------------------------------------------------------------------
Buffer::Pointer Buffer::Alloc(SzType sz)
{
Pointer mem = reinterpret_cast< Pointer >(malloc(sz * sizeof(Value)));
if (!mem)
{
return nullptr;
}
return mem;
}
// ------------------------------------------------------------------------------------------------
void Buffer::Free(Pointer buf)
{
if (buf != nullptr)
{
free(buf);
}
}
} // Namespace:: SqMod

230
source/Base/Buffer.hpp Normal file
View File

@ -0,0 +1,230 @@
#ifndef _BASE_BUFFER_HPP_
#define _BASE_BUFFER_HPP_
// ------------------------------------------------------------------------------------------------
#include "Config.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* ...
*/
class Buffer
{
public:
// --------------------------------------------------------------------------------------------
typedef SQChar Value;
// --------------------------------------------------------------------------------------------
typedef Value & Reference;
typedef const Value & ConstRef;
// --------------------------------------------------------------------------------------------
typedef Value * Pointer;
typedef const Value * ConstPtr;
// --------------------------------------------------------------------------------------------
typedef SQUint32 SzType;
/* --------------------------------------------------------------------------------------------
* ...
*/
Buffer();
/* --------------------------------------------------------------------------------------------
* ...
*/
Buffer(SzType sz);
/* --------------------------------------------------------------------------------------------
* ...
*/
Buffer(const Buffer &) = delete;
/* --------------------------------------------------------------------------------------------
* ...
*/
Buffer(Buffer && o);
/* --------------------------------------------------------------------------------------------
* ...
*/
~Buffer();
/* --------------------------------------------------------------------------------------------
* ...
*/
Buffer & operator = (const Buffer &) = delete;
/* --------------------------------------------------------------------------------------------
* ...
*/
Buffer & operator = (Buffer && o);
/* --------------------------------------------------------------------------------------------
* ...
*/
bool operator == (const Buffer & o) const
{
return (m_Size == o.m_Size);
}
/* --------------------------------------------------------------------------------------------
* ...
*/
bool operator != (const Buffer & o) const
{
return (m_Size != o.m_Size);
}
/* --------------------------------------------------------------------------------------------
* ...
*/
bool operator < (const Buffer & o) const
{
return (m_Size < o.m_Size);
}
/* --------------------------------------------------------------------------------------------
* ...
*/
bool operator > (const Buffer & o) const
{
return (m_Size > o.m_Size);
}
/* --------------------------------------------------------------------------------------------
* ...
*/
bool operator <= (const Buffer & o) const
{
return (m_Size <= o.m_Size);
}
/* --------------------------------------------------------------------------------------------
* ...
*/
bool operator >= (const Buffer & o) const
{
return (m_Size >= o.m_Size);
}
/* --------------------------------------------------------------------------------------------
* ...
*/
operator bool () const
{
return (m_Data != nullptr);
}
/* --------------------------------------------------------------------------------------------
* ...
*/
operator ! () const
{
return (m_Data == nullptr);
}
/* --------------------------------------------------------------------------------------------
* ...
*/
Pointer Begin()
{
return m_Data;
}
/* --------------------------------------------------------------------------------------------
* ...
*/
ConstPtr Begin() const
{
return m_Data;
}
/* --------------------------------------------------------------------------------------------
* ...
*/
Pointer End()
{
return m_Data + m_Size;
}
/* --------------------------------------------------------------------------------------------
* ...
*/
ConstPtr End() const
{
return m_Data + m_Size;
}
/* --------------------------------------------------------------------------------------------
* ...
*/
Pointer Data()
{
return m_Data;
}
/* --------------------------------------------------------------------------------------------
* ...
*/
ConstPtr Data() const
{
return m_Data;
}
/* --------------------------------------------------------------------------------------------
* ...
*/
SzType Size() const
{
return m_Size;
}
/* --------------------------------------------------------------------------------------------
* ...
*/
void Resize(SzType sz);
/* --------------------------------------------------------------------------------------------
* ...
*/
void Reserve(SzType sz);
/* --------------------------------------------------------------------------------------------
* ...
*/
void Increase(SzType sz)
{
Reserve(m_Size + sz);
}
protected:
/* --------------------------------------------------------------------------------------------
* ...
*/
void Copy(ConstPtr buf, SzType sz);
/* --------------------------------------------------------------------------------------------
* ...
*/
static Pointer Alloc(SzType sz);
/* --------------------------------------------------------------------------------------------
* ...
*/
static void Free(Pointer buf);
private:
// --------------------------------------------------------------------------------------------
Pointer m_Data;
SzType m_Size;
};
} // Namespace:: SqMod
#endif // _BASE_BUFFER_HPP_

View File

@ -28,10 +28,10 @@ namespace SqMod {
// ------------------------------------------------------------------------------------------------
static std::unique_ptr<std::mt19937> RG32_MT19937 = std::unique_ptr<std::mt19937>( \
new std::mt19937(static_cast<unsigned>(std::time(0))));
new std::mt19937(_SCU32(std::time(0))));
static std::unique_ptr<std::mt19937_64> RG64_MT19937 = std::unique_ptr<std::mt19937_64>( \
new std::mt19937_64(static_cast<unsigned>(std::time(0))));
new std::mt19937_64(_SCU32(std::time(0))));
// ------------------------------------------------------------------------------------------------
static std::uniform_int_distribution<Int8> Int8_Dist(std::numeric_limits<Int8>::min(), std::numeric_limits<Int8>::max());
@ -258,30 +258,30 @@ void LogFtl(const char * fmt, ...)
const SQChar * ToStringF(const char * fmt, ...)
{
// Acquire a buffer from the buffer pool
Core::Buffer vbuf = _Core->PullBuffer();
Buffer vbuf = _Core->PullBuffer(128);
// Get direct access to the buffer data
Core::Buffer::value_type * buf = vbuf.data();
Buffer::Pointer buf = vbuf.Data();
// Variable arguments structure
va_list args;
// Get the specified arguments
va_start (args, fmt);
// Run the specified format
int ret = std::vsnprintf(buf, vbuf.size() * sizeof(Core::Buffer::value_type), fmt, args);
int ret = std::vsnprintf(buf, vbuf.Size() * sizeof(Buffer::Value), fmt, args);
// Check for buffer overflows
if (static_cast<unsigned>(ret) >= vbuf.size())
if (_SCU32(ret) >= vbuf.Size())
{
// Throw error
LogErr("Buffer overflow object to string conversion: %d > %d", ret, vbuf.size());
// Return an empty string
buf[0] = '\0';
// Scale buffer
vbuf.Reserve(ret);
// Run the specified format
ret = std::vsnprintf(buf, vbuf.Size() * sizeof(Buffer::Value), fmt, args);
}
// Check for formatting errors
else if (ret < 0)
if (ret < 0)
{
// Throw error
LogErr("Failed to convert object to string");
LogErr("Failed to run the specified string format");
// Return an empty string
buf[0] = '\0';
buf[0] = 0;
}
// Return the buffer back to the buffer pool
_Core->PushBuffer(std::move(vbuf));
@ -293,11 +293,11 @@ const SQChar * ToStringF(const char * fmt, ...)
const SQChar * InsertStr(const SQChar * f, const std::vector< const SQChar * > & a)
{
// Acquire a buffer from the buffer pool
Core::Buffer vbuf = _Core->PullBuffer();
Buffer vbuf = _Core->PullBuffer(128);
// Get direct access to the buffer data
Core::Buffer::value_type * buf = vbuf.data();
Buffer::Pointer buf = vbuf.Data();
// Get the size of the buffer
const Core::Buffer::size_type sz = vbuf.size();
const Buffer::SzType sz = vbuf.Size();
// Size of the resulted string and the number of specified arguments
unsigned n = 0, s = a.size();
// See if the format string is valid
@ -375,17 +375,17 @@ const SQChar * InsStr(const SQChar * f)
const SQChar * LeftStr(const SQChar * t, SQChar f, SQUint32 w)
{
// Acquire a buffer from the buffer pool
Core::Buffer vbuf = _Core->PullBuffer();
Buffer vbuf = _Core->PullBuffer(w);
// Get direct access to the buffer data
Core::Buffer::value_type * buf = vbuf.data();
Buffer::Pointer buf = vbuf.Data();
// Get the length of the string
SQUint32 n = strlen(t);
// Fill the buffer with the specified fill character
memset(buf, f, w * sizeof(Core::Buffer::value_type));
memset(buf, f, w * sizeof(Buffer::Value));
// Is the width in bounds?
if (w >= vbuf.size())
if (w >= vbuf.Size())
{
LogWrn("Invalid width specified: %d > %d", w, vbuf.size());
LogWrn("Invalid width specified: %d > %d", w, vbuf.Size());
// Invalidate the width
w = 0;
}
@ -415,17 +415,17 @@ const SQChar * LeftStr(const SQChar * t, SQChar f, SQUint32 w)
const SQChar * LeftStr(const SQChar * t, SQChar f, SQUint32 w, SQUint32 o)
{
// Acquire a buffer from the buffer pool
Core::Buffer vbuf = _Core->PullBuffer();
Buffer vbuf = _Core->PullBuffer(w);
// Get direct access to the buffer data
Core::Buffer::value_type * buf = vbuf.data();
Buffer::Pointer buf = vbuf.Data();
// Get the length of the string
SQUint32 n = strlen(t);
// Fill the buffer with the specified fill character
memset(buf, f, w * sizeof(Core::Buffer::value_type));
memset(buf, f, w * sizeof(Buffer::Value));
// Is the width in bounds?
if (w >= vbuf.size())
if (w >= vbuf.Size())
{
LogWrn("Invalid width specified: %d > %d", w, vbuf.size());
LogWrn("Invalid width specified: %d > %d", w, vbuf.Size());
// Invalidate the width
w = 0;
}
@ -456,17 +456,17 @@ const SQChar * LeftStr(const SQChar * t, SQChar f, SQUint32 w, SQUint32 o)
const SQChar * RightStr(const SQChar * t, SQChar f, SQUint32 w)
{
// Acquire a buffer from the buffer pool
Core::Buffer vbuf = _Core->PullBuffer();
Buffer vbuf = _Core->PullBuffer(w);
// Get direct access to the buffer data
Core::Buffer::value_type * buf = vbuf.data();
Buffer::Pointer buf = vbuf.Data();
// Get the length of the string
SQUint32 n = strlen(t);
// Fill the buffer with the specified fill character
memset(buf, f, w * sizeof(Core::Buffer::value_type));
memset(buf, f, w * sizeof(Buffer::Value));
// Is the width in bounds?
if (w >= vbuf.size())
if (w >= vbuf.Size())
{
LogWrn("Invalid width specified: %d > %d", w, vbuf.size());
LogWrn("Invalid width specified: %d > %d", w, vbuf.Size());
// Invalidate the width
w = 0;
}
@ -496,17 +496,17 @@ const SQChar * RightStr(const SQChar * t, SQChar f, SQUint32 w)
const SQChar * RightStr(const SQChar * t, SQChar f, SQUint32 w, SQUint32 o)
{
// Acquire a buffer from the buffer pool
Core::Buffer vbuf = _Core->PullBuffer();
Buffer vbuf = _Core->PullBuffer(w);
// Get direct access to the buffer data
Core::Buffer::value_type * buf = vbuf.data();
Buffer::Pointer buf = vbuf.Data();
// Get the length of the string
SQUint32 n = strlen(t);
// Fill the buffer with the specified fill character
memset(buf, f, w * sizeof(Core::Buffer::value_type));
memset(buf, f, w * sizeof(Buffer::Value));
// Is the width in bounds?
if (w >= vbuf.size())
if (w >= vbuf.Size())
{
LogWrn("Invalid width specified: %d > %d", w, vbuf.size());
LogWrn("Invalid width specified: %d > %d", w, vbuf.Size());
// Invalidate the width
w = 0;
}
@ -537,17 +537,17 @@ const SQChar * RightStr(const SQChar * t, SQChar f, SQUint32 w, SQUint32 o)
const SQChar * CenterStr(const SQChar * t, SQChar f, SQUint32 w)
{
// Acquire a buffer from the buffer pool
Core::Buffer vbuf = _Core->PullBuffer();
Buffer vbuf = _Core->PullBuffer(w);
// Get direct access to the buffer data
Core::Buffer::value_type * buf = vbuf.data();
Buffer::Pointer buf = vbuf.Data();
// Get the length of the string
SQUint32 n = strlen(t);
// Fill the buffer with the specified fill character
memset(buf, f, w * sizeof(Core::Buffer::value_type));
memset(buf, f, w * sizeof(Buffer::Value));
// Is the width in bounds?
if (w >= vbuf.size())
if (w >= vbuf.Size())
{
LogWrn("Invalid width specified: %d > %d", w, vbuf.size());
LogWrn("Invalid width specified: %d > %d", w, vbuf.Size());
// Invalidate the width
w = 0;
}
@ -577,13 +577,13 @@ const SQChar * CenterStr(const SQChar * t, SQChar f, SQUint32 w)
// ------------------------------------------------------------------------------------------------
void InitMTRG32()
{
RG32_MT19937.reset(new std::mt19937(static_cast<unsigned>(std::time(0))));
RG32_MT19937.reset(new std::mt19937(_SCU32(std::time(0))));
}
// ------------------------------------------------------------------------------------------------
void InitMTRG64()
{
RG64_MT19937.reset(new std::mt19937_64(static_cast<unsigned>(std::time(0))));
RG64_MT19937.reset(new std::mt19937_64(_SCU32(std::time(0))));
}
// ------------------------------------------------------------------------------------------------