Skip to content

Commit

Permalink
Merge pull request #84 from w23/slightly_better_logs
Browse files Browse the repository at this point in the history
make it always write all log messages into OpenSource.log

And add portal.cfg
  • Loading branch information
w23 authored Feb 2, 2024
2 parents 48b074f + 56c53c9 commit 74e8521
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 18 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ jobs:
if: matrix.os == 'windows-latest'
run: |
cmake -E make_directory ${{ runner.workspace }}/OpenSource-win64
cmake -E copy ${{ github.workspace }}/misc/hl1.cfg ${{ github.workspace }}/misc/hl2.cfg ${{ github.workspace }}/misc/hl2eps.cfg ${{ github.workspace }}/misc/hl1.bat ${{ github.workspace }}/misc/hl2.bat ${{ github.workspace }}/misc/hl2eps.bat ${{ runner.workspace }}/build/Release/OpenSource.exe ${{ github.workspace }}/README.md ${{ runner.workspace }}/OpenSource-win64
cmake -E copy ${{ runner.workspace }}/build/Release/OpenSource.exe ${{ runner.workspace }}/OpenSource-win64/
cmake -E copy ${{ github.workspace }}/README.md ${{ runner.workspace }}/OpenSource-win64/
cmake -E copy ${{ github.workspace }}/misc/hl1.cfg ${{ github.workspace }}/misc/hl1.bat ${{ runner.workspace }}/OpenSource-win64/
cmake -E copy ${{ github.workspace }}/misc/hl2.cfg ${{ github.workspace }}/misc/hl2.bat ${{ runner.workspace }}/OpenSource-win64/
cmake -E copy ${{ github.workspace }}/misc/hl2eps.cfg ${{ github.workspace }}/misc/hl2eps.bat ${{ runner.workspace }}/OpenSource-win64/
cmake -E copy ${{ github.workspace }}/misc/portal.cfg ${{ github.workspace }}/misc/portal.bat ${{ runner.workspace }}/OpenSource-win64/
powershell Compress-Archive ${{ runner.workspace }}/OpenSource-win64/* ${{ runner.workspace }}/OpenSource-win64.zip
- name: Upload artifacts
if: matrix.os == 'windows-latest'
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ tags
*.opendb
*.db
*.user
.cache
16 changes: 9 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ add_subdirectory(src/atto)

set(SOURCES
src/OpenSource.c
src/bsp.c
src/atlas.c
src/filemap.c
src/bsp.c
src/cache.c
src/camera.c
src/collection.c
src/vmfparser.c
src/material.c
src/texture.c
src/cache.c
src/dxt.c
src/render.c
src/filemap.c
src/log.c
src/material.c
src/profiler.c
src/render.c
src/texture.c
src/vmfparser.c
)

set(HEADERS
Expand All @@ -37,6 +38,7 @@ set(HEADERS
src/etcpack.h
src/filemap.h
src/libc.h
src/log.h
src/material.h
src/mempools.h
src/profiler.h
Expand Down
1 change: 1 addition & 0 deletions misc/portal.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OpenSource.exe portal.cfg
31 changes: 25 additions & 6 deletions src/OpenSource.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "collection.h"
#include "mempools.h"
#include "common.h"
#include "texture.h"
#include "log.h"
//#include "profiler.h"
#include "camera.h"
#include "vmfparser.h"
Expand Down Expand Up @@ -502,6 +502,7 @@ static VMFAction configLandmarkCallback(VMFState *state, VMFEntryType entry, con
state->callback = configPatchCallback;
break;
default:
PRINTF("%s: Unexpected entry %d", __func__, entry);
return VMFAction_SemanticError;
}

Expand All @@ -518,12 +519,15 @@ static VMFAction configMapCallback(VMFState *state, VMFEntryType entry, const VM
} else if (strncasecmp("offset", kv->key.str, kv->key.length) == 0) {
cfg->map_offset = kv->value;
} else {
PRINTF("%s: Unexpected key \"" PRI_SV "\"", __func__, PRI_SVV(kv->key));
return VMFAction_SemanticError;
}
break;
case VMFEntryType_SectionClose:
if (cfg->map_name.length < 1)
if (cfg->map_name.length < 1) {
PRINTF("%s: Invalid map name \"" PRI_SV "\"", __func__, PRI_SVV(cfg->map_name));
return VMFAction_SemanticError;
}
Map *m = opensrcAllocMap(cfg->map_name);
if (m && cfg->map_offset.length >= 5) {
float x, y, z;
Expand All @@ -538,6 +542,7 @@ static VMFAction configMapCallback(VMFState *state, VMFEntryType entry, const VM
state->callback = configReadCallback;
break;
default:
PRINTF("%s: Unexpected entry %d", __func__, entry);
return VMFAction_SemanticError;
}

Expand All @@ -549,14 +554,17 @@ static VMFAction configPatchCallback(VMFState *state, VMFEntryType entry, const

switch (entry) {
case VMFEntryType_SectionOpen:
if (kv->key.length < 1)
if (kv->key.length < 1) {
PRINTF("%s: Unexpected section \"" PRI_SV "\"", __func__, PRI_SVV(kv->key));
return VMFAction_SemanticError;
}
cfg->map_name = kv->key;
state->callback = configLandmarkCallback;
break;
case VMFEntryType_SectionClose:
return VMFAction_Exit;
default:
PRINTF("%s: Unexpected entry %d", __func__, entry);
return VMFAction_SemanticError;
}

Expand Down Expand Up @@ -586,19 +594,25 @@ static VMFAction configReadCallback(VMFState *state, VMFEntryType entry, const V
} else if (strncasecmp("z_far", kv->key.str, kv->key.length) == 0) {
// FIXME null-terminate
g.R = (float)atof(kv->value.str);
} else
return VMFAction_SemanticError;
} else {
PRINTF("%s: Unexpected key \"" PRI_SV "\"", __func__, PRI_SVV(kv->key));
// TODO return VMFAction_SemanticError;
return VMFAction_Exit;
}
break;
case VMFEntryType_SectionOpen:
if (strncasecmp("patch_landmarks", kv->key.str, kv->key.length) == 0)
state->callback = configPatchCallback;
else if (strncasecmp("map", kv->key.str, kv->key.length) == 0) {
cfg->map_name.length = cfg->map_offset.length = 0;
state->callback = configMapCallback;
} else
} else {
PRINTF("%s: Unexpected section \"" PRI_SV "\"", __func__, PRI_SVV(kv->key));
return VMFAction_SemanticError;
}
break;
default:
PRINTF("%s: Unexpected entry %d", __func__, entry);
return VMFAction_SemanticError;
}

Expand Down Expand Up @@ -630,6 +644,9 @@ static int configReadFile(const char *cfgfile) {

aFileClose(&file);

// TODO remove
PRINTF("Parsing config file \"%s\" (%d):\n```\n%.*s\n```", cfgfile, pstate.data.length, pstate.data.length, pstate.data.str);

int result = VMFResult_Success == vmfParse(&pstate);

stackFreeUpToPosition(&stack_temp, buffer);
Expand Down Expand Up @@ -790,6 +807,8 @@ static Arg g_args[] = {
void attoAppInit(struct AAppProctable *proctable) {
//profilerInit();

logOpen("OpenSource.log");

g.collection_chain = NULL;
g.patches = NULL;
g.maps_count = 0;
Expand Down
12 changes: 9 additions & 3 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@

#define STR1(m) #m
#define STR(m) STR1(m)
#define PRINTF(fmt, ...) fprintf(stderr, __FILE__ ":" STR(__LINE__) ": " fmt "\n", __VA_ARGS__)
#define PRINT(msg) fprintf(stderr, __FILE__ ":" STR(__LINE__) ": %s\n", msg)

#define ASSERT(cond) if (!(cond)){PRINTF("%s failed", #cond); abort();}
#ifdef _MSC_VER
// MSVC compiler
#define PRINTF_ARG(fmt) _Printf_format_string_ fmt
#define PRINTF_ATTR(fmt_index, vargs_index)
#else
// gcc-compatible
#define PRINTF_ARG(fmt) fmt
#define PRINTF_ATTR(fmt_index, vargs_index) __attribute__((format(printf, fmt_index, vargs_index)))
#endif

#define COUNTOF(a) (sizeof(a) / sizeof(*(a)))

Expand Down
1 change: 1 addition & 0 deletions src/filemap.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "filemap.h"
#include "common.h"
#include "log.h"

#ifndef _WIN32
#include <sys/types.h>
Expand Down
31 changes: 31 additions & 0 deletions src/log.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "log.h"

#include <stdio.h>
#include <stdarg.h>

static struct {
FILE *logfile;
} glog = {0};

void logOpen(const char *logfile) {
glog.logfile = fopen(logfile, "w");
}

void logClose(void) {
fclose(glog.logfile);
}

void logPrintf(const char *format, ...) {
va_list args;
va_start(args, format);

if (glog.logfile) {
va_list args_copy;
va_copy(args_copy, args);
vfprintf(glog.logfile, format, args_copy);
va_end(args_copy);
}

vfprintf(stderr, format, args);
va_end(args);
}
11 changes: 11 additions & 0 deletions src/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

void logOpen(const char *logfile);
void logClose(void);

void logPrintf(PRINTF_ARG(const char *format), ...) PRINTF_ATTR(1, 2);

#define PRINTF(fmt, ...) logPrintf(__FILE__ ":" STR(__LINE__) ": " fmt "\n", __VA_ARGS__)
#define PRINT(msg) logPrintf(__FILE__ ":" STR(__LINE__) ": %s\n", msg)

#define ASSERT(cond) if (!(cond)){PRINTF("%s failed", #cond); abort();}
1 change: 1 addition & 0 deletions src/mempools.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "common.h"
#include "log.h"
#include <stddef.h>

typedef struct Stack {
Expand Down
2 changes: 1 addition & 1 deletion src/vmfparser.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "vmfparser.h"
#include "libc.h"
#include "log.h"

typedef enum {
VMFTokenType_End,
Expand Down

0 comments on commit 74e8521

Please sign in to comment.