diff --git a/ArtemisFgTools/ArtemisFgTools.csproj b/ArtemisFgTools/ArtemisFgTools.csproj index 2150e37..50affc2 100644 --- a/ArtemisFgTools/ArtemisFgTools.csproj +++ b/ArtemisFgTools/ArtemisFgTools.csproj @@ -2,9 +2,15 @@ Exe - net8.0 + net8.0-windows10.0.19041.0 enable enable + + + + + + diff --git a/ArtemisFgTools/Program.cs b/ArtemisFgTools/Program.cs index 7d326ae..9d0eecc 100644 --- a/ArtemisFgTools/Program.cs +++ b/ArtemisFgTools/Program.cs @@ -1,10 +1,191 @@ -namespace ArtemisFgTools +using ImageMagick; +using NLua; +namespace ArtemisFgTools { internal class Program { - static void Main(string[] args) + public class FgObject(string path, string head, List fuku, List> pose, Dictionary> face) { - Console.WriteLine("Hello, World!"); + public string Path { get; set; } = path; + public string Head { get; set; } = head; + public List Fuku { get; set; } = fuku; + public List> Pose { get; set; } = pose; + public Dictionary> Face { get; set; } = face; } + static void Main() + { + string luaFilePath = "G:/x221.local/pc/ja/extra/exlist.ipt"; + Dictionary? dictionary = ParseLuaTable(luaFilePath); + + if (dictionary != null) + { + if (dictionary["fg"] is Dictionary fgDictionary) + { + var size = fgDictionary["size"] as List; + fgDictionary.Remove("size"); + + //convert to FgObject + List fgObjects = []; + foreach (var fg in fgDictionary) + { + if (fg.Value is Dictionary fgValue) + { + var fuku = ConvertToStringList(fgValue["fuku"] as List); + var pose = ConvertToNestedStringList(fgValue["pose"] as List); + var face = ConvertToStringDictionary(fgValue["face"] as Dictionary); + //check null + if (fgValue["path"] is not string path || fgValue["head"] is not string head || fuku == null || pose == null || face == null) + { + Console.WriteLine("fg object has null value"); + continue; + } + fgObjects.Add(new FgObject(path, head, fuku, pose, face)); + } + } + Console.WriteLine($"fg count: {fgObjects.Count}"); + //Todo: Combine fgObjects with image position + //Todo: Get image position from png comment + String comment = ReadPngComment("G:/x221.local/fg/baa/fa/a0001.png"); + + } + else + { + Console.WriteLine("fg not found"); + } + } + } + + static Dictionary? ParseLuaTable(string luaFilePath) + { + Lua lua = new(); + try + { + lua.DoFile(luaFilePath); + } + catch (Exception ex) + { + Console.WriteLine($"Error loading Lua file: {ex.Message}"); + return null; + } + + LuaTable luaTable = lua.GetTable("exfgtable"); + if (luaTable != null) + { + return (Dictionary?)LuaTableToSs(luaTable); + } + else + { + Console.WriteLine("Lua table not found"); + return null; + } + } + + static object LuaTableToSs(LuaTable luaTable) + { + if (NeedConvertList(luaTable)) + { + List list = []; + foreach (var key in luaTable.Keys) + { + object value = luaTable[key]; + if (value is LuaTable nestedTable) + { + list.Add(LuaTableToSs(nestedTable)); + } + else + { + list.Add(value); + } + } + return list; + } + else + { + Dictionary dictionary = []; + foreach (var key in luaTable.Keys) + { + object value = luaTable[key]; + if (value is LuaTable nestedTable) + { + dictionary[key] = LuaTableToSs(nestedTable); + } + else + { + dictionary[key] = value; + } + } + return dictionary; + } + } + + private static bool NeedConvertList(LuaTable luaTable) + { + long index = 1; + foreach (var key in luaTable.Keys) + { + if (key is string) + { + return false; + } + else + { + long n = (long)key; + if (n != index) + { + return false; + } + index++; + } + + } + return true; + } + + private static List? ConvertToStringList(List? list) + { + return list?.ConvertAll(item => item?.ToString() ?? string.Empty); + } + + private static List> ConvertToNestedStringList(List? list) + { + if (list == null) return []; + return list.ConvertAll(item => ConvertToStringList(item as List) ?? []); + } + + private static Dictionary>? ConvertToStringDictionary(Dictionary? dictionary) + { + if (dictionary == null) return null; + + Dictionary> result = []; + foreach (var kvp in dictionary) + { + result[kvp.Key?.ToString() ?? string.Empty] = ConvertToStringList(kvp.Value as List) ?? []; + } + return result; + } + + public static string ReadPngComment(string filePath) + { + if (File.Exists(filePath)) + { + // Read image from file + using var image = new MagickImage(filePath); + + if(image.Comment != null) + { + return image.Comment; + } + else + { + throw new Exception("Comment not found"); + } + } + else + { + Console.WriteLine("File does not exist."); + } + } + + } }