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

Commit

Permalink
Cleaning up project structure and moving new debug code in from test …
Browse files Browse the repository at this point in the history
…project.
  • Loading branch information
jessefreeman committed Mar 20, 2020
1 parent 88d990a commit cbb41e8
Show file tree
Hide file tree
Showing 22 changed files with 2,803 additions and 54 deletions.
65 changes: 31 additions & 34 deletions Runners/PixelVision8/Runner/Editors/GameEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,38 +223,34 @@ public void Reset()
/// Returns a list of library paths
/// </summary>
/// <returns></returns>
public string[] LibraryPaths()
{

var sharedLibPaths = workspace.SharedLibDirectories();

var files = new List<string>();

for (int i = 0; i < sharedLibPaths.Count; i++)
{

var path = from p in workspace.GetEntities(sharedLibPaths[i])
where p.EntityName.EndsWith("lua") select p;

foreach (var file in files)
{
files.Add(file);
}

}

// // TODO need to go through and find all of the included libraries from the workspace
//
// // workspace.IncludeLibDirectoryFiles(files);
//
// var fileList = new List<string>();
//
// // TODO need to get the real paths
//
// // foreach (var file in files) fileList.Add(file.Key);

return files.ToArray();
}
// public string[] LibraryPaths()
// {
//
// var sharedLibPaths = workspace.SharedLibDirectories();
//
// var luaFiles = new List<string>();
//
// for (int i = 0; i < sharedLibPaths.Count; i++)
// {
//
// var files = from p in workspace.GetEntities(sharedLibPaths[i])
// where p.EntityName.EndsWith("lua") select p;
//
// foreach (var file in files)
// {
// if (luaFiles.IndexOf(file.Path) == -1)
// {
// var text = workspace.ReadTextFromFile(file);
//
// luaFiles.Add(file.Path);
// }
//
// }
//
// }
//
// return luaFiles.ToArray();
// }

//
// /// <summary>
Expand Down Expand Up @@ -319,7 +315,7 @@ public void ResetValidation()
/// Allows you to save the current game you are editing. Pass in SaveFlags to define which files should be exported.
/// </summary>
/// <param name="flags"></param>
public void Save(string path, SaveFlags[] flags)
public void Save(string path, SaveFlags[] flags, bool useSteps = false)
{
// TODO need to get the export service

Expand All @@ -333,7 +329,7 @@ public void Save(string path, SaveFlags[] flags)
// gameChip.version = ;

// TODO saving games doesn't work
runner.SaveGameData(path, targetGame, saveFlags);
runner.SaveGameData(path, targetGame, saveFlags, useSteps);
// workspace.SaveCart(workspace.WorkspacePath(WorkspaceFolders.CurrentGameDir), targetGame, saveFlags);

ResetValidation();
Expand Down Expand Up @@ -1158,6 +1154,7 @@ public bool LoadImage(string path)
return false;
}


Reset();

return true;
Expand Down
113 changes: 111 additions & 2 deletions Runners/PixelVision8/Runner/PixelVision8Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
using GifEncoder;
using Microsoft.Xna.Framework;
using MoonSharp.Interpreter;
using MoonSharp.VsCodeDebugger;
using MoonSharp.VsCodeDebugger.DebuggerLogic;
using PixelVision8.Engine;
using PixelVision8.Engine.Chips;
using PixelVision8.Runner.Editors;
Expand Down Expand Up @@ -82,6 +84,29 @@ public class PixelVision8Runner : DesktopRunner
// protected string rootPath;
protected bool shutdown;

private MoonSharpVsCodeDebugServer server;
private bool attachScript = true;

public override List<string> DefaultChips
{
get
{
var chips = new List<string>
{
typeof(ColorChip).FullName,
typeof(SpriteChip).FullName,
typeof(TilemapChip).FullName,
typeof(FontChip).FullName,
typeof(ControllerChip).FullName,
typeof(DisplayChip).FullName,
typeof(SoundChip).FullName,
typeof(MusicChip).FullName,
typeof(LuaDebugGameChip).FullName
};

return chips;
}
}

// protected string windowTitle;

Expand All @@ -90,6 +115,8 @@ public class PixelVision8Runner : DesktopRunner
// Default path to where PV8 workspaces will go
public PixelVision8Runner(string rootPath) : base(rootPath)
{
server = new MoonSharpVsCodeDebugServer(1985);
server.Start();
}

public PixelVision8Runner()
Expand Down Expand Up @@ -212,14 +239,76 @@ public override void ActivateEngine(IEngine engine)
// Save a reference to the controller chip so we can listen for special key events
controllerChip = engine.ControllerChip;


if (mode == RunnerMode.Loading)
{
((LuaGameChip)engine.GameChip).LuaScript.Globals["DebuggerAttached"] = new Func<bool>(AwaitDebuggerAttach);
}

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


}

public override void BaseActivateEngine(IEngine engine)
{
if (engine == null) return;

// Make the loaded engine active
ActiveEngine = engine;

LuaGameChip tempQualifier = ((LuaGameChip)ActiveEngine.GameChip);
// Kick off the first game script file

if (server.Current == null)
{
tempQualifier.LoadScript(tempQualifier.DefaultScriptPath);

var scriptPath = workspaceService.GetPhysicalPath(WorkspacePath.Parse(ActiveEngine.Name + "code.lua"));

server.AttachToScript(tempQualifier.LuaScript, scriptPath);
}

ActiveEngine.ResetGame();

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

// After loading the game, we are ready to run it.
ActiveEngine.RunGame();

// Reset the game's resolution
ResetResolution();

// Make sure that the first frame is cleared with the default color
ActiveEngine.GameChip.Clear();
}

private bool AwaitDebuggerAttach()
{

var connected = server.Connected;

if (connected == false && attachScript)
{

LuaGameChip tempQualifier = ((LuaGameChip)tmpEngine.GameChip);
// Kick off the first game script file
tempQualifier.LoadScript(tempQualifier.DefaultScriptPath);

var scriptPath = workspaceService.GetPhysicalPath(WorkspacePath.Parse(ActiveEngine.Name + "code.lua"));

server.AttachToScript(tempQualifier.LuaScript, scriptPath);

attachScript = false;
}

UpdateTitle();

return connected;

}

public void EjectDisk(string path)
{
ejectingDisk = true;
Expand Down Expand Up @@ -455,6 +544,16 @@ public override void BootDone(bool safeMode = false)

AutoLoadDefaultGame();
}
public override void DisplayWarning(string message)
{
base.DisplayWarning(message);

if (server?.GetDebugger() is AsyncDebugger debugger)
{
((MoonSharpDebugSession)debugger.Client)?.SendText(message);
}

}

public override void DisplayError(ErrorCode code, Dictionary<string, string> tokens = null,
Exception exception = null)
Expand Down Expand Up @@ -586,6 +685,13 @@ public override void ShutdownActiveEngine()

// Save the active disk
workspaceServicePlus.SaveActiveDisk();

if (server.Current != null)
{
server.Detach(server.Current);
}

attachScript = true;
}
catch (Exception e)
{
Expand Down Expand Up @@ -624,6 +730,7 @@ public override void ConfigureEngine(Dictionary<string, string> metaData = null)
luaScript.Globals["EnableAutoRun"] = new Action<bool>(EnableAutoRun);
luaScript.Globals["EnableBackKey"] = new Action<bool>(EnableBackKey);


if (mode == RunnerMode.Playing)
{
// Inject the PV8 runner special global function
Expand All @@ -637,6 +744,7 @@ public override void ConfigureEngine(Dictionary<string, string> metaData = null)
luaScript.Globals["DocumentPath"] = new Func<string>(() => documentsPath);
luaScript.Globals["TmpPath"] = new Func<string>(() => tmpPath);
luaScript.Globals["DiskPaths"] = new Func<WorkspacePath[]>(() => workspaceServicePlus.Disks);
luaScript.Globals["SharedLibPaths"] = new Func<WorkspacePath[]>(() => workspaceServicePlus.SharedLibDirectories().ToArray());
// luaScript.Globals["SaveActiveDisks"] = new Action(() =>
// {
// var disks = workspaceServicePlus.Disks;
Expand Down Expand Up @@ -918,7 +1026,8 @@ public void StartRecording()

public virtual void UpdateTitle()
{
Window.Title = DefaultWindowTitle + (Recording ? "" : "") + (screenShotActive ? " 📷" : "");
// Window.Title = DefaultWindowTitle + (Recording ? " ⚫" : "") + (screenShotActive ? " 📷" : "");
Window.Title = DefaultWindowTitle + ((server != null && server.Connected) ? " 🐞" : "") + (Recording ? "" : "") + (screenShotActive ? " 📷" : "");
}

public void StopRecording()
Expand Down
11 changes: 8 additions & 3 deletions Runners/PixelVision8/Runner/Services/BaseExportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ protected virtual void WorkerExporterCompleted(object sender, RunWorkerCompleted
{
if (locator.GetService(typeof(WorkspaceService).FullName) is WorkspaceService workspaceService)
{


// Aggregate all Get all the messages
foreach (var exporter in exporters)
Expand All @@ -136,7 +135,13 @@ protected virtual void WorkerExporterCompleted(object sender, RunWorkerCompleted

foreach (var response in exporter.Response)
{
message.Add(exporter.GetType().Name + "_" + response.Key, response.Value);
var tmpKey = exporter.GetType().Name + "_" + response.Key;
var tmpValue = response.Value;

if (message.ContainsKey(tmpKey))
message[tmpKey] = tmpValue;
else
message.Add(tmpKey, tmpValue);
}
}

Expand Down Expand Up @@ -178,7 +183,6 @@ public virtual void NextExporter()
public virtual void Restart()
{
currentParserID = 0;
totalSteps = 0;
currentStep = 0;
message.Clear();
}
Expand All @@ -187,6 +191,7 @@ public virtual void Clear()
{
exporters.Clear();
files.Clear();
totalSteps = 0;
}

#endregion
Expand Down
6 changes: 3 additions & 3 deletions Runners/PixelVision8/Runner/Services/LuaServicePlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ public long FileSize(WorkspacePath workspacePath)
if (((PixelVision8Runner) runner).ExportService is GameDataExportService exportService)
{

((PixelVision8Runner) runner).ExportService.Restart();
exportService.Clear();

((PixelVision8Runner) runner).ExportService.AddExporter(diskExporter);
exportService.AddExporter(diskExporter);

((PixelVision8Runner) runner).ExportService.StartExport();
exportService.StartExport();

// diskExporter.CalculateSteps();
//
Expand Down
3 changes: 2 additions & 1 deletion Runners/PixelVision8/Runner/Services/WorkspaceServicePlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void RebuildWorkspace()
var systemPaths = new List<IFileSystem>
{
new SubFileSystem(this,
WorkspacePath.Root.AppendDirectory("App").AppendDirectory("PixelVisionOS"))
WorkspacePath.Root.AppendDirectory("App").AppendDirectory("PixelVisionOS").AppendDirectory("System"))
};

// Create a path to the workspace system folder
Expand All @@ -87,6 +87,7 @@ public void RebuildWorkspace()

// Mount the PixelVisionOS directory
AddMount(new KeyValuePair<WorkspacePath, IFileSystem>(osPath, new MergedFileSystem(systemPaths)));

}

// Exports the active song in the music chip
Expand Down

0 comments on commit cbb41e8

Please sign in to comment.