Skip to content

Commit

Permalink
feat: add idTCP, idLogging
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitalita committed Oct 26, 2023
1 parent c586f0a commit 78d67a2
Show file tree
Hide file tree
Showing 8 changed files with 533 additions and 0 deletions.
77 changes: 77 additions & 0 deletions CommonLibSF/include/RE/I/idLogging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once

namespace RE
{
namespace idLogging
{
enum class Severity : std::uint8_t
{
Trace = 0,
Debug = 1,
Info = 2,
WARN = 3,
ERROR = 4,
FATAL = 5
};

struct ILogger
{
SF_RTTI_VTABLE(idLogging__ILogger);
~ILogger() = default; // 00
virtual bool ShouldLog(Severity severity) = 0; // 01
virtual void Log(const char* a_fileName, int a_line_number, Severity severity, const char* msg) = 0; // 02
void Printf(const char* a_fileName, int a_line_number, Severity severity, const char* a_fmt, ...)
{
va_list args;
va_start(args, a_fmt);
char buffer[1064];
vsnprintf(buffer, 1024, a_fmt, args);
va_end(args);
Log(a_fileName, a_line_number, severity, buffer);
}
};

ILogger* GetLoggerSingleton();
}

// in an anonymous namespace in the exe
struct NetSocketLogger : public idLogging::ILogger
{
SF_RTTI_VTABLE(__NetSocketLogger);
~NetSocketLogger() = default; // 00
// override idLogging::ILogger
bool ShouldLog([[maybe_unused]] idLogging::Severity severity) override { return false; } // 01
void Log(const char* a_fileName, int a_line_number, idLogging::Severity severity, const char* msg) override // 02
{
const char* severitystr;
char outputString[1032];
switch (severity) // Trace, Debug, Info, WARN, ERROR, FATAL, UNKNOWN
{
case idLogging::Severity::Trace:
severitystr = "Trace";
break;
case idLogging::Severity::Debug:

severitystr = "Debug";
break;
case idLogging::Severity::Info:
severitystr = "Info";
break;
case idLogging::Severity::WARN:
severitystr = "WARN";
break;
case idLogging::Severity::ERROR:
severitystr = "ERROR";
break;
case idLogging::Severity::FATAL:
severitystr = "FATAL";
break;
default:
severitystr = "UNKNOWN";
break;
}
snprintf(outputString, 1024, "%s: %s - [%s (%d)]\n", severitystr, msg, a_fileName, a_line_number);
WinAPI::OutputDebugString(outputString);
}
};
}
43 changes: 43 additions & 0 deletions CommonLibSF/include/RE/I/idTCP.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

namespace RE
{
enum class netadrtype_t
{
NA_BAD, // an address lookup failed
NA_LOOPBACK,
NA_BROADCAST,
NA_IP
};

struct netadr_t
{
netadrtype_t type; // 00
std::byte ip[4]; // 04
std::uint32_t port; // 08
};
static_assert(sizeof(netadr_t) == 0xC);

const char* NET_ErrorString(void);
void Net_SockadrToNetadr(WinAPI::sockaddr* s, netadr_t* a); // They violated the naming convention here :(
bool NET_WaitForData(std::uintptr_t socket, std::uint32_t timeoutMs);
void Sys_InitNetworking();

class idTCP
{
SF_RTTI_VTABLE(idTCP);

virtual ~idTCP(); // 00
bool Listen(std::uint16_t port, bool blocking); // 'blocking' is ignored
bool Accept(const idTCP& listener);
std::int32_t Close();
std::int32_t Read(void* buffer, std::uint32_t size);
std::int32_t Write(const void* buffer, std::uint32_t size, std::uint32_t timeoutMs);

// members
netadr_t address; // 08
std::uintptr_t socket; // 18
};
static_assert(sizeof(idTCP) == 0x20);

}
19 changes: 19 additions & 0 deletions CommonLibSF/include/RE/IDs.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ namespace RE::ID
inline constexpr REL::ID HasType{ 83208 };
}

namespace idLogging
{
inline constexpr REL::ID singleton{ 895197 };
}

namespace idTCP
{
inline constexpr REL::ID Accept{ 211257 };
inline constexpr REL::ID Close{ 211258 };
inline constexpr REL::ID Listen{ 211259 };
inline constexpr REL::ID Read{ 211264 };
inline constexpr REL::ID Write{ 211266 };
}

namespace LockPickedEvent
{
inline constexpr REL::ID GetEventSource{ 107115 };
Expand Down Expand Up @@ -309,4 +323,9 @@ namespace RE::ID

inline constexpr REL::ID RTDynamicCast{ 211916 };

// idCoreSocket functions
inline constexpr REL::ID NET_ErrorString{ 211261 };
inline constexpr REL::ID Net_SockadrToNetadr{ 211262 };
inline constexpr REL::ID NET_WaitForData{ 211263 };
inline constexpr REL::ID Sys_InitNetworking{ 211265 };
}
2 changes: 2 additions & 0 deletions CommonLibSF/include/RE/Starfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@
#include "RE/I/IPostAnimationChannelUpdateFunctor.h"
#include "RE/I/IStoreAnimationActions.h"
#include "RE/I/IVMObjectBindInterface.h"
#include "RE/I/idLogging.h"
#include "RE/I/idTCP.h"
#include "RE/IDs.h"
#include "RE/IDs_NiRTTI.h"
#include "RE/IDs_RTTI.h"
Expand Down
Loading

0 comments on commit 78d67a2

Please sign in to comment.