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.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 gameApi = self.makeGameApi();
gameModule.root_module.addImport("backlog", gameApi);
@ -189,7 +194,7 @@ pub const Program = struct {
exe.root_module.addImport("backlog", gameApi);
}
self.buildSystem.b.installArtifact(exe);
// self.buildSystem.b.installArtifact(exe);
const installed = self.buildSystem.b.addInstallArtifact(exe, .{});
{
@ -204,11 +209,6 @@ pub const Program = struct {
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;
}
};

View File

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

View File

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

View File

@ -25,35 +25,48 @@ pub export fn startup_module(p_allocator: *anyopaque, p_a: ?*anyopaque) bool {
const spec = gamespec.getSpec() catch return false;
for (backlog.moduleList) |li| {
core.engine_log("mod: {s}", .{li});
}
const args = getArgs() catch NwArgs{};
inline for (backlog.moduleList) |feature| {
if (@hasDecl(backlog, feature) and !std.mem.eql(u8, "core", feature)) {
const Struct = @field(backlog, feature);
if (core.isModuleEnabled(Struct.Module, spec)) {
var z1 = core.tracy.ZoneN(@src(), @ptrCast("Initializing Module"));
defer z1.End();
core.tracy.Message(Struct.Module.name);
Struct.start_module(spec, args, allocator) catch return false;
const moduleArgs = core.startup_getArgs(p_a.?);
if (core.MemoryTracker.MTGet()) |_| {
core.MemoryTracker.MTPrintStatsDelta();
// 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| {
if (@hasDecl(backlog, feature) and !std.mem.eql(u8, "core", feature)) {
const Struct = @field(backlog, feature);
if (core.isModuleEnabled(Struct.Module, spec)) {
var z1 = core.tracy.ZoneN(@src(), @ptrCast("Initializing Module"));
defer z1.End();
core.engine_logs("starting module >>>> " ++ feature ++ " <<<<");
core.tracy.Message(Struct.Module.name);
Struct.start_module(spec, args, allocator) catch return false;
if (core.MemoryTracker.MTGet()) |_| {
core.MemoryTracker.MTPrintStatsDelta();
}
shutdownList.append(allocator, Struct.shutdown_module) catch return false;
shutdownModuleNames.append(allocator, feature) catch return false;
}
}
}
shutdownList.append(allocator, Struct.shutdown_module) catch return false;
shutdownModuleNames.append(allocator, feature) catch return false;
core.engine_logs("module started >>>> " ++ feature ++ " <<<<");
core.setShutdownModule(shutdown_module);
} else {
inline for (backlog.moduleList) |feature| {
if (@hasDecl(backlog, feature)) {
core.engine_log("reloading module: {s}", .{feature});
}
}
}
core.setShutdownModule(shutdown_module);
return true;
}

View File

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

View File

@ -166,14 +166,15 @@ pub const PackerFS = struct {
const self: *@This() = @ptrCast(@alignCast(ctx));
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))) {
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| {
// std.debug.print("anywatch callback: {s}\n", .{watch.path});
watch.call(std.mem.span(path));
}
}