hot reloading and module watching done

This commit is contained in:
peterino2 2026-01-03 20:39:31 -08:00
parent ef91c4599b
commit e69a8d13ec
6 changed files with 50 additions and 29 deletions

View File

@ -180,6 +180,11 @@ pub const Program = struct {
exe.root_module.addImport("core", core); exe.root_module.addImport("core", core);
exe.root_module.addOptions("BacklogOptions", self.buildSystem.options); exe.root_module.addOptions("BacklogOptions", self.buildSystem.options);
if (!self.opts.static_build) {
const platform = self.buildSystem.nwdep.module("platform");
exe.root_module.addImport("platform", platform);
}
const gameModule = self.makeGameModule(); const gameModule = self.makeGameModule();
const gameApi = self.makeGameApi(); const gameApi = self.makeGameApi();
gameModule.root_module.addImport("backlog", gameApi); gameModule.root_module.addImport("backlog", gameApi);
@ -189,7 +194,7 @@ pub const Program = struct {
exe.root_module.addImport("backlog", gameApi); exe.root_module.addImport("backlog", gameApi);
} }
self.buildSystem.b.installArtifact(exe); // self.buildSystem.b.installArtifact(exe);
const installed = self.buildSystem.b.addInstallArtifact(exe, .{}); const installed = self.buildSystem.b.addInstallArtifact(exe, .{});
{ {
@ -204,11 +209,6 @@ pub const Program = struct {
run_exe.dependOn(&run.step); run_exe.dependOn(&run.step);
} }
// 2. create the actual library that the loader will try to load
// 3. set up linking, if static then we will link against the loader
// return the actual library not the loader
return exe; return exe;
} }
}; };

View File

@ -170,6 +170,7 @@ pub const ModuleLoader = struct {
} }
try core.fs().addFileChangedCallback(libFileName, dllChangedCallback, loaded); try core.fs().addFileChangedCallback(libFileName, dllChangedCallback, loaded);
core.engine_log("[ModuleLoader]: addFileChangedCallback '{s}'", .{libFileName});
try self.loadedModules.append(self.arena.allocator(), loaded); try self.loadedModules.append(self.arena.allocator(), loaded);
} }

View File

@ -97,6 +97,12 @@ pub fn startEngine(spec: *core.SpecVariantMap) bool {
var modalloc = allocator; var modalloc = allocator;
_ = modlaunch.startup_module(&modalloc, &modargs); _ = modlaunch.startup_module(&modalloc, &modargs);
} else { } else {
const platform = @import("platform").module;
platform.watchModules();
//if (comptime @hasDecl(backlog, "platform")) {
//backlog.platform.watchModules();
// }
core.loadModule(moduleName, true) catch return false; core.loadModule(moduleName, true) catch return false;
} }

View File

@ -25,18 +25,26 @@ pub export fn startup_module(p_allocator: *anyopaque, p_a: ?*anyopaque) bool {
const spec = gamespec.getSpec() catch return false; const spec = gamespec.getSpec() catch return false;
for (backlog.moduleList) |li| {
core.engine_log("mod: {s}", .{li});
}
const args = getArgs() catch NwArgs{}; const args = getArgs() catch NwArgs{};
const moduleArgs = core.startup_getArgs(p_a.?);
// firstLoad: bool,
// engine: *core.Engine,
// nameRegistry: *p2.NameRegistry,
// packerFS: *core.PackerFS,
// // luaState: *anyopaque,
// // luaAllocator: *anyopaque,
// debugDrawInterface: ?*debug.DebugDrawInterface,
if (moduleArgs.firstLoad) {
inline for (backlog.moduleList) |feature| { inline for (backlog.moduleList) |feature| {
if (@hasDecl(backlog, feature) and !std.mem.eql(u8, "core", feature)) { if (@hasDecl(backlog, feature) and !std.mem.eql(u8, "core", feature)) {
const Struct = @field(backlog, feature); const Struct = @field(backlog, feature);
if (core.isModuleEnabled(Struct.Module, spec)) { if (core.isModuleEnabled(Struct.Module, spec)) {
var z1 = core.tracy.ZoneN(@src(), @ptrCast("Initializing Module")); var z1 = core.tracy.ZoneN(@src(), @ptrCast("Initializing Module"));
defer z1.End(); defer z1.End();
core.engine_logs("starting module >>>> " ++ feature ++ " <<<<");
core.tracy.Message(Struct.Module.name); core.tracy.Message(Struct.Module.name);
Struct.start_module(spec, args, allocator) catch return false; Struct.start_module(spec, args, allocator) catch return false;
@ -46,13 +54,18 @@ pub export fn startup_module(p_allocator: *anyopaque, p_a: ?*anyopaque) bool {
shutdownList.append(allocator, Struct.shutdown_module) catch return false; shutdownList.append(allocator, Struct.shutdown_module) catch return false;
shutdownModuleNames.append(allocator, feature) catch return false; shutdownModuleNames.append(allocator, feature) catch return false;
core.engine_logs("module started >>>> " ++ feature ++ " <<<<");
} }
} }
} }
core.setShutdownModule(shutdown_module); core.setShutdownModule(shutdown_module);
} else {
inline for (backlog.moduleList) |feature| {
if (@hasDecl(backlog, feature)) {
core.engine_log("reloading module: {s}", .{feature});
}
}
}
return true; return true;
} }

View File

@ -89,7 +89,7 @@ pub fn start_module(spec: *core.SpecVariantMap, args: anytype, allocator: std.me
return; return;
} }
core.getModuleLoader().watchInitializeFn = watchModules; // core.getModuleLoader().watchInitializeFn = watchModules;
const parameters = windowing.PlatformParams.init(); const parameters = windowing.PlatformParams.init();

View File

@ -166,14 +166,15 @@ pub const PackerFS = struct {
const self: *@This() = @ptrCast(@alignCast(ctx)); const self: *@This() = @ptrCast(@alignCast(ctx));
for (self.fileWatchCallbacks.items) |*watch| { for (self.fileWatchCallbacks.items) |*watch| {
// std.debug.print("checking {s} {s}\n", .{ watch.path, path }); std.debug.print("checking {s} {s}\n", .{ watch.path, path });
if (std.mem.eql(u8, watch.path, std.mem.span(path))) { if (std.mem.eql(u8, watch.path, std.mem.span(path))) {
watch.call(std.mem.span(path)); watch.call(std.mem.span(path));
} }
// std.debug.print("registered callback: {s}\n", .{watch.path}); std.debug.print("registered callback: {s}\n", .{watch.path});
} }
for (self.anyWatchCallbacks.items) |*watch| { for (self.anyWatchCallbacks.items) |*watch| {
// std.debug.print("anywatch callback: {s}\n", .{watch.path});
watch.call(std.mem.span(path)); watch.call(std.mem.span(path));
} }
} }