forked from whoopdedo/publicscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptModule.cpp
More file actions
166 lines (140 loc) · 4.04 KB
/
Copy pathScriptModule.cpp
File metadata and controls
166 lines (140 loc) · 4.04 KB
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
/******************************************************************************
* ScriptModule.cpp
*
* This file is part of Public Scripts
* Copyright (C) 2005-2011 Tom N Harris <telliamed@whoopdedo.org>
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*****************************************************************************/
#include "ScriptModule.h"
#include "Allocator.h"
#include <cstring>
#ifdef _MSC_VER
#include <new>
#endif
#include <lg/scrmanagers.h>
#include <lg/malloc.h>
#include <windows.h>
#ifdef _MSC_VER
namespace std { const nothrow_t nothrow = nothrow_t(); }
#else
extern "C" { const void *__dyn_tls_init_callback = NULL; }
#endif
static int __cdecl NullPrintf(const char*, ...);
IMalloc *g_pMalloc = NULL;
IScriptMan *g_pScriptManager = NULL;
#ifdef __GNUC__
volatile MPrintfProc g_pfnMPrintf = NullPrintf;
#else
MPrintfProc g_pfnMPrintf = NullPrintf;
#endif
cMemoryAllocator g_Allocator;
cScriptModule g_ScriptModule;
static int __cdecl NullPrintf(const char*, ...)
{
return 0;
}
bool CheckGame(const char *);
cScriptModule::~cScriptModule()
{
if (m_pszName != sm_ScriptModuleName)
delete[] m_pszName;
#ifdef DEBUG
g_pfnMPrintf("cMemoryAllocator: Current %ld blocks for %ld bytes\n", g_Allocator.CountBlocks(), g_Allocator.CountSize());
g_pfnMPrintf("cMemoryAllocator: Total %ld allocations avg %ld bytes\n", g_Allocator.CountAlloc(), g_Allocator.CountAverage());
#endif
}
cScriptModule::cScriptModule()
{
m_pszName = const_cast<char*>(sm_ScriptModuleName);
}
void cScriptModule::SetName(const char* pszName)
{
if (m_pszName != sm_ScriptModuleName)
delete[] m_pszName;
if (pszName)
{
m_pszName = new char[::strlen(pszName)+1];
::strcpy(m_pszName, pszName);
}
else
m_pszName = const_cast<char*>(sm_ScriptModuleName);
}
STDMETHODIMP_(const char*) cScriptModule::GetName(void)
{
return m_pszName;
}
const sScrClassDesc* cScriptModule::GetScript(unsigned int i)
{
if (i < sm_ScriptsArraySize)
return &sm_ScriptsArray[i];
else
return NULL;
}
STDMETHODIMP_(const sScrClassDesc*) cScriptModule::GetFirstClass(tScrIter* pIterParam)
{
*reinterpret_cast<unsigned int*>(pIterParam) = 0;
return GetScript(0);
}
STDMETHODIMP_(const sScrClassDesc*) cScriptModule::GetNextClass(tScrIter* pIterParam)
{
const sScrClassDesc *pRet;
REGISTER unsigned int index = *reinterpret_cast<unsigned int*>(pIterParam);
pRet = GetScript(++index);
*reinterpret_cast<unsigned int*>(pIterParam) = index;
return pRet;
}
STDMETHODIMP_(void) cScriptModule::EndClassIter(tScrIter*)
{
// Nothing to do here
}
extern "C"
int __declspec(dllexport) __stdcall
ScriptModuleInit (const char* pszName,
IScriptMan* pScriptMan,
MPrintfProc pfnMPrintf,
IMalloc* pMalloc,
IScriptModule** pOutInterface)
{
*pOutInterface = NULL;
g_pScriptManager = pScriptMan;
g_pMalloc = g_Allocator.AttachMalloc(pMalloc, cScriptModule::sm_ScriptModuleName);
if (pfnMPrintf)
g_pfnMPrintf = pfnMPrintf;
if (!g_pScriptManager || !g_pMalloc)
return 0;
if (!CheckGame(pszName))
return 0;
g_ScriptModule.SetName(pszName);
g_ScriptModule.QueryInterface(IID_IScriptModule, reinterpret_cast<void**>(pOutInterface));
return 1;
}
extern "C"
BOOL WINAPI
DllMain (HINSTANCE hDLL, DWORD dwReason, PVOID lpResv)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
::DisableThreadLibraryCalls(hDLL);
return TRUE;
}
return TRUE;
#ifdef __GNUC__
lpResv = lpResv;
#endif
#ifdef __BORLANDC__
#pragma argsused(hDLL,dwReason)
#endif
}