From ecc2c97857224c247d6ff6903191c62e25013f6a Mon Sep 17 00:00:00 2001 From: Atvaark Date: Wed, 14 Oct 2015 22:26:58 +0200 Subject: [PATCH] Added an option to specify the max ftexs file count --- FtexTool/FtexDdsConverter.cs | 48 +++++++++++++++++------------ FtexTool/FtexToolArguments.cs | 14 +++++++++ FtexTool/Program.cs | 23 +++++++++++--- FtexTool/Properties/AssemblyInfo.cs | 4 +-- README.md | 6 ++++ 5 files changed, 68 insertions(+), 27 deletions(-) diff --git a/FtexTool/FtexDdsConverter.cs b/FtexTool/FtexDdsConverter.cs index 8dd30da..b20434c 100644 --- a/FtexTool/FtexDdsConverter.cs +++ b/FtexTool/FtexDdsConverter.cs @@ -39,9 +39,6 @@ public static DdsFile ConvertToDds(FtexFile file) case 2: result.Header.PixelFormat = DdsPixelFormat.DdsPfDxt1(); break; - case 3: - result.Header.PixelFormat = DdsPixelFormat.DdsPfDxt3(); // HACK: This is just a guess. The value isn't used in the Ground Zeroes files. - break; case 4: result.Header.PixelFormat = DdsPixelFormat.DdsPfDxt5(); break; @@ -53,7 +50,7 @@ public static DdsFile ConvertToDds(FtexFile file) return result; } - public static FtexFile ConvertToFtex(DdsFile file, FtexTextureType textureType) + public static FtexFile ConvertToFtex(DdsFile file, FtexTextureType textureType, int? ftexsFileCount) { FtexFile result = new FtexFile(); if (file.Header.PixelFormat.Equals(DdsPixelFormat.DdsPfA8R8G8B8())) @@ -62,9 +59,8 @@ public static FtexFile ConvertToFtex(DdsFile file, FtexTextureType textureType) result.PixelFormatType = 1; else if (file.Header.PixelFormat.Equals(DdsPixelFormat.DdsPfDxt1())) result.PixelFormatType = 2; - else if (file.Header.PixelFormat.Equals(DdsPixelFormat.DdsPfDxt3())) - result.PixelFormatType = 3; // HACK: This is just a guess. The value isn't used in the Ground Zeroes files. - else if (file.Header.PixelFormat.Equals(DdsPixelFormat.DdsPfDxt5())) + else if (file.Header.PixelFormat.Equals(DdsPixelFormat.DdsPfDxt3()) + || file.Header.PixelFormat.Equals(DdsPixelFormat.DdsPfDxt5())) result.PixelFormatType = 4; else throw new NotImplementedException(String.Format("Unknown PixelFormatType {0}", file.Header.PixelFormat)); @@ -74,7 +70,7 @@ public static FtexFile ConvertToFtex(DdsFile file, FtexTextureType textureType) result.Depth = Convert.ToInt16(file.Header.Depth); var mipMapData = GetMipMapData(file); - var mipMaps = GetMipMapInfos(mipMapData); + var mipMaps = GetMipMapInfos(mipMapData, ftexsFileCount); var ftexsFiles = GetFtexsFiles(mipMaps, mipMapData); result.MipMapCount = Convert.ToByte(mipMaps.Count()); result.NrtFlag = 2; @@ -120,8 +116,8 @@ private static List GetFtexsFiles(List mipMapInfo private static List GetFtexsChunks(FtexFileMipMapInfo mipMapInfo, byte[] mipMapData) { List ftexsFileChunks = new List(); - const int maxChunkSize = short.MaxValue/2 + 1; - int requiredChunks = (int) Math.Ceiling((double) mipMapData.Length/maxChunkSize); + const int maxChunkSize = short.MaxValue / 2 + 1; + int requiredChunks = (int)Math.Ceiling((double)mipMapData.Length / maxChunkSize); int mipMapDataOffset = 0; for (int i = 0; i < requiredChunks; i++) { @@ -136,7 +132,7 @@ private static List GetFtexsChunks(FtexFileMipMapInfo mipMapInfo return ftexsFileChunks; } - private static List GetMipMapInfos(List levelData) + private static List GetMipMapInfos(List levelData, int? ftexsFileCount) { List mipMapsInfos = new List(); for (int i = 0; i < levelData.Count; i++) @@ -148,28 +144,40 @@ private static List GetMipMapInfos(List levelData) mipMapsInfos.Add(mipMapInfo); } - SetMipMapFileNumber(mipMapsInfos); + SetFtexsFileNumbers(mipMapsInfos, ftexsFileCount); return mipMapsInfos; } - private static void SetMipMapFileNumber(ICollection mipMapsInfos) + private static void SetFtexsFileNumbers(ICollection mipMapsInfos, int? ftexsFileCount) { if (mipMapsInfos.Count == 1) { mipMapsInfos.Single().FtexsFileNumber = 1; + return; + } + + int currentFtexsFileNumber; + if (ftexsFileCount.HasValue) + { + currentFtexsFileNumber = Math.Min(mipMapsInfos.Count, ftexsFileCount.Value); } else { - int fileSize = 0; - foreach (var mipMapInfo in mipMapsInfos.OrderBy(m => m.DecompressedFileSize)) + // Guess the ftexs file count + currentFtexsFileNumber = GetFtexsFileCount(mipMapsInfos.Sum(m => m.DecompressedFileSize)); + } + + foreach (var mipMapInfo in mipMapsInfos) + { + mipMapInfo.FtexsFileNumber = Convert.ToByte(currentFtexsFileNumber); + if (currentFtexsFileNumber > 1) { - fileSize += mipMapInfo.DecompressedFileSize; - mipMapInfo.FtexsFileNumber = GetFtexsFileNumber(fileSize); + currentFtexsFileNumber--; } } } - private static byte GetFtexsFileNumber(int fileSize) + private static byte GetFtexsFileCount(int fileSize) { // TODO: Find the correct algorithm. if (fileSize <= 21872) @@ -202,8 +210,8 @@ private static List GetMipMapData(DdsFile file) Array.Copy(data, dataOffset, buffer, 0, size); mipMapDatas.Add(buffer); dataOffset += size; - width = Math.Max(width/2, minimumWidth); - height = Math.Max(height/2, minimumHeight); + width = Math.Max(width / 2, minimumWidth); + height = Math.Max(height / 2, minimumHeight); } return mipMapDatas; } diff --git a/FtexTool/FtexToolArguments.cs b/FtexTool/FtexToolArguments.cs index 1dab60f..f06b625 100644 --- a/FtexTool/FtexToolArguments.cs +++ b/FtexTool/FtexToolArguments.cs @@ -19,6 +19,7 @@ public FtexToolArguments() public string InputPath { get; set; } public bool DirectoryInput { get; set; } public string OutputPath { get; set; } + public int? FtexsFileCount { get; set; } public List Errors { @@ -69,5 +70,18 @@ public void ReadOutput(string outputPath) { OutputPath = outputPath; } + + public void ReadFtexsCount(string ftexsFileCount) + { + int count; + if (int.TryParse(ftexsFileCount, out count) && count > 0) + { + FtexsFileCount = count; + } + else + { + Errors.Add(string.Format("Invalid ftexs file count {0}", ftexsFileCount)); + } + } } } diff --git a/FtexTool/Program.cs b/FtexTool/Program.cs index 0cd66b5..d870532 100644 --- a/FtexTool/Program.cs +++ b/FtexTool/Program.cs @@ -3,7 +3,6 @@ using System.IO; using System.Linq; using FtexTool.Dds; -using FtexTool.Exceptions; using FtexTool.Ftex; using FtexTool.Ftex.Enum; using FtexTool.Ftexs; @@ -53,7 +52,11 @@ private static void Main(string[] args) } else if (arguments.InputPath.EndsWith(".dds")) { - PackDdsFile(arguments.InputPath, arguments.OutputPath, arguments.TextureType); + PackDdsFile( + arguments.InputPath, + arguments.OutputPath, + arguments.TextureType, + arguments.FtexsFileCount); } else { @@ -78,6 +81,7 @@ private static FtexToolArguments ParseArguments(string[] args) } bool expectType = false; + bool expectFtexs = false; bool expectInput = false; bool expectOutput = false; @@ -91,6 +95,11 @@ private static FtexToolArguments ParseArguments(string[] args) arguments.ReadType(arg); expectType = false; } + else if (expectFtexs) + { + arguments.ReadFtexsCount(arg); + expectFtexs = false; + } else if (expectInput) { arguments.ReadInput(arg); @@ -113,6 +122,10 @@ private static FtexToolArguments ParseArguments(string[] args) case "-type": expectType = true; break; + case "-f": + case "-ftexs": + expectFtexs = true; + break; case "-i": case "-input": expectInput = true; @@ -150,6 +163,7 @@ private static void ShowUsageInfo() " m|material\n" + " n|normal\n" + " c|cube\n" + + " -f|ftexs positive_number\n" + " -i|input file_name|folder_Name\n" + " -o|output folder_name\n" + "Examples:\n" + @@ -159,13 +173,13 @@ private static void ShowUsageInfo() " FtexTool -t n file.dds Packs a dds file as a normal map\n"); } - private static void PackDdsFile(string filePath, string outputPath, FtexTextureType textureType) + private static void PackDdsFile(string filePath, string outputPath, FtexTextureType textureType, int? ftexsFileCount) { string fileDirectory = String.IsNullOrEmpty(outputPath) ? Path.GetDirectoryName(filePath) : outputPath; string fileName = Path.GetFileNameWithoutExtension(filePath); DdsFile ddsFile = GetDdsFile(filePath); - FtexFile ftexFile = FtexDdsConverter.ConvertToFtex(ddsFile, textureType); + FtexFile ftexFile = FtexDdsConverter.ConvertToFtex(ddsFile, textureType, ftexsFileCount); foreach (var ftexsFile in ftexFile.FtexsFiles) { @@ -246,7 +260,6 @@ private static FtexFile GetFtexFile(string filePath) } catch (FileNotFoundException e) { - throw new MissingFtexsFileException("The ftexs file " + ftexsName + " could not be found.", e); } } return ftexFile; diff --git a/FtexTool/Properties/AssemblyInfo.cs b/FtexTool/Properties/AssemblyInfo.cs index 1ebffaa..f946c30 100644 --- a/FtexTool/Properties/AssemblyInfo.cs +++ b/FtexTool/Properties/AssemblyInfo.cs @@ -11,5 +11,5 @@ [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("d87a6cb1-0454-4123-8fc6-23f50523de89")] -[assembly: AssemblyVersion("0.3.0.0")] -[assembly: AssemblyFileVersion("0.3.0.0")] +[assembly: AssemblyVersion("0.3.1.0")] +[assembly: AssemblyFileVersion("0.3.1.0")] diff --git a/README.md b/README.md index cdc3df2..d036161 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ Options: m|material n|normal c|cube + -f|ftexs positive_number *Max ftexs file count* -i|input file_name|folder_Name -o|output folder_name ``` @@ -46,6 +47,11 @@ Converting a .dds file to .ftex: FtexTool file_name.dds ``` +Converting a .dds file to an .ftex and 3 .ftexs files: +``` +FtexTool -f 3 file_name.dds +``` + Converting a .dds normalmap file to .ftex ``` FtexTool -t n file_name.dds