Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/game/shared/ms/groupfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ bool CGameGroupFile::ReadEntry(const char* pszName, byte* pBuffer, unsigned long
byte* _pBuffer = msnew byte[Entry.FileSize];
bool bSuccess = false;

if (Entry.FileSize == cFile.Read(_pBuffer, Entry.FileSize))
unsigned long BytesRead = cFile.Read(_pBuffer, Entry.FileSize);
if (BytesRead == Entry.FileSize)
{
memcpy(pBuffer, _pBuffer, DataSize);
memcpy(pBuffer, _pBuffer, BytesRead);
DataSize = BytesRead;
bSuccess = true;
}

Expand Down
2 changes: 2 additions & 0 deletions src/game/tools/scriptpack/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ set(SRC_Files
"cbase.h"
"packer.cpp"
"packer.h"
"logger.cpp"
"logger.h"
"scriptpack.cpp"
"${BASEDIR}/src/game/shared/ms/crc/checksum_crc.cpp"
"${BASEDIR}/src/game/shared/ms/crc/checksum_crc.h"
Expand Down
60 changes: 60 additions & 0 deletions src/game/tools/scriptpack/logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "logger.h"
#include <sstream>
#include <ctime>
#include <iomanip>
#include <stdexcept>

std::string Logger::logFilename = "./output.txt";

void Logger::SetLogFile(const std::string& filename) {
Logger::logFilename = filename;
}

void Logger::EnableFileLogging(bool enable) {
Logger::isFileLoggingEnabled = enable;
}

std::string Logger::getCurrentTimestamp() {
std::time_t now = std::time(nullptr);
std::tm* ltm = std::localtime(&now);

std::stringstream ss;
ss << std::put_time(ltm, "%Y-%m-%d %H:%M:%S");
return ss.str();
}

Logger::Logger() {
logFileStream.open(logFilename, std::ios::trunc);

if (!logFileStream.is_open()) {
std::cerr << "[LOGGER INIT ERROR] Could not open log file: " << logFilename
<< ". Logging will be console-only." << std::endl;
} else {
logFileStream << "--- Logger Initialized at " << getCurrentTimestamp() << " ---\n";
logFileStream.flush();
}
}

Logger& Logger::GetInstance() {
static Logger instance;
return instance;
}

void Logger::RawLog(const std::string& message) {
if (isFileLoggingEnabled && logFileStream.is_open()) {
logFileStream << message << std::endl;
}

std::cout << message << std::endl;
}

void Logger::Log(const std::string& message) {
std::string timestamp = getCurrentTimestamp();
std::string fullMessage = "[" + timestamp + "] " + message;

if (isFileLoggingEnabled && logFileStream.is_open()) {
logFileStream << fullMessage << std::endl;
}

std::cout << fullMessage << std::endl;
}
49 changes: 49 additions & 0 deletions src/game/tools/scriptpack/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include <string>
#include <fstream>
#include <iostream>
#include <format>

class Logger {
public:
// *** 1. Global Access Method (The Singleton Gateway) ***
static Logger& GetInstance();

// *** 2. Configuration Methods ***
static void SetLogFile(const std::string& filename);
// New method to control logging dynamically
void EnableFileLogging(bool enable);

void RawLog(const std::string& message);

// *** 3. Main Logging Method ***
void Log(const std::string& message);

template <typename... Args>
void Log(std::format_string<Args...> fmt, Args&&... args)
{
std::string message = std::vformat(fmt.get(), std::make_format_args(args...));
this->Log(message);
}

// *** 3. Prevent Copying/Moving ***
// Required for the Singleton pattern
Logger(const Logger&) = delete;
Logger& operator=(const Logger&) = delete;
Logger(Logger&&) = delete;
Logger& operator=(Logger&&) = delete;

private:
// *** 4. Private Constructor ***
// Opens the file stream once when the Singleton is created.
Logger();

// Member variables
std::ofstream logFileStream;
bool isFileLoggingEnabled = true;
static std::string logFilename;

// Helper to get the current timestamp string
std::string getCurrentTimestamp();
};
Loading