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
/
WorkspaceServicePlus.cs
executable file
·468 lines (367 loc) · 15.6 KB
/
WorkspaceServicePlus.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//
// 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 PixelVision8.Player;
using PixelVision8.Runner.Exporters;
using PixelVision8.Runner;
using PixelVision8.Runner.Workspace;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace PixelVision8.Editor
{
public class WorkspaceServicePlus : WorkspaceService
{
// private bool disksInvalid = true;
private readonly List<WorkspacePath> _disks = new List<WorkspacePath>();
public WorkspaceServicePlus(KeyValuePair<WorkspacePath, IFileSystem> mountPoint) : base(mountPoint)
{
}
// private SortedList<WorkspacePath, IFileSystem> DiskMount => Mounts as SortedList<WorkspacePath, IFileSystem>;
public int TotalDisks => _disks.Count;
public int MaxDisks { get; set; } = 2;
public WorkspacePath[] Disks => _disks.ToArray();
public void MountWorkspace(string name)
{
var filePath = WorkspacePath.Root.AppendDirectory("User");
// Make sure that the user directory exits
if (Exists(filePath))
{
filePath = filePath.AppendDirectory(name);
// If the filesystem doesn't exit, we want to create it
if (!Exists(filePath)) CreateDirectory(filePath);
}
var workspaceDisk = new SubFileSystem(this, filePath);
Mounts.Add(
new KeyValuePair<WorkspacePath, IFileSystem>(WorkspacePath.Root.AppendDirectory("Workspace"),
workspaceDisk));
}
public void RebuildWorkspace()
{
var osPath = WorkspacePath.Root.AppendDirectory("PixelVisionOS");
if (Exists(osPath)) Mounts.Remove(Get(osPath));
var systemPaths = new List<IFileSystem>
{
new SubFileSystem(this,
WorkspacePath.Root.AppendDirectory("App").AppendDirectory("PixelVisionOS"))
};
// Create a path to the workspace system folder
var path = WorkspacePath.Root.AppendDirectory("Workspace").AppendDirectory("System");
// Look to see if the workspace system folder exists
if (Exists(path))
// Add the workspace system folder to the os file system
systemPaths.Add(new SubFileSystem(this, path));
// Mount the PixelVisionOS directory
AddMount(new KeyValuePair<WorkspacePath, IFileSystem>(osPath, new MergedFileSystem(systemPaths)));
}
// Exports the active song in the music chip
public void ExportSong(string path, MusicChip musicChip, SoundChip soundChip, int id)
{
var currentSong = musicChip.songs[id];
var selectedPatterns = new int[currentSong.end];
Array.Copy(currentSong.patterns, selectedPatterns, selectedPatterns.Length);
var filePath = WorkspacePath.Parse(path);
if (Exists(filePath))
{
filePath = filePath.AppendDirectory("Wavs").AppendDirectory("Songs");
if (!Exists(filePath)) CreateDirectory(filePath);
try
{
filePath = UniqueFilePath(filePath.AppendFile("song " + id + " - " + currentSong.name + ".wav"));
Console.WriteLine("Export song to " + filePath);
// TODO exporting sprites doesn't work
if (locator.GetService(typeof(GameDataExportService).FullName) is GameDataExportService
exportService)
{
exportService.ExportSong(filePath.Path, musicChip, soundChip, selectedPatterns);
//
exportService.StartExport();
}
}
catch (Exception e)
{
// TODO this needs to go through the error system?
Console.WriteLine(e);
throw;
}
// TODO saving song doesn't work
// runner.exportService.ExportSong(filePath.Path, musicChip, soundChip);
//
// runner.StartExport();
}
}
public void ExportPattern(WorkspacePath filePath, MusicChip musicChip, SoundChip soundChip, int id)
{
var selectedPatterns = new int[id];
if (!Exists(filePath)) CreateDirectory(filePath);
try
{
filePath = UniqueFilePath(filePath.AppendFile("pattern+" + id + ".wav"));
// TODO exporting sprites doesn't work
if (locator.GetService(typeof(GameDataExportService).FullName) is GameDataExportService exportService)
{
exportService.ExportSong(filePath.Path, musicChip, soundChip, selectedPatterns);
//
exportService.StartExport();
}
}
catch (Exception e)
{
// TODO this needs to go through the error system?
Console.WriteLine(e);
throw;
}
}
public string MountDisk(string path)
{
IFileSystem disk;
string entityName;
var attr = File.GetAttributes(path);
if (attr.HasFlag(FileAttributes.Directory))
entityName = new DirectoryInfo(path).Name;
else
entityName = Path.GetFileNameWithoutExtension(path);
if (path.EndsWith(".pv8") || path.EndsWith(".zip"))
disk = ZipFileSystem.Open(path);
else
disk = new PhysicalFileSystem(path);
if (disk == null) return null;
// Test to see if the disk is a valid game
if (ValidateGameInDir(disk) == false &&
disk.Exists(WorkspacePath.Root.AppendFile("info.json")) == false)
return null;
// Update the root path to just be the name of the entity
var rootPath = WorkspacePath.Root.AppendDirectory("Disks").AppendDirectory(entityName);
// Add the new disk
AddDisk(rootPath, disk);
// Return the disk name
return entityName;
}
public string AutoRunGameFromDisk(string diskName)
{
var diskPath = WorkspacePath.Root.AppendDirectory("Disks")
.AppendDirectory(diskName);
var autoRunPath = diskPath.AppendFile("info.json");
// Try to read the disk's info file and see if there is an auto run path
try
{
// Only run a disk if there is an auto run file in there
if (Exists(autoRunPath))
{
var json = ReadTextFromFile(autoRunPath);
var autoRunData = Json.Deserialize(json) as Dictionary<string, object>;
var tmpPath = autoRunData["AutoRun"] as string;
// Get the auto run from the json file
var newDiskPath = WorkspacePath.Parse($"/Disks/{diskName}{tmpPath}");
// Change the disk path to the one in the auto-run file
if (Exists(newDiskPath)) diskPath = newDiskPath;
}
}
catch
{
// ignored
}
// Always validate that the disk is a valid game before trying to load it.
if (ValidateGameInDir(diskPath))
{
// TODO need to make sure the auto run disk is at the top of the list
// Move the new disk to the top of the list
var diskPaths = new List<string>();
// Add the remaining disks
foreach (var disk in Disks) diskPaths.Add(DiskPhysicalRoot(disk));
// TODO this shouldn't eject the disk that is about to be loaded since it could force a zip file to be rewritten before it is loaded
// Remove the old disks
EjectAll();
// Mount all the disks
foreach (var oldPath in diskPaths) MountDisk(oldPath);
return diskPath.Path;
}
return null;
}
public string DiskPhysicalRoot(WorkspacePath disk)
{
var physicalPath = "";
if (Exists(disk))
{
if (Get(disk).Value is PhysicalFileSystem fileSystem)
physicalPath = fileSystem.PhysicalRoot;
else if (Get(disk).Value is ZipFileSystem system) physicalPath = system.PhysicalRoot;
}
return physicalPath;
}
public void SaveActiveDisk()
{
// Create a new mount point for the current game
var rootPath = WorkspacePath.Root.AppendDirectory("Game");
// Save the active disk if it is a zip file system
if (currentDisk is ZipFileSystem disk) SaveDisk(rootPath);
// Make sure we don't have a disk with the same name
if (Exists(rootPath)) Mounts.Remove(Get(rootPath));
}
public override void ShutdownSystem()
{
foreach (var disk in Disks) SaveDisk(disk);
base.ShutdownSystem();
}
// Make sure you can only eject a disk by forcing the path to be in the disk mounts scope
public void EjectDisk(WorkspacePath filePath)
{
RemoveDisk(filePath);
// What happens when there are no disks
if (TotalDisks > 1)
try
{
// Get the next disk name
var diskName = Disks.First().Path.Replace("/", "");
// Attempt to run the fist disk
AutoRunGameFromDisk(diskName);
}
catch
{
// ignored
}
}
public int GenerateSprites(string path, PixelVision targetGame)
{
var count = 0;
var filePath = WorkspacePath.Parse(path);
var srcPath = filePath.AppendDirectory("SpriteBuilder");
var fileData = new Dictionary<string, byte[]>();
if (Exists(srcPath))
{
// Get all the files in the folder
var files = from file in GetEntities(srcPath)
where file.GetExtension() == ".png"
select file;
foreach (var file in files)
{
var name = file.EntityName.Substring(0, file.EntityName.Length - file.GetExtension().Length);
var bytes = OpenFile(file, FileAccess.Read).ReadAllBytes();
if (fileData.ContainsKey(name))
fileData[name] = bytes;
else
fileData.Add(name, bytes);
count++;
// Console.WriteLine("Parse File " + name);
}
try
{
// TODO exporting sprites doesn't work
if (locator.GetService(typeof(GameDataExportService).FullName) is GameDataExportService
exportService)
{
exportService.ExportSpriteBuilder(path + "sb-sprites.lua", targetGame, fileData);
//
exportService.StartExport();
}
}
catch (Exception e)
{
// TODO this needs to go through the error system?
Console.WriteLine(e);
throw;
}
}
return count;
}
public void AddDisk(WorkspacePath path, IFileSystem disk)
{
// If we are out of open disks, remove the last one
if (TotalDisks == MaxDisks) RemoveDisk(Disks.Last());
// Attempt to remove the disk if it is already inserted
RemoveDisk(path);
// Add the new disk to the disk mount
Mounts.Add(new KeyValuePair<WorkspacePath, IFileSystem>(path, disk));
if (!_disks.Contains(path)) _disks.Add(path);
// InvalidateDisks();
}
public void RemoveDisk(WorkspacePath path)
{
if (Exists(path))
{
// Check to see if this is a zip
SaveDisk(path);
// Remove disk from the mount point
Mounts.Remove(Get(path));
if (_disks.Contains(path)) _disks.Remove(path);
//
// InvalidateDisks();
}
}
public void SaveDisk(WorkspacePath path)
{
var diskExporter = new ZipDiskExporter(path.Path, this);
diskExporter.CalculateSteps();
while (diskExporter.Completed == false)
{
diskExporter.NextStep();
}
}
public Dictionary<string, object> CreateZipFile(WorkspacePath path,
Dictionary<WorkspacePath, WorkspacePath> files)
{
var fileHelper = new WorkspaceFileLoadHelper(this);
var zipExporter = new ZipExporter(path.Path, fileHelper, files);
zipExporter.CalculateSteps();
while (zipExporter.Completed == false)
{
zipExporter.NextStep();
}
try
{
if ((bool) zipExporter.Response["success"])
{
var zipPath = WorkspacePath.Parse(zipExporter.fileName);
SaveExporterFiles(new Dictionary<string, byte[]>() {{zipExporter.fileName, zipExporter.Bytes}});
}
}
catch (Exception e)
{
// Change the success to false
zipExporter.Response["success"] = false;
zipExporter.Response["message"] = e.Message;
}
return zipExporter.Response;
}
public void EjectAll()
{
foreach (var path in Disks) RemoveDisk(path);
}
public override bool Exists(WorkspacePath path)
{
// Manually return true if the path is Disks since it's not a real mount point
if (path == WorkspacePath.Root.AppendDirectory("Disks")) return MaxDisks > 0;
return base.Exists(path);
}
public override List<WorkspacePath> SharedLibDirectories()
{
// Create paths to the System/Libs and Workspace/Libs folder
var paths = base.SharedLibDirectories();
// Add disks
for (int i = 0; i < TotalDisks; i++)
{
var tmpPath = Disks[i].AppendDirectory("System").AppendDirectory("Libs");
if (Exists(tmpPath))
paths.Insert(0, tmpPath);
}
return paths;
}
}
}