2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-07-03 17:51:54 +00:00

Import code from previous AssetBuilder version

This commit is contained in:
Jan
2019-09-24 10:45:09 +02:00
parent 5609557516
commit 0d8432d4f7
919 changed files with 154412 additions and 26 deletions

View File

@ -0,0 +1,67 @@
using System;
using System.IO;
using System.Linq;
using ZoneCodeGenerator.Interface;
using ZoneCodeGenerator.Parsing.CommandFile.Impl;
using ZoneCodeGenerator.Parsing.CommandFile.PostProcessor;
using ZoneCodeGenerator.Parsing.Impl;
using ZoneCodeGenerator.Persistence;
namespace ZoneCodeGenerator.Parsing.CommandFile
{
static class CommandFileReader
{
private static readonly IDataPostProcessor[] postProcessors =
{
new PostProcessorDefaultBlock(),
};
public static bool ReadFile(string path, CUISession session)
{
try
{
using (IIncludingParsingStream streamFileSystem = new IncludingStreamFileSystem(path))
{
var state = new CommandParserState(session.Repository);
using (IParsingFileStream preprocessorStream = new CommandFilePreprocessor(streamFileSystem))
{
var lexer = new Lexer(preprocessorStream);
var parser = new Parser<ICommandParserState>(state, lexer);
if (!parser.Parse())
return false;
preprocessorStream.Close();
streamFileSystem.Close();
if (!state.Apply(session))
{
Console.WriteLine("Finalizing from command file failed");
return false;
}
if (!PostProcessRepository(session.Repository))
{
Console.WriteLine("Postprocessing commands failed.");
return false;
}
}
return true;
}
}
catch (IOException)
{
}
return false;
}
private static bool PostProcessRepository(IDataRepository repository)
{
return postProcessors.All(postProcessor => postProcessor.PostProcess(repository));
}
}
}

View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
using ZoneCodeGenerator.Domain;
using ZoneCodeGenerator.Domain.FastFileStructure;
using ZoneCodeGenerator.Persistence;
namespace ZoneCodeGenerator.Parsing.CommandFile
{
interface ICommandParserState : IParserState<ICommandParserState>
{
string Game { get; set; }
IReadOnlyDataRepository Repository { get; }
List<FastFileBlock> FastFileBlocks { get; }
DataTypeWithMembers DataTypeInUse { get; set; }
}
}

View File

@ -0,0 +1,56 @@
using System.Text.RegularExpressions;
using ZoneCodeGenerator.Parsing.Impl;
namespace ZoneCodeGenerator.Parsing.CommandFile.Impl
{
class CommandFilePreprocessor : IParsingFileStream
{
private static readonly Regex includeRegex = new Regex(@"^\s*include\s*(?:\""(.*)\""|\<(.*)\>)\s*$");
private readonly IIncludingParsingStream parsingStream;
private readonly ICommentProcessor commentProcessor;
public CommandFilePreprocessor(IIncludingParsingStream parsingStream)
{
this.parsingStream = parsingStream;
commentProcessor = new CommentProcessor();
}
public void Dispose()
{
parsingStream.Close();
}
public bool EndOfStream => parsingStream.EndOfStream;
public string Filename => parsingStream.Filename;
public int Line => parsingStream.Line;
public string ReadLine()
{
return Preprocess(parsingStream.ReadLine());
}
public void Close()
{
parsingStream.Close();
}
private string Preprocess(string line)
{
line = commentProcessor.RemoveComments(line);
var includeMatch = includeRegex.Match(line);
if (includeMatch.Success)
{
var filename = includeMatch.Groups[1].Success ? includeMatch.Groups[1].Value : includeMatch.Groups[2].Value;
parsingStream.IncludeFile(filename);
return "";
}
return line;
}
}
}

View File

@ -0,0 +1,53 @@
using System.Collections.Generic;
using ZoneCodeGenerator.Domain;
using ZoneCodeGenerator.Domain.FastFileStructure;
using ZoneCodeGenerator.Interface;
using ZoneCodeGenerator.Parsing.CommandFile.Tests;
using ZoneCodeGenerator.Parsing.Testing;
using ZoneCodeGenerator.Persistence;
namespace ZoneCodeGenerator.Parsing.CommandFile.Impl
{
class CommandParserState : ICommandParserState
{
private static readonly ITokenTest<ICommandParserState>[] tests = {
new TestAsset(),
new TestBlock(),
new TestCondition(),
new TestCount(),
new TestGame(),
new TestReorder(),
new TestScriptString(),
new TestUse()
};
public string Game { get; set; }
public IReadOnlyDataRepository Repository { get; }
public List<FastFileBlock> FastFileBlocks { get; }
public DataTypeWithMembers DataTypeInUse { get; set; }
public CommandParserState(IReadOnlyDataRepository repository)
{
Repository = repository;
FastFileBlocks = new List<FastFileBlock>();
DataTypeInUse = null;
}
public IEnumerable<ITokenTest<ICommandParserState>> GetTests()
{
return tests;
}
public bool Apply(CUISession session)
{
session.Game = Game;
foreach (var block in FastFileBlocks)
{
session.Repository.Add(block);
}
return true;
}
}
}

View File

@ -0,0 +1,33 @@
using System.Linq;
using ZoneCodeGenerator.Domain;
using ZoneCodeGenerator.Domain.FastFileStructure;
using ZoneCodeGenerator.Persistence;
namespace ZoneCodeGenerator.Parsing.CommandFile.PostProcessor
{
class PostProcessorDefaultBlock : IDataPostProcessor
{
public bool PostProcess(IDataRepository repository)
{
var memberDataTypes =
repository.GetAllStructs()
.AsEnumerable<DataTypeWithMembers>()
.Concat(repository.GetAllUnions());
var defaultTemp = repository.GetAllFastFileBlocks().First(block => block.BlockType == FastFileBlock.Type.Temp && block.IsDefault) ??
repository.GetAllFastFileBlocks().First(block => block.BlockType == FastFileBlock.Type.Temp);
var defaultNormal = repository.GetAllFastFileBlocks().First(block => block.BlockType == FastFileBlock.Type.Normal && block.IsDefault) ??
repository.GetAllFastFileBlocks().First(block => block.BlockType == FastFileBlock.Type.Normal);
foreach (var memberType in memberDataTypes)
{
var info = repository.GetInformationFor(memberType);
info.Block = info.IsAsset ? defaultTemp : defaultNormal;
}
return true;
}
}
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZoneCodeGenerator.Domain;
using ZoneCodeGenerator.Parsing.Matching;
using ZoneCodeGenerator.Parsing.Matching.Matchers;
using ZoneCodeGenerator.Parsing.Testing;
namespace ZoneCodeGenerator.Parsing.CommandFile.Tests
{
class TestAsset : AbstractTokenTest<ICommandParserState>
{
private const string AssetTypeNameToken = "name";
private const string AssetEnumEntryToken = "enumEntry";
private static readonly TokenMatcher[] matchers = {
new MatcherLiteral("asset"),
new MatcherTypename().WithName(AssetTypeNameToken),
new MatcherName().WithName(AssetEnumEntryToken),
new MatcherLiteral(";")
};
public TestAsset() : base(matchers)
{
}
protected override void ProcessMatch(ICommandParserState state)
{
var assetTypeName = GetMatcherTokens(AssetTypeNameToken)[0];
var assetType = state.Repository.GetDataTypeByName(assetTypeName);
if (assetType == null)
{
throw new LoadingException($"Could not find type '{assetTypeName}' to mark it as an asset.");
}
if (!(assetType is DataTypeWithMembers assetTypeWithMembers))
{
throw new LoadingException($"Type of asset '{assetTypeName}' needs to be struct or union.");
}
var assetInfo = state.Repository.GetInformationFor(assetTypeWithMembers);
if (assetType == null)
{
throw new LoadingException($"Could not find information for type '{assetTypeName}' to mark it as an asset.");
}
var enumEntryName = GetMatcherTokens(AssetEnumEntryToken)[0];
var enumEntry = state.Repository.GetAllEnums()
.SelectMany(_enum => _enum.Members)
.FirstOrDefault(member => member.Name.Equals(enumEntryName, StringComparison.CurrentCultureIgnoreCase));
assetInfo.AssetEnumEntry = enumEntry ?? throw new LoadingException(
$"Could not find enum entry '{enumEntryName}' as an asset type index for asset type '{assetTypeName}'.");
}
}
}

View File

@ -0,0 +1,52 @@
using System;
using System.Linq;
using ZoneCodeGenerator.Domain.FastFileStructure;
using ZoneCodeGenerator.Parsing.Matching;
using ZoneCodeGenerator.Parsing.Matching.Matchers;
using ZoneCodeGenerator.Parsing.Testing;
namespace ZoneCodeGenerator.Parsing.CommandFile.Tests
{
class TestBlock : AbstractTokenTest<ICommandParserState>
{
private const string BlockNumberToken = "num";
private const string BlockTypeToken = "type";
private const string BlockNameToken = "name";
private const string DefaultToken = "default";
private static readonly TokenMatcher[] matchers = {
new MatcherLiteral("block"),
new MatcherNumber().WithName(BlockNumberToken),
new MatcherName().WithName(BlockTypeToken),
new MatcherName().WithName(BlockNameToken),
new MatcherGroupLoop(MatcherGroupLoop.LoopMode.ZeroOneMultiple,new MatcherGroupOr(
new MatcherLiteral("default").WithName(DefaultToken)
)),
new MatcherLiteral(";")
};
public TestBlock() : base(matchers)
{
}
protected override void ProcessMatch(ICommandParserState state)
{
var blockName = GetMatcherTokens(BlockNameToken)[0];
var blockNumber = int.Parse(GetMatcherTokens(BlockNumberToken)[0]);
var blockTypeInput = GetMatcherTokens(BlockTypeToken)[0];
if (!Enum.TryParse(blockTypeInput, true, out FastFileBlock.Type blockType))
{
var blockTypeValues = Enum.GetValues(typeof(FastFileBlock.Type)).OfType<FastFileBlock.Type>()
.Select(type => type.ToString());
throw new TestFailedException($"Unknown fastfile block type '{blockTypeInput}'. Must be one of the following: {string.Join(", ", blockTypeValues)}");
}
var block = new FastFileBlock(blockName, blockNumber, blockType, HasMatcherTokens(DefaultToken));
state.FastFileBlocks.Add(block);
}
}
}

View File

@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Text;
using ZoneCodeGenerator.Parsing.Matching;
using ZoneCodeGenerator.Parsing.Matching.Matchers;
using ZoneCodeGenerator.Parsing.Testing;
namespace ZoneCodeGenerator.Parsing.CommandFile.Tests
{
class TestCondition : AbstractTokenTest<ICommandParserState>
{
private const string TypeNameToken = "typeName";
private const string ConditionStatementTag = "conditionStatement";
private const string ConditionChainTag = "conditionChain";
private const string ConditionChainLinkTag = "conditionChainLink";
private const string OperationTag = "operation";
private const string OperandTag = "operand";
private static readonly TokenMatcher operand = new MatcherGroupOr(
new MatcherGroupAnd(
new MatcherTypename(),
new MatcherGroupLoop(MatcherGroupLoop.LoopMode.ZeroOneMultiple, new MatcherArray())
),
new MatcherNumber(),
new MatcherLiteral("true"),
new MatcherLiteral("false")
).WithTag(OperandTag);
private static readonly TokenMatcher operation = new MatcherGroupOr(
new MatcherLiteral("+"),
new MatcherLiteral("-"),
new MatcherLiteral("*"),
new MatcherLiteral("/"),
new MatcherGroupAnd(new MatcherLiteral("<"), new MatcherLiteral("<")),
new MatcherGroupAnd(new MatcherLiteral(">"), new MatcherLiteral(">"))
).WithTag(OperationTag);
private static readonly TokenMatcher conditionStatement = new MatcherGroupOr(
new MatcherGroupAnd(
new MatcherLiteral("("),
new MatcherWithTag(ConditionStatementTag),
new MatcherLiteral(")")
),
new MatcherGroupAnd(
new MatcherWithTag(OperandTag),
new MatcherGroupOptional(new MatcherGroupAnd(
new MatcherWithTag(OperationTag),
new MatcherWithTag(OperandTag)
))
)
).WithTag(ConditionStatementTag);
private static readonly TokenMatcher conditionChainLink = new MatcherGroupOr(
new MatcherGroupAnd(new MatcherLiteral("="), new MatcherLiteral("=")),
new MatcherGroupAnd(new MatcherLiteral("!"), new MatcherLiteral("=")),
new MatcherGroupAnd(new MatcherLiteral("<"), new MatcherLiteral("=")),
new MatcherGroupAnd(new MatcherLiteral(">"), new MatcherLiteral("=")),
new MatcherLiteral("<"),
new MatcherLiteral(">")
).WithTag(ConditionChainLinkTag);
private static readonly TokenMatcher conditionChain = new MatcherGroupAnd(
new MatcherWithTag(ConditionStatementTag),
new MatcherWithTag(ConditionChainLinkTag),
new MatcherWithTag(ConditionStatementTag)
).WithTag(ConditionChainTag);
private static readonly TokenMatcher[] matchers = {
new MatcherLiteral("set"),
new MatcherLiteral("condition"),
new MatcherTypename().WithName(TypeNameToken),
new MatcherGroupOr(
new MatcherLiteral("never"),
new MatcherWithTag(ConditionChainTag)
),
new MatcherLiteral(";")
};
public TestCondition() : base(matchers)
{
AddTaggedMatcher(conditionChain);
AddTaggedMatcher(conditionChainLink);
AddTaggedMatcher(conditionStatement);
AddTaggedMatcher(operation);
AddTaggedMatcher(operand);
}
protected override void ProcessMatch(ICommandParserState state)
{
}
}
}

View File

@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZoneCodeGenerator.Parsing.Matching;
using ZoneCodeGenerator.Parsing.Matching.Matchers;
using ZoneCodeGenerator.Parsing.Testing;
namespace ZoneCodeGenerator.Parsing.CommandFile.Tests
{
class TestCount : AbstractTokenTest<ICommandParserState>
{
private const string TypeNameToken = "typeName";
private const string ConditionStatementTag = "conditionStatement";
private const string ConditionChainTag = "conditionChain";
private const string ConditionChainLinkTag = "conditionChainLink";
private const string OperationTag = "operation";
private const string OperandTag = "operand";
private static readonly TokenMatcher operand = new MatcherGroupOr(
new MatcherGroupAnd(
new MatcherTypename(),
new MatcherGroupLoop(MatcherGroupLoop.LoopMode.ZeroOneMultiple, new MatcherArray())
),
new MatcherNumber()
).WithTag(OperandTag);
private static readonly TokenMatcher operation = new MatcherGroupOr(
new MatcherLiteral("+"),
new MatcherLiteral("-"),
new MatcherLiteral("*"),
new MatcherLiteral("/"),
new MatcherGroupAnd(new MatcherLiteral("<"), new MatcherLiteral("<")),
new MatcherGroupAnd(new MatcherLiteral(">"), new MatcherLiteral(">"))
).WithTag(OperationTag);
private static readonly TokenMatcher conditionStatement = new MatcherGroupOr(
new MatcherGroupAnd(
new MatcherLiteral("("),
new MatcherWithTag(ConditionStatementTag),
new MatcherLiteral(")")
),
new MatcherGroupAnd(
new MatcherWithTag(OperandTag),
new MatcherGroupOptional(new MatcherGroupAnd(
new MatcherWithTag(OperationTag),
new MatcherWithTag(OperandTag)
))
)
).WithTag(ConditionStatementTag);
private static readonly TokenMatcher conditionChainLink = new MatcherGroupOr(
new MatcherGroupAnd(new MatcherLiteral("|"), new MatcherLiteral("|")),
new MatcherGroupAnd(new MatcherLiteral("&"), new MatcherLiteral("&"))
).WithTag(ConditionChainLinkTag);
private static readonly TokenMatcher conditionChain = new MatcherGroupAnd(
new MatcherWithTag(ConditionStatementTag),
new MatcherGroupLoop(MatcherGroupLoop.LoopMode.ZeroOneMultiple, new MatcherGroupAnd(
new MatcherWithTag(ConditionChainLinkTag),
new MatcherWithTag(ConditionStatementTag)
))
).WithTag(ConditionChainTag);
private static readonly TokenMatcher[] matchers = {
new MatcherLiteral("set"),
new MatcherLiteral("count"),
new MatcherTypename().WithName(TypeNameToken),
new MatcherWithTag(ConditionChainTag),
new MatcherLiteral(";")
};
public TestCount() : base(matchers)
{
AddTaggedMatcher(conditionStatement);
AddTaggedMatcher(conditionChain);
AddTaggedMatcher(conditionChainLink);
AddTaggedMatcher(operation);
AddTaggedMatcher(operand);
}
protected override void ProcessMatch(ICommandParserState state)
{
}
}
}

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZoneCodeGenerator.Domain.FastFileStructure;
using ZoneCodeGenerator.Parsing.Matching;
using ZoneCodeGenerator.Parsing.Matching.Matchers;
using ZoneCodeGenerator.Parsing.Testing;
namespace ZoneCodeGenerator.Parsing.CommandFile.Tests
{
class TestGame : AbstractTokenTest<ICommandParserState>
{
private const string GameNameToken = "name";
private static readonly TokenMatcher[] matchers = {
new MatcherLiteral("game"),
new MatcherName().WithName(GameNameToken),
new MatcherLiteral(";")
};
public TestGame() : base(matchers)
{
}
protected override void ProcessMatch(ICommandParserState state)
{
if (!string.IsNullOrEmpty(state.Game))
{
throw new TestFailedException($"Game has already been set with value '{state.Game}'");
}
state.Game = GetMatcherTokens(GameNameToken)[0];
}
}
}

View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Text;
using ZoneCodeGenerator.Parsing.Matching;
using ZoneCodeGenerator.Parsing.Matching.Matchers;
using ZoneCodeGenerator.Parsing.Testing;
namespace ZoneCodeGenerator.Parsing.CommandFile.Tests
{
class TestReorder : AbstractTokenTest<ICommandParserState>
{
private const string TypeNameToken = "typeName";
private const string ReorderMemberNameToken = "member";
private static readonly TokenMatcher[] matchers = {
new MatcherLiteral("reorder"),
new MatcherGroupOptional(new MatcherTypename().WithName(TypeNameToken)),
new MatcherLiteral(":"),
new MatcherGroupLoop(MatcherGroupLoop.LoopMode.OneMultiple, new MatcherName().WithName(ReorderMemberNameToken)),
new MatcherLiteral(";")
};
public TestReorder() : base(matchers)
{
}
protected override void ProcessMatch(ICommandParserState state)
{
}
}
}

View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZoneCodeGenerator.Domain;
using ZoneCodeGenerator.Parsing.Matching;
using ZoneCodeGenerator.Parsing.Matching.Matchers;
using ZoneCodeGenerator.Parsing.Testing;
namespace ZoneCodeGenerator.Parsing.CommandFile.Tests
{
class TestScriptString : AbstractTokenTest<ICommandParserState>
{
private const string MemberTypeNameToken = "name";
private static readonly TokenMatcher[] matchers = {
new MatcherLiteral("set"),
new MatcherLiteral("scriptstring"),
new MatcherTypename().WithName(MemberTypeNameToken),
new MatcherLiteral(";")
};
public TestScriptString() : base(matchers)
{
}
protected override void ProcessMatch(ICommandParserState state)
{
}
}
}

View File

@ -0,0 +1,41 @@
using ZoneCodeGenerator.Domain;
using ZoneCodeGenerator.Parsing.Matching;
using ZoneCodeGenerator.Parsing.Matching.Matchers;
using ZoneCodeGenerator.Parsing.Testing;
namespace ZoneCodeGenerator.Parsing.CommandFile.Tests
{
class TestUse : AbstractTokenTest<ICommandParserState>
{
private const string TypeNameToken = "typeName";
private static readonly TokenMatcher[] matchers = {
new MatcherLiteral("use"),
new MatcherTypename().WithName(TypeNameToken),
new MatcherLiteral(";")
};
public TestUse() : base(matchers)
{
}
protected override void ProcessMatch(ICommandParserState state)
{
var typeName = GetMatcherTokens(TypeNameToken)[0];
var dataTypeToUse = state.Repository.GetDataTypeByName(typeName);
if (dataTypeToUse == null)
{
throw new LoadingException($"Could not find data type '{typeName}'");
}
if (!(dataTypeToUse is DataTypeWithMembers dataTypeWithMembersToUse))
{
throw new LoadingException($"To use data type '{typeName}' it must either be a struct or a union.");
}
state.DataTypeInUse = dataTypeWithMembersToUse;
}
}
}