-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathh_export.cpp
173 lines (142 loc) · 4.45 KB
/
h_export.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
//
// FoXBot - AI Bot for Halflife's Team Fortress Classic
//
// (http://foxbot.net)
//
// h_export.cpp
//
// Copyright (C) 2003 - Tom "Redfox" Simpson
//
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License for more details at:
// http://www.gnu.org/copyleft/gpl.html
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
#include "extdll.h"
#include "enginecallback.h"
#include "bot.h"
// meta mod stuff
#include <meta_api.h>
#include <cstring>
#include <string>
extern bool mr_meta;
#ifndef __linux__
HINSTANCE h_Library = nullptr;
#else
void *h_Library = nullptr;
#endif
enginefuncs_t g_engfuncs;
globalvars_t *gpGlobals = nullptr;
char g_argv[256] = {};
// static FILE *fp;
GETENTITYAPI other_GetEntityAPI = nullptr;
GETNEWDLLFUNCTIONS other_GetNewDLLFunctions = nullptr;
GIVEFNPTRSTODLL other_GiveFnptrsToDll = nullptr;
extern int mod_id;
#ifndef __linux__
// Required DLL entry point
BOOL WINAPI DllMain(HINSTANCE hinstDLL, const DWORD fdwReason, LPVOID lpvReserved) {
if (fdwReason == DLL_PROCESS_ATTACH) {
}
else if (fdwReason == DLL_PROCESS_DETACH) {
// Do not call FreeLibrary here
}
return true;
}
#endif
#if defined(_WIN32) && !defined(__GNUC__) && defined(_MSC_VER)
#pragma comment(linker, "/EXPORT:GiveFnptrsToDll=_GiveFnptrsToDll@8,@1")
#pragma comment(linker, "/SECTION:.data,RW")
#endif
C_DLLEXPORT void WINAPI GiveFnptrsToDll(enginefuncs_t *pengfuncsFromEngine, globalvars_t *pGlobals) {
std::memcpy(&g_engfuncs, pengfuncsFromEngine, sizeof(enginefuncs_t));
gpGlobals = pGlobals;
if (mr_meta) {
return;
}
char game_dir[256];
char mod_name[32];
// Find the directory name of the currently running MOD
(*g_engfuncs.pfnGetGameDir)(game_dir);
unsigned int pos = 0;
if (std::strchr(game_dir, '/') != nullptr) {
pos = std::strlen(game_dir) - 1;
// Scan backwards till first directory separator
while (pos && game_dir[pos] != '/') {
pos--;
}
if (pos == 0) {
// Error getting directory name
ALERT(at_error, "FoxBot - Error determining MOD directory name!");
}
pos++;
}
std::strcpy(mod_name, &game_dir[pos]);
if (strcasecmp(mod_name, "tfc") == 0) {
mod_id = TFC_DLL;
#ifndef __linux__
h_Library = LoadLibraryA("tfc\\dlls\\tfc.dll");
#else
h_Library = dlopen("tfc/dlls/tfc.so", RTLD_NOW);
#endif
}
if (h_Library == nullptr) {
// Directory error or Unsupported MOD
ALERT(at_error, "FoXBot - MOD dll not found (or unsupported MOD)!");
return;
}
other_GetEntityAPI = reinterpret_cast<GETENTITYAPI>(GetProcAddress(h_Library, "GetEntityAPI"));
if (other_GetEntityAPI == nullptr) {
// Can't find GetEntityAPI
ALERT(at_error, "FoXBot - Can't get MOD's GetEntityAPI!");
return;
}
other_GetNewDLLFunctions = reinterpret_cast<GETNEWDLLFUNCTIONS>(GetProcAddress(h_Library, "GetNewDLLFunctions"));
other_GiveFnptrsToDll = reinterpret_cast<GIVEFNPTRSTODLL>(GetProcAddress(h_Library, "GiveFnptrsToDll"));
if (other_GiveFnptrsToDll == nullptr) {
// Can't find GiveFnptrsToDll
ALERT(at_error, "FoXBot - Can't get MOD's GiveFnptrsToDll!");
return;
}
GetEngineFunctions(pengfuncsFromEngine, nullptr);
// Give the engine functions to the other DLL
other_GiveFnptrsToDll(pengfuncsFromEngine, pGlobals);
}
/*#if defined(__GNUC__)
void *operator new(size_t size) {
if (size == 0)
return (calloc(1, 1));
return (calloc(1, size));
}
void *operator new[](size_t size) {
if (size == 0)
return (calloc(1, 1));
return (calloc(1, size));
}
void operator delete(void *ptr) {
if (ptr)
free(ptr);
}
void operator delete(void *ptr, size_t) {
if (ptr)
free(ptr);
}
void operator delete[](void *ptr) {
if (ptr)
free(ptr);
}
#endif*/