|
1 | 1 | using System;
|
2 | 2 | using System.IO;
|
3 | 3 | using System.Linq;
|
| 4 | +using ICSharpCode.SharpZipLib.Zip; |
4 | 5 |
|
5 | 6 | namespace ReleaseTool
|
6 | 7 | {
|
7 | 8 | internal static class ReleaseTool
|
8 | 9 | {
|
9 | 10 | private static TextWriter _originalOut;
|
10 | 11 |
|
| 12 | + private static string CleanPath(string path) |
| 13 | + { |
| 14 | + return path.Trim().Replace('\\', '/').Trim('/').Trim(); |
| 15 | + } |
| 16 | + public static void CopyAll(DirectoryInfo source, DirectoryInfo target) |
| 17 | + { |
| 18 | + Directory.CreateDirectory(target.FullName); |
| 19 | + |
| 20 | + // Copy each file into the new directory. |
| 21 | + foreach (FileInfo fi in source.GetFiles()) |
| 22 | + { |
| 23 | + fi.CopyTo(Path.Combine(target.FullName, fi.Name), true); |
| 24 | + } |
| 25 | + |
| 26 | + // Copy each subdirectory using recursion. |
| 27 | + foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) |
| 28 | + { |
| 29 | + DirectoryInfo nextTargetSubDir = |
| 30 | + target.CreateSubdirectory(diSourceSubDir.Name); |
| 31 | + CopyAll(diSourceSubDir, nextTargetSubDir); |
| 32 | + } |
| 33 | + } |
| 34 | + |
11 | 35 | private static void Main(string[] args)
|
12 | 36 | {
|
13 |
| - var fgc = Console.ForegroundColor; |
14 |
| - if (args.Length != 1 || !Directory.Exists(args[0]) || !args[0].Replace('/', '\\').TrimEnd('\\').EndsWith("\\translation", StringComparison.OrdinalIgnoreCase)) |
| 37 | + if (args.Length != 1 || !Directory.Exists(args[0])) |
15 | 38 | {
|
16 |
| - Console.ForegroundColor = ConsoleColor.Red; |
17 |
| - Console.WriteLine("Error: Invalid arguments"); |
18 |
| - Console.ForegroundColor = fgc; |
19 |
| - Console.WriteLine("Drag the translation folder from the repository on to this tool's exe to generate a release. Detailed results will be saved to results.txt in the program directory."); |
| 39 | + ShowInvalidArgsError(); |
20 | 40 | }
|
21 | 41 | else
|
22 | 42 | {
|
23 | 43 | var root = new DirectoryInfo(args[0]);
|
24 |
| - if (root.Exists) |
| 44 | + var translationName = root.Name.Replace("-master", ""); //todo let user enter it |
| 45 | + |
| 46 | + var translationDir = Path.Combine(root.FullName, "translation"); |
| 47 | + if (!Directory.Exists(translationDir)) |
| 48 | + { |
| 49 | + ShowInvalidArgsError(); |
| 50 | + } |
| 51 | + else |
25 | 52 | {
|
26 | 53 | Console.ForegroundColor = ConsoleColor.Yellow;
|
27 | 54 | Console.WriteLine("Beginning release creation!");
|
| 55 | + Console.ForegroundColor = ConsoleColor.White; |
| 56 | + |
| 57 | + var programDir = Path.GetDirectoryName(typeof(ReleaseTool).Assembly.Location) ?? |
| 58 | + Environment.CurrentDirectory; |
| 59 | + |
| 60 | + var detailsPath = Path.Combine(programDir, "results.txt"); |
| 61 | + |
| 62 | + var outputPath = Path.Combine(programDir, translationName + "_Release_" + DateTime.Now.ToString("yyyy-MM-dd") + ".zip"); |
| 63 | + File.Delete(outputPath); |
| 64 | + using (var output = ZipFile.Create(outputPath)) |
| 65 | + { |
| 66 | + Console.WriteLine("Writing the release to " + outputPath); |
| 67 | + |
| 68 | + output.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct)); |
| 69 | + |
| 70 | + var readmePath = Path.Combine(root.FullName, "README.md"); |
| 71 | + if (File.Exists(readmePath)) output.Add(readmePath, "README.md"); |
| 72 | + |
| 73 | + var licensePath = Path.Combine(root.FullName, "LICENSE"); |
| 74 | + if (File.Exists(licensePath)) output.Add(licensePath, "LICENSE"); |
28 | 75 |
|
29 |
| - _originalOut = Console.Out; |
30 |
| - var detailsPath = Path.Combine(Environment.CurrentDirectory, "results.txt"); |
31 |
| - Console.SetOut(new StreamWriter(detailsPath)); |
| 76 | + var configDir = Path.Combine(root.FullName, "config"); |
| 77 | + foreach (var file in Directory.GetFiles(configDir, "*", SearchOption.AllDirectories)) |
| 78 | + { |
| 79 | + var entryName = Path.Combine("BepInEx", CleanPath(file.Substring(root.FullName.Length))); |
| 80 | + output.Add(file, entryName); |
| 81 | + } |
32 | 82 |
|
33 |
| - var result = ScanResursively(root); |
| 83 | + var tlDir = Path.Combine(root.FullName, "Translation\\en"); |
34 | 84 |
|
35 |
| - Console.Out.Flush(); |
36 |
| - Console.SetOut(_originalOut); |
| 85 | + // Make a working copy |
| 86 | + Console.WriteLine("Creating a work copy of the translation files..."); |
| 87 | + var copyDir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "releasetool_tl_temp")); |
| 88 | + if (copyDir.Exists) copyDir.Delete(true); |
| 89 | + CopyAll(new DirectoryInfo(tlDir), copyDir); |
37 | 90 |
|
38 |
| - Console.ForegroundColor = ConsoleColor.Green; |
39 |
| - Console.WriteLine($"Creating release done, {result.GetPercent() * 100:F3}% completed."); |
40 |
| - Console.ForegroundColor = ConsoleColor.Gray; |
41 |
| - Console.WriteLine("Detailed results were saved to " + detailsPath); |
| 91 | + Console.WriteLine("Cleaning untranslated lines and files..."); |
| 92 | + _originalOut = Console.Out; |
| 93 | + Console.SetOut(new StreamWriter(detailsPath)); |
| 94 | + // Clean up the copied files |
| 95 | + var result = CleanTranslations(copyDir); |
| 96 | + Console.Out.Flush(); |
| 97 | + Console.SetOut(_originalOut); |
| 98 | + |
| 99 | + // Use the cleaned up copy |
| 100 | + tlDir = copyDir.FullName; |
| 101 | + |
| 102 | + Console.WriteLine("Writing cleaned translation files into archives..."); |
| 103 | + |
| 104 | + var texDir = Path.Combine(tlDir, "Texture"); |
| 105 | + if (Directory.Exists(texDir) && Directory.GetFiles(texDir, "*.png", SearchOption.AllDirectories).Any()) |
| 106 | + { |
| 107 | + var texZipPath = Path.GetTempFileName(); |
| 108 | + using (var texZipFile = ZipFile.Create(texZipPath)) |
| 109 | + { |
| 110 | + texZipFile.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct)); |
| 111 | + |
| 112 | + foreach (var file in Directory.GetFiles(texDir, "*.png", SearchOption.AllDirectories)) |
| 113 | + { |
| 114 | + var entryName = CleanPath(file.Substring(texDir.Length)); |
| 115 | + //Console.WriteLine("Adding texture to texture archive: " + entryName); |
| 116 | + texZipFile.Add(file, entryName); |
| 117 | + } |
| 118 | + |
| 119 | + texZipFile.CommitUpdate(); |
| 120 | + } |
| 121 | + output.Add(texZipPath, "BepInEx\\Translation\\en\\Texture\\" + translationName + "_Textures.zip"); |
| 122 | + } |
| 123 | + |
| 124 | + var assetDir = Path.Combine(tlDir, "RedirectedResources\\assets"); |
| 125 | + if (Directory.Exists(assetDir) && Directory.GetFiles(assetDir, "*.txt", SearchOption.AllDirectories).Any()) |
| 126 | + { |
| 127 | + var assZipPath = Path.GetTempFileName(); |
| 128 | + using (var assZipFile = ZipFile.Create(assZipPath)) |
| 129 | + { |
| 130 | + assZipFile.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct)); |
| 131 | + |
| 132 | + foreach (var file in Directory.GetFiles(assetDir, "*.txt", SearchOption.AllDirectories)) |
| 133 | + { |
| 134 | + var entryName = CleanPath(file.Substring(assetDir.Length)); |
| 135 | + //Console.WriteLine("Adding to redirected assets archive: " + entryName); |
| 136 | + assZipFile.Add(file, entryName); |
| 137 | + } |
| 138 | + |
| 139 | + assZipFile.CommitUpdate(); |
| 140 | + } |
| 141 | + output.Add(assZipPath, "BepInEx\\Translation\\en\\RedirectedResources\\assets\\" + translationName + "_Assets.zip"); |
| 142 | + } |
| 143 | + |
| 144 | + var textDir = Path.Combine(tlDir, "Text"); |
| 145 | + if (Directory.Exists(textDir) && Directory.GetFiles(textDir, "*.txt", SearchOption.AllDirectories).Any()) |
| 146 | + { |
| 147 | + var textZipPath = Path.GetTempFileName(); |
| 148 | + using (var textZipFile = ZipFile.Create(textZipPath)) |
| 149 | + { |
| 150 | + textZipFile.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct)); |
| 151 | + |
| 152 | + foreach (var file in Directory.GetFiles(textDir, "*.txt", SearchOption.AllDirectories)) |
| 153 | + { |
| 154 | + var entryName = CleanPath(file.Substring(textDir.Length)); |
| 155 | + //Console.WriteLine("Adding to redirected assets archive: " + entryName); |
| 156 | + textZipFile.Add(file, entryName); |
| 157 | + } |
| 158 | + |
| 159 | + textZipFile.CommitUpdate(); |
| 160 | + } |
| 161 | + output.Add(textZipPath, "BepInEx\\Translation\\en\\Text\\" + translationName + "_Text.zip"); |
| 162 | + } |
| 163 | + |
| 164 | + foreach (ZipEntry entry in output) |
| 165 | + { |
| 166 | + entry.CompressionMethod = CompressionMethod.Deflated; |
| 167 | + } |
| 168 | + |
| 169 | + output.CommitUpdate(); |
| 170 | + |
| 171 | + Console.ForegroundColor = ConsoleColor.Green; |
| 172 | + Console.WriteLine($"Creating release done, {result.GetPercent() * 100:F3}% completed."); |
| 173 | + Console.ForegroundColor = ConsoleColor.Gray; |
| 174 | + Console.WriteLine("Detailed results were saved to " + detailsPath); |
| 175 | + |
| 176 | + copyDir.Delete(true); |
| 177 | + } |
42 | 178 | }
|
43 | 179 | }
|
44 | 180 |
|
45 |
| - Console.ForegroundColor = fgc; |
| 181 | + Console.ForegroundColor = ConsoleColor.White; |
46 | 182 | Console.WriteLine("Press any key to exit...");
|
47 | 183 | Console.ReadKey(true);
|
48 | 184 | }
|
49 | 185 |
|
50 |
| - private static Results ScanResursively(DirectoryInfo dir) |
| 186 | + private static void ShowInvalidArgsError() |
| 187 | + { |
| 188 | + Console.ForegroundColor = ConsoleColor.Red; |
| 189 | + Console.WriteLine("Error: Invalid arguments"); |
| 190 | + Console.ForegroundColor = ConsoleColor.White; |
| 191 | + Console.WriteLine("Drag the repository folder (it must contain a translation folder) on to this tool's exe to generate a release. Detailed results will be saved to results.txt in the program directory."); |
| 192 | + } |
| 193 | + |
| 194 | + private static Results CleanTranslations(DirectoryInfo dir) |
51 | 195 | {
|
52 | 196 | var files = dir.GetFiles("*.txt", SearchOption.TopDirectoryOnly);
|
53 | 197 | var folders = dir.GetDirectories();
|
@@ -82,7 +226,7 @@ private static Results ScanResursively(DirectoryInfo dir)
|
82 | 226 |
|
83 | 227 | foreach (var folder in folders)
|
84 | 228 | {
|
85 |
| - var r = ScanResursively(folder); |
| 229 | + var r = CleanTranslations(folder); |
86 | 230 | folderPercentage.Add(r);
|
87 | 231 | }
|
88 | 232 |
|
|
0 commit comments