Created
July 28, 2023 23:42
-
-
Save howmanysmall/8d93d676bf72aeea936571fc3446ac90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--!optimize 2 | |
--!strict | |
local fs = require("@lune/fs") | |
local process = require("@lune/process") | |
local stdio = require("@lune/stdio") | |
getfenv(1).Vector2 = { | |
new = function() | |
return {X = 0, Y = 0} | |
end; | |
} | |
local function CreateAssets(AssetsPath: string, DefinitionPath: string, ExportName: string) | |
if not fs.isFile(AssetsPath) then | |
return 0 | |
end | |
local require = require | |
local Assets: {[string]: any} = require(process.cwd .. (string.gsub(AssetsPath, ".lua", ""))) | |
assert(string.match(DefinitionPath, "%.d%.ts$"), "Not a valid definition path?") | |
local String = { | |
"type AssetId = `rbxassetid://${string}`;"; | |
""; | |
"interface SpritesheetEntry {"; | |
"\tImage: AssetId;"; | |
"\tImageRectOffset: Vector2;"; | |
"\tImageRectSize: Vector2;"; | |
"}"; | |
""; | |
} | |
local Length = 8 | |
local Interfaces: {string} = {} | |
local InterfacesLength = 0 | |
local function CreateInterface(Table: {[string]: any}, InterfaceName: string) | |
local Interface = {string.format("interface %* {", InterfaceName)} | |
local InterfaceLength = 1 | |
for AssetName, AssetValue in Table do | |
InterfaceLength += 1 | |
Interface[InterfaceLength] = string.format( | |
"\t%*: %*;", | |
AssetName, | |
if type(AssetValue) == "string" then "AssetId" else "SpritesheetEntry" | |
) | |
end | |
Interface[InterfaceLength + 1] = "}" | |
Interface[InterfaceLength + 2] = "" | |
return table.concat(Interface, "\n") | |
end | |
for InterfaceName, InterfaceValue in Assets do | |
if type(InterfaceValue) == "table" then | |
InterfacesLength += 1 | |
Interfaces[InterfacesLength] = InterfaceName | |
Length += 1 | |
String[Length] = CreateInterface(InterfaceValue, InterfaceName) | |
elseif type(InterfaceValue) == "string" then | |
Length += 1 | |
String[Length] = string.format("\t%*: AssetId;", InterfaceName) | |
end | |
end | |
local ExportString = {"interface TarmacAssets {"} | |
local ExportLength = 1 | |
table.sort(Interfaces) | |
for _, InterfaceName in Interfaces do | |
ExportLength += 1 | |
ExportString[ExportLength] = string.format("\t%*: %*;", InterfaceName, InterfaceName) | |
end | |
ExportString[ExportLength + 1] = "}" | |
ExportString[ExportLength + 2] = "" | |
ExportString[ExportLength + 3] = `declare const {ExportName}: TarmacAssets;` | |
ExportString[ExportLength + 4] = `export = {ExportName};` | |
ExportString[ExportLength + 5] = "" | |
String[Length + 1] = table.concat(ExportString, "\n") | |
fs.writeFile(DefinitionPath, table.concat(String, "\n")) | |
return 1 | |
end | |
local AssetsPath = stdio.prompt("text", "Please write the path to the assets file.", "src/shared/assets.lua") | |
local DefinitionPath = | |
stdio.prompt("text", "Please write the path to the assets definition file.", "src/shared/assets.d.ts") | |
local ExportName = stdio.prompt("text", "What do you want the export to be called?", "Assets") | |
CreateAssets(AssetsPath, DefinitionPath, ExportName) | |
return CreateAssets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment