|
1 | 1 | -- TRIGON.IM 12 dec 2021 |
2 | 2 | -- Упрощенная версия texture либы от dash |
| 3 | +-- 2024.12.27 dec 2024 добавлена проверка is_normal_image, чтобы всякие 429 и 403 от imgur не кешировали говно |
3 | 4 |
|
4 | 5 | matex = matex or {} |
5 | 6 |
|
6 | 7 | file.CreateDir("matex") |
7 | 8 |
|
8 | | -function matex.url(url, useproxy) |
| 9 | +local PNG_START = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A} |
| 10 | +local PNG_TRAIL = {0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82} |
| 11 | +local is_png = function(raw) |
| 12 | + for i = 1, 8 do |
| 13 | + if PNG_START[i] ~= string.byte(raw, i) then return false end |
| 14 | + if PNG_TRAIL[i] ~= string.byte(raw, -(9 - i)) then return false end |
| 15 | + end |
| 16 | + return true |
| 17 | +end |
| 18 | + |
| 19 | + |
| 20 | +local JPG_START = {0xFF, 0xD8, 0xFF} |
| 21 | +local JPG_TRAIL = {0xFF, 0xD9} |
| 22 | +local is_jpg = function(raw) |
| 23 | + for i = 1, 3 do |
| 24 | + if JPG_START[i] ~= string.byte(raw, i) then return false end |
| 25 | + if i == 3 then break end |
| 26 | + if JPG_TRAIL[i] ~= string.byte(raw, -(3 - i)) then return false end |
| 27 | + end |
| 28 | + return true |
| 29 | +end |
| 30 | + |
| 31 | + |
| 32 | +-- https://mimesniff.spec.whatwg.org/#matching-an-image-type-pattern |
| 33 | +-- https://www.garykessler.net/library/file_sigs.html |
| 34 | +local function is_normal_image(raw) |
| 35 | + local png, jpg = is_png(raw), is_jpg(raw) |
| 36 | + return png or jpg |
| 37 | +end |
| 38 | + |
| 39 | +function matex.download(url, callback, useproxy) |
9 | 40 | local id = util.CRC(url) |
10 | 41 |
|
11 | 42 | local filepath = "matex/" .. id .. ".png" |
12 | 43 | local matpath = "../data/matex/" .. id .. ".png" |
13 | 44 |
|
14 | | - local def = {material = nil} |
15 | | - |
16 | 45 | if file.Exists(filepath, "DATA") then |
17 | | - def.material = Material(matpath, "noclamp smooth") |
18 | | - return def |
| 46 | + callback( Material(matpath, "noclamp smooth") ) |
| 47 | + return |
19 | 48 | end |
20 | 49 |
|
21 | 50 | local baseurl = useproxy and "https://proxy.duckduckgo.com/iu/?u=" .. url or url |
22 | | - |
23 | 51 | http.Fetch(baseurl, function(body) |
24 | | - file.Write(filepath, body) |
25 | | - def.material = Material(matpath, "noclamp smooth") |
26 | | - return def.material |
27 | | - end, function(error) |
28 | | - if useproxy then def.material = Material("nil") return def end |
29 | | - return matex.url(url, true) |
| 52 | + if is_normal_image(body) then file.Write(filepath, body) end |
| 53 | + callback( Material(matpath, "noclamp smooth") ) |
| 54 | + end, function() |
| 55 | + if useproxy then callback( Material("nil") ) return end |
| 56 | + matex.download(url, callback, true) |
30 | 57 | end) |
31 | | - |
32 | | - return def |
33 | 58 | end |
34 | 59 |
|
35 | | --- function matex.imgur(id) |
36 | | --- return matex.url("https://i.imgur.com/" .. id .. ".png") |
37 | | --- end |
38 | | - |
39 | | --- local furl = fl.memoize(matex.url) |
40 | | --- function matex.url_async(url) |
41 | | --- return furl(url).material |
42 | | --- end |
43 | | - |
44 | | --- function matex.imgur_async(id) |
45 | | --- return furl("https://i.imgur.com/" .. id .. ".png").material |
46 | | --- end |
47 | | - |
48 | | - |
| 60 | +local cache = {} |
| 61 | +function matex.now(url) |
| 62 | + if cache[url] then return cache[url].material end |
| 63 | + cache[url] = {material = nil} |
| 64 | + matex.download(url, function(material) cache[url].material = material end) |
| 65 | + return cache[url].material |
| 66 | +end |
49 | 67 |
|
50 | | -/* example |
51 | | -local mater = matex.imgur("TZcJ1CK") |
52 | 68 |
|
| 69 | +--[[ |
| 70 | +-- example: |
53 | 71 | hook.Add("HUDPaint", "mater", function() |
54 | | - if not mater.material then return end |
55 | | -
|
56 | | - surface.SetDrawColor(color_white) |
57 | | - surface.SetMaterial(mater.material) |
58 | | - surface.DrawTexturedRect(35, 35, 570, 460) |
| 72 | + local mater = matex.now("https://i.imgur.com/TZcJ1CK.png") |
| 73 | + if mater then |
| 74 | + surface.SetDrawColor(color_white) |
| 75 | + surface.SetMaterial(mater) |
| 76 | + surface.DrawTexturedRect(35, 35, 570, 460) |
| 77 | + end |
59 | 78 | end) |
60 | 79 | -- hook.Remove("HUDPaint", "mater") |
61 | | -*/ |
| 80 | +--]] |
0 commit comments