Skip to content

Commit

Permalink
Added an option to specify the max ftexs file count
Browse files Browse the repository at this point in the history
  • Loading branch information
Atvaark committed Oct 14, 2015
1 parent 92b3f82 commit ecc2c97
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 27 deletions.
48 changes: 28 additions & 20 deletions FtexTool/FtexDdsConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()))
Expand All @@ -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));
Expand All @@ -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;
Expand Down Expand Up @@ -120,8 +116,8 @@ private static List<FtexsFile> GetFtexsFiles(List<FtexFileMipMapInfo> mipMapInfo
private static List<FtexsFileChunk> GetFtexsChunks(FtexFileMipMapInfo mipMapInfo, byte[] mipMapData)
{
List<FtexsFileChunk> ftexsFileChunks = new List<FtexsFileChunk>();
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++)
{
Expand All @@ -136,7 +132,7 @@ private static List<FtexsFileChunk> GetFtexsChunks(FtexFileMipMapInfo mipMapInfo
return ftexsFileChunks;
}

private static List<FtexFileMipMapInfo> GetMipMapInfos(List<byte[]> levelData)
private static List<FtexFileMipMapInfo> GetMipMapInfos(List<byte[]> levelData, int? ftexsFileCount)
{
List<FtexFileMipMapInfo> mipMapsInfos = new List<FtexFileMipMapInfo>();
for (int i = 0; i < levelData.Count; i++)
Expand All @@ -148,28 +144,40 @@ private static List<FtexFileMipMapInfo> GetMipMapInfos(List<byte[]> levelData)
mipMapsInfos.Add(mipMapInfo);
}

SetMipMapFileNumber(mipMapsInfos);
SetFtexsFileNumbers(mipMapsInfos, ftexsFileCount);
return mipMapsInfos;
}

private static void SetMipMapFileNumber(ICollection<FtexFileMipMapInfo> mipMapsInfos)
private static void SetFtexsFileNumbers(ICollection<FtexFileMipMapInfo> 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)
Expand Down Expand Up @@ -202,8 +210,8 @@ private static List<byte[]> 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;
}
Expand Down
14 changes: 14 additions & 0 deletions FtexTool/FtexToolArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> Errors
{
Expand Down Expand Up @@ -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));
}
}
}
}
23 changes: 18 additions & 5 deletions FtexTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand All @@ -78,6 +81,7 @@ private static FtexToolArguments ParseArguments(string[] args)
}

bool expectType = false;
bool expectFtexs = false;
bool expectInput = false;
bool expectOutput = false;

Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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" +
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions FtexTool/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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
Expand Down

0 comments on commit ecc2c97

Please sign in to comment.