Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
Cleaning up the way games start up to better support debugging in Pix…
Browse files Browse the repository at this point in the history
…el Vision 8.
  • Loading branch information
jessefreeman committed Feb 16, 2020
1 parent c82f8b5 commit 5463388
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 147 deletions.
2 changes: 1 addition & 1 deletion Disks/RunnerTools/LoadTool/code.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function Update(timeDelta)

else
PreloaderComplete()

end

end
Expand Down
130 changes: 71 additions & 59 deletions Runners/PixelVision8/Runner/PixelVision8Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,63 +211,12 @@ public override void ActivateEngine(IEngine engine)
// Save a reference to the controller chip so we can listen for special key events
controllerChip = engine.ControllerChip;

// Get a reference to the Lua game
var game = engine.GameChip as LuaGameChip;

// Get the script
var luaScript = game.LuaScript;

// Inject the PV8 runner special global function
luaScript.Globals["IsExporting"] = new Func<bool>(ExportService.IsExporting);
luaScript.Globals["ReadExportPercent"] = new Func<int>(ExportService.ReadExportPercent);
luaScript.Globals["ReadExportMessage"] = new Func<string>(ExportService.ReadExportMessage);
luaScript.Globals["ShutdownSystem"] = new Action(ShutdownSystem);
luaScript.Globals["QuitCurrentTool"] = (QuitCurrentToolDelagator) QuitCurrentTool;
luaScript.Globals["RefreshActionKeys"] = new Action(RefreshActionKeys);
luaScript.Globals["DocumentPath"] = new Func<string>(() => documentsPath);
luaScript.Globals["TmpPath"] = new Func<string>(() => tmpPath);
luaScript.Globals["DiskPaths"] = new Func<WorkspacePath[]>(() => workspaceServicePlus.Disks);
luaScript.Globals["SaveActiveDisks"] = new Action(() =>
{
var disks = workspaceServicePlus.Disks;
foreach (var disk in disks) workspaceServicePlus.SaveDisk(disk);
});
luaScript.Globals["EjectDisk"] = new Action<string>(EjectDisk);
luaScript.Globals["EnableAutoRun"] = new Action<bool>(EnableAutoRun);
luaScript.Globals["EnableBackKey"] = new Action<bool>(EnableBackKey);
luaScript.Globals["RebuildWorkspace"] = new Action(workspaceServicePlus.RebuildWorkspace);
luaScript.Globals["MountDisk"] = new Action<WorkspacePath>(path =>
{
var segments = path.GetDirectorySegments();
var systemPath = Path.PathSeparator.ToString();
if (segments[0] == "Disk")
{
}
else if (segments[0] == "Workspace")
{
// TODO the workspace could have a different name so we should check the bios
systemPath = Path.Combine(documentsPath, segments[0]);
}
for (var i = 1; i < segments.Length; i++) systemPath = Path.Combine(systemPath, segments[i]);
systemPath = Path.Combine(systemPath,
path.IsDirectory ? Path.PathSeparator.ToString() : path.EntityName);
// Console.WriteLine("Mount Disk From " + systemPath);
MountDisk(systemPath);
});


// Activate the game
base.ActivateEngine(engine);

// Force the lua script to use this boot done logic instead
luaScript.Globals["BootDone"] = new Action<bool>(BootDone);

}

public void EjectDisk(string path)
Expand Down Expand Up @@ -669,11 +618,69 @@ public override void ConfigureEngine(Dictionary<string, string> metaData = null)
{
base.ConfigureEngine(metaData);

var luaGameChip = tmpEngine.GameChip as LuaGameChip;
// Get a reference to the Lua game
var game = tmpEngine.GameChip as LuaGameChip;

// Get the script
var luaScript = game.LuaScript;

// Inject the PV8 runner special global function
luaScript.Globals["IsExporting"] = new Func<bool>(ExportService.IsExporting);
luaScript.Globals["ReadExportPercent"] = new Func<int>(ExportService.ReadExportPercent);
luaScript.Globals["ReadExportMessage"] = new Func<string>(ExportService.ReadExportMessage);
luaScript.Globals["ShutdownSystem"] = new Action(ShutdownSystem);
luaScript.Globals["QuitCurrentTool"] = (QuitCurrentToolDelagator)QuitCurrentTool;
luaScript.Globals["RefreshActionKeys"] = new Action(RefreshActionKeys);
luaScript.Globals["DocumentPath"] = new Func<string>(() => documentsPath);
luaScript.Globals["TmpPath"] = new Func<string>(() => tmpPath);
luaScript.Globals["DiskPaths"] = new Func<WorkspacePath[]>(() => workspaceServicePlus.Disks);
luaScript.Globals["SaveActiveDisks"] = new Action(() =>
{
var disks = workspaceServicePlus.Disks;
foreach (var disk in disks) workspaceServicePlus.SaveDisk(disk);
});
luaScript.Globals["EjectDisk"] = new Action<string>(EjectDisk);
luaScript.Globals["EnableAutoRun"] = new Action<bool>(EnableAutoRun);
luaScript.Globals["EnableBackKey"] = new Action<bool>(EnableBackKey);
luaScript.Globals["RebuildWorkspace"] = new Action(workspaceServicePlus.RebuildWorkspace);
luaScript.Globals["MountDisk"] = new Action<WorkspacePath>(path =>
{
var segments = path.GetDirectorySegments();
var systemPath = Path.PathSeparator.ToString();
if (segments[0] == "Disk")
{
}
else if (segments[0] == "Workspace")
{
// TODO the workspace could have a different name so we should check the bios
systemPath = Path.Combine(documentsPath, segments[0]);
}
for (var i = 1; i < segments.Length; i++) systemPath = Path.Combine(systemPath, segments[i]);
systemPath = Path.Combine(systemPath,
path.IsDirectory ? Path.PathSeparator.ToString() : path.EntityName);
// Console.WriteLine("Mount Disk From " + systemPath);
MountDisk(systemPath);
});

if (mode == RunnerMode.Loading)
{
// Force the lua script to use this boot done logic instead
luaScript.Globals["BootDone"] = new Action<bool>(BootDone);
}

// var luaGameChip = tmpEngine.GameChip as LuaGameChip;

// Register the game editor with the lua service
UserData.RegisterType<GameEditor>();
luaGameChip.LuaScript.Globals["gameEditor"] = Editor;
luaScript.Globals["gameEditor"] = Editor;
}

public override void RunGame()
Expand Down Expand Up @@ -721,14 +728,19 @@ public override void SaveGameData(string path, IEngine engine, SaveFlags saveFla

public override void ConfigureServices()
{

base.ConfigureServices();

var luaService = new LuaServicePlus(this);
serviceManager.AddService(typeof(ExportService).FullName, ExportService);
}

// Register Lua Service
tmpEngine.AddService(typeof(LuaService).FullName, luaService);
public override void CreateLuaService()
{

luaService = new LuaServicePlus(this);

tmpEngine.AddService(typeof(ExportService).FullName, ExportService);
// Register Lua Service
serviceManager.AddService(typeof(LuaService).FullName, luaService);
}

public void OnFileDropped(object gameWindow, string path)
Expand Down
105 changes: 57 additions & 48 deletions SDK/Engine/Chips/Game/LuaGameChip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,55 +38,14 @@ public Script LuaScript
{
get
{
if (_luaScript == null) _luaScript = new Script(CoreModules.Preset_Default);
if (_luaScript == null) _luaScript = new Script(CoreModules.Preset_SoftSandbox);

return _luaScript;
}
}

protected virtual void RegisterLuaServices()
{
}

#region Lifecycle

public override void Init()
{
if (LuaScript?.Globals["Init"] == null) return;

LuaScript.Call(LuaScript.Globals["Init"]);
}

public override void Update(int timeDelta)
{
base.Update(timeDelta);

if (LuaScript?.Globals["Update"] == null) return;

LuaScript.Call(LuaScript.Globals["Update"], timeDelta);
}

public override void Draw()
{
if (LuaScript?.Globals["Draw"] == null) return;

LuaScript.Call(LuaScript.Globals["Draw"]);
}

public override void Shutdown()
{
if (LuaScript?.Globals["Shutdown"] == null) return;

LuaScript.Call(LuaScript.Globals["Shutdown"]);
}

public override void Reset()
{
// Setup the GameChip
base.Reset();

// if (LuaScript == null) return;

try
{
// Look to see if there are any lua services registered in the game engine
Expand Down Expand Up @@ -292,16 +251,66 @@ public override void Reset()
LuaScript.Globals["NewSpriteCollection"] =
new Func<string, SpriteData[], SpriteCollection>(NewSpriteCollection);

// Register any extra services
RegisterLuaServices();
}

// Kick off the first game script file
LoadScript(DefaultScriptPath);
#region Lifecycle

public override void Init()
{
if (LuaScript?.Globals["Init"] == null) return;

LuaScript.Call(LuaScript.Globals["Init"]);
}

public override void Update(int timeDelta)
{
base.Update(timeDelta);

if (LuaScript?.Globals["Update"] == null) return;

LuaScript.Call(LuaScript.Globals["Update"], timeDelta);
}

public override void Draw()
{
if (LuaScript?.Globals["Draw"] == null) return;

LuaScript.Call(LuaScript.Globals["Draw"]);
}

public override void Shutdown()
{
if (LuaScript?.Globals["Shutdown"] == null) return;

LuaScript.Call(LuaScript.Globals["Shutdown"]);
}

public override void Reset()
{
// Setup the GameChip
base.Reset();

// Reset the game
if (LuaScript.Globals["Reset"] != null) LuaScript.Call(LuaScript.Globals["Reset"]);

}

public override void Configure()
{
base.Configure();

// Register any extra services
RegisterLuaServices();
}

// public virtual void LoadDefaultScript()
// {
// // Kick off the first game script file
// LoadScript(DefaultScriptPath);
//
// // Reset the game
//
// }

#endregion

#region Scripts
Expand Down
5 changes: 5 additions & 0 deletions SDK/Engine/IEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public interface IEngine : IEngineChips, IUpdate, IDraw

Dictionary<string, string> MetaData { get; }

/// <summary>
/// Reset the game in memory
/// </summary>
void ResetGame();

/// <summary>
/// Run the game in memory
/// </summary>
Expand Down
19 changes: 9 additions & 10 deletions SDK/Engine/PixelVisionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public PixelVisionEngine(IServiceLocator serviceLocator, string[] chips = null,
/// <tocexclude />
public GameChip GameChip { get; set; }

public virtual void ResetGame()
{
if (GameChip == null) return;

foreach (var chip in Chips) chip.Value.Reset();

}

/// <summary>
/// Attempts to run a game that has been loaded into memory via the
/// LoadGame() method. It resets the display and game as well as calling
Expand All @@ -124,17 +132,8 @@ public virtual void RunGame()
{
if (GameChip == null) return;

// Make sure all chips are reset to their default values
// chipManager.Reset();
foreach (var chip in Chips) chip.Value.Reset();

// Call init on all chips
// chipManager.Init();
var chipNames = Chips.Keys.ToList();

foreach (var chipName in chipNames) Chips[chipName].Init();
foreach (var chip in Chips) chip.Value.Init();

// running = true;
}

/// <summary>
Expand Down

0 comments on commit 5463388

Please sign in to comment.