-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
134 lines (112 loc) · 4.98 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace GoTex
{
public class Program
{
public static string AppTempDirectory = Path.Combine(Path.GetTempPath(), "GoTex");
public static Regex MipMapRegex = new Regex(@"(.*)_M([0-9]{1,2})\.([a-z]{3})$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
public static Regex WidthHeightRegex = new Regex(@"([0-9]+)x([0-9]+)", RegexOptions.Compiled);
public static void Main(string[] args)
{
string inputDirectory = GetDirectory(GetArgument(args, "--input"));
string outputDirectory = GetDirectory(GetArgument(args, "--output"));
if (outputDirectory.EndsWith(@"\_Work\Data\Textures", StringComparison.OrdinalIgnoreCase))
outputDirectory = Path.Combine(outputDirectory, "_Compiled");
string[] files = GetFiles(inputDirectory, "*.TGA|*.BMP|*.PNG|*.JPG", SearchOption.AllDirectories);
if (files.Any())
{
SetUp();
List<Texture> textures = GetTextures(inputDirectory, outputDirectory, files);
ConvertTextures(textures);
CleanUp();
Console.WriteLine("Done!");
}
else
{
AssemblyName assembly = Assembly.GetExecutingAssembly().GetName();
Console.WriteLine(assembly.Name + " v" + assembly.Version.Major + "." + assembly.Version.Minor);
Console.WriteLine("");
Console.WriteLine("Error: No image files found.");
Console.WriteLine("");
Console.WriteLine("Usage: --input \"InputDirectory\" --output \"OutputDirectory\"");
}
}
private static string GetArgument(string[] args, string arg)
{
for (int i = 0; i < args.Length; i++)
{
if (args[i].Equals(arg, StringComparison.OrdinalIgnoreCase) && args.Length >= i)
return args[i + 1];
}
return null;
}
private static string GetDirectory(string directory)
{
return (!string.IsNullOrEmpty(directory) ? new DirectoryInfo(directory).FullName : Environment.CurrentDirectory).TrimEnd('\\');
}
private static string[] GetFiles(string directory, string filters, SearchOption searchOption)
{
return filters.Split('|').SelectMany(filter => Directory.GetFiles(directory, filter, searchOption)).ToArray();
}
private static void SetUp()
{
if (Directory.Exists(AppTempDirectory))
Directory.Delete(AppTempDirectory, true);
Directory.CreateDirectory(AppTempDirectory);
foreach (string resource in Assembly.GetExecutingAssembly().GetManifestResourceNames())
{
File.WriteAllBytes(Path.Combine(AppTempDirectory, resource.Replace("GoTex.Tools.", "")), GetResourceBytes(resource));
}
}
private static byte[] GetResourceBytes(string resource)
{
using (Stream rs = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
{
using (MemoryStream ms = new MemoryStream())
{
rs.CopyTo(ms);
return ms.ToArray();
}
}
}
private static List<Texture> GetTextures(string inputDirectory, string outputDirectory, string[] files)
{
Dictionary<string, List<string>> textures = new Dictionary<string, List<string>>();
foreach (string file in files)
{
string fileName = Path.GetFileName(file);
Match match = MipMapRegex.Match(fileName);
string textureName = match.Success ? match.Groups[1].Value : Path.GetFileNameWithoutExtension(file);
if (!textures.ContainsKey(textureName))
textures[textureName] = new List<string>();
textures[textureName].Add(file);
}
List<Texture> result = textures.Select(t => new Texture(inputDirectory, outputDirectory, t.Key, textures[t.Key])).ToList();
return result;
}
private static void ConvertTextures(List<Texture> textures)
{
Parallel.ForEach(textures, (Texture texture) =>
{
try
{
texture.Convert();
}
catch (Exception ex)
{
Console.WriteLine($"Error on {texture.TextureName}: {ex.Message}");
};
});
}
private static void CleanUp()
{
Directory.Delete(AppTempDirectory, true);
}
}
}