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
fc50a6d
commit 410243e
Showing
8 changed files
with
584 additions
and
344 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
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
192
Runners/PixelVision8/Runner/Exporters/ZipDiskExporter.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,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(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.