remove test; save more robustly

This commit is contained in:
wan-may 2024-07-13 13:26:34 -03:00
parent b8e00cf340
commit bfed3af852
5 changed files with 122 additions and 12 deletions

View File

@ -51,7 +51,6 @@ end
local function getHeader( filename )
local offset = 2 + love.data.unpack( "<I4", assert(love.filesystem.read( "data", filename, 14 )), 11 )
local header, size = assert( love.filesystem.read( filename, offset ) )
print( "BMP HEADER", filename, size, "\n", string.byte( header, 1, size ) )
return header
end
@ -74,7 +73,7 @@ local formats = {
header = getHeader( "data/earth/sailable.bmp" ),
w = 512,
h = 285,
--technically this information is in the header already but I don't want to write code to parse it since we only use this one header
--technically this information is in the header already but I don't want to write code to parse it we only use one palette ever
palette = {
[0] = 0,
[8] = 1,
@ -159,7 +158,6 @@ function formats.ai:encode( data )
local x = math.floor(self.w * (wx + 180) / 360)
--idk why exactly I need to round the value here instead of truncating it
local y = math.floor(0.5 + self.h * (wy + 100) / 200)
print( "export ai marker", i, x, y, wx, wy )
--get index into byte array
local idx = 2 + y * self.w + x
--in-editor we could have two points in the same place, possibly of the same or different types
@ -224,7 +222,6 @@ function formats.travel:encode( points )
local offset = math.floor( y * self.w + x - 2 )
--1 := white
bitmap[offset] = 1
print( "encoded:", point.idx, wx, wy, x, y )
end
--fold into 4-bit pixel array
@ -313,7 +310,7 @@ formats.territory = formats.africa
for fmt, tbl in pairs( formats ) do
t[fmt] = function( data ) return tbl:encode( data ) end
--TESTING
tbl:test()
--tbl:test()
end
return t

104
lib/mkdir.lua Normal file
View File

@ -0,0 +1,104 @@
-- mkdir only
-- A portable filesystem API using LuaJIT's FFI
-- Retrieved 2024-07-13 from https://gist.githubusercontent.com/Techcable/503f35ceea9554fb81cf3a5c1aa550da/raw/33a29f59207335b743824fbb657e4721a12ce280/fs.lua
local ffi = require("ffi")
local table = require("table")
require("string")
-- Cache needed functions and locals
local C, errno, string = ffi.C, ffi.errno, ffi.string
local concat, insert = table.concat, table.insert
-- "Standard" C99 functions
ffi.cdef[[
char *strerror(int errnum);
]]
local exists, mkdir, PATH_SEPARATOR
if ffi.os == "Windows" then
ffi.cdef[[
bool CreateDirectoryA(const char *path, void *lpSecurityAttributes);
int _access(const char *path, int mode);
]]
function exists(path)
assert(type(path) == "string", "path isn't a string")
local result = C._access(path, 0) -- Check existence
return result == 0
end
function mkdir(path, _)
assert(type(path) == "string", "path isn't a string")
if not C.CreateDirectoryA(path, nil) then
local message = string(C.strerror(errno()))
error("Unable to create directory " .. path .. ": " .. message)
end
end
PATH_SEPARATOR = "\\"
elseif ffi.os == "Linux" or ffi.os == "OSX" then
ffi.cdef[[
int mkdir(const char *path, int mode);
int access(const char *path, int amode);
]]
function exists(path)
assert(type(path) == "string", "path isn't a string")
local result = C.access(path, 0) -- Check existence
return result == 0
end
function mkdir(path, mode)
assert(type(path) == "string", "path isn't a string")
local mode = tonumber(mode or "755", 8)
if C.mkdir(path, mode) ~= 0 then
local message = string(C.strerror(errno()))
error("Unable to create directory " .. path .. ": " .. message)
end
end
PATH_SEPARATOR = "/"
else
error("Unsupported operating system: " .. ffi.os)
end
local function join(...)
local parts = {}
for i = 1, select("#", ...) do
local part = select(i, ...)
insert(parts, part)
end
return concat(parts, PATH_SEPARATOR)
end
local function splitPath(path)
assert(type(path) == "string", "path isn't a string!")
local parts = {}
local lastIndex = 0
for i = 1, path:len() do
if path:sub(i, i) == PATH_SEPARATOR then
insert(parts, path:sub(lastIndex, i - 1))
lastIndex = i + 1
end
end
insert(parts, path:sub(lastIndex))
return parts
end
local function mkdirs(path)
local parts = splitPath(path)
local currentPath = parts[1]
for i=2, #parts do
if not exists(currentPath) then
mkdir(currentPath)
end
-- Note: This isn't suboptimal, since we really do need the intermediate results
currentPath = currentPath .. PATH_SEPARATOR .. parts[i]
end
if not exists(path) then
mkdir(path)
end
end
return {
exists = exists,
join = join,
mkdir = mkdir,
mkdirs = mkdirs,
splitPath = splitPath,
PATH_SEPERATOR = PATH_SEPARATOR
}

15
map.lua
View File

@ -1,3 +1,6 @@
local love = assert( love )
local io = io
local mkdir = assert( require 'lib.mkdir' )
local lg = love.graphics
local AI = require 'ai'
local Cities = require 'cities'
@ -179,16 +182,22 @@ function map.draw()
end
local function write( filename, string )
--
print( "Writing", string:len(), "bytes to", filename )
os.rename( filename, filename..".bak" ) --just in case :^)
local file = assert( io.open( filename, "wb+" ) )
local file = assert( io.open( filename, "wb" ) )
assert( file:write( string ) )
assert( file:flush() ) --your toilet is set to stun, not kill
file:close()
end
function map.save()
--should be cross platform-ish
for _, folder in ipairs{ "/data/", "/data/earth/", "/data/graphics/" } do
assert( mkdir.exists( map.path ) )
local path = map.path..folder
if not mkdir.exists( path ) then mkdir.mkdir( path ) end
end
--OK back to normal
for k, layer in pairs( layers ) do
write( map.path..tostring( layer.filename ), assert( layer:save() ) )
end

View File

@ -4,7 +4,7 @@ local button = require( "button" )
local map = require( "map" )
local t = {}
local saveLocation = map.path
local saveLocation = false
local floppy = love.graphics.newImage( "icons/save.png" )
floppy:setFilter( "nearest", "nearest" )
local saveButton = button.new{
@ -35,8 +35,9 @@ local cancelButton = button.new{
function t.start()
modal.start( t )
saveLocation = saveLocation or map.path
button.selected = saveButton
saveButton.name = "save to "..map.path
saveButton.name = "save to "..saveLocation
button.displayGroup( "saveModal", true )
end
@ -47,7 +48,7 @@ function t.draw()
end
function t.directorydropped( path )
function t.directorydropped( path )
saveLocation = path
map.path = path
saveButton.name = "save to "..map.path

View File

@ -107,7 +107,6 @@ end
local function dfs( idx, adj, unvisited )
if not unvisited[idx] then return end
print( "visiting node", idx )
unvisited[ idx ] = nil
for i in pairs( adj[idx] ) do dfs( i, adj, unvisited ) end
end