8000 Make private functions properly private · uramer/CoreScripts@6a11fe9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6a11fe9

Browse files
committed
Make private functions properly private
Use timers for threadHandler scheduler
1 parent 16dffc4 commit 6a11fe9

File tree

10 files changed

+255
-247
lines changed

10 files changed

+255
-247
lines changed

lib/lua/jsonInterface.lua

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ end
1414

1515
local jsonInterface = {}
1616

17-
-- Remove all text from before the actual JSON content starts
18-
function jsonInterface.removeHeader(content)
17+
--
18+
-- private
19+
--
1920

21+
-- Remove all text from before the actual JSON content starts
22+
-- Only used for cjson, dkjson ignores comments on its own
23+
local function removeHeader(content)
2024
local closestBracketIndex
2125

2226
local bracketIndex1 = content:find("\n%[")
@@ -31,24 +35,23 @@ function jsonInterface.removeHeader(content)
3135
return content:sub(closestBracketIndex)
3236
end
3337

38+
--
39+
-- public
40+
--
41+
3442
function jsonInterface.load(fileName)
3543
local res = fileDrive.LoadAsync(fileName)
3644
if not res then
3745
error("Failed to load json file " .. fileName)
3846
end
39-
return jsonInterface.decode(res.content)
40-
end
41-
42-
43-
function jsonInterface.writeToFile(fileName, content)
44-
return fileDrive.SaveAsync(fileName, content)
47+
return jsonInterface.decode(res.content, fileName)
4548
end
4649

4750
-- Save data to JSON in a slower but human-readable way, with identation and a specific order
4851
-- to the keys, provided via dkjson
4952
function jsonInterface.save(fileName, data, keyOrderArray)
5053
local content = jsonInterface.encode(data, keyOrderArray)
51-
return jsonInterface.writeToFile(fileName, content)
54+
return fileDrive.SaveAsync(fileName, content)
5255
end
5356

5457
-- Save data to JSON in a fast but minimized way, provided via Lua CJSON, ideal for large files
@@ -57,23 +60,22 @@ function jsonInterface.quicksave(fileName, data)
5760
return jsonInterface.save(fileName, data)
5861
end
5962

60-
6163
-- Parse a JSON string and return it as a table
6264
-- fileName is an optional argument, used for logging
6365
function jsonInterface.decode(content, fileName)
6466
if cjsonExists then
6567
if content:sub(1, 2) == "//" then
66-
content = jsonInterface.removeHeader(content)
68+
content = removeHeader(content)
6769
end
6870
local decodedContent = nil
69-
local status, error = pcall(function() decodedContent = cjson.decode(content) end)
71+
local status, error = pcall(function()
72+
decodedContent = cjson.decode(content)
73+
end)
7074

7175
if status then
7276
return decodedContent
7377
else
74-
if not fileName then
75-
fileName = "string"
76-
end
78+
fileName = fileName or "string"
7779
tes3mp.LogMessage(enumerations.log.ERROR, "Could not decode " .. fileName .. " using Lua CJSON " ..
7880
"due to improperly formatted JSON! Error:\n" .. error .. "\n" .. fileName .. " is being decoded " ..
7981
"via the slower dkjson instead.")

scripts/drive/file.lua

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
local effil = require('effil')
21
local request = require("drive.file.request")
32

4-
local fileClient = {
3+
local fileDrive = {
54
thread = nil
65
}
76

8-
function fileClient.ThreadWork(input, output)
7+
--
8+
-- private
9+
--
10+
11+
local function ThreadWork(input, output)
912
local Run = require("drive.file.thread")
1013
Run(input, output)
1114
end
1215

13-
function fileClient.Initiate()
14-
fileClient.thread = threadHandler.CreateThread(fileClient.ThreadWork)
15-
fileClient.ProcessResponse(threadHandler.SendAsync(
16-
fileClient.thread,
17-
request.initiate()
18-
))
19-
end
20-
21-
function fileClient.ProcessResponse(res)
16+
local function ProcessResponse(res)
2217
if res.log then
2318
tes3mp.LogMessage(enumerations.log.INFO, "[FileClient] " .. res.log)
2419
end
@@ -27,38 +22,50 @@ function fileClient.ProcessResponse(res)
2722
end
2823
end
2924

30-
function fileClient.Send(req, callback)
31-
local res = threadHandler.Send(fileClient.thread, req, function(res)
32-
fileClient.ProcessResponse(res)
25+
local function Initiate()
26+
fileDrive.thread = threadHandler.CreateThread(ThreadWork)
27+
ProcessResponse(threadHandler.SendAsync(
28+
fileDrive.thread,
29+
request.initiate()
30+
))
31+
end
32+
33+
local function Send(req, callback)
34+
local res = threadHandler.Send(fileDrive.thread, req, function(res)
35+
ProcessResponse(res)
3336
callback(res)
3437
end)
3538
return res
3639
end
3740

38-
function fileClient.SendAsync(req)
39-
local res = threadHandler.SendAsync(fileClient.thread, req)
40-
fileClient.ProcessResponse(res)
41+
local function SendAsync(req)
42+
local res = threadHandler.SendAsync(fileDrive.thread, req)
43+
ProcessResponse(res)
4144
return res
4245
end
4346

44-
function fileClient.Save(path, content, callback)
45-
fileClient.Send(request.save(path, content), callback)
47+
--
48+
-- public
49+
--
50+
51+
function fileDrive.Save(path, content, callback)
52+
Send(request.save(path, content), callback)
4653
end
4754

48-
function fileClient.SaveAsync(path, content)
49-
return fileClient.SendAsync(request.save(path, content))
55+
function fileDrive.SaveAsync(path, content)
56+
return SendAsync(request.save(path, content))
5057
end
5158

52-
function fileClient.Load(path, callback)
53-
fileClient.Send(request.load(path), callback)
59+
function fileDrive.Load(path, callback)
60+
Send(request.load(path), callback)
5461
end
5562

56-
function fileClient.LoadAsync(path)
57-
return fileClient.SendAsync(request.load(path))
63+
function fileDrive.LoadAsync(path)
64+
return SendAsync(request.load(path))
5865
end
5966

6067
async.Wrap(function()
61-
fileClient.Initiate()
68+
Initiate()
6269
end)
6370

64-
return fileClient
71+
return fileDrive

scripts/drive/postgres.lua

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
local request = require("drive.postgres.request")
22

3-
local postgresDrive = {}
3+
local postgresDrive = {
4+
threads = {},
5+
currentJobs = {}
6+
}
47

5-
postgresDrive.threads = {}
8+
--
9+
-- private
10+
--
611

7-
postgresDrive.currentJobs = {}
8-
9-
function postgresDrive.ThreadWork(input, output)
12+
local function ThreadWork(input, output)
1013
local Run = require("drive.postgres.thread")
1114
Run(input, output)
1215
end
1316

14-
function postgresDrive.Initiate()
15-
for i = 1, config.postgres.threadCount do
16-
local threadId = threadHandler.CreateThread(postgresDrive.ThreadWork)
17+
local function Initiate()
18+
for _ = 1, config.postgres.threadCount do
19+
local threadId = threadHandler.CreateThread(ThreadWork)
1720
table.insert(postgresDrive.threads, threadId)
1821
postgresDrive.currentJobs[threadId] = 0
1922
end
@@ -41,7 +44,10 @@ function postgresDrive.Initiate()
4144
local doneMigrations = postgresDrive.QueryAsync([[SELECT * FROM migrations]])
4245
local doneTable = {}
4346
if doneMigrations.error then
44-
tes3mp.LogMessage(enumerations.log.INFO, "[Postgres] Seeding database for the first time, ignore the SQL error above!")
47+
tes3mp.LogMessage(
48+
enumerations.log.INFO,
49+
"[Postgres] Seeding database for the first time, ignore the SQL error above!"
50+
)
4551
ProcessMigration(0, "0000_migrations")
4652
else
4753
for i = 1, doneMigrations.count do
@@ -57,15 +63,15 @@ function postgresDrive.Initiate()
5763
end
5864
end
5965

60-
function postgresDrive.ProcessResponse(res)
66+
local function ProcessResponse(res)
6167
if res.error then
6268
tes3mp.LogMessage(enumerations.log.ERROR, "[Postgres] [[" .. res.error .. "]]")
6369
elseif res.log then
6470
tes3mp.LogMessage(enumerations.log.VERBOSE, "[Postgres] [[" .. res.log .. "]]")
6571
end
6672
end
6773

68-
function postgresDrive.ChooseThread()
74+
local function ChooseThread()
6975
local minThread = postgresDrive.threads[1]
7076
local min = postgresDrive.currentJobs[minThread]
7177
for _, thread in pairs(postgresDrive.threads) do
@@ -80,49 +86,53 @@ function postgresDrive.ChooseThread()
8086
return minThread
8187
end
8288

83-
function postgresDrive.StartJob(thread)
89+
local function StartJob(thread)
8490
postgresDrive.currentJobs[thread] = postgresDrive.currentJobs[thread] + 1
8591
end
8692

87-
function postgresDrive.FinishJob(thread)
93+
local function FinishJob(thread)
8894
postgresDrive.currentJobs[thread] = postgresDrive.currentJobs[thread] - 1
8995
if postgresDrive.currentJobs[thread] < 0 then
9096
postgresDrive.currentJobs[thread] = 0
9197
end
9298
end
9399

94-
function postgresDrive.Send(thread, req, callback)
95-
postgresDrive.StartJob(thread)
100+
local function Send(thread, req, callback)
101+
StartJob(thread)
96102
threadHandler.Send(
97103
thread,
98104
req,
99105
function(res)
100-
postgresDrive.FinishJob(thread)
101-
postgresDrive.ProcessResponse(res)
106+
FinishJob(thread)
107+
ProcessResponse(res)
102108
if callback ~= nil then
103109
callback(res)
104110
end
105111
end
106112
)
107113
end
108114

109-
function postgresDrive.SendAsync(thread, req)
110-
postgresDrive.StartJob(thread)
115+
local function SendAsync(thread, req)
116+
StartJob(thread)
111117
local res = threadHandler.SendAsync(
112118
thread,
113119
req
114120
)
115-
postgresDrive.FinishJob(thread)
116-
postgresDrive.ProcessResponse(res)
121+
FinishJob(thread)
122+
ProcessResponse(res)
117123
return res
118124
end
119125

126+
--
127+
-- public
128+
--
129+
120130
function postgresDrive.Connect(connectString)
121131
local results = {}
122132
for _, thread in pairs(postgresDrive.threads) do
123133
table.insert(
124134
results,
125-
postgresDrive.SendAsync(thread, request.Connect(connectString))
135+
SendAsync(thread, request.Connect(connectString))
126136
)
127137
end
128138
return results
@@ -132,7 +142,7 @@ function postgresDrive.ConnectAsync(connectString, timeout)
132142
local tasks = {}
133143
for _, thread in pairs(postgresDrive.threads) do
134144
table.insert(tasks, function()
135-
return postgresDrive.SendAsync(thread, request.Connect(connectString))
145+
return SendAsync(thread, request.Connect(connectString))
136146
end)
137147
end
138148
return async.WaitAll(tasks, timeout)
@@ -143,7 +153,7 @@ function postgresDrive.Disconnect()
143153
for _, thread in pairs(postgresDrive.threads) do
144154
table.insert(
145155
results,
146-
postgresDrive.SendAsync(thread, request.Disconnect())
156+
SendAsync(thread, request.Disconnect())
147157
)
148158
end
149159
return results
@@ -153,31 +163,31 @@ function postgresDrive.DisconnectAsync()
153163
local tasks = {}
154164
for _, thread in pairs(postgresDrive.threads) do
155165
table.insert(tasks, function()
156-
return postgresDrive.SendAsync(thread, request.Disconnect())
166+
return SendAsync(thread, request.Disconnect())
157167
end)
158168
end
159169
return async.WaitAll(tasks)
160170
end
161171

162172
function postgresDrive.Query(sql, parameters, callback, numericalIndices)
163-
local thread = postgresDrive.ChooseThread()
173+
local thread = ChooseThread()
164174
if numericalIndices then
165-
postgresDrive.Send(thread, request.QueryNumerical(sql, parameters), callback)
175+
Send(thread, request.QueryNumerical(sql, parameters), callback)
166176
else
167-
postgresDrive.Send(thread, request.Query(sql, parameters), callback)
177+
Send(thread, request.Query(sql, parameters), callback)
168178
end
169179
end
170180

171181
function postgresDrive.QueryAsync(sql, parameters, numericalIndices)
172-
local thread = postgresDrive.ChooseThread()
182+
local thread = ChooseThread()
173183
if numericalIndices then
174-
return postgresDrive.SendAsync(thread, request.QueryNumerical(sql, parameters))
184+
return SendAsync(thread, request.QueryNumerical(sql, parameters))
175185
else
176-
return postgresDrive.SendAsync(thread, request.Query(sql, parameters))
186+
return SendAsync(thread, request.Query(sql, parameters))
177187
end
178188
end
179189

180-
postgresDrive.Initiate()
190+
Initiate()
181191

182192
-- make sure the disconnect handler is the every last
183193
customEventHooks.registerHandler("OnServerPostInit", function(eventStatus)

0 commit comments

Comments
 (0)
0