Skip to content

Commit d6fa8f2

Browse files
committed
Move scriptfiles environment variable setup to InitModule so paths are correctly set for some users. Add getenv_portable (linux) and some checks in setenv_portable so it doesn't override environment vars, highly untested, changes are more than welcome.
1 parent 422b841 commit d6fa8f2

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

amx-deps/src/CFunctions.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,6 @@ int CFunctions::amxLoadPlugin(lua_State *luaVM) {
127127

128128
loadedPlugins[pluginName] = pSampPlugin;
129129

130-
//Setup environment variables
131-
fs::path scriptfilespath = fs::path("mods/deathmatch/resources/amx/scriptfiles");
132-
if (exists(scriptfilespath)) {
133-
setenv_portable("AMXFILE", scriptfilespath.string().c_str(), 1);
134-
} else {
135-
pModuleManager->ErrorPrintf("scriptfiles directory doesn't exist at: %s\n", scriptfilespath.string());
136-
}
137-
138130
lua_pushboolean(luaVM, 1);
139131
return 1;
140132
}

amx-deps/src/ml_base.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
#include "StdInc.h"
2020

2121
#include <locale.h>
22+
#include <filesystem>
2223

2324
using namespace std;
25+
namespace fs = std::filesystem;
2426

2527
ILuaModuleManager10 *pModuleManager = NULL;
2628
lua_State *mainVM = NULL;
@@ -105,6 +107,20 @@ MTAEXPORT bool InitModule ( ILuaModuleManager10 *pManager, char *szModuleName, c
105107
PATH += ";mods/deathmatch/resources/amx/plugins/";
106108
setenv_portable("PATH", PATH.c_str(), 1);
107109

110+
//Setup environment variables
111+
fs::path scriptfilespath = fs::canonical(fs::current_path() / fs::path("mods/deathmatch/resources/amx/scriptfiles"));
112+
113+
const char* envvar = getenv_portable("MTA_SCRIPTFILESDIR");
114+
if (envvar != NULL)
115+
fs::path scriptfilespath = envvar;
116+
117+
if (exists(scriptfilespath)) {
118+
setenv_portable("AMXFILE", scriptfilespath.string().c_str(), 0);
119+
}
120+
else {
121+
pModuleManager->ErrorPrintf("scriptfiles directory doesn't exist at: %s\n", scriptfilespath.string());
122+
}
123+
108124
return true;
109125
}
110126

amx-deps/src/util.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,37 @@ extern map < AMX *, AMXPROPS > loadedAMXs;
1010

1111
int setenv_portable(const char* name, const char* value, int overwrite) {
1212
#ifdef WIN32
13+
if (!overwrite) {
14+
const char* envvar = getenv_portable(name);
15+
if (envvar != NULL) {
16+
return 1; //It's not null, we succeeded, don't set it
17+
}
18+
} //Otherwise continue and set it anyway
1319
return _putenv_s(name, value);
1420
#else
1521
return setenv(name, value, overwrite);
1622
#endif
1723
}
1824

25+
//Credit: https://stackoverflow.com/questions/4130180/how-to-use-vs-c-getenvironmentvariable-as-cleanly-as-possible
26+
const char* getenv_portable(const char* name)
27+
{
28+
#ifdef WIN32
29+
const DWORD buffSize = 65535;
30+
static char buffer[buffSize];
31+
if (GetEnvironmentVariableA(name, buffer, buffSize))
32+
{
33+
return buffer;
34+
}
35+
else
36+
{
37+
return 0;
38+
}
39+
#else
40+
return getenv(name);
41+
#endif
42+
}
43+
1944
#ifndef WIN32
2045
void *getProcAddr ( HMODULE hModule, const char *szProcName )
2146
{

amx-deps/src/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
// Util functions
3131
int setenv_portable(const char* name, const char* value, int overwrite);
32+
const char* getenv_portable(const char* name);
3233
std::string ToUTF8(const char * str);
3334
std::string ToOriginalCP(const char * str);
3435
void lua_pushamxstring(lua_State *luaVM, AMX *amx, cell addr);

0 commit comments

Comments
 (0)