2022-05-19 01:51:26 -07:00
|
|
|
#include "debug.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-10-29 10:57:11 -07:00
|
|
|
#include <SDL.h>
|
2022-05-19 01:51:26 -07:00
|
|
|
|
2022-09-15 02:38:23 -07:00
|
|
|
#include "memory.h"
|
|
|
|
#include "platform_compat.h"
|
|
|
|
#include "window_manager_private.h"
|
|
|
|
|
2022-09-23 05:43:44 -07:00
|
|
|
namespace fallout {
|
|
|
|
|
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);
|
2022-09-01 08:41:37 -07:00
|
|
|
static void _debug_putc(int ch);
|
|
|
|
static void _debug_scroll();
|
2022-06-19 03:13:34 -07:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-06-18 00:58:15 -07:00
|
|
|
_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);
|
2022-05-28 01:57:32 -07:00
|
|
|
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];
|
2022-12-08 12:05:50 -08:00
|
|
|
vsnprintf(string, sizeof(string), format, args);
|
2022-05-19 01:51:26 -07:00
|
|
|
|
|
|
|
rc = gDebugPrintProc(string);
|
|
|
|
} else {
|
|
|
|
#ifdef _DEBUG
|
2022-10-29 10:57:11 -07:00
|
|
|
SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, format, args);
|
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
|
|
|
{
|
2022-09-01 08:41:37 -07:00
|
|
|
char* buffer;
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
|
|
|
|
buffer = NULL;
|
|
|
|
|
|
|
|
if (gDebugPrintProc == _debug_mono) {
|
|
|
|
buffer = (char*)0xB0000;
|
|
|
|
} else if (gDebugPrintProc == _debug_screen) {
|
|
|
|
buffer = (char*)0xB8000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer != NULL) {
|
|
|
|
for (y = 0; y < 25; y++) {
|
|
|
|
for (x = 0; x < 80; x++) {
|
|
|
|
*buffer++ = ' ';
|
|
|
|
*buffer++ = 7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_cury = 0;
|
|
|
|
_curx = 0;
|
|
|
|
}
|
2022-05-19 01:51:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-01 08:41:37 -07:00
|
|
|
static void _debug_putc(int ch)
|
2022-05-19 01:51:26 -07:00
|
|
|
{
|
2022-09-01 08:41:37 -07:00
|
|
|
char* buffer;
|
|
|
|
|
|
|
|
buffer = (char*)0xB0000;
|
|
|
|
|
|
|
|
switch (ch) {
|
|
|
|
case 7:
|
|
|
|
printf("\x07");
|
|
|
|
return;
|
|
|
|
case 8:
|
|
|
|
if (_curx > 0) {
|
|
|
|
_curx--;
|
|
|
|
buffer += 2 * _curx + 2 * 80 * _cury;
|
|
|
|
*buffer++ = ' ';
|
|
|
|
*buffer = 7;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case 9:
|
|
|
|
do {
|
|
|
|
_debug_putc(' ');
|
|
|
|
} while ((_curx - 1) % 4 != 0);
|
|
|
|
return;
|
|
|
|
case 13:
|
|
|
|
_curx = 0;
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
buffer += 2 * _curx + 2 * 80 * _cury;
|
|
|
|
*buffer++ = ch;
|
|
|
|
*buffer = 7;
|
|
|
|
_curx++;
|
|
|
|
if (_curx < 80) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// FALLTHROUGH
|
|
|
|
case 10:
|
|
|
|
_curx = 0;
|
|
|
|
_cury++;
|
|
|
|
if (_cury > 24) {
|
|
|
|
_cury = 24;
|
|
|
|
_debug_scroll();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2022-05-19 01:51:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 0x4C71AC
|
2022-09-01 08:41:37 -07:00
|
|
|
static void _debug_scroll()
|
2022-05-19 01:51:26 -07:00
|
|
|
{
|
2022-09-01 08:41:37 -07:00
|
|
|
char* buffer;
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
|
|
|
|
buffer = (char*)0xB0000;
|
|
|
|
|
|
|
|
for (y = 0; y < 24; y++) {
|
|
|
|
for (x = 0; x < 80 * 2; x++) {
|
|
|
|
buffer[0] = buffer[80 * 2];
|
|
|
|
buffer++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (x = 0; x < 80; x++) {
|
|
|
|
*buffer++ = ' ';
|
|
|
|
*buffer++ = 7;
|
|
|
|
}
|
2022-05-19 01:51:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 0x4C71E8
|
|
|
|
void _debug_exit(void)
|
|
|
|
{
|
|
|
|
if (_fd != NULL) {
|
|
|
|
fclose(_fd);
|
|
|
|
}
|
|
|
|
}
|
2022-09-23 05:43:44 -07:00
|
|
|
|
|
|
|
} // namespace fallout
|