-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
358 lines (302 loc) · 10.6 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using RocksmithToolkitLib.DLCPackage;
using RocksmithToolkitLib.DLCPackage.Manifest;
using RocksmithToolkitLib.Extensions;
namespace CDLCRenamer
{
class Program
{
private const string WorkingFolder = "c:\\temp\\";
private static readonly string[] InvalidStrings = { "\\", "/", "?", "*", ":", "\"", "<", ">", "|", "&" };
private static string _artistSongSeparator = "_";
private static string _spaceSeparator = "-";
private static bool _padVersion;
private static bool _overrideCleanName;
private static bool _includeSubfolders = true;
private static bool _enableLogging;
static void Main()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
GetOptions();
var fileNames = GetFileList();
ProcessFiles(fileNames);
Console.WriteLine();
Console.WriteLine(@"Done! Press any key to exit.");
Console.ReadKey();
}
static void GetOptions()
{
if (!File.Exists("options.ini")) return;
var options = File.ReadAllLines("options.ini");
foreach (var option in options.Where(line => !line.StartsWith("#")))
{
if (option.Contains("Artist-Song-Separator:"))
_artistSongSeparator = option.Replace("Artist-Song-Separator:", "").Trim().Replace("\"", "");
if (option.Contains("Space-Character:"))
_spaceSeparator = option.Replace("Space-Character:", "").Trim().Replace("\"", "");
if (option.Contains("Zero-Pad-Version:") && option.ToLower().Contains("true"))
_padVersion = true;
if (option.Contains("Override-Clean-Name:") && option.ToLower().Contains("true"))
_overrideCleanName = true;
if (option.Contains("Include-Subfolders:") && option.ToLower().Contains("false"))
_includeSubfolders = false;
if (option.Contains("Enable-Logging:") && option.ToLower().Contains("true"))
_enableLogging = true;
}
}
private static void ProcessFiles(IEnumerable<string> fileNames)
{
var cleanupTempFolder = false;
if (!Directory.Exists(WorkingFolder))
{
Directory.CreateDirectory(WorkingFolder);
cleanupTempFolder = true;
}
var writer = StreamWriter.Null;
if (_enableLogging)
{
writer = new StreamWriter("Songs-Renamed-" + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + ".txt");
}
foreach (var filePathAndName in fileNames)
{
if (!filePathAndName.IsValidPSARC()) continue;
if (filePathAndName.Contains("rs1compat")) continue;
var filePath = Path.GetDirectoryName(filePathAndName) + "\\";
string unpackedDir;
try
{
unpackedDir = Packer.Unpack(filePathAndName, WorkingFolder, false, false, false);
}
catch (Exception ex)
{
LogErrorMessage(filePathAndName, "unpacker error", ex, writer);
continue;
}
Attributes2014 attrs = null;
var jsonFiles = Directory.GetFiles(unpackedDir, "*.json", SearchOption.AllDirectories);
if (jsonFiles.Length > 0 && !String.IsNullOrEmpty(jsonFiles[0]))
attrs = Manifest2014<Attributes2014>.LoadFromFile(jsonFiles[0]).Entries.ToArray()[0].Value.ToArray()[0].Value;
if (attrs == null) continue;
var version = GetVersionFromFileName(filePathAndName);
var dynamicDifficulty = GetDynamicDifficultyFromMetadata(attrs);
var newFileName = _overrideCleanName
? GetFileNameSafeString(attrs.ArtistNameSort).Replace(" ", _spaceSeparator) +
_artistSongSeparator +
GetFileNameSafeString(attrs.SongNameSort).Replace(" ", _spaceSeparator) +
version +
dynamicDifficulty +
"_p.psarc"
: attrs.ArtistNameSort.GetValidName(true, true).Replace(" ", _spaceSeparator) +
_artistSongSeparator +
attrs.SongNameSort.GetValidName(true, true).Replace(" ", _spaceSeparator) +
version +
dynamicDifficulty +
"_p.psarc";
var artist = attrs.ArtistName;
var song = attrs.SongName;
if (_enableLogging)
{
writer.WriteLine("Old Filename: " + filePathAndName);
writer.WriteLine("New Filename: " + filePath + newFileName);
writer.WriteLine(" Artist: " + artist);
writer.WriteLine(" Artist Sort: " + attrs.ArtistNameSort);
writer.WriteLine(" Song: " + song);
writer.WriteLine(" Song Sort: " + attrs.SongNameSort);
writer.WriteLine("Dynamic Diff: " + (attrs.MaxPhraseDifficulty > 0));
writer.WriteLine(" DLC Author: " + GetAuthorFromMetadata(unpackedDir));
writer.WriteLine();
}
try
{
DeleteDirectory(unpackedDir);
File.Move(filePathAndName, filePath + newFileName);
Console.WriteLine(Path.GetFileName(filePathAndName) + @" -> " + newFileName);
}
catch (IOException ex)
{
if (ex.Message.Contains("Cannot create a file when that file already exists"))
{
newFileName = FileNameHelper.GetNextFileName(newFileName);
try
{
File.Move(filePathAndName, filePath + newFileName);
}
catch (Exception exception)
{
LogErrorMessage(filePathAndName, newFileName, exception, writer);
}
}
else
{
LogErrorMessage(filePathAndName, newFileName, ex, writer);
}
}
catch (Exception ex)
{
LogErrorMessage(filePathAndName, newFileName, ex, writer);
}
}
writer.Dispose();
if (cleanupTempFolder && IsDirectoryEmpty(WorkingFolder))
{
Directory.Delete(WorkingFolder);
}
}
private static void LogErrorMessage(string fileName, string newFileName, Exception exception, TextWriter writer)
{
if (_enableLogging)
{
writer.WriteLine("Error encountered!");
writer.WriteLine(fileName + @" -> " + newFileName);
writer.WriteLine(exception.Message);
writer.WriteLine(exception.InnerException);
}
Console.WriteLine(@"Error encountered!");
Console.WriteLine(Path.GetFileName(fileName) + @" -> " + newFileName);
Console.WriteLine(exception.Message);
Console.WriteLine(exception.InnerException);
Console.WriteLine();
Console.WriteLine(@"Press any key to continue.");
Console.ReadKey();
}
private static string GetFileNameSafeString(string value)
{
foreach (var invalidChar in InvalidStrings)
{
if (value.Contains(invalidChar))
{
value = value.Replace(invalidChar, "");
}
}
return value;
}
private static string GetAuthorFromMetadata(string unpackedDir)
{
var author = string.Empty;
if (!File.Exists(unpackedDir + "\\toolkit.version"))
return author;
var lines = File.ReadAllLines(unpackedDir + "\\toolkit.version");
foreach (var line in lines.Where(line => line.Contains("Package Author")))
{
author = line.Replace("Package Author:", "").Trim();
}
return author;
}
private static string GetVersionFromFileName(string fileName)
{
var version = "1";
var regex = new Regex(@"([vV]+[0-9])([_.-][0-9])?");
var match = regex.Match(fileName);
if (match.Success)
{
version = match.Value.ToLower().Replace("v", "");
}
return GetFormattedVersionString(version);
}
private static string GetFormattedVersionString(string version)
{
if (_padVersion && version.Length == 1)
{
version = version + "_0";
}
return _artistSongSeparator + "v" + version;
}
private static string GetDynamicDifficultyFromMetadata(Attributes2014 attr)
{
return attr.MaxPhraseDifficulty > 0 ? _artistSongSeparator + "DD" : string.Empty;
}
private static IEnumerable<string> GetFileList()
{
var dir = Directory.GetCurrentDirectory();
var fileNames = _includeSubfolders
? Directory.GetFiles(dir, "*_p.psarc", SearchOption.AllDirectories)
: Directory.GetFiles(dir, "*_p.psarc");
return fileNames;
}
private static bool IsDirectoryEmpty(string path)
{
return !Directory.EnumerateFileSystemEntries(path).Any();
}
//from: http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true
private static void DeleteDirectory(string destinationDir)
{
const int magicDust = 10;
for (var gnomes = 1; gnomes <= magicDust; gnomes++)
{
try
{
Directory.Delete(destinationDir, true);
}
catch (DirectoryNotFoundException)
{
return; // good!
}
catch (IOException)
{
// System.IO.IOException: The directory is not empty
//System.Diagnostics.Debug.WriteLine("Gnomes prevent deletion of {0}! Applying magic dust, attempt #{1}.", destinationDir, gnomes);
Thread.Sleep(50);
continue;
}
return;
}
}
//from: http://stackoverflow.com/questions/189549/embedding-dlls-in-a-compiled-executable
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var dllName = args.Name.Contains(',') ? args.Name.Substring(0, args.Name.IndexOf(',')) : args.Name.Replace(".dll", "");
dllName = dllName.Replace(".", "_");
if (dllName.EndsWith("_resources")) return null;
var rm = new System.Resources.ResourceManager(typeof(Program).Namespace + ".Properties.Resources", Assembly.GetExecutingAssembly());
var bytes = (byte[])rm.GetObject(dllName);
return Assembly.Load(bytes);
}
}
//from: http://social.msdn.microsoft.com/Forums/vstudio/en-US/feb18912-d1ae-46e0-b8aa-b739f5d2a86d/file-rename-algorithm
public static class FileNameHelper
{
public static string GetNextFileName(string baseFileName)
{
var result = baseFileName;
var filePath = Path.GetDirectoryName(baseFileName);
var extensionPart = Path.GetExtension(baseFileName);
while (File.Exists(result))
{
var fileNamePart = Path.GetFileNameWithoutExtension(result);
var actuals = GetBaseAndCount(fileNamePart);
var currentCount = actuals.Item2;
fileNamePart = actuals.Item1;
if (filePath != null)
result = $"{Path.Combine(filePath, fileNamePart)} ({++currentCount}){extensionPart}";
}
return Path.GetFileName(result);
}
private static Tuple<string, int> GetBaseAndCount(string fileNamePart)
{
var currentCount = 1;
var baseName = fileNamePart;
// if string is non-null and non empty and last char is closing parenthesis
if (!string.IsNullOrEmpty(fileNamePart) && fileNamePart[fileNamePart.Length - 1] == ')')
{
var lastOpeningParenthesis = fileNamePart.LastIndexOf('(');
if (lastOpeningParenthesis >= 0)
{
// if found opening and closing parenthesis, pull out the number, if parses successfully
var numberLength = fileNamePart.Length - lastOpeningParenthesis - 2;
if (int.TryParse(fileNamePart.Substring(lastOpeningParenthesis + 1, numberLength),
out currentCount))
{
baseName = fileNamePart.Substring(0, lastOpeningParenthesis);
}
}
}
return new Tuple<string, int>(baseName, currentCount);
}
}
}