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
/
GameEditor.cs
executable file
·2338 lines (1981 loc) · 77.3 KB
/
GameEditor.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// 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 Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using PixelVision8.Player;
using PixelVision8.Player.Audio;
using PixelVision8.Runner.Chips;
using PixelVision8.Runner.Exporters;
using PixelVision8.Runner;
using PixelVision8.Runner.Workspace;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Buttons = PixelVision8.Player.Buttons;
namespace PixelVision8.Runner.Editors
{
/// <summary>
/// This class allows you to edit the current sandbox game.
/// </summary>
// [MoonSharpUserData]
public class GameEditor
{
protected bool _invalid;
public ColorChip activeColorChip;
// private GCControllerChip controllerChip;
private ColorChip colorChip;
// private ColorMapChip colorMapChip; // TODO this should have a GC version of the chip
private DisplayChip displayChip;
private FontChip fontChip;
//
// public IEngine targetGame;
//
private GameChip _gameChip;
private MusicChip musicChip;
protected DesktopRunner runner;
protected IServiceLocator serviceManager;
private SfxrSoundChip soundChip;
private SpriteChip spriteChip;
private PixelVision targetGame;
private TilemapChip tilemapChip;
protected WorkspaceServicePlus workspace;
// private ColorChip colorPaletteChip;
/// <summary>
/// Creates a new Game Editor instance and loads the game's system and meta data
/// </summary>
/// <param name="workspace"></param>
/// <param name="luaService"></param>
// [MoonSharpHidden]
public GameEditor(DesktopRunner runner, IServiceLocator serviceManager)
{
this.runner = runner;
this.serviceManager = serviceManager;
// Get a reference to the workspace from the runner instance
workspace = runner.workspaceService as WorkspaceServicePlus;
}
public virtual List<string> DefaultChips
{
get
{
var chips = new List<string>
{
typeof(ColorChip).FullName,
typeof(SpriteChip).FullName,
typeof(TilemapChip).FullName,
typeof(FontChip).FullName,
typeof(ControllerChip).FullName,
typeof(DisplayChip).FullName,
typeof(SfxrSoundChip).FullName,
typeof(MusicChip).FullName,
typeof(LuaGameChip).FullName
};
return chips;
}
}
public bool Invalid => _invalid;
/// <summary>
/// Helper method to build save flags.
/// </summary>
/// <param name="flags"></param>
/// <returns></returns>
private FileFlags BuildSaveFlags(FileFlags[] flags)
{
// Since Lua doesn't know how to handle bit flags, we need to do the conversion on the C# side of things
var saveFlags = FileFlags.System;
for (var i = 0; i < flags.Length; i++)
if (flags[i] != FileFlags.System)
saveFlags |= flags[i];
return saveFlags;
}
/// <summary>
/// This allows you to load game files inside of the Workspace directory into the Game Editor. It
/// accepts an array of SaveFlags enums which define what game files to load. Use this method to load only
/// what you need to edit to speed up parsing game data.
/// </summary>
/// <param name="flags">Supply an array of SaveFlags enums for each file you want to load into the Game Editor instance.</param>
/// <returns>Returns a bool if the loading process was successful.</returns>
// TODO should use workspace path as argument
public bool Load(string path, FileFlags[] flags)
{
// // If the path is not valid, return null
// if (path == null)
// {
// return false;
// }
//
// TODO this is hard coded to the disks directory and should be from the bios or somewhere else?
// Convert to a system path
var filePath = WorkspacePath.Parse(path);
// If the file doesn't exist, return false.
if (!workspace.Exists(filePath)) return false;
var files = workspace.GetGameEntities(filePath);
var saveFlags = BuildSaveFlags(flags);
try
{
targetGame = new PixelVision(DefaultChips.ToArray(), "GameEditor")
{
ServiceLocator = serviceManager
};
// targetGame.Init();
runner.ParseFiles(files, targetGame, saveFlags);
}
catch (Exception e)
{
Console.WriteLine("Game Editor Load Error:\n" + e.Message);
return false;
}
Reset();
// TODO this needs to be tied into the preload system and not imediatly loaded
// var success = false;//workspace.ReadGameFromWorkspace(targetGame, saveFlags, true);
return true;
}
public void Reset()
{
// targetGame = new PixelVisionEngine(null, null, runner.defaultChips.ToArray());
// Configure the game editor now that all the chips have been loaded
_gameChip = targetGame.GameChip;
// Since the game is not attached to a runner it will throw an error when trying to load lua serivce
try
{
// Configure the game chip
_gameChip.Reset();
}
catch
{
// Console.WriteLine("Game Editor Reset Error:\n"+e.Message);
// Do nothing with any missing service error
}
//
// // Get references to all of the chips
// controllerChip = targetGame.controllerChip as GCControllerChip;
colorChip = targetGame.ColorChip;
// colorMapChip = targetGame.chipManager.GetChip(ColorMapParser.chipName, false) as ColorChip;
//
// colorMapChip = targetGame.colorMapChip;
displayChip = targetGame.DisplayChip;
spriteChip = targetGame.SpriteChip;
fontChip = targetGame.FontChip;
tilemapChip = targetGame.TilemapChip;
soundChip = targetGame.SoundChip as SfxrSoundChip;
musicChip = targetGame.MusicChip; // TODO need to create a SfxrMusicChip
// Console.WriteLine("MC Tracks " + musicChip.totalTracks);
songGenerator = new SfxrMusicGeneratorChip();
//
// colorPaletteChip = targetGame.chipManager.GetChip(ColorPaletteParser.chipName, false) as ColorChip;
// ChangeColorMode();
}
/// <summary>
/// Returns a list of library paths
/// </summary>
/// <returns></returns>
// public string[] LibraryPaths()
// {
//
// var sharedLibPaths = workspace.SharedLibDirectories();
//
// var luaFiles = new List<string>();
//
// for (int i = 0; i < sharedLibPaths.Count; i++)
// {
//
// var files = from p in workspace.GetEntities(sharedLibPaths[i])
// where p.EntityName.EndsWith("lua") select p;
//
// foreach (var file in files)
// {
// if (luaFiles.IndexOf(file.Path) == -1)
// {
// var text = workspace.ReadTextFromFile(file);
//
// luaFiles.Add(file.Path);
// }
//
// }
//
// }
//
// return luaFiles.ToArray();
// }
//
// /// <summary>
// /// Returns true if an engine is loaded
// /// </summary>
// public bool gameLoaded
// {
// get { return targetGame != null; }
// }
//
//
public void Invalidate()
{
_invalid = true;
}
public void ResetValidation()
{
_invalid = false;
}
//
/// <summary>
/// Allows you to save the current game you are editing. Pass in SaveFlags to define which files should be exported.
/// </summary>
/// <param name="flags"></param>
public void Save(string path, FileFlags[] flags, bool useSteps = false)
{
// TODO need to get the export service
// TODO should only save flags that are supplied
var saveFlags = FileFlags.None;
for (var i = 0; i < flags.Length; i++) saveFlags |= flags[i];
targetGame.SetMetadata("version", runner.SystemVersion);
// gameChip.version = ;
// TODO saving games doesn't work
runner.SaveGameData(path, targetGame, saveFlags, useSteps);
// workspace.SaveCart(workspace.WorkspacePath(WorkspaceFolders.CurrentGameDir), targetGame, saveFlags);
ResetValidation();
}
//
/// <summary>
/// Change the game's max size setting.
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
public int GameMaxSize(int? size = null)
{
if (size.HasValue)
{
_gameChip.maxSize = size.Value;
Invalidate();
}
return _gameChip.maxSize;
}
/// <summary>
/// Change the game's save slot settings.
/// </summary>
/// <param name="total"></param>
/// <returns></returns>
public int GameSaveSlots(int? total = null)
{
if (total.HasValue)
{
_gameChip.SaveSlots = total.Value;
Invalidate();
}
return _gameChip.SaveSlots;
}
/// <summary>
/// Lock or unlock the game specs.
/// </summary>
/// <param name="isLocked"></param>
/// <returns></returns>
public bool GameSpecsLocked(bool? isLocked = null)
{
if (isLocked.HasValue)
{
_gameChip.lockSpecs = isLocked.Value;
Invalidate();
}
return _gameChip.lockSpecs;
}
/// <summary>
/// Change the name of the game.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string Name(string name = null)
{
// If a new name is supplied, set it on the game chip
if (name != null)
{
targetGame.GetMetadata("name", name);
Invalidate();
}
// Return the latest name value from the gameChip
return targetGame.GetMetadata("name", GetType().Name);
}
/// <summary>
/// Get the current version of the runner.
/// </summary>
/// <returns></returns>
public string Version()
{
return targetGame.GetMetadata("version", runner.SystemVersion);
}
public string Ext(string value = null)
{
if (value != null)
{
targetGame.SetMetadata("ext", value);
Invalidate();
}
return targetGame.GetMetadata("ext", ".pv8");
}
public int BackgroundColor(int? id = null)
{
return _gameChip.BackgroundColor(id);
}
public string BackgroundColorHex()
{
return colorChip.ReadColorAt(BackgroundColor());
}
public string Color(int id, string value = null)
{
if (value == null) return activeColorChip.ReadColorAt(id);
activeColorChip.UpdateColorAt(id, value);
return value;
}
public void ClearColors(string color = null)
{
colorChip.Clear(color);
}
public void ClearSprites()
{
spriteChip.Clear();
}
/// <summary>
/// Get the TotalDisks colors or change the limit for how many colors the color chip can store.
/// </summary>
/// <param name="ignoreEmpty"></param>
/// <param name="total"></param>
/// <returns></returns>
public int TotalColors(bool ignoreEmpty = false)
{
// if (TotalDisks.HasValue)
// activeColorChip.maxColors = TotalDisks.Value;
return ignoreEmpty ? activeColorChip.totalUsedColors : activeColorChip.total;
}
// public int MaximumColors(int? value)
// {
// if (value.HasValue) colorChip.maxColors = value.Value;
//
// return colorChip.maxColors;
// }
// Since we want to be able to edit this value but the interface doesn't allow it, we hide it in lua and use the overload instead
// [MoonSharpHidden]
public int ColorsPerSprite()
{
return _gameChip.ColorsPerSprite();
}
/// <summary>
/// Get the maximum number or sprites or change it.
/// </summary>
/// <param name="total"></param>
/// <returns></returns>
public int MaxSpriteCount(int? total)
{
if (total.HasValue) targetGame.SpriteChip.maxSpriteCount = total.Value;
return _gameChip.MaxSpriteCount();
}
/// <summary>
/// Get the display size or change it.
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public Point DisplaySize(int? width = null, int? height = null)
{
var size = new Point();
var resize = false;
if (width.HasValue)
{
size.X = width.Value;
resize = true;
}
else
{
size.X = displayChip.Width;
}
if (height.HasValue)
{
size.Y = height.Value;
resize = true;
}
else
{
size.Y = displayChip.Height;
}
if (resize) displayChip.ResetResolution(size.X, size.Y);
// TODO need a flag to tell the runner to change the resolution
return new Point(displayChip.Width, displayChip.Height);
}
/// <summary>
/// Get the over scan value or change it
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
// public Point OverscanBorder(int? x, int? y)
// {
// if (x.HasValue) displayChip.OverscanX = x.Value;
//
// if (y.HasValue) displayChip.OverscanY = y.Value;
//
// return new Point(displayChip.OverscanX, displayChip.OverscanY);
// }
public void RedrawDisplay()
{
throw new NotImplementedException();
}
public Point ScrollPosition(int? x = null, int? y = null)
{
return _gameChip.ScrollPosition(x, y);
}
public void WriteSaveData(string key, string value)
{
_gameChip.WriteSaveData(key, value);
}
public string ReadSaveData(string key, string defaultValue = "undefined")
{
return _gameChip.ReadSaveData(key, defaultValue);
}
public bool Key(Keys key, InputState state = InputState.Down)
{
throw new NotImplementedException();
}
public bool MouseButton(int button, InputState state = InputState.Down)
{
throw new NotImplementedException();
}
public bool Button(Buttons button, InputState state = InputState.Down, int controllerID = 0)
{
throw new NotImplementedException();
}
public Point MousePosition()
{
throw new NotImplementedException();
}
public string InputString()
{
throw new NotImplementedException();
}
/// <summary>
/// Get the TotalDisks number of songs or change it
/// </summary>
/// <param name="total"></param>
/// <returns></returns>
public int TotalSongs(int? total = null)
{
if (total.HasValue) musicChip.totalSongs = total.Value;
return musicChip.totalSongs;
}
public void PlaySong(int id, bool loop = true, int startAt = 0)
{
_gameChip.PlaySong(id, loop, startAt);
}
public void PlayPattern(int id, bool loop = true)
{
_gameChip.PlayPattern(id);
}
/// <summary>
/// Get a song's name or change it
/// </summary>
/// <param name="id"></param>
/// <param name="value"></param>
/// <returns></returns>
public string SongName(int id, string value = null)
{
if (value != null) musicChip.songs[id].name = value;
return musicChip.songs[id].name;
}
public int SongStart(int id, int? pos = null)
{
if (pos.HasValue) musicChip.songs[id].start = pos.Value;
return musicChip.songs[id].start;
}
/// <summary>
/// Return the patterns in a song or change them.
/// </summary>
/// <param name="id"></param>
/// <param name="patterns"></param>
/// <returns></returns>
public int[] SongPatterns(int id, int[] patterns = null)
{
if (patterns != null)
{
var newPatterns = new int[patterns.Length];
Array.Copy(patterns, newPatterns, patterns.Length);
musicChip.songs[id].patterns = newPatterns;
}
return musicChip.songs[id].patterns;
}
/// <summary>
/// Get the pattern at a position in the song or change it.
/// </summary>
/// <param name="id"></param>
/// <param name="position"></param>
/// <param name="newPattern"></param>
/// <returns></returns>
public int SongPatternAt(int id, int position, int? newPattern)
{
if (newPattern.HasValue) musicChip.songs[id].UpdatePatternAt(position, newPattern.Value);
return musicChip.songs[id].patterns[position];
}
/// <summary>
/// Get the end position of a song or change it.
/// </summary>
/// <param name="id"></param>
/// <param name="pos"></param>
/// <returns></returns>
public int SongEnd(int id, int? pos = null)
{
if (pos.HasValue) musicChip.songs[id].end = pos.Value;
return musicChip.songs[id].end;
}
public void PauseSong()
{
_gameChip.PauseSong();
}
public void StopSong()
{
_gameChip.StopSong();
}
public void RewindSong(int position = 0, int loopID = 0)
{
_gameChip.RewindSong();
}
public Point SpriteSize()
{
return _gameChip.SpriteSize();
}
public int[] Sprite(int id, int[] data = null)
{
return _gameChip.Sprite(id, data);
}
/// <summary>
/// Helper utility for flipping pixel data.
/// </summary>
/// <param name="data"></param>
/// <param name="flipH"></param>
/// <param name="flipV"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public int[] FlipPixelData(int[] data, bool flipH = false, bool flipV = false, int width = 8, int height = 8)
{
Utilities.FlipPixelData(ref data, width, height, flipH, flipV);
return data;
}
// public int[] Sprites(int[] ids, int width)
// {
// return gameChip.Sprites(ids, width);
// }
public int TotalSprites(bool ignoreEmpty = false)
{
return _gameChip.TotalSprites(ignoreEmpty);
}
public int Flag(int column, int row, byte? value = null)
{
return _gameChip.Flag(column, row, value);
}
public TileData Tile(int column, int row, int? spriteID = null, int? colorOffset = null,
byte? flag = null)
{
var tileData = _gameChip.Tile(column, row, spriteID, colorOffset, flag);
// RenderTile(tileData, column, row);
return tileData;
}
// public void RebuildTilemap()
// {
// gameChip.RebuildTilemap();
// }
public Point TilemapSize(int? width = null, int? height = null, bool clear = false)
{
return _gameChip.TilemapSize(width, height, clear);
}
public void UpdateTiles(int[] ids, int? colorOffset = null, byte? flag = null)
{
_gameChip.UpdateTiles(ids, colorOffset, flag);
}
/// <summary>
/// This allows you to fill a tilemap with a specific tile ID. It's used in the Tilemap editor. Define the position you
/// want to start at and any tile matching that ID touching will be replaced.
/// </summary>
/// <param name="value"></param>
/// <param name="column"></param>
/// <param name="row"></param>
/// <param name="mode"></param>
/// <param name="scaleX"></param>
/// <param name="scaleY"></param>
/// <param name="colorOffset"></param>
public void FloodFillTilemap(int value, int column, int row, int mode = 0, int scaleX = 1, int scaleY = 1,
int colorOffset = -1)
{
var canvas = NewCanvas(tilemapChip.columns, tilemapChip.rows);
var tiles = tilemapChip.tiles;
var total = canvas.Pixels.Length;
// Loop through all the tiles and build the pixels
for (var i = 0; i < total; i++) canvas.Pixels[i] = mode == 0 ? tiles[i].SpriteId : tiles[i].Flag;
if (mode == 0)
{
// Build the pattern
var size = scaleX * scaleY;
var pattern = new int[size];
var spriteCols = 16;
var spritePos = value == -1 ? new Point(-1, -1) : _gameChip.CalculatePosition(value, spriteCols);
for (var i = 0; i < size; i++)
{
var offset = _gameChip.CalculatePosition(i, scaleX);
// TODO this is not right
offset.X += spritePos.X;
offset.Y += spritePos.Y;
pattern[i] = value == -1
? value
: (_gameChip.CalculateIndex(offset.X, offset.Y, spriteCols) + 1) * -100;
}
canvas.SetPattern(pattern, scaleX, scaleY);
}
else if (mode == 1)
{
canvas.SetPattern(new[] { value }, 1, 1);
}
canvas.FloodFill(column, row);
// Copy the pixel data back to the tilemap
for (var i = 0; i < total; i++)
if (mode == 0)
{
var tile = tiles[i];
if (canvas.Pixels[i] < -1)
{
var pixel = canvas.Pixels[i] / -100 - 1;
tile.SpriteId = pixel;
if (colorOffset > -1) tile.ColorOffset = colorOffset;
}
}
else if (mode == 1)
{
tiles[i].Flag = unchecked((byte)canvas.Pixels[i]);
}
tilemapChip.Invalidate();
}
public int[] ConvertTextToSprites(string text, string fontName = "default")
{
throw new NotImplementedException();
}
public int[] ConvertCharacterToPixelData(char character, string fontName)
{
throw new NotImplementedException();
}
/// <summary>
/// Exports a song to a wav file.
/// </summary>
/// <param name="path"></param>
/// <param name="id"></param>
public void ExportSong(string path, int id)
{
workspace.ExportSong(path, musicChip, soundChip, id);
}
public void ExportPattern(WorkspacePath path, int id)
{
workspace.ExportPattern(path, musicChip, soundChip, id);
}
public bool RunBackgroundScript(string scriptName, string[] args = null)
{
try
{
// filePath = UniqueFilePath(filePath.AppendFile("pattern+" + id + ".wav"));
// TODO exporting sprites doesn't work
if (serviceManager.GetService(typeof(GameDataExportService).FullName) is GameDataExportService exportService)
{
exportService.Restart();
exportService.AddExporter(new BackgroundScriptRunner(scriptName, serviceManager.GetService(typeof(LuaService).FullName) as LuaService, args));
//
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>
/// <param name="flag"></param>
/// <returns></returns>
public bool UniqueSprites(bool? flag = null)
{
if (flag.HasValue) spriteChip.unique = flag.Value;
return spriteChip.unique;
}
public string[] Colors()
{
return colorChip.hexColors;
}
/// <summary>
/// Change the flag for importing tiles if they are not found in the Sprite Chip
/// </summary>
/// <param name="flag"></param>
/// <returns></returns>
public bool ImportTiles(bool? flag = null)
{
if (flag.HasValue) tilemapChip.autoImport = flag.Value;
return tilemapChip.autoImport;
}
/// <summary>
/// Toggle the flag for debug color allowing the mask color to be shown instead of defaulting to the background color.
/// </summary>
/// <param name="flag"></param>
/// <returns></returns>
public bool DebugColor(bool? flag = null)
{
if (flag.HasValue) colorChip.debugMode = flag.Value;
return colorChip.debugMode;
}
/// <summary>
/// Change the color mode of the game to edit specific color spaces in the Color Chip.
/// </summary>
/// <param name="mode"></param>
// public void ChangeColorMode(int mode = 0)
// {
// if (mode == 1)
// {
// // Create a new color map chip based on the exsiting color chip
// if (targetGame.GetChip(ColorMapParser.chipName, false) == null)
// {
// // Since we need pagincation and other values only on the color chip we'll create one here
// // Create new color map chip
// var colorMapChip = new ColorChip();
//
// // Add the chip to the engine
// targetGame.ActivateChip(ColorMapParser.chipName, colorMapChip, false);
//
// // Register the temporary color chip as a ColorMapChip
// // targetGame.chipManager.ActivateChip(typeof(ColorMapChip).FullName, colorMapChip, false);
//
// var colors = colorChip.hexColors;
//
// colorMapChip.total = colors.Length;
//
// for (var i = 0; i < colors.Length; i++) colorMapChip.UpdateColorAt(i, colors[i]);
//
// // Debug.Log("Create New Color Map Chip");
// }
//
// // Debug.Log("Color Map Chip Exists " + (targetGame.colorMapChip != null));
//
// // Since we are using a color chip we need to make sure we call the rigt chip because its not registered with the engine
// activeColorChip = targetGame.GetChip(ColorMapParser.chipName, false) as ColorChip;
// }
// // else if (mode == 2)
// // {
// // activeColorChip = targetGame.GetChip(FlagColorParser.FlagColorChipName, false) as ColorChip;
// // }
// else
// {
// activeColorChip = targetGame.ColorChip;
// }
// }
/// <summary>
/// This will go through the Sprite Chip memory and remove any duplicate sprites it finds.
/// </summary>
// public void OptimizeSprites()
// {
// // Console.WriteLine("Optimize sprites " + spriteChip.width + " " + spriteChip.height);
//
// var tmpSpriteChip = new SpriteChip { width = 8, height = 8 };
//
//
// tmpSpriteChip.Resize(spriteChip.textureWidth, spriteChip.textureHeight);
//
// // Loop through all the sprites and copy them to the new chip
// var total = spriteChip.TotalSprites;
//
// var tmpPixelData = new int[8 * 8];
// var nextSpriteID = 0;
//
// // Copy the sprites to the temp chip
// for (var i = 0; i < total; i++)
// {
// spriteChip.ReadSpriteAt(i, ref tmpPixelData);
//
// if (tmpSpriteChip.FindSprite(tmpPixelData) == -1)
// {
// tmpSpriteChip.UpdateSpriteAt(nextSpriteID, tmpPixelData);
//
// nextSpriteID++;
// }
// }
//
// spriteChip.Clear();
//
// total = tmpSpriteChip.SpritesInMemory;
//
// for (var i = 0; i < total; i++)
// {
// tmpSpriteChip.ReadSpriteAt(i, ref tmpPixelData);
// spriteChip.UpdateSpriteAt(i, tmpPixelData);
// }
//
// }
/// <summary>
/// Returns the TotalDisks number of sprites in memory.
/// </summary>
/// <returns></returns>
public int SpritesInRam()
{
return spriteChip.SpritesInMemory;
}