Repository navigation
Expand file tree
/
Copy pathxrLuaFix.cpp
More file actions
75 lines (60 loc) · 1.98 KB
/
Copy pathxrLuaFix.cpp
File metadata and controls
75 lines (60 loc) · 1.98 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
#include "pch.hpp"
#include "xrLuaFix.h"
#include "LibStr.h"
#include "LibMath.h"
#include "LibTab.h"
#include "Logger.h"
#include "LibCompat.h"
#include "lfs.h"
#include <string.h>
extern "C" int luaopen_lua_pack(lua_State* L);
extern "C" int luaopen_marshal(lua_State* L);
// Checks global _VERSION and jit.version_num for signs of LuaJIT 1.x.x
static bool is_luajit_one(lua_State* L) noexcept
{
lua_getglobal(L, "_VERSION");
const char* version = lua_tostring(L, -1);
bool is_luajit_one = version && strstr(version, "LuaJIT 1.");
lua_pop(L, 1);
if (is_luajit_one)
return true;
lua_getglobal(L, "jit");
if (!lua_istable(L, -1))
lua_pop(L, 1);
else
{
lua_getfield(L, -1, "version_num");
const int version_num = static_cast<int>(lua_tointeger(L, -1));
is_luajit_one = version_num >= 10000 && version_num < 20000;
lua_pop(L, 2);
}
return is_luajit_one;
}
// Original xrLuaFix was extending lua libs only when global RvP function is called
// Starting from Call of Chernobyl, engine always does that.
int luaopen_xrluafix(lua_State* L)
{
// Original xrLuaFix for Shadow of Chernobyl
luaL_dostring(L, "RvP = function() end");
// Anomaly compatibility
luaL_register(L, "lua_extensions", nullptr);
open_string(L);
open_table(L);
open_log(L);
// LuaJIT 2.x generally has good RNG,
// so override math library only for LuaJIT 1.x.x.
// Don't touch if it's not LuaJIT either.
if (is_luajit_one(L))
open_math(L);
else
open_stkcompat(L);
luaopen_lua_pack(L);
luaopen_marshal(L);
// luafilesystem adheres to modules-create-no-globals
// (https://github.com/lunarmodules/luafilesystem/commit/8e7217e74fbe5da0a9c1fee03d191b5a0266cedd),
// thus requiring you to write "local lfs = require("lfs")" in scripts.
luaopen_lfs(L);
// But we want to create globals exactly, so that you can use it right away.
lua_setglobal(L, "lfs");
return 0;
}