Compare commits

..

No commits in common. "00e8bcc038f0690cb3c87aaeaddee1cbc6176f98" and "3848c7c446f9e533cb805bd259abe9ae55de7af8" have entirely different histories.

4 changed files with 132 additions and 320 deletions

View File

@ -8,8 +8,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="14.0.0" /> <PackageReference Include="Magick.NET-Q16-AnyCPU" Version="13.10.0" />
<PackageReference Include="Magick.NET.Core" Version="14.0.0" /> <PackageReference Include="Magick.NET.Core" Version="13.10.0" />
<PackageReference Include="NLua" Version="1.7.3" /> <PackageReference Include="NLua" Version="1.7.3" />
<PackageReference Include="System.Threading.Tasks" Version="4.3.0" /> <PackageReference Include="System.Threading.Tasks" Version="4.3.0" />
</ItemGroup> </ItemGroup>

View File

@ -1,124 +0,0 @@
using System.Reflection;
using System.Xml.Linq;
namespace ArtemisFgTools
{
public class FgHelper
{
public class FgObject(string path, string head, List<string> fuku, List<List<string>> pose, Dictionary<string, List<string>> face)
{
public string Path { get; set; } = path;
public string Head { get; set; } = head;
public List<string> Fuku { get; set; } = fuku;
public List<List<string>> Pose { get; set; } = pose;
public Dictionary<string, List<string>> Face { get; set; } = face;
}
//{"fg",ch="零",size="z1",mx=40,mode=3,resize=1,path=":fg/rei/z1/",file="rei_z1a0200",face="a0001",head="rei_z1a",lv=4,id=11},
public class FgRecord : IEquatable<FgRecord>
{
public string ChName { get; set; }
public string Size { get; set; }
public string File { get; set; }
public string Face { get; set; }
public override bool Equals(object? obj)
{
return Equals(obj as FgRecord);
}
public bool Equals(FgRecord? other)
{
return other != null &&
ChName == other.ChName &&
Size == other.Size &&
File == other.File &&
Face == other.Face;
}
public override int GetHashCode()
{
return HashCode.Combine(ChName, Size, File, Face);
}
}
public static HashSet<FgRecord> FetchFgObjectsFromScript(string path)
{
if (!Directory.Exists(path))
throw new DirectoryNotFoundException("The path does not exist.");
else
{
List<string> fgScriptLine = [];
foreach (string f in Directory.GetFiles(path, "*.ast"))
{
using StreamReader sr = new(f);
string? line;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("{\"fg\","))
fgScriptLine.Add(line);
}
}
if (fgScriptLine.Count == 0)
throw new Exception("No valid fg script line found.");
else
{
HashSet<FgRecord> fgRecords = [];
foreach (var line in fgScriptLine)
{
FgRecord? fgRecord = ParseScriptFGLine(line);
if (fgRecord != null)
fgRecords.Add(fgRecord);
}
if (fgRecords.Count == 0)
throw new Exception("No valid fg object found.");
else
return fgRecords;
}
}
}
public static FgRecord? ParseScriptFGLine(string input)
{
FgRecord fgRecord = new();
input = input.Trim('{', '}');
var pairs = input.Split(',');
foreach (var pair in pairs)
{
var keyValue = pair.Split(['=', '='], 2);
if (keyValue.Length == 2)
{
string key = keyValue[0].Trim();
string value = keyValue[1].Trim().Trim('"').Trim('}');
PropertyInfo? property = typeof(FgRecord).GetProperty(char.ToUpper(key[0]) + key[1..]);
if (property != null)
{
if (property.PropertyType == typeof(int))
{
if (int.TryParse(value, out int intValue))
property.SetValue(fgRecord, intValue);
else
throw new Exception($"Invalid integer value '{value}' for property '{key}'.");
}
else if (property.PropertyType == typeof(string))
{
property.SetValue(fgRecord, value);
}
// 其他类型的处理可以在这里添加
}
}
}
//补个chname
if (fgRecord.File != null)
fgRecord.ChName = GetCharacterEngName(fgRecord.File);
return fgRecord.Size == null ? null : fgRecord;
}
public static string GetCharacterEngName(string input)
{
int underscoreIndex = input.IndexOf('_');
if (underscoreIndex > 0)
return input[..underscoreIndex];
throw new Exception("Not supported character name format.");
}
}
}

View File

@ -1,70 +1,58 @@
using ImageMagick; using ImageMagick;
using NLua; using NLua;
using System.Reflection.Metadata.Ecma335;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using static ArtemisFgTools.FgHelper;
namespace ArtemisFgTools namespace ArtemisFgTools
{ {
internal class Program internal class Program
{ {
static void Main(string[] args) public class FgObject(string path, string head, List<string> fuku, List<List<string>> pose, Dictionary<string, List<string>> face)
{ {
if (args.Length == 1) public string Path { get; set; } = path;
{ public string Head { get; set; } = head;
if (args[0] == "-h") public List<string> Fuku { get; set; } = fuku;
Console.WriteLine("Usage: tools.exe -c <fgPath> (-s <scriptPath> | -t <luaTablePath>) -o <outputPath>"); public List<List<string>> Pose { get; set; } = pose;
else public Dictionary<string, List<string>> Face { get; set; } = face;
Console.WriteLine("Invalid arguments, Please check the usage via -h");
} }
else if (args.Length != 6) static void Main()
Console.WriteLine("Invalid arguments, Please check the usage via -h");
else if (args[0] != "-c" || !(args[2] == "-s" || args[2] == "-t") || args[4] != "-o")
Console.WriteLine("Invalid arguments, Please check the usage via -h");
else
{ {
if (!Directory.Exists(args[5])) Console.WriteLine("请输入立绘fg文件夹的所在路径无需\"\"");
Directory.CreateDirectory(args[5]); string? fgImagePath = Console.ReadLine();
if (!Directory.Exists(args[1]))
Console.WriteLine("Invalid fg path"); Console.WriteLine("请输入exlist的文件路径");
else if (args[2] == "-s") string? luaFilePath = Console.ReadLine();
Console.WriteLine("请输入保存位置:");
string? savePath = Console.ReadLine();
if (string.IsNullOrEmpty(fgImagePath) || string.IsNullOrEmpty(luaFilePath) || string.IsNullOrEmpty(savePath))
{ {
if (!Directory.Exists(args[3])) Console.WriteLine("路径不能为空");
Console.WriteLine("Invalid script path"); return;
else
PreProcess2(args[1], args[5], args[3]);
} }
else if (!Directory.Exists(fgImagePath) || !File.Exists(luaFilePath))
{ {
if (!File.Exists(args[3])) Console.WriteLine("路径不存在");
Console.WriteLine("Invalid lua table path"); return;
else
PreProcess(args[1], args[5], args[3]);
} }
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
} }
Console.WriteLine("Press any key to exit..."); Dictionary<object, object>? dictionary = ParseLuaTable(luaFilePath);
Console.ReadKey();
}
private static void PreProcess2(string fgImagePath, string savePath, string scriptPath) if (dictionary != null)
{ {
HashSet<FgRecord> fgRecords = FetchFgObjectsFromScript(scriptPath);
if (fgRecords.Count == 0)
throw new Exception("No valid fg object found.");
//重新写个我也懒得将FgRecord转FgObject了
//Tips:如果有单独饰品素材,可能前面的解析会有遗漏 //反正遥かなるニライカナイ里没有戴眼镜的角色 (笑
Process2(fgImagePath, savePath, fgRecords);
}
private static void PreProcess(string fgImagePath, string savePath, string luaFilePath)
{
Dictionary<object, object> dictionary = ParseLuaTable(luaFilePath) ?? throw new Exception("Lua table parsing failed");
if (dictionary["fg"] is Dictionary<object, object> fgDictionary) if (dictionary["fg"] is Dictionary<object, object> fgDictionary)
{ {
if (fgDictionary["size"] is not List<object> size || size.Count == 0) if (fgDictionary["size"] is not List<object> size || size.Count == 0)
{
throw new Exception("size not found or empty"); throw new Exception("size not found or empty");
}
fgDictionary.Remove("size"); fgDictionary.Remove("size");
//convert to FgObject
List<FgObject> fgObjects = []; List<FgObject> fgObjects = [];
foreach (var fg in fgDictionary) foreach (var fg in fgDictionary)
{ {
@ -75,53 +63,14 @@ namespace ArtemisFgTools
var face = ConvertToStringDictionary(fgValue["face"] as Dictionary<object, object>); var face = ConvertToStringDictionary(fgValue["face"] as Dictionary<object, object>);
//check null //check null
if (fgValue["path"] is not string path || fgValue["head"] is not string head || fuku == null || pose == null || face == null) if (fgValue["path"] is not string path || fgValue["head"] is not string head || fuku == null || pose == null || face == null)
throw new Exception("fg object has null value"); {
Console.WriteLine("fg object has null value");
continue;
}
path = path[4..]; // remove :fg/../ path = path[4..]; // remove :fg/../
fgObjects.Add(new FgObject(path, head, fuku, pose, face)); fgObjects.Add(new FgObject(path, head, fuku, pose, face));
} }
} }
Process(fgImagePath, savePath, size, fgObjects);
}
else
Console.WriteLine("fg not found");
}
private static void Process2(string fgImagePath, string savePath, HashSet<FgRecord> fgRecords)
{
var parallelOptions = new ParallelOptions
{
MaxDegreeOfParallelism = 6 // 设置最大并行度
};
Parallel.ForEach(fgRecords, parallelOptions, fgRecord =>
{
string originImageBase = Path.Combine(fgImagePath, fgRecord.ChName, fgRecord.Size, fgRecord.File + ".png");
string originImageFace = Path.Combine(fgImagePath, fgRecord.ChName, fgRecord.Size, fgRecord.Face + ".png");
string targetFilename = $"{fgRecord.File}_{fgRecord.Face}.png";
string targetPath = Path.Combine(savePath, fgRecord.Size);
if (!File.Exists(originImageBase) || !File.Exists(originImageFace))
{
Console.WriteLine("ERROR, Image not found. Details:");
Console.WriteLine($"Base: {originImageBase}");
Console.WriteLine($"Face: {originImageFace}");
return;
}
if (!Directory.Exists(targetPath))
Directory.CreateDirectory(targetPath);
targetPath = Path.Combine(targetPath, targetFilename);
if (File.Exists(targetPath))
{
Console.WriteLine("File already exists, skipping...");
return;
}
ProcessAndSaveLite(originImageBase, originImageFace, targetPath);
});
}
private static void Process(string fgImagePath, string savePath, List<object> size, List<FgObject> fgObjects)
{
foreach (var fgObject in fgObjects) foreach (var fgObject in fgObjects)
{ {
foreach (var siz in size) foreach (var siz in size)
@ -140,7 +89,7 @@ namespace ArtemisFgTools
return; return;
} }
bool special = false; bool special = false;
string special_text = ""; string special_text="";
string fuku_current = fuku; string fuku_current = fuku;
int index = fuku_current.IndexOf('|'); int index = fuku_current.IndexOf('|');
if (index != -1) if (index != -1)
@ -201,18 +150,11 @@ namespace ArtemisFgTools
} }
} }
} }
else
private static void ProcessAndSaveLite(string baseImg, string faceImg, string target)
{ {
using MagickImage firstImage = new(baseImg); Console.WriteLine("fg not found");
List<int> comment1 = ReadPngComment(firstImage.Comment); //base }
using MagickImage secondImage = new(faceImg); }
List<int> comment2 = ReadPngComment(secondImage.Comment); //face
int x = comment2[0] - comment1[0]; // face x - base x
int y = comment2[1] - comment1[1]; // face y - base y
firstImage.Composite(secondImage, x, y, CompositeOperator.Over);
firstImage.Write(target);
Console.WriteLine($"Image {Path.GetFileName(target)} processing completed.");
} }
private static void ProcessAndSave(string baseImg, string layerImg, string layer2Img, string target, bool special) private static void ProcessAndSave(string baseImg, string layerImg, string layer2Img, string target, bool special)
@ -264,7 +206,9 @@ namespace ArtemisFgTools
LuaTable luaTable = lua.GetTable("exfgtable"); LuaTable luaTable = lua.GetTable("exfgtable");
if (luaTable != null) if (luaTable != null)
{
return (Dictionary<object, object>?)LuaTableToSs(luaTable); return (Dictionary<object, object>?)LuaTableToSs(luaTable);
}
else else
{ {
Console.WriteLine("Lua table not found"); Console.WriteLine("Lua table not found");

View File

@ -1,8 +0,0 @@
{
"profiles": {
"ArtemisFgTools": {
"commandName": "Project",
"commandLineArgs": "-c\r\nG:\\x221.local\\1\\lab\\fg\r\n-s\r\nG:\\x221.local\\1\\lab\\script\r\n-o\r\nG:\\x221.local\\1\\lab\\output"
}
}
}