Skip to content

Commit a3260ca

Browse files
committed
try some fucked up shit
1 parent 5ccba07 commit a3260ca

File tree

4 files changed

+262
-119
lines changed

4 files changed

+262
-119
lines changed

package.json

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
1-
{
2-
"name": "asprite-dmi",
3-
"displayName": "DMI Editor",
4-
"description": "DMI Editor Extension for Aseprite",
5-
"version": "1.1.0",
6-
"contributors": [
7-
{
8-
"name": "Seefaaa",
9-
"url": "https://github.com/Seefaaa"
10-
},
11-
{
12-
"name": "Space Station 13 Community",
13-
"url": "https://github.com/spacestation13"
14-
}
15-
],
16-
"publisher": "Space Station 13 Community",
17-
"license": "GPL-3.0",
18-
"categories": [ "Scripts" ],
19-
"contributes": {
20-
"scripts": [
21-
{ "path": "./scripts/classes/cache.lua" },
22-
{ "path": "./scripts/classes/preferences.lua" },
23-
{ "path": "./scripts/classes/editor/_editor.lua" },
24-
{ "path": "./scripts/classes/editor/rendering.lua" },
25-
{ "path": "./scripts/classes/editor/state.lua" },
26-
{ "path": "./scripts/classes/statesprite.lua" },
27-
{ "path": "./scripts/classes/widget.lua" },
28-
{ "path": "./scripts/functions/other.lua" },
29-
{ "path": "./scripts/functions/string.lua" },
30-
{ "path": "./scripts/functions/table.lua" },
31-
{ "path": "./scripts/constants.lua" },
32-
{ "path": "./scripts/main.lua" }
33-
]
34-
}
35-
}
1+
{
2+
"name": "asprite-dmi",
3+
"displayName": "DMI Editor",
4+
"description": "DMI Editor Extension for Aseprite",
5+
"version": "1.1.0",
6+
"contributors": [
7+
{
8+
"name": "Seefaaa",
9+
"url": "https://github.com/Seefaaa"
10+
},
11+
{
12+
"name": "Space Station 13 Community",
13+
"url": "https://github.com/spacestation13"
14+
}
15+
],
16+
"publisher": "Space Station 13 Community",
17+
"license": "GPL-3.0",
18+
"categories": [ "Scripts" ],
19+
"contributes": {
20+
"scripts": [
21+
{ "path": "./scripts/classes/cache.lua" },
22+
{ "path": "./scripts/classes/preferences.lua" },
23+
{ "path": "./scripts/classes/editor/_editor.lua" },
24+
{ "path": "./scripts/classes/editor/rendering.lua" },
25+
{ "path": "./scripts/classes/editor/state.lua" },
26+
{ "path": "./scripts/classes/statesprite.lua" },
27+
{ "path": "./scripts/classes/widget.lua" },
28+
{ "path": "./scripts/functions/inception.lua" },
29+
{ "path": "./scripts/functions/other.lua" },
30+
{ "path": "./scripts/functions/string.lua" },
31+
{ "path": "./scripts/functions/table.lua" },
32+
{ "path": "./scripts/constants.lua" },
33+
{ "path": "./scripts/main.lua" }
34+
]
35+
}
36+
}

scripts/functions/inception.lua

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
--- @diagnostic disable: lowercase-global
2+
3+
-- shits fucked
4+
5+
local LUA_VERSION = "5.4.6"
6+
local LUA_DOWNLOAD_URL = string.format("https://www.lua.org/ftp/lua-%s.tar.gz", LUA_VERSION)
7+
8+
--- Downloads a file using available system tools
9+
--- @param url string URL to download from
10+
--- @param output string Output file path
11+
--- @return boolean success, string? error
12+
function download_file(url, output)
13+
-- Try curl first
14+
local curl_success = os.execute(string.format("curl -L -o %q %q", output, url))
15+
if curl_success then
16+
return true
17+
end
18+
19+
-- Try wget as fallback
20+
local wget_success = os.execute(string.format("wget -O %q %q", output, url))
21+
if wget_success then
22+
return true
23+
end
24+
25+
return false, "Neither curl nor wget are available"
26+
end
27+
28+
--- Extracts a tar.gz file
29+
--- @param archive string Path to archive
30+
--- @param destination string Destination directory
31+
--- @return boolean success, string? error
32+
function extract_archive(archive, destination)
33+
local success = os.execute(string.format("tar -xzf %q -C %q", archive, destination))
34+
return success == true, success and nil or "Failed to extract archive"
35+
end
36+
37+
--- Check if required build tools are available
38+
--- @return boolean
39+
function check_build_tools()
40+
local success = os.execute("which make >/dev/null 2>&1")
41+
if not success then
42+
return false
43+
end
44+
success = os.execute("which gcc >/dev/null 2>&1")
45+
return success == true
46+
end
47+
48+
--- Downloads and prepares Lua source code
49+
--- @param build_dir string Build directory
50+
--- @return boolean success, string? error
51+
local function prepare_lua_source(build_dir)
52+
-- Create build directory if it doesn't exist
53+
app.fs.makeAllDirectories(build_dir)
54+
55+
-- Download Lua source
56+
local archive = app.fs.joinPath(build_dir, "lua.tar.gz")
57+
local success, err = download_file(LUA_DOWNLOAD_URL, archive)
58+
if not success then
59+
return false, "Failed to download Lua source: " .. (err or "unknown error")
60+
end
61+
62+
-- Extract archive
63+
success, err = extract_archive(archive, build_dir)
64+
if not success then
65+
return false, err
66+
end
67+
68+
-- Remove archive
69+
os.remove(archive)
70+
71+
return true
72+
end
73+
74+
--- Compiles Lua for Unix-like systems
75+
--- @param plugin_path string Path where the extension is installed
76+
--- @return boolean success, string? error
77+
function compile_lua(plugin_path)
78+
if not check_build_tools() then
79+
return false, "Required build tools (make, gcc) not found"
80+
end
81+
82+
local build_dir = app.fs.joinPath(app.fs.tempPath, "lua_build")
83+
local success, err = prepare_lua_source(build_dir)
84+
if not success then
85+
return false, err
86+
end
87+
88+
local src_dir = app.fs.joinPath(build_dir, "lua-" .. LUA_VERSION)
89+
90+
-- Configure make for shared library
91+
local commands = {
92+
string.format("cd %q", src_dir),
93+
"make clean",
94+
"make MYCFLAGS=-fPIC MYLDFLAGS=-shared linux"
95+
}
96+
97+
-- Execute build commands
98+
for _, cmd in ipairs(commands) do
99+
local success = os.execute(cmd)
100+
if not success then
101+
return false, "Failed to execute: " .. cmd
102+
end
103+
end
104+
105+
-- Copy resulting library to plugin directory
106+
local lib_name = app.os.macos and "liblua.dylib" or "liblua.so"
107+
local src_lib = app.fs.joinPath(src_dir, "src", lib_name)
108+
local dest_lib = app.fs.joinPath(plugin_path, lib_name)
109+
110+
if not app.fs.isFile(src_lib) then
111+
return false, "Failed to build Lua library"
112+
end
113+
114+
-- Copy built library to plugin directory
115+
local copy_cmd = string.format("cp %q %q", src_lib, dest_lib)
116+
success = os.execute(copy_cmd) == 0
117+
if not success then
118+
return false, "Failed to copy library to plugin directory"
119+
end
120+
121+
-- Clean up build directory
122+
os.execute(string.format("rm -rf %q", build_dir))
123+
124+
return true
125+
end

scripts/functions/other.lua

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
1-
--- @diagnostic disable: lowercase-global
2-
3-
--- Finds the first transparent color in the given image.
4-
--- @param image Image The image to search for transparent color.
5-
--- @return Color color The first transparent color found in the image, or a fully transparent black color if no transparent color is found.
6-
function transparent_color(image)
7-
for it in image:pixels() do
8-
local color = Color(it())
9-
if color.alpha == 0 then
10-
if color.index == 0 then
11-
return color
12-
end
13-
end
14-
end
15-
return Color(0)
16-
end
17-
18-
--- Function to load image from bytes file.
19-
--- Thanks to `Astropulse` for sharing [this](https://community.aseprite.org/t/loading-ui-images-for-graphicscontext-elements-at-lightning-speed/21128) article.
20-
--- @param file string The path to the file.
21-
--- @return Image image The image loaded from the file.
22-
function load_image_bytes(file)
23-
local file = io.open(file, "rb")
24-
25-
assert(file, "File not found")
26-
27-
local width = tonumber(file:read("*line"))
28-
local height = tonumber(file:read("*line"))
29-
local bytes = file:read("*a")
30-
31-
file:close()
32-
33-
assert(width and height and bytes, "Invalid file")
34-
35-
image = Image(width, height)
36-
image.bytes = bytes
37-
38-
return image
39-
end
40-
41-
--- Function to save image to bytes file.
42-
--- @param image Image The image to save.
43-
--- @param file string The path to the file.
44-
function save_image_bytes(image, file)
45-
local file = io.open(file, "wb")
46-
47-
assert(file, "File not found")
48-
49-
file:write(image.width .. "\n")
50-
file:write(image.height .. "\n")
51-
file:write(image.bytes)
52-
53-
file:close()
54-
end
1+
--- @diagnostic disable: lowercase-global
2+
3+
--- Finds the first transparent color in the given image.
4+
--- @param image Image The image to search for transparent color.
5+
--- @return Color color The first transparent color found in the image, or a fully transparent black color if no transparent color is found.
6+
function transparent_color(image)
7+
for it in image:pixels() do
8+
local color = Color(it())
9+
if color.alpha == 0 then
10+
if color.index == 0 then
11+
return color
12+
end
13+
end
14+
end
15+
return Color(0)
16+
end
17+
18+
--- Function to load image from bytes file.
19+
--- Thanks to `Astropulse` for sharing [this](https://community.aseprite.org/t/loading-ui-images-for-graphicscontext-elements-at-lightning-speed/21128) article.
20+
--- @param file string The path to the file.
21+
--- @return Image image The image loaded from the file.
22+
function load_image_bytes(file)
23+
local file = io.open(file, "rb")
24+
25+
assert(file, "File not found")
26+
27+
local width = tonumber(file:read("*line"))
28+
local height = tonumber(file:read("*line"))
29+
local bytes = file:read("*a")
30+
31+
file:close()
32+
33+
assert(width and height and bytes, "Invalid file")
34+
35+
image = Image(width, height)
36+
image.bytes = bytes
37+
38+
return image
39+
end
40+
41+
--- Function to save image to bytes file.
42+
--- @param image Image The image to save.
43+
--- @param file string The path to the file.
44+
function save_image_bytes(image, file)
45+
local file = io.open(file, "wb")
46+
47+
assert(file, "File not found")
48+
49+
file:write(image.width .. "\n")
50+
file:write(image.height .. "\n")
51+
file:write(image.bytes)
52+
53+
file:close()
54+
end

scripts/main.lua

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ function init(plugin)
4242
if app.sprite and app.sprite.filename:ends_with(".dmi") and not opening_dmi_noeditor then
4343
local filename = app.sprite.filename
4444
app.command.CloseFile { ui = false }
45-
app.alert {
46-
title = "Lua Library",
47-
text = "opening",
48-
}
4945
loadlib(plugin.path)
5046

5147
Editor.new(DIALOG_NAME, filename)
@@ -229,34 +225,55 @@ end
229225
--- Loads the DMI library.
230226
--- @param plugin_path string Path where the extension is installed.
231227
function loadlib(plugin_path)
232-
app.alert {
233-
title = "Lua Library",
234-
text = "brr",
235-
}
236-
if not libdmi then
237-
-- Here, we handle loading a full version of Lua instead of the locked-down version that Aseprite uses
238-
if not app.os.windows then
239-
-- Lua already looks for .so, but for macOS we need to manually add .dylib since Lua is a 'good language'
240-
if app.os.macos then
241-
package.cpath = package.cpath .. ";?.dylib"
242-
end
243-
else
244-
-- Load Lua library if needed (Windows only)
245-
if LUA_LIB then
246-
local x = package.loadlib(app.fs.joinPath(plugin_path, LUA_LIB), "")
247-
app.alert {
248-
title = "Lua Library",
249-
text = x and "Lua library loaded successfully." or "Lua library failed to load.",
250-
}
251-
end
252-
end
253-
254-
-- Load DMI library
255-
libdmi = package.loadlib(app.fs.joinPath(plugin_path, DMI_LIB), "luaopen_dmi_module")()
256-
general_check()
257-
end
228+
if not app.os.windows then
229+
local success, err = compile_lua(plugin_path)
230+
if not success then
231+
app.alert {
232+
title = "Lua Compilation Error",
233+
text = err or "Unknown error during Lua compilation",
234+
}
235+
return
236+
end
237+
238+
-- Update library path based on OS
239+
if app.os.macos then
240+
package.cpath = package.cpath .. ";?.dylib"
241+
LUA_LIB = "liblua.dylib"
242+
else
243+
package.cpath = package.cpath .. ";?.so"
244+
LUA_LIB = "liblua.so"
245+
end
246+
end
247+
248+
-- Load Lua library
249+
if LUA_LIB then
250+
local lua_path = app.fs.joinPath(plugin_path, LUA_LIB)
251+
local success, err = package.loadlib(lua_path, "")
252+
if not success then
253+
app.alert {
254+
title = "Lua Library Error",
255+
text = "Failed to load Lua library: " .. (err or "unknown error"),
256+
}
257+
return
258+
end
259+
end
260+
261+
-- Load DMI library
262+
local dmi_path = app.fs.joinPath(plugin_path, DMI_LIB)
263+
local success, lib = pcall(package.loadlib, dmi_path, "luaopen_dmi_module")
264+
if not success then
265+
app.alert {
266+
title = "DMI Library Error",
267+
text = "Failed to load DMI library: " .. (lib or "unknown error"),
268+
}
269+
return
270+
end
271+
272+
libdmi = lib()
273+
general_check()
258274
end
259275

276+
260277
--- General checks.
261278
function general_check()
262279
if libdmi.check_update() then

0 commit comments

Comments
 (0)