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

Commit

Permalink
Adding support to call exporters from Lua.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessefreeman committed Feb 19, 2020
1 parent 972a66f commit d59a0dd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 93 deletions.
31 changes: 31 additions & 0 deletions Runners/PixelVision8/Runner/Editors/GameEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,37 @@ public void ExportPattern(WorkspacePath path, int id)
workspace.ExportPattern(path, musicChip, soundChip, id);
}

public bool ExportScript(string scriptName, string outputFileName)
{

try
{
// filePath = UniqueFilePath(filePath.AppendFile("pattern+" + id + ".wav"));

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

exportService.AddExporter(new LuaExporter(scriptName, outputFileName));
//
exportService.StartExport();

return true;
}

}
catch (Exception e)
{
// TODO this needs to go through the error system?
Console.WriteLine(e);

}

return false;

}

/// <summary>
/// Change the value if unique sprites are loaded in
/// </summary>
Expand Down
75 changes: 3 additions & 72 deletions Runners/PixelVision8/Runner/Services/ExportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

using System.Collections.Generic;
using System.ComponentModel;
// using System.Diagnostics;
using System.Threading;
using PixelVision8.Engine;
using PixelVision8.Engine.Chips;
Expand Down Expand Up @@ -77,38 +76,13 @@ public void ExportGame(string path, IEngine engine, SaveFlags saveFlags)
{
Reset();

// exporting = true;

// Make sure that the texture factory has the current mask color if any images are going to be exported.
// textureFactory.maskColor = new ColorData(engine.colorChip.maskColor);

// Console.WriteLine("MASK COLOR " + engine.colorChip.maskColor);

// Console.WriteLine("Export Game " + path);

// var watch = Stopwatch.StartNew();

// Save the engine so we can work with it during loading
targetEngine = engine;

// Step 1. Load the system snapshot
if ((saveFlags & SaveFlags.System) == SaveFlags.System)
AddExporter(new SystemExporter(path + "data.json", targetEngine));

// Step 2 (optional). Load up the Lua script
// if ((saveFlags & SaveFlags.Code) == SaveFlags.Code)
// {
// // //var scriptExtension = ".lua";
// //
// // var paths = files.Keys.Where(s => textExtensions.Any(x => s.EndsWith(x))).ToList();
// //
// // foreach (var fileName in paths)
// // {
// // parser = LoadScript(fileName, files[fileName]);
// // AddExporter(parser);
// // }
// }

// Step 3 (optional). Look for new colors
if ((saveFlags & SaveFlags.Colors) == SaveFlags.Colors)
{
Expand All @@ -130,48 +104,9 @@ public void ExportGame(string path, IEngine engine, SaveFlags saveFlags)
var imageExporter = new PNGWriter();

AddExporter(new SpriteExporter(path + "sprites.png", targetEngine, imageExporter));
// var spriteChip = targetEngine.spriteChip;
//
// var pixelData = spriteChip.texture.GetPixels();
//
// //TODO need to crop the pixel data so we only save out what we need
//
// AddExporter(new ImageExporter("sprite.cache.png", pixelData, spriteChip.textureWidth, spriteChip.textureHeight, targetEngine.colorChip.colors, textureFactory));
////
////
// parser = LoadSprites(files);
// if (parser != null)
// AddExporter(parser);

}

// Step 6 (optional). Look for tile map to load
// if ((saveFlags & SaveFlags.Tilemap) == SaveFlags.Tilemap)
// {
// var imageExporter = new PNGWriter();
//
// var tmp = new TilemapExporter(path + "tilemap.png", targetEngine, imageExporter);
// AddExporter(tmp);
// }
//
// if ((saveFlags & SaveFlags.TilemapFlags) == SaveFlags.TilemapFlags)
// {
// var imageExporter = new PNGWriter();
//
// AddExporter(new TilemapFlagExporter(path + "tilemap-flags.png", targetEngine, imageExporter));
// }

// if ((saveFlags & SaveFlags.FlagColors) == SaveFlags.FlagColors)
// {
// AddExporter(new FlagColorExporter(path + "flags.png", targetEngine, textureFactory));
// //AddExporter(new FlagTileExporter(path + "flags.png", targetEngine, textureFactory));
// }

// if ((saveFlags & SaveFlags.TileColorOffset) == SaveFlags.TileColorOffset)
// {
// AddExporter(new TileColorOffsetExporter(path + "tile-color-offsets.json", targetEngine));
// }

//
// Step 7 (optional). Look for fonts to load
if ((saveFlags & SaveFlags.Fonts) == SaveFlags.Fonts)
{
Expand Down Expand Up @@ -222,10 +157,7 @@ public void ExportGame(string path, IEngine engine, SaveFlags saveFlags)

totalParsers = exporters.Count;
currentParserID = 0;

// watch.Stop();

// UnityEngine.Debug.Log("Game Exporter Setup Time - " + watch.ElapsedMilliseconds);

}

public void AddExporter(IAbstractExporter exporter)
Expand Down Expand Up @@ -304,8 +236,7 @@ public void StartExport(bool useSteps = true)

private void WorkerExportSteps(object sender, DoWorkEventArgs e)
{
// var result = e.Result;


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

for (var i = 0; i <= total; i++) //some number (total)
Expand Down
2 changes: 2 additions & 0 deletions Runners/PixelVision8/Runner/Services/LuaServicePlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public override void ConfigureScript(Script luaScript)
luaScript.Globals["SaveText"] = new Action<WorkspacePath, string>(SaveText);
luaScript.Globals["SaveImage"] = new Action<WorkspacePath, Image>(SaveImage);

luaScript.Globals["AddExporter"] = new Action<WorkspacePath, Image>(SaveImage);

luaScript.Globals["NewWorkspacePath"] = new Func<string, WorkspacePath>(WorkspacePath.Parse);

UserData.RegisterType<WorkspacePath>();
Expand Down
19 changes: 0 additions & 19 deletions SDK/Runner/Parsers/SpriteImageParser.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
//
// 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.Linq;
Expand Down
6 changes: 4 additions & 2 deletions SDK/Runner/Services/LoadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public LoadService(IFileLoadHelper fileLoadHelper)

public bool Completed => currentParserID >= TotalParsers;

public float Percent => currentStep / (float) TotalSteps;
public float Percent => TotalSteps == 0 ? 1f : currentStep / (float) TotalSteps;

/// <summary>
/// This can be used to display a message while preloading
Expand Down Expand Up @@ -222,7 +222,7 @@ public void NextParser()

public void StartLoading()
{

loadingWorker = new BackgroundWorker
{
// TODO need a way to of locking this.
Expand Down Expand Up @@ -446,6 +446,8 @@ protected void LoadSaveData(string[] files)

if (!string.IsNullOrEmpty(file))
{


// var fileContents = Encoding.UTF8.GetString(ReadAllBytes(file));

AddParser(new SystemParser(file, _fileLoadHelper, targetEngine));
Expand Down

0 comments on commit d59a0dd

Please sign in to comment.