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

Commit

Permalink
Adding new disk exporters.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessefreeman committed Mar 6, 2020
1 parent fc50a6d commit 410243e
Show file tree
Hide file tree
Showing 8 changed files with 584 additions and 344 deletions.
3 changes: 3 additions & 0 deletions Runners/PixelVision8/PixelVision8Runner.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="Runner\Audio\SfxrMusicGeneratorChip.cs" />
<Compile Include="Runner\Editors\GameEditor.cs" />
<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" />
Expand All @@ -102,6 +103,8 @@
<Compile Include="Runner\Exporters\SpriteExporter.cs" />
<Compile Include="Runner\Exporters\SystemExporter.cs" />
<Compile Include="Runner\Exporters\TilemapJsonExporter.cs" />
<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\LoadServicePlus.cs" />
Expand Down
78 changes: 78 additions & 0 deletions Runners/PixelVision8/Runner/Exporters/DiskExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// 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.IO;
using System.Linq;
using PixelVision8.Runner.Services;
using PixelVision8.Runner.Utils;
using PixelVision8.Runner.Workspace;

namespace PixelVision8.Runner.Exporters
{
public class DiskExporter : ZipExporter
{
private long maxFileSize;

public DiskExporter(string fileName, IFileLoadHelper fileLoadHelper, Dictionary<WorkspacePath, WorkspacePath> srcFiles, long maxFileSize = 512, int compressionLevel = 4) : base(fileName, fileLoadHelper, srcFiles, compressionLevel)
{
this.maxFileSize = maxFileSize;
}

public override void CalculateSteps()
{
base.CalculateSteps();

steps.Add(ValidateSize);
; }

private void ValidateSize()
{

if ((bool)Response["success"])
{

// TODO need to add lib files to zip (but they are going to have different source and destination paths)

// Copy the file to the right location
var fileSize = (long)Response["fileSize"];

if (fileSize > maxFileSize)
{

// Change the response message to reflect that the file is to big to save
Response["message"] =
"The game is too big to compile. You'll need to increase the game's size to create a new build with the current files.";

// Set the success back to false
Response["success"] = false;

bytes = null;

}

}

StepCompleted();
}
}

}
192 changes: 192 additions & 0 deletions Runners/PixelVision8/Runner/Exporters/ZipDiskExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
//
// 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.IO;
using System.Linq;
using PixelVision8.Runner.Services;
using PixelVision8.Runner.Utils;
using PixelVision8.Runner.Workspace;

namespace PixelVision8.Runner.Exporters
{
public class ZipDiskExporter : AbstractExporter
{
protected ZipExporter zipExporter;
private WorkspacePath diskPath;
private string physicalPath;
private string physicalBackupPath;
private ZipFileSystem zipFileSystem;
private WorkspaceService workspaceService;

public Dictionary<string, object> Response = new Dictionary<string, object>
{
{"success", false},
{"message", ""}
};

public ZipDiskExporter(string fileName, WorkspaceService workspaceService) : base(fileName)
{

diskPath = WorkspacePath.Parse(fileName);
this.workspaceService = workspaceService;
this.FileLoadHelper = new WorkspaceFileLoadHelper(this.workspaceService);

}

public override void CalculateSteps()
{
base.CalculateSteps();

if (workspaceService.Exists(diskPath))
{
zipFileSystem = workspaceService.Get(diskPath).Value as ZipFileSystem;

if (zipFileSystem == null || zipFileSystem.PhysicalRoot == null)
{
return;
}

// Get the physical path
physicalPath = zipFileSystem.PhysicalRoot;
physicalBackupPath = zipFileSystem.PhysicalRoot + ".bak";

steps.Add(BackupZip);

// Get all the files
var srcFiles = zipFileSystem.GetEntitiesRecursive(WorkspacePath.Root).ToArray();

// Convert files into the correct format: src/dest path
var files = new Dictionary<WorkspacePath, WorkspacePath>();
foreach (var file in srcFiles)
{
files.Add(diskPath.AppendPath(file), file);
}

// Create the zip exporter
zipExporter = new ZipExporter(diskPath.Path, FileLoadHelper, files);

// Calculate all the zip steps
zipExporter.CalculateSteps();

for (int i = 0; i < zipExporter.totalSteps; i++)
{
steps.Add(NextZipStep);
}

// Save the disk
steps.Add(SaveDisk);

steps.Add(CheckForErrors);

steps.Add(Cleanup);

}

}

private void NextZipStep()
{
zipExporter.NextStep();

// Update response from zip exporter
Response["success"] = zipExporter.Response["success"];
Response["message"] = zipExporter.Response["message"];

StepCompleted();

}

private void CheckForErrors()
{
// Need to check for an error
if ((bool)Response["success"] == false)
{

// Restore the old zip
if (File.Exists(physicalBackupPath))
{
// Rename the old zip back to its original name
File.Move(physicalBackupPath, physicalPath);
}
}

StepCompleted();
}

private void BackupZip()
{
// Move the original file so we keep it safe
if (File.Exists(physicalPath)) File.Move(physicalPath, physicalBackupPath);

StepCompleted();
}

private void Cleanup()
{

// Restore the old zip
if (File.Exists(physicalBackupPath))
{
// Delete the failed zip
if (File.Exists(physicalBackupPath))
{
File.Delete(physicalBackupPath);
}

}

StepCompleted();
}

private void SaveDisk()
{
try
{
if ((bool)zipExporter.Response["success"])
{
using (var fs = new FileStream(physicalPath, FileMode.Create, FileAccess.Write))
{
fs.Write(zipExporter.bytes, 0, zipExporter.bytes.Length);
}

}

}
catch (Exception e)
{
// Change the success to false
Response["success"] = false;
Response["message"] = e.Message;

}

StepCompleted();
}

public override void Dispose()
{
base.Dispose();
zipExporter.Dispose();
}
}

}

0 comments on commit 410243e

Please sign in to comment.