Implement resource auto-updates.

This commit is contained in:
Mahlon E. Smith 2025-08-22 11:47:04 -07:00
parent e01dc377a7
commit 3d8dc5cdf2
Signed by: mahlon
SSH key fingerprint: SHA256:dP84sRGKZRpOOiPD/+GuOq+SHSxEw9qi5BWLQobaHm0
5 changed files with 42 additions and 16 deletions

Binary file not shown.

1
resources/.version Normal file
View file

@ -0,0 +1 @@
0.0.1

5
src/lib/constants.nim Normal file
View file

@ -0,0 +1,5 @@
# vim: set et sta sw=4 ts=4 :
# Don't forget to update resources/.version to match this!
const VERSION* = "0.0.1"

View file

@ -9,13 +9,16 @@ import
std/files, std/files,
std/paths, std/paths,
std/strformat, std/strformat,
std/strutils,
std/tempfiles std/tempfiles
import
constants
type type
EmbeddedFS* = object EmbeddedFS* = object
EmbeddedFSError* = object of CatchableError EmbeddedFSError* = object of CatchableError
let embeddedFs* = EmbeddedFS()
when not defined( emscripten ): when not defined( emscripten ):
import zippy/tarballs import zippy/tarballs
@ -33,6 +36,7 @@ else:
let confFile* = confDir / Path("config.ini") let confFile* = confDir / Path("config.ini")
proc path*( data: EmbeddedFS, path: string ): Path = proc path*( data: EmbeddedFS, path: string ): Path =
## Returns the path of an embedded file. ## Returns the path of an embedded file.
return dataDir / Path( path ) return dataDir / Path( path )
@ -48,23 +52,38 @@ proc `[]`*( data: EmbeddedFS, path: string ): string =
return f.readAll return f.readAll
# TODO: version check, extract over old stuff proc newEmbeddedFS(): EmbeddedFS =
# no-op if versions match ## Initialize the embedded filesystem.
# ##
proc update*( e: EmbeddedFS ) = ## If the current runtime version is different than the binary version,
when not defined( emscripten ): ## re-extract the embedded filesystem to disk.
result = EmbeddedFS()
when defined( emscripten ): return
try:
var fsVersion = readFile( $result.path(".version") )
fsVersion.removeSuffix
if fsVersion == VERSION: return
except:
discard
echo "Extracting embedded resources for v{VERSION}".fmt
createDir( getDataDir() ) createDir( getDataDir() )
createDir( $confDir ) createDir( $confDir )
# check existing path / version
let ( tarball, path ) = createTempFile( "{appname}_resources_".fmt, ".tgz" ) let ( tarball, path ) = createTempFile( "{appname}_resources_".fmt, ".tgz" )
tarball.write( RESOURCES ) tarball.write( RESOURCES )
tarball.close tarball.close
removeDir( $dataDir )
extractAll( path, $dataDir ) extractAll( path, $dataDir )
removeFile( path ) removeFile( path )
let source = dataDir / Path( "config.ini" ) let source = dataDir / Path( "config.ini" )
if not fileExists( confFile ): moveFile( source, confFile ) if not fileExists( confFile ): copyFile( $source, $confFile )
let embeddedFs* = newEmbeddedFS()

View file

@ -7,6 +7,7 @@ import reasings
import raylib import raylib
import import
lib/constants,
lib/configuration, lib/configuration,
lib/embeddedfs lib/embeddedfs