-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
428 lines (342 loc) · 11 KB
/
init.lua
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
local lib = {
_VERSION = 2.2,
_URL = "https://github.com/Be1zebub/Emoji.lua",
_LICENSE = [[
MIT LICENSE
Copyright (c) 2022 incredible-gmod.ru
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
}
--—————————————————— Helpers ——————————————————--
local fs, httpGET, jsonDecode
if gmod then -- gmod compat
fs, httpGET, jsonDecode = file, http.Fetch, util.JSONToTable
elseif process and process.argv and (function()
for _, arg in pairs(process.argv) do
if arg:find("luvit", 1, true) then
return true
end
end
return false
end)() then -- luvit compat
fs = {
Write = require("fs").writeFileSync,
Read = require("fs").readFileSync,
CreateDir = require("fs").mkdirSync
}
local request = require("coro-http").request
httpGET = function(url, cback)
local success, res, response = pcall(request, "GET", url)
if success then
cback(response)
end
end
jsonDecode = require("json").decode
end
fs.CreateDir("emoji.lua")
fs.CreateDir("emoji.lua/db")
fs.CreateDir("emoji.lua/assets")
local function ReadOrFetch(path, url, cback)
if fs.Exists(path, "DATA") then
cback(fs.Read(path, "DATA"))
return true
else
httpGET(url, function(response)
fs.Write(path, response)
cback(response)
end)
end
end
--—————————————————— Emoji meta ——————————————————--
do
local Emoji = {}
Emoji.__index = Emoji
Emoji.IsEmoji = true
function Emoji:Path(assetPack)
assetPack = isstring(assetPack) and assetPack or (
self.parent.assets[assetPack] or
self.parent.defaultAssets
)
return "assets/".. assetPack .."/".. self.uid ..".png"
end
-- "1f44d-1f3fd", "emoji/twitter" > "emoji/twitter/1f44d-1f3fd.png"
function Emoji:URL(assetPack)
return self.parent.raw .. self:Path(assetPack)
end
-- "1f44d-1f3fd", "emoji/google" > "https://raw.githubusercontent.com/Be1zebub/Emoji.lua/assets/emoji/google/1f44d-1f3fd.png"
function Emoji:Download(assetPack)
local running = coroutine.running()
httpGET(self:URL(assetPack), function(raw, _, _, code)
if code ~= 200 or raw:find("<!DOCTYPE HTML>", 1, true) then
coroutine.resume(running)
else
coroutine.resume(running, raw)
end
end)
return coroutine.yield()
end
-- "1f44d-1f3fd", "emoji/twitter" > response from https://raw.githubusercontent.com/Be1zebub/Emoji.lua/assets/emoji/google/1f44d-1f3fd.png
--[[
coroutine.wrap(function()
local raw = Emojis:Download("1f44d-1f3fd", "twitter")
end)()
]]--
if gmod then
function Emoji:Material(assetPack, flags)
local running = coroutine.running()
local path ="emoji.lua/".. self:Path(assetPack)
local mat
if ReadOrFetch(path, self:URL(assetPack), function()
mat = Material("data/".. path, flags or "smooth mips")
coroutine.resume(running, mat)
end) then
return mat
end
return coroutine.yield()
end
--[[
coroutine.wrap(function()
local imaterial = Emojis:Material("1f44d-1f3fd", "apple")
end)()
]]--
end
function Emoji:__tostring()
return string.format("Emote(%q, %q)", self.parent.dbName, self.uid)
end
function lib.Emoji(uid, parent)
return setmetatable({parent = parent, uid = uid}, Emoji)
end
end
--—————————————————— Emojis meta ——————————————————--
do
local Emojis = {}
Emojis.__index = Emojis
function Emojis:Lookup(alias)
local emoji = self.aliases[alias:gsub(":", "")]
if emoji then
emoji = lib.Emoji(emoji, self)
end
return emoji
end
-- ":airplane:" > Emote("emoji", "2708-fe0f")
function Emojis:Search(query, isPattern, storage)
isPattern = isPattern or false
storage = storage or {}
for _, data in ipairs(self.db) do
for _, alias in ipairs(data.aliases) do
if alias:find(query, 1, isPattern) then
storage[#storage + 1] = self:Lookup(alias)
end
end
end
return storage
end
-- "thumb" > {Emote("emoji", "1f44d"), Emote("emoji", "1f44e")}
function Emojis:Parse(text)
local out = {}
for alias in text:gmatch(":[%w%p]+:") do
local emote = self:Lookup(alias)
if emote then
local start, finish = text:find(alias)
local pre = text:sub(1, start - 1)
if #pre > 0 then
out[#out + 1] = pre
end
text = text:sub(finish + 1)
out[#out + 1] = emote
end
end
if #text > 0 then
out[#out + 1] = text
end
return out
end
-- ":wave: world! I love :new_moon:️" > {Emoji("emoji", "1f44b"), "world! I love", Emoji("emoji", "1f311")}
function lib.Emojis(repo, dbName, defaultAssets, branch)
local running = coroutine.running()
local instance = setmetatable({
dbName = dbName,
db = {},
aliases = {},
assets = {},
raw = "https://raw.githubusercontent.com/".. repo .."/".. (branch or "master") .."/"
}, Emojis)
if ReadOrFetch("emoji.lua/db/".. dbName ..".json", instance.raw .."db/".. dbName ..".json", function(json)
local db = jsonDecode(json)
instance.assets = db.assets
instance.db = db.emoji
instance.defaultAssets = defaultAssets or db.defaultAssets or instance.assets[1]
for _, dir in ipairs(instance.assets) do
fs.CreateDir("emoji.lua/assets/".. dir)
end
for _, data in ipairs(instance.db) do
for _, alias in ipairs(data.aliases) do
instance.aliases[alias] = data.uid
end
end
coroutine.resume(running, instance)
end) then
return instance
end
return coroutine.yield()
end
end
--—————————————————— Collection ——————————————————--
do
local Collection = {}
Collection.__index = Collection
function Collection:Add(emotes)
self.list[#self.list + 1] = emotes
return self
end
function Collection:Lookup(alias)
for _, emotes in ipairs(self.list) do
local emote = emotes:Lookup(alias)
if emote then
return emote
end
end
end
function Collection:Search(query, isPattern, storage)
storage = storage or {}
for _, emotes in ipairs(self.list) do
emotes:Search(query, isPattern, storage)
end
return storage
end
function Collection:Parse(text)
return self.list[1].Parse(self, text)
end
function lib.Collection(...)
local instance = setmetatable({list = {}}, Collection)
if ... then
for _, emotes in ipairs({...}) do
instance:Add(emotes)
end
end
return instance
end
end
--—————————————————— Debug menu ——————————————————--
do
function lib.Debug()
surface.CreateFont("Emoji.lua", {
size = 20,
font = "Roboto",
extended = true,
antialias = true
})
surface.CreateFont("Emoji.lua/Entry", {
size = 16,
font = "Roboto",
extended = true,
antialias = true
})
coroutine.wrap(function()
local collection = lib.Collection()
:Add(lib.Emojis("Be1zebub/Emoji.lua", "emoji", "emoji/noto"))
:Add(lib.Emojis("Be1zebub/Emoji.lua", "elite-pepe"))
:Add(lib.Emojis("Be1zebub/Emoji.lua", "elite-gorgeous"))
:Add(lib.Emojis("Be1zebub/Emoji.lua", "elite-roflan"))
local menu = vgui.Create("DFrame")
menu:SetSize(440, 300)
menu:Center()
menu:MakePopup()
menu:SetTitle("Emoji.lua v".. (math.floor(lib._VERSION) == lib._VERSION and lib._VERSION ..".0" or lib._VERSION))
menu:DockPadding(11, 26 + 11, 11, 11)
menu.Paint = function(me, w, h)
surface.SetDrawColor(54, 57, 63)
surface.DrawRect(0, 0, w, h)
surface.SetDrawColor(32, 34, 37)
surface.DrawRect(0, 0, w, 26)
end
local scroll = menu:Add("DScrollPanel")
scroll:Dock(FILL)
scroll.AddText = function(me, text)
local msg = me:Add("EditablePanel")
msg:Dock(TOP)
msg:DockMargin(0, 8, 0, 0)
coroutine.wrap(function()
for _, part in ipairs(collection:Parse(text)) do
if part.IsEmoji then
if inc_ui then
local img = msg:Add("inc_ui/material")
img:Dock(LEFT)
img:SetWide(msg:GetTall())
img:SetIcon(part:URL())
else
local img = msg:Add("DImage")
img:Dock(LEFT)
img:SetWide(msg:GetTall())
img:SetMaterial(part:Material(nil, "smooth mips"))
end
else
local txt = msg:Add("DLabel")
txt:Dock(LEFT)
txt:SetText(part)
txt:SetFont("Emoji.lua")
txt:SizeToContents()
end
end
end)()
end
local entry = menu:Add("DTextEntry")
entry:Dock(BOTTOM)
entry:SetTall(32)
entry:SetTextColor(Color(235, 235, 235))
entry:SetCursorColor(Color(220, 220, 220))
entry:SetFont("Emoji.lua/Entry")
entry.OnEnter = function(me)
scroll:AddText(me:GetValue())
end
entry.Paint = function(me, w, h)
draw.RoundedBox(6, 0, 0, w, h, Color(64, 68, 75))
me:DrawTextEntryText(me:GetTextColor(), me:GetHighlightColor(), me:GetCursorColor())
end
scroll:AddText(":wtf: Gorgeous freeman :pleasure: emote pack :not_bad:")
scroll:AddText(":wave: world! I love :new_moon_with_face:")
scroll:AddText("i have :medal_sports:, because im cool :owo:")
scroll:AddText(":hmm: hmm, seems good :very_dovolen:")
end)()
end
--lib.Debug()
end
--—————————————————— Emoji helpers ——————————————————--
do
local skinTones = {
"1f3fb", -- light
"1f3fc", -- medium light
"1f3fd", -- medium
"1f3fe", -- medium dark
"1f3ff" -- dark
}
function lib.Codepoints(emoji, skinTone)
local out = {}
for _, c in utf8.codes(emoji) do
out[#out + 1] = string.format("%x", c)
end
out = table.concat(out, "-")
if skinTone and skinTones[skinTone] then
out = out .."-".. skinTones[skinTone]
end
return out
end
-- "✈️" > 2708-fe0f
-- "👍", 3 > 1f44d-1f3fd
end
return lib