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

Commit

Permalink
Cleaning up parser logic and how loader/loader service work together.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessefreeman committed Jan 28, 2021
1 parent 2a93e96 commit a7b1a63
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 111 deletions.
5 changes: 5 additions & 0 deletions PixelVision8Lite.CoreDesktop.csproj
Expand Up @@ -35,6 +35,8 @@
<EmbeddedResource Remove="SDK/Lua/**" />
<Content Remove="Content/Effects/**" />
<EmbeddedResource Remove="Disks/APIExamples/AddScript/**" />
<Content Remove="Content\Fonts\**" />
<EmbeddedResource Remove="Content\Fonts\**" />
</ItemGroup>

<!-- Point this variable to the API Example you want to run. -->
Expand Down Expand Up @@ -164,6 +166,9 @@
<ItemGroup>
<None Remove="Disks/APIExamples/AddScript/**" />
<None Remove="SDK/Runner/Utils/GifUtils/**" />
<None Remove="Content\Effects\**" />
<None Remove="Content\Fonts\**" />
<None Remove="Content\PixelVisionOS\**" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion SDK/Editor/Editors/GameEditor.cs
Expand Up @@ -1149,7 +1149,7 @@ public bool LoadFont(string path)

loadService.targetEngine = targetGame;

loadService.AddParser(new FontParser(pngReader, targetGame.ColorChip, targetGame.FontChip));
loadService.AddParser(new FontParser("", pngReader, targetGame.ColorChip, targetGame.FontChip));

loadService.LoadAll();

Expand Down
2 changes: 1 addition & 1 deletion SDK/Editor/Exporters/SpriteBuilderExporter.cs
Expand Up @@ -37,7 +37,7 @@ internal class SpriteDataParser : SpriteImageParser
public int[] ids;
public int totalSpritesInTexture;

public SpriteDataParser(IImageParser parser, ColorChip colorChip, SpriteChip spriteChip) : base(parser,
public SpriteDataParser(IImageParser parser, ColorChip colorChip, SpriteChip spriteChip) : base("", parser,
colorChip, spriteChip)
{
}
Expand Down
2 changes: 1 addition & 1 deletion SDK/Lua/Services/LuaService.cs
Expand Up @@ -283,7 +283,7 @@ public ImageData ReadImage(WorkspacePath src, string maskHex = "#ff00ff", string



var imageParser = new SpriteImageParser(reader, tmpColorChip);
var imageParser = new SpriteImageParser("", reader, tmpColorChip);

// Manually call each step
imageParser.ParseImageData();
Expand Down
2 changes: 1 addition & 1 deletion SDK/Runner/CSharpRunner.cs
Expand Up @@ -121,7 +121,7 @@ where fileExtensions.Any(val => p.EndsWith(val))
var fileHelper = new FileLoadHelper();
var imageParser = new PNGParser(_graphics.GraphicsDevice);

var loader = new Loader(fileHelper, imageParser, _graphics.GraphicsDevice);
var loader = new Loader(fileHelper, imageParser);

// Process the files
loader.ParseFiles(gameFiles.ToArray(), _tmpEngine);
Expand Down
2 changes: 1 addition & 1 deletion SDK/Runner/Parsers/AbstractParser.cs
Expand Up @@ -31,7 +31,7 @@ public abstract class AbstractParser : IAbstractParser

public int CurrentStep { get; protected set; }

public string SourcePath;
protected string SourcePath;

public virtual byte[] bytes { get; set; }

Expand Down
41 changes: 14 additions & 27 deletions SDK/Runner/Parsers/ColorParser.cs
Expand Up @@ -36,8 +36,9 @@ public class ColorParser : ImageParser
protected Color tmpColor;
protected int totalColors;

public ColorParser(IImageParser parser, ColorChip colorChip) : base(parser)
public ColorParser(string sourceFile, IImageParser parser, ColorChip colorChip) : base(parser)
{
SourcePath = sourceFile;
this.colorChip = colorChip;
// unique = colorChip.unique;
magenta = Utilities.HexToColor(colorChip.maskColor);
Expand All @@ -57,20 +58,7 @@ public override void CalculateSteps()

public virtual void ReadColors()
{
// TODO this should be removed in future releases, it's only here to support legacy games

// If we are loading a legacy game and no system colors are defined, used the image parser's palette

// if (colorChip.supportedColors == null)
// {
// string[] systemColors = imageParser.colorPalette.Select(c => ((ColorData) c).ToHex()).ToArray();
////
// for (int i = 0; i < systemColors.Length; i++)
// {
// colorChip.AddSupportedColor(systemColors[i]);
// }
// }


// Parse colors as normal

var srcColors = Parser.colorPixels;
Expand All @@ -85,24 +73,14 @@ public virtual void ReadColors()
if (tmpColor.A < 1) // && !ignoreTransparent)
tmpColor = magenta;


// if(tmpColor != magenta)
colors.Add(tmpColor);
colors.Add(tmpColor);
}

totalColors = colors.Count;

CurrentStep++;
}
//
// public void ResetColorChip()
// {
// // Clear the colors first
//// colorChip.Clear();
//
// currentStep++;
// }


public void UpdateColors()
{
for (var i = 0; i < totalColors; i++)
Expand All @@ -116,4 +94,13 @@ public void UpdateColors()
CurrentStep++;
}
}

public partial class Loader
{
[FileParser("colors.png")]
public void ParseColors(string file, IPlayerChips engine)
{
AddParser(new ColorParser(file, _imageParser, engine.ColorChip));
}
}
}
19 changes: 4 additions & 15 deletions SDK/Runner/Parsers/FontParser.cs
Expand Up @@ -47,8 +47,7 @@ public class FontParser : SpriteImageParser
private List<string> uniqueFontColors;
private int[] fontMap;

public FontParser(IImageParser parser, ColorChip colorChip, FontChip fontChip) : base(parser,
colorChip, fontChip)
public FontParser(string sourceFile, IImageParser parser, ColorChip colorChip, FontChip fontChip) : base(sourceFile, parser, colorChip, fontChip)
{
this.fontChip = fontChip;
}
Expand Down Expand Up @@ -109,20 +108,10 @@ protected override void ProcessSpriteData()

public partial class Loader
{
[FileParser("font.png", "Fonts")]
public void ParseFonts(string[] files, IPlayerChips engine)
[FileParser("font.png")]
public void ParseFonts(string file, IPlayerChips engine)
{
for (int i = 0; i < files.Length; i++)
{
// We only want to parse a single sprite file so just take the first one in the list
// var imageParser = new PNGParser(files[i], _graphicsDevice, engine.ColorChip.maskColor);

AddParser(new FontParser(_imageParser, engine.ColorChip, engine.FontChip)
{
SourcePath = files[i]
});
}

AddParser(new FontParser(file, _imageParser, engine.ColorChip, engine.FontChip));
}
}
}
16 changes: 8 additions & 8 deletions SDK/Runner/Parsers/Loader.cs
Expand Up @@ -37,13 +37,11 @@ public partial class Loader
public class FileParser : Attribute
{
public string FileType;
public string FlagType;
public MethodInfo MethodInfo;

public FileParser(string fileType, string flagType)
public FileParser(string fileType)
{
FileType = fileType;
FlagType = flagType;
}

}
Expand All @@ -60,16 +58,13 @@ public FileParser(string fileType, string flagType)
public int TotalSteps;
private readonly IFileLoader _fileLoadHelper;

private GraphicsDevice _graphicsDevice;

public List<FileParser> ParserMapping = new List<FileParser>();

private IImageParser _imageParser;

public Loader(IFileLoader fileLoadHelper, IImageParser imageParser, GraphicsDevice graphicsDevice = null)
public Loader(IFileLoader fileLoadHelper, IImageParser imageParser)
{
_fileLoadHelper = fileLoadHelper;
_graphicsDevice = graphicsDevice;
_imageParser = imageParser;

var methods = GetType().GetMethods().Where(m=>m.GetCustomAttributes(typeof(FileParser), false).Length > 0).ToArray();
Expand Down Expand Up @@ -152,7 +147,12 @@ public virtual void ParseFiles(string[] files, IPlayerChips engine)

if (filesToParse.Length > 0)
{
parserInfo.MethodInfo.Invoke(this, new object[]{filesToParse, engine});

for (int j = 0; j < filesToParse.Length; j++)
{
parserInfo.MethodInfo.Invoke(this, new object[]{filesToParse[j], engine});
}

}

}
Expand Down
9 changes: 9 additions & 0 deletions SDK/Runner/Parsers/MetaDataParser.cs
Expand Up @@ -46,4 +46,13 @@ public void ApplySettings()
}

}

public partial class Loader
{
[FileParser("info.json")]
public void ParseMetaData(string file, IPlayerChips engine)
{
AddParser(new MetaDataParser(file, _fileLoadHelper, engine));
}
}
}
15 changes: 6 additions & 9 deletions SDK/Runner/Parsers/SpriteImageParser.cs
Expand Up @@ -40,9 +40,11 @@ public class SpriteImageParser : ImageParser
protected int x, y;
public ImageData ImageData;

public SpriteImageParser(IImageParser parser, ColorChip colorChip, SpriteChip spriteChip = null) : base(parser)
public SpriteImageParser(string sourceFile, IImageParser parser, ColorChip colorChip, SpriteChip spriteChip = null) : base(parser)
{

SourcePath = sourceFile;

Parser = parser;

// this.chips = chips;
Expand Down Expand Up @@ -249,15 +251,10 @@ public override void Dispose()

public partial class Loader
{
[FileParser("sprite.png", "Sprites")]
public void ParseSprites(string[] files, IPlayerChips engine)
[FileParser("sprite.png")]
public void ParseSprites(string file, IPlayerChips engine)
{

AddParser(new SpriteImageParser(_imageParser, engine.ColorChip, engine.SpriteChip)
{
SourcePath = files[0]
});

AddParser(new SpriteImageParser(file, _imageParser, engine.ColorChip, engine.SpriteChip));
}
}
}
19 changes: 19 additions & 0 deletions SDK/Runner/Parsers/SystemParser.cs
Expand Up @@ -678,4 +678,23 @@ public void ConfigureMetaSprites(Dictionary<string, object> data)
}
}
}

public partial class Loader
{
[FileParser("data.png")]
public void ParseSystem(string file, IPlayerChips engine)
{
// if (!string.IsNullOrEmpty(files[0]))
// {
// var fileContents = Encoding.UTF8.GetString(ReadAllBytes(file));

var jsonParser = new SystemParser(file, _fileLoadHelper, engine);

jsonParser.CalculateSteps();

while (jsonParser.completed == false) jsonParser.NextStep();
// }

}
}
}
13 changes: 11 additions & 2 deletions SDK/Runner/Parsers/TilemapJsonParser.cs
Expand Up @@ -294,7 +294,16 @@ public virtual void ConfigureTilemapV1()
StepCompleted();
}



}

public partial class Loader
{
[FileParser("tilemap.json")]
public void ParseTilemapJson(string file, IPlayerChips engine)
{

AddParser(new TilemapJsonParser(file, _fileLoadHelper, engine));

}
}
}
18 changes: 5 additions & 13 deletions SDK/Runner/Parsers/TilemapParser.cs
Expand Up @@ -29,8 +29,8 @@ public class TilemapParser : SpriteImageParser
private readonly TilemapChip tilemapChip;
private bool autoResize;

public TilemapParser(IImageParser parser, ColorChip colorChip, SpriteChip spriteChip, TilemapChip tilemapChip, bool autoResize = false) :
base(parser, colorChip, spriteChip)
public TilemapParser(string sourceFile, IImageParser parser, ColorChip colorChip, SpriteChip spriteChip, TilemapChip tilemapChip, bool autoResize = false) :
base(sourceFile, parser, colorChip, spriteChip)
{
this.tilemapChip = tilemapChip;

Expand Down Expand Up @@ -101,18 +101,10 @@ protected override void ProcessSpriteData()

public partial class Loader
{
[FileParser("tilemap.png", "Tilemap")]
public void ParseTilemapImage(string[] files, IPlayerChips engine)
[FileParser("tilemap.png")]
public void ParseTilemapImage(string file, IPlayerChips engine)
{

// We only want to parse a single sprite file so just take the first one in the list
// var imageParser = new PNGParser(files[0], _graphicsDevice, engine.ColorChip.maskColor);

AddParser(new TilemapParser(_imageParser, engine.ColorChip, engine.SpriteChip, engine.TilemapChip, true)
{
SourcePath = files[0]
});

AddParser(new TilemapParser(file, _imageParser, engine.ColorChip, engine.SpriteChip, engine.TilemapChip, true));
}
}
}
6 changes: 3 additions & 3 deletions SDK/Runner/Parsers/WavParser.cs
Expand Up @@ -61,10 +61,10 @@ public void ConfigureSamples()

public partial class Loader
{
[FileParser(".wav", "Music")]
public void ParseWave(string[] files, IPlayerChips engine)
[FileParser(".wav")]
public void ParseWave(string[] file, IPlayerChips engine)
{
AddParser(new WavParser(files, _fileLoadHelper, engine ));
AddParser(new WavParser(file, _fileLoadHelper, engine ));
}
}

Expand Down

0 comments on commit a7b1a63

Please sign in to comment.