fallout2-ce/src/debug.cc

241 lines
4.1 KiB
C++
Raw Normal View History

2022-05-19 01:51:26 -07:00
#include "debug.h"
#include "memory.h"
#include "platform_compat.h"
2022-05-19 01:51:26 -07:00
#include "window_manager_private.h"
#include <stdarg.h>
2022-06-19 03:13:34 -07:00
#include <stdio.h>
2022-05-19 01:51:26 -07:00
#include <stdlib.h>
#include <string.h>
2022-05-29 12:08:13 -07:00
#ifdef _WIN32
2022-05-19 01:51:26 -07:00
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
2022-05-29 12:08:13 -07:00
#endif
2022-05-19 01:51:26 -07:00
2022-06-19 03:13:34 -07:00
static int _debug_puts(char* string);
static void _debug_clear();
static int _debug_mono(char* string);
static int _debug_log(char* string);
static int _debug_screen(char* string);
static void _debug_putc(char ch);
2022-05-19 01:51:26 -07:00
// 0x51DEF8
2022-06-19 03:13:34 -07:00
static FILE* _fd = NULL;
2022-05-19 01:51:26 -07:00
// 0x51DEFC
2022-06-19 03:13:34 -07:00
static int _curx = 0;
2022-05-19 01:51:26 -07:00
// 0x51DF00
2022-06-19 03:13:34 -07:00
static int _cury = 0;
2022-05-19 01:51:26 -07:00
// 0x51DF04
2022-06-19 03:13:34 -07:00
static DebugPrintProc* gDebugPrintProc = NULL;
2022-05-19 01:51:26 -07:00
// 0x4C6CD0
void _GNW_debug_init()
{
atexit(_debug_exit);
}
// 0x4C6CDC
void _debug_register_mono()
{
if (gDebugPrintProc != _debug_mono) {
if (_fd != NULL) {
fclose(_fd);
_fd = NULL;
}
gDebugPrintProc = _debug_mono;
_debug_clear();
}
}
// 0x4C6D18
void _debug_register_log(const char* fileName, const char* mode)
{
if ((mode[0] == 'w' && mode[1] == 'a') && mode[1] == 't') {
if (_fd != NULL) {
fclose(_fd);
}
_fd = compat_fopen(fileName, mode);
2022-05-19 01:51:26 -07:00
gDebugPrintProc = _debug_log;
}
}
// 0x4C6D5C
void _debug_register_screen()
{
if (gDebugPrintProc != _debug_screen) {
if (_fd != NULL) {
fclose(_fd);
_fd = NULL;
}
gDebugPrintProc = _debug_screen;
}
}
// 0x4C6D90
void _debug_register_env()
{
const char* type = getenv("DEBUGACTIVE");
if (type == NULL) {
return;
}
2022-05-21 08:22:03 -07:00
char* copy = (char*)internal_malloc(strlen(type) + 1);
2022-05-19 01:51:26 -07:00
if (copy == NULL) {
return;
}
strcpy(copy, type);
compat_strlwr(copy);
2022-05-19 01:51:26 -07:00
if (strcmp(copy, "mono") == 0) {
// NOTE: Uninline.
_debug_register_mono();
} else if (strcmp(copy, "log") == 0) {
_debug_register_log("debug.log", "wt");
} else if (strcmp(copy, "screen") == 0) {
// NOTE: Uninline.
_debug_register_screen();
} else if (strcmp(copy, "gnw") == 0) {
if (gDebugPrintProc != _win_debug) {
if (_fd != NULL) {
fclose(_fd);
_fd = NULL;
}
gDebugPrintProc = _win_debug;
}
}
internal_free(copy);
}
// 0x4C6F18
void _debug_register_func(DebugPrintProc* proc)
{
if (gDebugPrintProc != proc) {
if (_fd != NULL) {
fclose(_fd);
_fd = NULL;
}
gDebugPrintProc = proc;
}
}
// 0x4C6F48
int debugPrint(const char* format, ...)
{
va_list args;
va_start(args, format);
int rc;
if (gDebugPrintProc != NULL) {
char string[260];
vsprintf(string, format, args);
rc = gDebugPrintProc(string);
} else {
#ifdef _DEBUG
char string[260];
vsprintf(string, format, args);
2022-05-29 12:08:13 -07:00
#ifdef _WIN32
2022-05-19 01:51:26 -07:00
OutputDebugStringA(string);
2022-05-29 12:08:13 -07:00
#else
2022-06-12 01:44:22 -07:00
printf("%s", string);
2022-05-29 12:08:13 -07:00
#endif
2022-05-19 01:51:26 -07:00
#endif
rc = -1;
}
va_end(args);
return rc;
}
// 0x4C6F94
2022-06-19 03:13:34 -07:00
static int _debug_puts(char* string)
2022-05-19 01:51:26 -07:00
{
if (gDebugPrintProc != NULL) {
return gDebugPrintProc(string);
}
return -1;
}
// 0x4C6FAC
2022-06-19 03:13:34 -07:00
static void _debug_clear()
2022-05-19 01:51:26 -07:00
{
// TODO: Something with segments.
}
// 0x4C7004
2022-06-19 03:13:34 -07:00
static int _debug_mono(char* string)
2022-05-19 01:51:26 -07:00
{
if (gDebugPrintProc == _debug_mono) {
while (*string != '\0') {
char ch = *string++;
_debug_putc(ch);
}
}
return 0;
}
// 0x4C7028
2022-06-19 03:13:34 -07:00
static int _debug_log(char* string)
2022-05-19 01:51:26 -07:00
{
if (gDebugPrintProc == _debug_log) {
if (_fd == NULL) {
return -1;
}
2022-07-06 04:12:48 -07:00
if (fprintf(_fd, "%s", string) < 0) {
2022-05-19 01:51:26 -07:00
return -1;
}
if (fflush(_fd) == EOF) {
return -1;
}
}
return 0;
}
// 0x4C7068
2022-06-19 03:13:34 -07:00
static int _debug_screen(char* string)
2022-05-19 01:51:26 -07:00
{
if (gDebugPrintProc == _debug_screen) {
2022-07-06 04:12:48 -07:00
printf("%s", string);
2022-05-19 01:51:26 -07:00
}
return 0;
}
// 0x4C709C
2022-06-19 03:13:34 -07:00
static void _debug_putc(char ch)
2022-05-19 01:51:26 -07:00
{
// TODO: Something with segments.
}
// 0x4C71AC
void _debug_scroll()
{
// TODO: Something with segments.
}
// 0x4C71E8
void _debug_exit(void)
{
if (_fd != NULL) {
fclose(_fd);
}
}