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

Commit

Permalink
Cleaning up parsers, creating disks, and APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessefreeman committed Mar 4, 2020
1 parent da3f0ea commit fc50a6d
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 363 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace PixelVision8.Runner.Exporters
/// Leverage the built in sprite parser to do the cutting up and indexing work for us
/// </summary>
/// TODO this needs to extend Sprite Data Parser
internal class SpriteDataParser : SpriteParser
internal class SpriteDataParser : SpriteImageParser
{
public int[] ids;
public int totalSpritesInTexture;
Expand Down
61 changes: 26 additions & 35 deletions Runners/PixelVision8/Runner/Services/LuaServicePlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public override void ConfigureScript(Script luaScript)
luaScript.Globals["StopWav"] = new Action(StopWav);

luaScript.Globals["CreateDisk"] =
new Func<string, WorkspacePath[], WorkspacePath, int, string[], Dictionary<string, object>>(CreateDisk);
new Func<string, Dictionary<WorkspacePath, WorkspacePath>, WorkspacePath, int, Dictionary<string, object>>(CreateDisk);
luaScript.Globals["CreateExe"] =
new Func<string, WorkspacePath[], WorkspacePath, WorkspacePath, string[], Dictionary<string, object>>(
CreateExe);
Expand Down Expand Up @@ -259,48 +259,39 @@ public long FileSize(WorkspacePath workspacePath)
return workspace.OpenFile(workspacePath, FileAccess.Read).ReadAllBytes().Length / 1024;
}

public Dictionary<string, object> CreateDisk(string gameName, WorkspacePath[] filePaths,
WorkspacePath exportPath, int maxFileSize = 512, string[] libFileNames = null)

public Dictionary<string, object> CreateDisk(string name, Dictionary<WorkspacePath, WorkspacePath> files, WorkspacePath dest, int maxFileSize = 512)
{

// }

// public Dictionary<string, object> CreateDisk(string gameName, WorkspacePath[] filePaths,
// WorkspacePath exportPath, int maxFileSize = 512, string[] libFileNames = null)
// {

var response = new Dictionary<string, object>
{
{"success", false},
{"message", ""}
};

// try
// {
// Create a path to the temp directory for the builds
var tmpExportPath = WorkspacePath.Root.AppendDirectory("Tmp").AppendDirectory("Builds");

// Make sure there is a builds folder in the Tmp directory
if (workspace.Exists(tmpExportPath) == false) workspace.CreateDirectory(tmpExportPath);
// Create a path to the temp directory for the builds
var tmpExportPath = WorkspacePath.Root.AppendDirectory("Tmp").AppendDirectory("Builds");

// Create a folder with the timestamp
tmpExportPath = tmpExportPath.AppendDirectory(DateTime.Now.ToString("yyyyMMddHHmmss"));
workspace.CreateDirectory(tmpExportPath);
// Make sure there is a builds folder in the Tmp directory
if (workspace.Exists(tmpExportPath) == false) workspace.CreateDirectory(tmpExportPath);

// Add the zip filename to it
var tmpZipPath = tmpExportPath.AppendFile(gameName + ".pv8");

// var oldLength = filePaths.Length;
// // TODO need to add all the lib files in
//
// Array.Resize(ref filePaths, oldLength + libFileNames.Length);
// var j = 0;
//
// for (int i = oldLength; i < filePaths.Length; i++)
// {
// filePaths[i] = Work
// j++;
// }
// Create a folder with the timestamp
tmpExportPath = tmpExportPath.AppendDirectory(DateTime.Now.ToString("yyyyMMddHHmmss"));
workspace.CreateDirectory(tmpExportPath);

// Add the zip filename to it
var tmpZipPath = tmpExportPath.AppendFile(name + ".pv8");

// TODO make sure using is ok here so it closes out the stream when done
var stream = workspace.CreateFile(tmpZipPath);

response = workspace.CreateZipFile(stream, filePaths);
response = workspace.CreateZipFile(stream, files);

if ((bool)response["success"])
{
Expand Down Expand Up @@ -328,25 +319,25 @@ public long FileSize(WorkspacePath workspacePath)
}

// Move the new build over
exportPath = workspace.UniqueFilePath(exportPath.AppendDirectory("Build"));
dest = workspace.UniqueFilePath(dest.AppendDirectory("Build"));

// Create the directory for the new file
workspace.CreateDirectoryRecursive(exportPath);
workspace.CreateDirectoryRecursive(dest);

exportPath = exportPath.AppendFile(tmpZipPath.EntityName);
dest = dest.AppendFile(tmpZipPath.EntityName);

// Copy over the file
workspace.Copy(tmpZipPath, exportPath);
workspace.Copy(tmpZipPath, dest);

// Update the response
response["success"] = true;
response["message"] = "A new build was created in " + exportPath + ".";
response["path"] = exportPath.Path;
response["message"] = "A new build was created in " + dest + ".";
response["path"] = dest.Path;
}
else
{
// Update the message for the failed build
response["message"] = "Unable to create a build for '" + gameName + "'.";
response["message"] = "Unable to create a build for '" + name + "'.";
}


Expand Down
47 changes: 32 additions & 15 deletions Runners/PixelVision8/Runner/Services/WorkspaceServicePlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,27 +415,40 @@ public void SaveDisk(WorkspacePath path)
// Move the original file so we keep it safe
if (File.Exists(fileNameZip)) File.Move(fileNameZip, fileNameZip + ".bak");

var files = zipFileSystem.GetEntitiesRecursive(WorkspacePath.Root).ToArray();

// using (var fileStream = new FileStream(fileNameZip, FileMode.Create))
// {

var response = CreateZipFile(new FileStream(fileNameZip, FileMode.Create), files, path);
var srcFiles = zipFileSystem.GetEntitiesRecursive(WorkspacePath.Root).ToArray();

var files = new Dictionary<WorkspacePath, WorkspacePath>();
foreach (var file in srcFiles)
{
files.Add(path.AppendPath(file), file);
}

var response = CreateZipFile(new FileStream(fileNameZip, FileMode.Create), files);
if (((bool)response["success"]))
{
File.Delete(fileNameZip + ".bak");
}
else
{
Console.WriteLine((string)response["error"]);
if (File.Exists(fileNameZip + ".bak")) File.Move(fileNameZip + ".bak", fileNameZip);
if (File.Exists(fileNameZip + ".bak"))
{
// Delete the failed zip
if (File.Exists(fileNameZip))
{
File.Delete(fileNameZip);
}

// Rename the old zip back to its original name
File.Move(fileNameZip + ".bak", fileNameZip);
}
}

}
}
}

public Dictionary<string, object> CreateZipFile(Stream zipFS, WorkspacePath[] files, WorkspacePath sourceRoot = default)
public Dictionary<string, object> CreateZipFile(Stream zipFS, Dictionary<WorkspacePath, WorkspacePath> files)
{
var response = new Dictionary<string, object>
{
Expand All @@ -454,22 +467,25 @@ public void SaveDisk(WorkspacePath path)
var buffer = new byte[4096];
try
{
foreach (var file in files)
foreach (var filePaths in files)
{
var srcFile = filePaths.Key;
var destFile = filePaths.Value;

// We can only save files
if (file.IsFile && !file.EntityName.StartsWith("."))
if (srcFile.IsFile && !srcFile.EntityName.StartsWith(".") && destFile.IsFile)
{
var tmpPath = file.Path.Substring(1);


// Using GetFileName makes the result compatible with XP
// as the resulting path is not absolute.
var entry = new ZipEntry(tmpPath)
var entry = new ZipEntry(destFile.Path.Substring(1))
{
// Could also use the last write time or similar for the file.
DateTime = DateTime.Now
};
archive.PutNextEntry(entry);

using (var fs = OpenFile(sourceRoot.AppendPath(file), FileAccess.Read))
using (var fs = OpenFile(srcFile, FileAccess.Read))
{
// Using a fixed size buffer here makes no noticeable difference for output
// but keeps a lid on memory usage.
Expand All @@ -485,6 +501,8 @@ public void SaveDisk(WorkspacePath path)
archive.CloseEntry();
}

}

var fileSize = zipFS.Length / 1024;

// Finish is important to ensure trailing information for a Zip file is appended. Without this
Expand All @@ -499,7 +517,6 @@ public void SaveDisk(WorkspacePath path)
}
catch (Exception e)
{
// Console.WriteLine("Archive Error: " + e);
response["error"] = e.Message;
response["success"] = false;
}
Expand Down
22 changes: 10 additions & 12 deletions SDK/Engine/Chips/Game/GameChip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,15 +444,6 @@ public Point Display(bool visible = true)
return display;
}

/// <summary>
/// Returns the visible bounds of the display as a Rectangle.
/// </summary>
/// <returns></returns>
// public Rectangle VisibleBounds()
// {
// return displayChip.visibleBounds;
// }

/// <summary>
/// This method allows you to draw raw pixel data directly to the display. Depending on which draw mode you
/// use, the pixel data could be rendered as a sprite or drawn directly onto the tilemap cache. Sprites drawn
Expand Down Expand Up @@ -932,9 +923,6 @@ public Point Display(bool visible = true)
/// An optional int value to override the scroll Y position. This is useful when you need to change the top y position
/// from where to sample the tilemap data from.
/// </param>
/// <param name="DrawMode">
/// This accepts DrawMode Tile and TilemapCache.
/// </param>
public void DrawTilemap(int x = 0, int y = 0, int columns = 0, int rows = 0, int? offsetX = null,
int? offsetY = null)
{
Expand Down Expand Up @@ -1899,6 +1887,16 @@ public Point CalculatePosition(int index, int width)

private StringBuilder _sb = new StringBuilder();

public int ReadFPS()
{
return fps;
}

public int ReadTotalSprites()
{
return CurrentSprites;
}

/// <summary>
/// This allows you to call the TextUtil's WordWrap helper to wrap a string of text to a specified character
/// width. Since the FontChip only knows how to render characters as sprites, this can be used to calculate
Expand Down
4 changes: 2 additions & 2 deletions SDK/Engine/Chips/Game/LuaGameChip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ protected virtual void RegisterLuaServices()

#region Debug

LuaScript.Globals["ReadFPS"] = new Func<int>(() => fps);
LuaScript.Globals["ReadTotalSprites"] = new Func<int>(() => CurrentSprites);
LuaScript.Globals["ReadFPS"] = new Func<int>(ReadFPS);
LuaScript.Globals["ReadTotalSprites"] = new Func<int>(ReadTotalSprites);

#endregion

Expand Down
3 changes: 1 addition & 2 deletions SDK/PixelVision8.SDK.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Runner\Parsers\MetaDataParser.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Runner\Parsers\PNGFileReader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Runner\Parsers\PNGReader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Runner\Parsers\SpriteParser.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Runner\Parsers\SystemParser.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Runner\Parsers\TilemapJsonParser.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Runner\Parsers\TilemapParser.cs" />
Expand All @@ -85,4 +84,4 @@
<ItemGroup>
<Content Include="$(MSBuildThisFileDirectory)Runner\Utils\AnimatedGifEncoder\LICENSE.txt" />
</ItemGroup>
</Project>
</Project>
40 changes: 20 additions & 20 deletions SDK/Runner/Parsers/SpriteImageParser.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
//
// 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 All @@ -13,16 +32,12 @@ public class SpriteImageParser : ImageParser
protected Color[] colorData;
protected int cps;
protected int index;
// protected Color maskColor;
protected int maxSprites;
protected SpriteChip spriteChip;
protected int[] spriteData;
protected int spriteHeight;
protected int spritesAdded;
protected int spriteWidth;
// protected Color[] srcColors;
protected Color[] tmpPixels;
// protected int totalPixels;
protected int totalSprites;
protected int x, y;
protected Image image;
Expand Down Expand Up @@ -98,8 +113,7 @@ public virtual void CutOutSprites()
ConvertColorsToIndexes(cps);

ProcessSpriteData();
// }


index++;

}
Expand Down Expand Up @@ -136,23 +150,9 @@ protected virtual void ProcessSpriteData()
}
}

public virtual bool IsEmpty(Color[] pixels)
{
var total = pixels.Length;
var transPixels = 0;

for (var i = 0; i < total; i++)
if (pixels[i].A < 1)
transPixels++;

return transPixels == total;
}

public virtual void ConvertColorsToIndexes(int totalColors)
{

spriteData = image.GetSpriteData(index, totalColors);

}

public override void Dispose()
Expand Down

0 comments on commit fc50a6d

Please sign in to comment.