This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca3fdca
commit 88d990a
Showing
12 changed files
with
282 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
Runners/PixelVision8/Runner/Services/BaseExportService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.