Skip to content

Instantly share code, notes, and snippets.

@karl-zylinski
Last active August 1, 2024 19:48
Show Gist options
  • Save karl-zylinski/89c7c3dc909ed89486a3bbc688005e9b to your computer and use it in GitHub Desktop.
Save karl-zylinski/89c7c3dc909ed89486a3bbc688005e9b to your computer and use it in GitHub Desktop.
// extra things I add to hot reload script to make auto reload work
odin build file_version_builder
IF %ERRORLEVEL% NEQ 0 exit /b 1
file_version_builder.exe
IF %ERRORLEVEL% NEQ 0 exit /b 1
// put this file in a `file_version_builder` subdir
package file_version_builder
import "core:os"
import "core:fmt"
import "core:strings"
dir_path_to_file_infos :: proc(path: string) -> []os.File_Info {
d, derr := os.open(path, os.O_RDONLY)
if derr != 0 {
panic("open failed")
}
defer os.close(d)
{
file_info, ferr := os.fstat(d)
defer os.file_info_delete(file_info)
if ferr != 0 {
panic("stat failed")
}
if !file_info.is_dir {
panic("not a directory")
}
}
file_infos, _ := os.read_dir(d, -1)
return file_infos
}
main :: proc() {
f, _ := os.open("file_version.odin", os.O_WRONLY | os.O_CREATE | os.O_TRUNC)
defer os.close(f)
fmt.fprintln(f, "package game")
fmt.fprintln(f, "")
fmt.fprintln(f, "import \"core:os\"")
fmt.fprintln(f, "")
fmt.fprintln(f, "FileVersion :: struct {")
fmt.fprintln(f, "\tpath: string,")
fmt.fprintln(f, "\tmodification_time: os.File_Time,")
fmt.fprintln(f, "}")
fmt.fprintln(f, "")
fmt.fprintln(f, "file_versions := []FileVersion {")
// enable this stuff to get auto reload for .odin files... I don't recommend it
/*files_in_this_folder := dir_path_to_file_infos(".")
for a in files_in_this_folder {
if a.name == "atlas.odin" || a.name == "file_version.odin" {
continue
}
if strings.has_suffix(a.name, ".odin") {
mod, mod_err := os.last_write_time_by_name(a.fullpath)
if mod_err != os.ERROR_NONE {
continue
}
fmt.fprintf(f, "\t{{ path = %q, modification_time = %v }},\n", a.fullpath, mod)
}
}*/
textures := dir_path_to_file_infos("textures")
for a in textures {
if strings.has_suffix(a.name, ".ase") || strings.has_suffix(a.name, ".aseprite") || strings.has_suffix(a.name, ".png") {
mod, mod_err := os.last_write_time_by_name(a.fullpath)
if mod_err != os.ERROR_NONE {
continue
}
fmt.fprintf(f, "\t{{ path = %q, modification_time = %v }},\n", a.fullpath, mod)
}
}
fmt.fprintln(f, "}")
fmt.fprintln(f, "")
}
// stuff to add to `game_update`:
if g_mem.auto_reload {
for f in file_versions {
if mod, mod_err := os.last_write_time_by_name(f.path); mod_err == os.ERROR_NONE {
if mod != f.modification_time {
reload_error := libc.system("build_hot_reload.bat")
if reload_error == 0 {
return true
}
}
}
}
}
if rl.IsKeyPressed(.F1) {
g_mem.auto_reload = !g_mem.auto_reload
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment