-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadController.lua
More file actions
197 lines (169 loc) · 5.9 KB
/
threadController.lua
File metadata and controls
197 lines (169 loc) · 5.9 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
local PATH = (...):match("^(.-)[^%.]+$")
local mintmousse = require(PATH .. "conf")
local threadCommand = require(PATH .. "threadCommand")
local loggerController = mintmousse._logger:extend("Controller")
local iconLogger = loggerController:extend("Icon")
local threadController = {
threadChannel = love.thread.getChannel(mintmousse.READONLY_THREAD_LOCATION)
}
threadController.start = function(config)
local thread = threadController.threadChannel:peek()
if not thread:isRunning() then
thread:start(PATH)
end
if config then
if config.title ~= nil and type(config.title) ~= "string" then
loggerController:warning("config.title expected to be type string")
config.title = nil
end
if config.host ~= nil and type(config.host) ~= "string" then
loggerController:warning("config.host expected to be type string")
config.host = nil
end
if config.port ~= nil and type(config.port) ~= "number" then
loggerController:warning("config.port expected to be type number")
config.port = nil
end
if config.autoIncrement ~= nil then
config.autoIncrement = config.autoIncrement == true
end
if config.whitelist ~= nil then
threadController.addToWhitelist(config.whitelist, "config.whitelist")
end
end
-- Whitelist is sent separately, so remove it from config, and added
-- back after so we don't break the config table
local whitelist
if config then
whitelist, config.whitelist = config.whitelist, nil
end
threadCommand.call("start", {
config = config,
})
if config then
config.whitelist = whitelist
end
end
threadController.stop = function(noWait)
threadCommand.commandQueue:performAtomic(function(channel)
channel:clear()
threadCommand.call("quit")
end)
if not noWait then
threadController.wait()
end
end
threadController.wait = function()
threadController.threadChannel:performAtomic(function(channel)
local thread = channel:peek()
thread:wait()
end)
end
threadController.addToWhitelist = function(additions, context)
if additions == nil then return end
local source = context or "additions"
if type(additions) == "table" then
if #additions == 0 then return end
for i, addition in ipairs(additions) do
if type(addition) ~= "string" then
loggerController:warning(source .. "[" .. tostring(i) .. "] expected to be type string")
return
end
end
elseif type(additions) ~= "string" then
loggerController:warning(source, "expected to be type table or string")
return
end
threadCommand.call("addToWhitelist", { additions = additions })
end
threadController.removeFromWhitelist = function(removals)
if removals == nil then return end
if type(removals) == "table" then
if #removals == 0 then return end
for i, removal in ipairs(removals) do
if type(removal) ~= "string" then
loggerController:warning("removals[" .. tostring(i) .. "] expected to be type string")
return
end
end
elseif type(removals) ~= "string" then
loggerController:warning("removals expected to be type table or string")
return
end
threadCommand.call("removeFromWhitelist", { removals = removals })
end
threadController.clearWhitelist = function()
threadCommand.call("clearWhitelist")
end
local pngMagicNumber = string.char(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a)
local jpegMagicNumber = string.char(0xff, 0xd8, 0xff)
threadController.setIcon = function(icon)
if type(icon) == "userdata" then
if icon.typeOf and icon:typeOf("Data") then
icon = icon:getString()
end
end
if type(icon) == "table" then
threadCommand.call("setSchemaIcon", {
icon = icon,
})
return
elseif type(icon) == "string" then
if lfs.getInfo(icon, "file") then
local temp = icon:lower()
local extension = icon:lower():match("%.(%w+)$")
if extension == "png" or extension == "jpeg" or extension == "jpg" or
extension == "svg" or extension == "ico" then
threadCommand.call("setIconFromFile", {
filepath = icon,
})
return
end
iconLogger:warning("Valid file provided, invalid file extension. Only .PNG, .JPEG, .JPG, .ICO, or .SVG are supported for icon from file.")
return
elseif icon:sub(#pngMagicNumber) == pngMagicNumber then
threadController.setIconRaw(icon, "image/png")
return
elseif icon:sub(#jpegMagicNumber) == jpegMagicNumber then
threadController.setIconRaw(icon, "image/jpeg")
return
end
end
iconLogger:warning("Invalid icon provided. Please supply either an SVG schema table, a file path to a .PNG, .JPEG, .JPG, or .SVG image, or raw PNG or JPEG image data (identified by their magic numbers).")
end
threadController.setIconRaw = function(icon, iconType)
threadCommand.call("setIconRaw", {
icon = icon,
iconType = iconType,
})
end
-- https://realfavicongenerator.net @ 2026 Q1
threadController.setIconRFG = function(filepath)
iconLogger:assert(type(filepath) == "string", "Filepath must be type String")
iconLogger:assert(filepath:lower():match("%.zip$"), "Invalid filepath, must end with .ZIP file extension. Gave:", filepath)
iconLogger:assert(love.filesystem.getInfo(filepath), "Invalid filepath, couldn't find file at given path. Gave:", filepath)
threadCommand.call("setIconRFG", {
filepath = filepath,
})
end
threadController.setTile = function(title)
loggerController:assert(type(title) == "string", "Title must be type String")
threadCommand.call("setTitle", {
title = title,
})
end
threadController.notify = function(message)
if type(message) == "string" then
message = {
text = message,
}
end
loggerController:assert(type(message) == "table", "Message must be type Table")
if not message.title and not message.text then
return -- If we have nothing to send; why send it?
end
threadCommand.call("notify", {
message = message,
})
end
return threadController