aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDedicatedTest/tier0.cpp
blob: ad533a9c360ceee52f8e2ebf3331ee9672fa2c58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "pch.h"
#include "tier0.h"
#include <stdio.h>
#include <iostream>

void* ResolveTier0Function(const char* name)
{
	HMODULE tier0 = GetModuleHandle(L"tier0.dll");

	// todo: maybe cache resolved funcs? idk the performance hit of getprocaddress
	std::cout << "ResolveTier0Function " << name << " " << tier0 << "::" <<  GetProcAddress(tier0, name) << std::endl;
	return GetProcAddress(tier0, name);
}

// memory stuff
typedef IMemAlloc* (*Tier0CreateGlobalMemAlloc)();
void* operator new(size_t n)
{
	// we should be trying to use tier0's g_pMemAllocSingleton, but atm i can't get it stable
	// this actually seems relatively stable though, somehow???
	return malloc(n);

	//IMemAlloc** alloc = (IMemAlloc**)ResolveTier0Function("g_pMemAllocSingleton");
	//if (!alloc)
	//{
	//	Tier0CreateGlobalMemAlloc createAlloc = (Tier0CreateGlobalMemAlloc)ResolveTier0Function("CreateGlobalMemAlloc");
	//	if (!createAlloc)
	//		return malloc(n);
	//	else
	//		(*alloc) = createAlloc();
	//}
	//
	//return (*alloc)->m_vtable->Alloc(*alloc, n);
}

void operator delete(void* p) throw()
{
	// we should be trying to use tier0's g_pMemAllocSingleton, but atm i can't get it stable
	// this actually seems relatively stable though, somehow???
	free(p);

	//IMemAlloc** alloc = (IMemAlloc**)ResolveTier0Function("g_pMemAllocSingleton");
	//if (!alloc)
	//{
	//	Tier0CreateGlobalMemAlloc createAlloc = (Tier0CreateGlobalMemAlloc)ResolveTier0Function("CreateGlobalMemAlloc");
	//	if (!createAlloc)
	//	{
	//		free(p);
	//		return;
	//	}
	//	else
	//		(*alloc) = createAlloc();
	//}
	//
	//(*alloc)->m_vtable->Free(*alloc, p);
}

typedef void(*Tier0Error)(const char* fmt, ...);
void Error(const char* fmt, ...)
{
	Tier0Error tier0Func = (Tier0Error)ResolveTier0Function("Error");

	// reformat args because you can't pass varargs between funcs
	char buf[1024];

	va_list args;
	va_start(args, fmt);
	vsnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);

	// tier0 isn't loaded yet
	//if (tier0Func)
	//	tier0Func(buf);
	//else
		std::cout << "FATAL ERROR " << buf << std::endl;
}

typedef double(*Tier0FloatTime)();
double Plat_FloatTime()
{
	Tier0FloatTime tier0Func = (Tier0FloatTime)ResolveTier0Function("Plat_FloatTime");

	if (tier0Func)
	{
		return tier0Func();
	}
	else
		return 0.0f;
}