2025-08-26 14:27:39 +01:00
|
|
|
structure Converter =
|
|
|
|
|
struct
|
2025-08-26 14:53:19 +01:00
|
|
|
fun loadIO (io, str) =
|
|
|
|
|
case TextIO.inputLine io of
|
|
|
|
|
SOME line => loadIO (io, str ^ line)
|
|
|
|
|
| NONE => str
|
|
|
|
|
|
2025-08-26 15:17:10 +01:00
|
|
|
fun convertFile (fullPath, filename) =
|
2025-08-26 14:53:19 +01:00
|
|
|
let
|
|
|
|
|
val io = TextIO.openIn fullPath
|
|
|
|
|
val text = loadIO (io, "")
|
|
|
|
|
val () = TextIO.closeIn io
|
|
|
|
|
in
|
|
|
|
|
case Parser.parse text of
|
|
|
|
|
SOME (canvasWidth, canvasHeight, tree) =>
|
|
|
|
|
let
|
|
|
|
|
val maxSide = Int.max (canvasWidth, canvasHeight)
|
|
|
|
|
val squares = LayerTree.flatten (maxSide, tree)
|
|
|
|
|
val exportString =
|
|
|
|
|
CollisionTree.toExportString (squares, canvasWidth, canvasHeight)
|
|
|
|
|
|
|
|
|
|
val pathWithoutExtension = String.substring
|
|
|
|
|
(fullPath, 0, String.size fullPath - 4)
|
|
|
|
|
val outputFilePath = pathWithoutExtension ^ ".sml"
|
|
|
|
|
val io = TextIO.openOut outputFilePath
|
|
|
|
|
val () = TextIO.output (io, exportString)
|
|
|
|
|
in
|
|
|
|
|
TextIO.closeOut io
|
|
|
|
|
end
|
|
|
|
|
| NONE => (* we have an error, but ignore *) ()
|
|
|
|
|
end
|
|
|
|
|
|
2025-08-26 14:27:39 +01:00
|
|
|
fun endsWithDsc str =
|
|
|
|
|
if String.size str >= 4 then
|
|
|
|
|
let
|
|
|
|
|
val size = String.size str
|
|
|
|
|
val expectedExtension = String.substring (str, size - 4, 4)
|
|
|
|
|
in
|
|
|
|
|
expectedExtension = ".dsc"
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
false
|
|
|
|
|
|
|
|
|
|
fun loop (dir, rootPath) =
|
|
|
|
|
case OS.FileSys.readDir dir of
|
|
|
|
|
SOME path =>
|
|
|
|
|
let
|
|
|
|
|
val folderPath = String.concat [rootPath, "/", path]
|
2025-08-26 14:35:35 +01:00
|
|
|
val () =
|
|
|
|
|
if OS.FileSys.isDir folderPath then
|
|
|
|
|
(* handle recursive directory *)
|
|
|
|
|
let val newDir = OS.FileSys.openDir folderPath
|
|
|
|
|
in loop (newDir, folderPath)
|
|
|
|
|
end
|
|
|
|
|
else if OS.FileSys.isLink folderPath then
|
|
|
|
|
(* ignore *)
|
|
|
|
|
()
|
|
|
|
|
else if endsWithDsc path then
|
|
|
|
|
(* is a file ending with .dsc extension *)
|
2025-08-26 15:17:10 +01:00
|
|
|
convertFile (folderPath, path)
|
2025-08-26 14:35:35 +01:00
|
|
|
else
|
|
|
|
|
(* is a file but doesn't end with .dsc, so ignore *)
|
|
|
|
|
()
|
2025-08-26 14:27:39 +01:00
|
|
|
in
|
2025-08-26 14:35:35 +01:00
|
|
|
loop (dir, rootPath)
|
2025-08-26 14:27:39 +01:00
|
|
|
end
|
|
|
|
|
| NONE => OS.FileSys.closeDir dir
|
|
|
|
|
|
|
|
|
|
fun main () =
|
|
|
|
|
let
|
|
|
|
|
val path = OS.FileSys.getDir ()
|
|
|
|
|
val dir = OS.FileSys.openDir path
|
|
|
|
|
in
|
|
|
|
|
loop (dir, path)
|
|
|
|
|
end
|
|
|
|
|
end
|