-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimprove_mocks.py
More file actions
139 lines (118 loc) · 3.72 KB
/
Copy pathimprove_mocks.py
File metadata and controls
139 lines (118 loc) · 3.72 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
import sys
path = "/root/citizen-lua/runtime/citizen.lua"
with open(path, "r") as f:
lines = f.readlines()
config_discovery_lua = r"""
-- ---------------------------------------------------------------------------
-- Config Discovery System (Convars)
-- ---------------------------------------------------------------------------
local _convarRegistry = {}
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")
_convarRegistry[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 = { ".", "..", "../..", "../../..", "../../../..", "../../../../.." }
-- Also add script-relative paths if script path is available
local scriptPath = arg and arg[1]
if scriptPath then
local current = scriptPath
for i = 1, 6 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
-- Initialize discovery
_discoverConfigs()
function GetConvar(name, default)
return _convarRegistry[name] or os.getenv(name) or default
end
function GetConvarInt(name, default)
local v = _convarRegistry[name] or os.getenv(name)
return v and tonumber(v) or default
end
"""
# Same refactored _makeBag...
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
"""
# Apply improvements using line replacement
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, "w") as f:
f.writelines(new_lines)
print("Citizen.lua improved successfully.")