Skip to content

Commit

Permalink
change: [独自ヘッダー]ファイル冒頭に特定ヘッダーが無ければ読込をスキップするように
Browse files Browse the repository at this point in the history
サンプルマップにも反映
  • Loading branch information
automatic9045 committed Jul 10, 2023
1 parent 2b4bca7 commit b9775a6
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 15 deletions.
23 changes: 16 additions & 7 deletions AtsEx/MapStatements/HeaderSet.Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace AtsEx.MapStatements
{
internal partial class HeaderSet
{
private readonly static Identifier UseAtsExHeader;
private readonly static string UseAtsExHeaderFullName;

private readonly static Identifier NoMapPluginHeader;
private readonly static string NoMapPluginHeaderFullName;

Expand All @@ -23,6 +26,9 @@ internal partial class HeaderSet

static HeaderSet()
{
UseAtsExHeader = new Identifier(Namespace.Root, "useatsex");
UseAtsExHeaderFullName = $"[[{UseAtsExHeader.FullName}]]";

NoMapPluginHeader = new Identifier(Namespace.Root, "nompi");
NoMapPluginHeaderFullName = $"[[{NoMapPluginHeader.FullName}]]";

Expand All @@ -41,29 +47,32 @@ private static (IDictionary<Identifier, IReadOnlyList<Header>> Headers, IReadOnl
ConcurrentDictionary<Identifier, IReadOnlyList<Header>> headers = new ConcurrentDictionary<Identifier, IReadOnlyList<Header>>();
List<Header> noMapPluginHeaders = new List<Header>();

string fileName = Path.GetFileName(filePath);

string text;
using (StreamReader sr = new StreamReader(filePath))
{
text = sr.ReadToEnd();
}

List<MapTextParser.TextWithPosition> statements = MapTextParser.GetStatementsFromText(text);
statements.ForEach(s =>
bool isFirstStatement = true;
IEnumerable<MapTextParser.TextWithPosition> statements = MapTextParser.GetStatementsFromText(text);
foreach (MapTextParser.TextWithPosition s in statements)
{
if (s.Text.StartsWith("include'") && s.Text.EndsWith("'") && s.Text.Length - s.Text.Replace("'", "").Length == 2)
{
string includePath = s.Text.Split('\'')[1];
int headerCloseBracketIndex = includePath.IndexOf(HeaderNameCloseBracket);

if (isFirstStatement && !includePath.StartsWith(UseAtsExHeaderFullName)) break;
isFirstStatement = false;

if (includePath.StartsWith(HeaderNameOpenBracket) && headerCloseBracketIndex != -1)
{
string headerFullName = includePath.Substring(HeaderNameOpenBracket.Length, headerCloseBracketIndex - HeaderNameOpenBracket.Length);
string headerArgument = includePath.Substring(headerCloseBracketIndex + HeaderNameCloseBracket.Length);

Identifier identifier = Identifier.Parse(headerFullName);
Header header = new Header(identifier, headerArgument, s.LineIndex, s.CharIndex);
if (header.Name.Namespace is null || !header.Name.Namespace.IsChildOf(Namespace.Root)) return;
if (header.Name.Namespace is null || !header.Name.Namespace.IsChildOf(Namespace.Root)) continue;

List<Header> list = headers.GetOrAdd(identifier, new List<Header>()) as List<Header>;
list.Add(header);
Expand All @@ -85,7 +94,7 @@ private static (IDictionary<Identifier, IReadOnlyList<Header>> Headers, IReadOnl
string includeRelativePath = includePath;
string includeAbsolutePath = Path.Combine(Path.GetDirectoryName(filePath), includeRelativePath);

if (!File.Exists(includeAbsolutePath)) return;
if (!File.Exists(includeAbsolutePath)) continue;

(IDictionary<Identifier, IReadOnlyList<Header>> headersInIncludedMap, IReadOnlyList<Header> noMapPluginHeadersInIncludedMap) = Load(includeAbsolutePath, readDepth - 1);

Expand All @@ -98,7 +107,7 @@ private static (IDictionary<Identifier, IReadOnlyList<Header>> Headers, IReadOnl
noMapPluginHeaders.AddRange(noMapPluginHeadersInIncludedMap);
}
}
});
}

return (headers, noMapPluginHeaders);
}
Expand Down
12 changes: 4 additions & 8 deletions AtsEx/MapStatements/MapTextParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ namespace AtsEx.MapStatements
{
internal static class MapTextParser
{
public static List<TextWithPosition> GetStatementsFromText(string text)
public static IEnumerable<TextWithPosition> GetStatementsFromText(string text)
{
string trimmedLine = text.ToLower();

List<TextWithPosition> statements = new List<TextWithPosition>();

{
bool isInString = false;

Expand Down Expand Up @@ -45,7 +43,7 @@ public static List<TextWithPosition> GetStatementsFromText(string text)
int nextLineBreakIndex = text.IndexOf('\n', i);
if (nextLineBreakIndex == -1)
{
return statements;
yield break;
}
else
{
Expand All @@ -55,7 +53,7 @@ public static List<TextWithPosition> GetStatementsFromText(string text)
break;

case '#':
return statements;
yield break;

case '\'':
isInString = !isInString;
Expand All @@ -81,7 +79,7 @@ public static List<TextWithPosition> GetStatementsFromText(string text)
string notTrimmedStatementText = text.Substring(notTrimmedLastStatementEndIndex + 1, n - notTrimmedLastStatementEndIndex);

int headSpaceCount = notTrimmedStatementText.Length - notTrimmedStatementText.TrimStart().Length;
statements.Add(new TextWithPosition(lineIndex, notTrimmedLastStatementEndIndex + headSpaceCount + 1 - notTrimmedLastLineBreakIndex, statementText));
yield return new TextWithPosition(lineIndex, notTrimmedLastStatementEndIndex + headSpaceCount + 1 - notTrimmedLastLineBreakIndex, statementText);
}

lastStatementEndIndex = i;
Expand All @@ -94,8 +92,6 @@ public static List<TextWithPosition> GetStatementsFromText(string text)
n++;
}
}

return statements;
}

internal class TextWithPosition
Expand Down
2 changes: 2 additions & 0 deletions _SampleScenarios/AtsEx.Samples/Maps/Basic/BaseMap.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
BveTs Map 2.02:utf-8

include '[[AtsEx::USEATSEX]]';

include '[[AtsEx::NOMPI]] このマップでは AtsEX マッププラグインを使用しています。正常動作には車両に AtsEX を搭載する必要があります。詳細は https://automatic9045.github.io をご覧ください。';
include '[[AtsEx::NOMPI]] This map requires an AtsEX-powered vehicle to execute map plugins. For more information, visit https://automatic9045.github.io';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BveTs Map 2.02:utf-8

include '[[AtsEx::USEATSEX]]';
include '[[AtsEx::READDEPTH]]1';

include '<AtsEx::MapPluginUsing>MapPluginUsing.xml';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BveTs Map 2.02:utf-8

include '[[AtsEx::USEATSEX]]';
include '[[AtsEx::READDEPTH]]1';

include '<AtsEx::MapPluginUsing>MapPluginUsing.xml';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BveTs Map 2.02:utf-8

include '[[AtsEx::USEATSEX]]';
include '[[AtsEx::READDEPTH]]1';

include '<AtsEx::MapPluginUsing>MapPluginUsing.xml';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BveTs Map 2.02:utf-8

include '[[AtsEx::USEATSEX]]';
include '[[AtsEx::READDEPTH]]1';

include '<AtsEx::MapPluginUsing>MapPluginUsing.xml';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BveTs Map 2.02:utf-8

include '[[AtsEx::USEATSEX]]';
include '[[AtsEx::READDEPTH]]1';

include '<AtsEx::MapPluginUsing>MapPluginUsing.xml';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BveTs Map 2.02:utf-8

include '[[AtsEx::USEATSEX]]';
include '[[AtsEx::READDEPTH]]1';

include '<AtsEx::MapPluginUsing>MapPluginUsing.xml';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BveTs Map 2.02:utf-8

include '[[AtsEx::USEATSEX]]';
include '[[AtsEx::READDEPTH]]1';

include '<AtsEx::MapPluginUsing>MapPluginUsing.xml';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BveTs Map 2.02:utf-8

include '[[AtsEx::USEATSEX]]';
include '[[AtsEx::READDEPTH]]1';

include '<AtsEx::MapPluginUsing>MapPluginUsing.xml';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BveTs Map 2.02:utf-8

include '[[AtsEx::USEATSEX]]';
include '[[AtsEx::READDEPTH]]1';

Structure.Load('..\..\Structures.csv');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BveTs Map 2.02:utf-8

include '[[AtsEx::USEATSEX]]';
include '[[AtsEx::READDEPTH]]1';

include '<AtsEx::MapPluginUsing>MapPluginUsing.xml';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BveTs Map 2.02:utf-8

include '[[AtsEx::USEATSEX]]';
include '[[AtsEx::READDEPTH]]1';

include '<AtsEx::MapPluginUsing>MapPluginUsing.xml'; // MapPluginUsing も独自ヘッダーの一種です。
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BveTs Map 2.02:utf-8

include '[[AtsEx::USEATSEX]]';
include '[[AtsEx::READDEPTH]]1';

include '<AtsEx::MapPluginUsing>MapPluginUsing.xml';
Expand Down

0 comments on commit b9775a6

Please sign in to comment.