This repository has been archived by the owner on Oct 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripttools.cpp
196 lines (168 loc) · 4.98 KB
/
scripttools.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* =============================================================================
* D2Lobby2
* Copyright (C) 2023 Nicholas Hastings
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 2.0 or later, as published
* by the Free Software Foundation.
*
* 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/>.
*
* As a special exception, you are also granted permission to link the code
* of this program (as well as its derivative works) to "Dota 2," the
* "Source Engine, and any Game MODs that run on software by the Valve Corporation.
* You must obey the GNU General Public License in all respects for all other
* code used. Additionally, this exception is granted to all derivative works.
*/
#include "d2lobby.h"
#include <filesystem.h>
#include <tier1/fmtstr.h>
static bool s_bScriptLastCompileScriptMissing = false;
static int s_ScriptServerRunScriptDepth = 0;
static HSCRIPT VScriptServerCompileScript(const char *pszScriptName, bool bWarnMissing)
{
s_bScriptLastCompileScriptMissing = false;
if (!scriptvm)
{
return NULL;
}
const char *pszClearVMExtension = ".lua";
const char *pszIncomingExtension = V_strrchr(pszScriptName, '.');
if (pszIncomingExtension && (V_strcmp(pszIncomingExtension, pszClearVMExtension) != 0))
{
Msg("Script file type does not match VM type\n");
return NULL;
}
CFmtStr clearScriptPath;
if (pszIncomingExtension)
{
clearScriptPath.sprintf("scripts/vscripts/%s", pszScriptName);
}
else
{
clearScriptPath.sprintf("scripts/vscripts/%s%s", pszScriptName, pszClearVMExtension);
}
CUtlBuffer bufferScript;
// Prefer the unencrypted form of the script if available
bool bResult = filesystem->ReadFile(clearScriptPath, "GAME", bufferScript);
if (!bResult)
{
// No compiled one available - abort
if (bWarnMissing)
{
DevMsg("Script not found (%s) \n", pszScriptName);
}
s_bScriptLastCompileScriptMissing = true;
return NULL;
}
const char *pBase = (const char *)bufferScript.Base();
// empty file - set the "missing, not failed compile" flag, then return NULL
if (bufferScript.TellPut() == 0)
{
s_bScriptLastCompileScriptMissing = true;
return NULL;
}
if (!pBase || !*pBase)
{
return NULL;
}
char szFullPath[MAX_PATH];
const char *pFullPath = filesystem->RelativePathToFullPath(clearScriptPath.Access(), "GAME", szFullPath, ARRAYSIZE(szFullPath));
if (!pFullPath)
{
//The reported name affects squirrel debugging. Breakpoints will not work if the relative path does not match up here and in the squirrel project.
//We might want to always report the clear name. There's no clearly right way to do it since I don't believe we could possibly debug an encrypted file.
V_strncpy(szFullPath, clearScriptPath.Access(), ARRAYSIZE(szFullPath));
pFullPath = szFullPath;
}
V_FixSlashes(szFullPath, '/'); //all scripts paths use forward slashes for standardization
HSCRIPT hScript = scriptvm->CompileScript(pBase, szFullPath);
if (!hScript)
{
DevMsg("FAILED to compile and execute script file named %s\n", pszScriptName);
}
return hScript;
}
CON_COMMAND(script_execute, "")
{
if (!*args[1])
{
Msg("No script specified\n");
return;
}
if (!scriptvm)
{
Msg("Scripting disabled or no server running\n");
return;
}
// Prevent infinite recursion in VM
if (s_ScriptServerRunScriptDepth > 16)
{
Warning("IncludeScript stack overflow\n");
return;
}
s_ScriptServerRunScriptDepth++;
HSCRIPT hScript = VScriptServerCompileScript(args[1], true);
bool bSuccess = false;
if (hScript)
{
bSuccess = (scriptvm->Run(hScript) != SCRIPT_ERROR);
if (!bSuccess)
{
DevMsg("Error running script named %s\n", args[1]);
}
scriptvm->ReleaseScript(hScript);
}
s_ScriptServerRunScriptDepth--;
}
CON_COMMAND(script, "Run the text as a script")
{
if (!*args[1])
{
Msg("No script specified\n");
return;
}
if (!scriptvm)
{
Msg("Scripting disabled or no server running\n");
return;
}
const char *pszScript = args.GetCommandString();
pszScript += 6;
while (*pszScript == ' ')
{
pszScript++;
}
if (!*pszScript)
{
return;
}
if (*pszScript != '\"')
{
scriptvm->Run(pszScript);
}
else
{
pszScript++;
const char *pszEndQuote = pszScript;
while (*pszEndQuote != '\"')
{
pszEndQuote++;
}
if (!*pszEndQuote)
{
return;
}
*((char *)pszEndQuote) = 0;
scriptvm->Run(pszScript);
*((char *)pszEndQuote) = '\"';
}
}