-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimprove_mocks_v2.py
More file actions
146 lines (127 loc) · 4.22 KB
/
Copy pathimprove_mocks_v2.py
File metadata and controls
146 lines (127 loc) · 4.22 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
import sys
import re
# 1. Patch citizen.lua
path_citizen = "/root/citizen-lua/runtime/citizen.lua"
with open(path_citizen, "r") as f:
lines = f.readlines()
config_discovery_lua = r"""
-- ---------------------------------------------------------------------------
-- Config Discovery System (Convars)
-- ---------------------------------------------------------------------------
__cfx_convar_registry = __cfx_convar_registry or {}
local function _parseCfg(filePath)
local f = io.open(filePath, "r")
if not f then return end
local dir = filePath:match("(.*[\\/])") or ""
for line in f:lines() do
local key, value = line:match("^%s*setr?%s+([^%s]+)%s+(.-)%s*$")
if key and value then
-- Strip trailing comments
value = value:gsub("%s*#.*$", "")
-- Strip surrounding quotes
value = value:gsub('^"(.-)"$', "%1"):gsub("^'(.-)'$", "%1")
-- Trim whitespace
value = value:gsub("^%s*(.-)%s*$", "%1")
__cfx_convar_registry[key] = value
end
local execFile = line:match("^%s*exec%s+[\"']?(.-)['\"]?%s*$")
if execFile then
_parseCfg(dir .. execFile)
end
end
f:close()
end
local function _discoverConfigs()
local searchPaths = { ".", "..", "../..", "../../..", "../../../..", "../../../../.." }
local scriptPath = arg and arg[1]
if scriptPath then
local current = scriptPath
for i = 1, 10 do
local parent = current:match("(.*)[/\\/]")
if parent then
table.insert(searchPaths, parent)
current = parent
else break end
end
end
for _, base in ipairs(searchPaths) do
local cfgPath = base .. "/server.cfg"
local f = io.open(cfgPath, "r")
if f then
f:close()
_parseCfg(cfgPath)
break
end
end
end
_discoverConfigs()
function GetConvar(name, default)
return __cfx_convar_registry[name] or os.getenv(name) or default
end
function GetConvarInt(name, default)
local v = __cfx_convar_registry[name] or os.getenv(name)
return v and tonumber(v) or default
end
"""
make_bag_lua = """
local function _makeBag(bagId)
if not _bagStore[bagId] then _bagStore[bagId] = {} end
local bag = {
set = function(self, key, value, replicated) _bagStore[bagId][key] = value end,
get = function(self, key) return _bagStore[bagId][key] end
}
return setmetatable(bag, {
__index = function(t, key)
if key == "set" or key == "get" then return bag[key] end
return _bagStore[bagId][key]
end,
__newindex = function(_, key, value) _bagStore[bagId][key] = value end,
__tostring = function(_) return string.format("StateBag(%s)", bagId) end
})
end
"""
new_lines = []
skip = False
for line in lines:
if "local function _makeBag(bagId)" in line:
new_lines.append(make_bag_lua + "\n")
skip = True
continue
if skip and "GlobalState =" in line:
skip = False
new_lines.append(line)
continue
if "function GetConvar(name, default)" in line:
new_lines.append(config_discovery_lua + "\n")
skip = True
continue
if skip and "PerformHttpRequest" in line:
skip = False
new_lines.append(line)
continue
if not skip: new_lines.append(line)
with open(path_citizen, "w") as f:
f.writelines(new_lines)
# 2. Patch fxserver.lua properly
path_fx = "/root/citizen-lua/runtime/fxserver.lua"
with open(path_fx, "r") as f:
lines_fx = f.readlines()
new_lines_fx = []
skip_fx = False
for line in lines_fx:
if "function GetConvar(name, default)" in line:
new_lines_fx.append("-- (using citizen.lua GetConvar)\n")
skip_fx = True
continue
if "function GetConvarInt(name, default)" in line:
new_lines_fx.append("-- (using citizen.lua GetConvarInt)\n")
skip_fx = True
continue
if skip_fx and line.strip() == "end":
skip_fx = False
continue
if not skip_fx:
new_lines_fx.append(line)
with open(path_fx, "w") as f:
f.writelines(new_lines_fx)
print("Citizen.lua and FxServer.lua patched.")