I’ve been writing an plugin for modifying a xml file such as this:
<TextureAtlas imagePath="SlimeArrow.png">
<SubTexture name="SlimeArrowHud" x="67" y="35" width="5" height="9"/>
<SubTexture name="Slime" x="66" y="98" width="11" height="11"/>
<SubTexture name="SlimeArrow" x="81" y="52" width="13" height="4"/>
<SubTexture name="SlimeArrowBuried" x="81" y="57" width="13" height="4"/>
<SubTexture name="SlimeArrowPickup" x="114" y="66" width="12" height="12"/>
</TextureAtlas>
However, my script always returns nil for opening a file (Permission is granted,)
can anyone help?
Code
local c = app.fgColor;
local FileName;
local line_number = 0;
---------------------------
local dlg = Dialog("Atlas Editior")
dlg :separator{ text="SubTextureInfo:" }
:entry{ id="Texture_name", label="Name:", text="DuctTape"}
:button{ text="Chose XML File", -- Chose XML button
onclick=function()
local handle = io.popen('powershell.exe Add-Type -AssemblyName System.Windows.Forms; $dialog = New-Object System.Windows.Forms.OpenFileDialog; $dialog.ShowDialog() ^| Out-Null; $dialog.FileName') -- CMD command
FileName = handle:read("*a") -- Gets file name
handle:close()
if (FileName != "")
dlg:modify{ title = "Atlas Editior - " .. FileName} -- Shows what file name you use.
else
dlg:modify{ title = "Atlas Editior" .. FileName} -- Shows what file name you use.
end
end }
:button{ text="Generate From Selection", -- Non Working Code for editing Atlases, does not work :(
onclick=function()
if (FileName && FileName != "") then
local rectangle = app.activeSprite.selection.bounds -- Get selection box
line_number = 0
-- app.alert("Opening: " .. FileName)
local file, err = io.open(FileName, "r")
-- For whatever reason, file always returns nil, along with nil.
if (file ~= nil) then
local i = 0;
local lines = {}
for line in file:lines() do -- Creates a table of lines.
table.insert(lines, line)
end
file:close()
for line in lines do -- iterate over each line in the file
if line:find(dlg.Texture_name) then -- if the target string is found in the line
line_number = i
line = line:gsub('x="[%d%.%-]+"', 'x="' .. rectangle.x .. '"') -- Subs Values
line = line:gsub('y="[%d%.%-]+"', 'y="' .. rectangle.y .. '"')
line = line:gsub('width="[%d%.%-]+"', 'width="' .. rectangle.width .. '"')
line = line:gsub('height="[%d%.%-]+"', 'height="' .. rectangle.height .. '"')
lines[i] = line
break
end
i = i + 1
end
if (line_number == 0) then
for line in lines do -- iterate over each line in the file
if line:find("<//TextureAtlas>") then -- if the target string is found in the line
local NewLine = " <SubTexture name=\"" ..dlg.Texture_name.."\" x=\"".. rectangle.x .."\" y=\"".. rectangle.y .."\" width=\"".. rectangle.width .."\" height=\"".. rectangle.height .."\"//>" -- Inserts new subtexture with formatting (I hope)
table.insert(lines, i, NewLine)
break -- Texture names can only show up once per atlas, saves time!
end
i = i + 1
end
end
file, err = io.open(FileName, "w") -- Caution! Wipes the file!
for line in ipairs(lines) do -- Rewrite the file
file:write(line .. '\n')
end
file:close()
else
app.alert("Couldn't open file: " .. err) -- err is always nil, so it crashes
end
else
app.alert("Please select a file")
end
end }
:show{wait=false}
---------------------------------------