Skip to content

Commit

Permalink
Merge pull request #479 from 7474/fix-linter-sp
Browse files Browse the repository at this point in the history
データの種類のチェックに優先順をつけた
  • Loading branch information
7474 committed Jul 1, 2022
2 parents c772512 + 4341731 commit 7ecb77b
Showing 1 changed file with 83 additions and 50 deletions.
133 changes: 83 additions & 50 deletions SRC.Sharp/SRCDataLinter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
using SRCCore.Filesystem;
using SRCCore.TestLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SRCDataLinter
Expand Down Expand Up @@ -37,10 +39,16 @@ static async Task Main(string[] args)
.SelectMany(x => Directory.EnumerateFiles(x, "*.eve", SearchOption.AllDirectories)))
.Concat(args.Where(x => File.Exists(x)));

foreach (var file in files)
var procedFiles = new HashSet<string>();
foreach (var lintProc in LintProcs)
{
var fileInfo = new FileInfo(file);
hasError |= await ValidateFileAsync(hasError, SRC, fileInfo);
foreach (var file in files.Where(x => !procedFiles.Contains(x)))
{
var fileInfo = new FileInfo(file);
var res = await lintProc.Execute(SRC, fileInfo);
hasError |= res.HasError;
if (res.Processed) { procedFiles.Add(file); }
}
}

sw.Stop();
Expand Down Expand Up @@ -68,76 +76,101 @@ private static async Task<Stream> OpenUtf8Async(SRCCore.SRC SRC, FileInfo file)
}
}

private static async Task<bool> ValidateFileAsync(bool hasError, SRCCore.SRC SRC, FileInfo file)
static LintProcDef[] LintProcs = new LintProcDef[] {
new LintProcDef("^sp.txt$|^mind.txt$", async (SRC, file) => {
SRC.SPDList.Load(file.Name, await OpenUtf8Async(SRC, file));
}),
new LintProcDef("^alias.txt$", async (SRC, file) => {
SRC.ALDList.Load(file.Name, await OpenUtf8Async(SRC, file));
}),
new LintProcDef("^unit.txt$|^robot.txt$", async (SRC, file) => {
SRC.UDList.Load(file.Name, await OpenUtf8Async(SRC, file));
}),
new LintProcDef("^pilot.txt$", async (SRC, file) => {
SRC.PDList.Load(file.Name, await OpenUtf8Async(SRC, file));
}),
new LintProcDef("^non_pilot.txt$", async (SRC, file) => {
SRC.NPDList.Load(file.Name, await OpenUtf8Async(SRC, file));
}),
new LintProcDef("^pilot_message.txt$", async (SRC, file) => {
SRC.MDList.Load(file.Name, false, await OpenUtf8Async(SRC, file));
}),
new LintProcDef("^pilot_dialog.txt$", async (SRC, file) => {
SRC.DDList.Load(file.Name, await OpenUtf8Async(SRC, file));
}),
new LintProcDef("^effect.txt$", async (SRC, file) => {
SRC.EDList.Load(file.Name, true, await OpenUtf8Async(SRC, file));
}),
new LintProcDef("^animation.txt$", async (SRC, file) => {
SRC.ADList.Load(file.Name, false, await OpenUtf8Async(SRC, file));
}),
new LintProcDef("^item.txt$", async (SRC, file) => {
SRC.IDList.Load(file.Name, await OpenUtf8Async(SRC, file));
}),
new LintProcDef(".*", (SRC, file) => {
if (file.Name.ToLower().EndsWith(".eve"))
{
SRC.Event.LoadEventData(file.FullName, "");
}
else
{
Console.WriteLine($"Not supported [{file.FullName}]");
}
return Task.CompletedTask;
}),
};
}

class ProcResult
{
public bool HasError { get; set; }
public bool Processed { get; set; }
}

// delegate ProcResult LintProc(SRCCore.SRC SRC, FileInfo file);
delegate Task LintProc(SRCCore.SRC SRC, FileInfo file);
class LintProcDef
{
Regex FilePattern;
LintProc LintProc;
public LintProcDef(string filePattern, LintProc lintProc)
{
FilePattern = new Regex(filePattern, RegexOptions.IgnoreCase);
LintProc = lintProc;
}

public async Task<ProcResult> Execute(SRCCore.SRC SRC, FileInfo file)
{
var result = new ProcResult();
if (!FilePattern.IsMatch(file.Name)) { return result; }
result.Processed = true;
try
{
switch (file.Name.ToLower())
{
case "unit.txt":
case "robot.txt":
SRC.UDList.Load(file.Name, await OpenUtf8Async(SRC, file));
break;
case "pilot.txt":
SRC.PDList.Load(file.Name, await OpenUtf8Async(SRC, file));
break;
case "non_pilot.txt":
SRC.NPDList.Load(file.Name, await OpenUtf8Async(SRC, file));
break;
case "pilot_message.txt":
SRC.MDList.Load(file.Name, false, await OpenUtf8Async(SRC, file));
break;
case "pilot_dialog.txt":
SRC.DDList.Load(file.Name, await OpenUtf8Async(SRC, file));
break;
case "effect.txt":
SRC.EDList.Load(file.Name, true, await OpenUtf8Async(SRC, file));
break;
case "animation.txt":
SRC.ADList.Load(file.Name, false, await OpenUtf8Async(SRC, file));
break;
case "item.txt":
SRC.IDList.Load(file.Name, await OpenUtf8Async(SRC, file));
break;
case "alias.txt":
SRC.ALDList.Load(file.Name, await OpenUtf8Async(SRC, file));
break;
default:
if (file.Name.ToLower().EndsWith(".eve"))
{
SRC.Event.LoadEventData(file.FullName, "");
}
else
{
Console.WriteLine($"Not supported [{file.FullName}]");
return false;
}
break;
}
await LintProc(SRC, file);
}
catch (InvalidSrcDataException ex)
{
foreach (var id in ex.InvalidDataList)
{
Console.Error.WriteLine($"{file.FullName}({id.line_num}): error: {id.msg}[{id.dname}]");
}
hasError = true;
result.HasError = true;
}
catch (Exception ex)
{
Console.Error.WriteLine($"{file.FullName}({1}): error: {ex.Message}");
Console.Error.WriteLine(ex.StackTrace);
hasError = true;
result.HasError = true;
}
foreach (var id in SRC.DataErrors)
{
Console.Error.WriteLine($"{file.FullName}({id.line_num}): warning: {id.msg}[{id.dname}]");
}
hasError |= SRC.HasDataError;
result.HasError |= SRC.HasDataError;
SRC.ClearDataError();
Console.WriteLine($"Checked [{file.FullName}]");

return hasError;
return result;
}
}
}

0 comments on commit 7ecb77b

Please sign in to comment.