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

Commit

Permalink
Cleaning up file export.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessefreeman committed Mar 10, 2020
1 parent ca3fdca commit 88d990a
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 234 deletions.
4 changes: 2 additions & 2 deletions Runners/PixelVision8/PixelVision8Runner.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
<Compile Include="Runner\Exporters\ColorPaletteExporter.cs" />
<Compile Include="Runner\Exporters\DiskExporter.cs" />
<Compile Include="Runner\Exporters\FontExporter.cs" />
<Compile Include="Runner\Exporters\IExport.cs" />
<Compile Include="Runner\Exporters\IImageExporter.cs" />
<Compile Include="Runner\Exporters\ImageExporter.cs" />
<Compile Include="Runner\Exporters\LuaScriptExporter.cs" />
Expand All @@ -106,7 +105,8 @@
<Compile Include="Runner\Exporters\ZipDiskExporter.cs" />
<Compile Include="Runner\Exporters\ZipExporter.cs" />
<Compile Include="Runner\PixelVision8Runner.cs" />
<Compile Include="Runner\Services\ExportService.cs" />
<Compile Include="Runner\Services\BaseExportService.cs" />
<Compile Include="Runner\Services\GameDataExportService.cs" />
<Compile Include="Runner\Services\LoadServicePlus.cs" />
<Compile Include="Runner\Services\LuaPlatformAccessorService.cs" />
<Compile Include="Runner\Services\LuaServicePlus.cs" />
Expand Down
4 changes: 2 additions & 2 deletions Runners/PixelVision8/Runner/Editors/GameEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -901,9 +901,9 @@ public bool ExportScript(string scriptName, string outputFileName)
// filePath = UniqueFilePath(filePath.AppendFile("pattern+" + id + ".wav"));

// TODO exporting sprites doesn't work
if (serviceManager.GetService(typeof(ExportService).FullName) is ExportService exportService)
if (serviceManager.GetService(typeof(GameDataExportService).FullName) is GameDataExportService exportService)
{
exportService.Reset();
exportService.Restart();

exportService.AddExporter(new LuaScriptExporter(scriptName, outputFileName, serviceManager.GetService(typeof(LuaService).FullName) as LuaServicePlus));
//
Expand Down
4 changes: 0 additions & 4 deletions Runners/PixelVision8/Runner/Exporters/DiskExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@
// Shawn Rakowski - @shwany
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using PixelVision8.Runner.Services;
using PixelVision8.Runner.Utils;
using PixelVision8.Runner.Workspace;

namespace PixelVision8.Runner.Exporters
Expand Down
28 changes: 0 additions & 28 deletions Runners/PixelVision8/Runner/Exporters/IExport.cs

This file was deleted.

10 changes: 4 additions & 6 deletions Runners/PixelVision8/Runner/PixelVision8Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ public PixelVision8Runner()
// protected List<Dictionary<string, string>> metaDataHistory = new List<Dictionary<string, string>>();
protected string Documents => Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

public ExportService ExportService { get; private set; }
public GameDataExportService ExportService { get; protected set; }
public bool Recording { get; set; }

protected override void ConfigureRunner()
{
base.ConfigureRunner();

screenshotService = new ScreenshotService(workspaceServicePlus);
ExportService = new ExportService(); //TODO Need to create a new AudioClipAdaptor
ExportService = new GameDataExportService(); //TODO Need to create a new AudioClipAdaptor

autoShutdown = bios.ReadBiosData("AutoShutdown", "True") == "True";

Expand Down Expand Up @@ -749,18 +749,16 @@ public override void SaveGameData(string path, IEngine engine, SaveFlags saveFla
// Export the current game

// TODO exporter needs a callback when its completed
ExportService.ExportGame(path, engine, saveFlags);
ExportService.ExportGame(path, engine, saveFlags, useSteps);

// TODO this should be moved into the ExportGame class
ExportService.StartExport(useSteps);
}

public override void ConfigureServices()
{

base.ConfigureServices();

serviceManager.AddService(typeof(ExportService).FullName, ExportService);
serviceManager.AddService(typeof(GameDataExportService).FullName, ExportService);
}

public override void CreateLuaService()
Expand Down
194 changes: 194 additions & 0 deletions Runners/PixelVision8/Runner/Services/BaseExportService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
//
// Copyright (c) Jesse Freeman, Pixel Vision 8. All rights reserved.
//
// Licensed under the Microsoft Public License (MS-PL) except for a few
// portions of the code. See LICENSE file in the project root for full
// license information. Third-party libraries used by Pixel Vision 8 are
// under their own licenses. Please refer to those libraries for details
// on the license they use.
//
// Contributors
// --------------------------------------------------------
// This is the official list of Pixel Vision 8 contributors:
//
// Jesse Freeman - @JesseFreeman
// Christina-Antoinette Neofotistou @CastPixel
// Christer Kaitila - @McFunkypants
// Pedro Medeiros - @saint11
// Shawn Rakowski - @shwany
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using PixelVision8.Engine.Services;
using PixelVision8.Runner.Exporters;

namespace PixelVision8.Runner.Services
{
public abstract class BaseExportService : AbstractService
{
protected readonly List<IAbstractExporter> exporters = new List<IAbstractExporter>();
protected int currentParserID;
protected int currentStep;
protected bool exporting;
protected BackgroundWorker exportWorker;
public Dictionary<string, byte[]> files = new Dictionary<string, byte[]>();
protected Dictionary<string, object> message = new Dictionary<string, object>();
protected int totalParsers => exporters.Count;
public int totalSteps;
public bool completed => currentParserID >= totalParsers;
protected float percent => currentStep / (float)totalSteps;

public virtual bool IsExporting()
{
return exporting;
}

// TODO need to make this work like loading does
public virtual int ReadExportPercent()
{
return (int)(percent * 100);
}

public virtual Dictionary<string, object> ReadExportMessage()
{
return message;
}

public virtual void AddExporter(IAbstractExporter exporter)
{
// Calculate the steps for the exporter
exporter.CalculateSteps();

exporters.Add(exporter);

totalSteps += exporter.totalSteps;
}

public virtual void StartExport(bool useSteps = true)
{
Restart();

if (useSteps == false)
{
ExportAll();


}
else
{
exportWorker = new BackgroundWorker
{
// TODO need a way to of locking this.

WorkerSupportsCancellation = true,
WorkerReportsProgress = true
};

exportWorker.DoWork += WorkerExportSteps;
// bgw.ProgressChanged += WorkerLoaderProgressChanged;
exportWorker.RunWorkerCompleted += WorkerExporterCompleted;

// bgw.WorkerReportsProgress = true;
exportWorker.RunWorkerAsync();

exporting = true;
}
}

protected virtual void WorkerExportSteps(object sender, DoWorkEventArgs e)
{

var total = totalSteps; //some number (this is your variable to change)!!

for (var i = 0; i <= total; i++) //some number (total)
{
try
{
NextExporter();
}
catch (Exception exception)
{
Console.WriteLine(exception);
// throw;
}

Thread.Sleep(1);
exportWorker.ReportProgress((int)(percent * 100), i);
}
}

protected virtual void WorkerExporterCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (locator.GetService(typeof(WorkspaceService).FullName) is WorkspaceService workspaceService)
{


// Aggregate all Get all the messages
foreach (var exporter in exporters)
{
if (exporter.bytes != null)
{
files.Add(exporter.fileName, exporter.bytes);
}

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

workspaceService.SaveExporterFiles(files);

files.Clear();

}

exporting = false;
}

#region Main APIs

public virtual void ExportAll()
{
while (completed == false) NextExporter();

WorkerExporterCompleted(null, null);
}


public virtual void NextExporter()
{
if (completed) return;

var parser = exporters[currentParserID];

parser.NextStep();

currentStep++;

if (parser.completed)
{
currentParserID++;
}
}

public virtual void Restart()
{
currentParserID = 0;
totalSteps = 0;
currentStep = 0;
message.Clear();
}

public virtual void Clear()
{
exporters.Clear();
files.Clear();
}

#endregion
}
}

0 comments on commit 88d990a

Please sign in to comment.