Converter.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using SongCore.Utilities;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using UnityEngine;
  8. using LogSeverity = IPA.Logging.Logger.Level;
  9. namespace SongCore
  10. {
  11. public class Converter
  12. {
  13. internal static int ConcurrentProcesses = 4;
  14. internal static int ActiveProcesses = 0;
  15. internal static int ConvertedCount = 0;
  16. internal static bool doneConverting = false;
  17. public static Stack<string> ToConvert = new Stack<string>();
  18. public static string oldFolderPath = Environment.CurrentDirectory + "/CustomSongs";
  19. public static void PrepareExistingLibrary()
  20. {
  21. Logging.Log("Attempting to Convert Existing Library");
  22. if (!Directory.Exists(oldFolderPath))
  23. {
  24. Logging.Log("No Existing Library to Convert", LogSeverity.Notice);
  25. return;
  26. }
  27. Utils.GrantAccess(oldFolderPath);
  28. Loader.Instance._progressBar.ShowMessage("Converting Existing Song Library");
  29. var oldFolders = Directory.GetDirectories(oldFolderPath).ToList();
  30. float i = 0;
  31. foreach (var folder in oldFolders)
  32. {
  33. i++;
  34. if (Directory.Exists(folder))
  35. {
  36. var results = Directory.GetFiles(folder, "info.json", SearchOption.AllDirectories);
  37. foreach (var result in results)
  38. {
  39. var songPath = Path.GetDirectoryName(result.Replace('\\', '/'));
  40. if (!Directory.Exists(songPath)) continue;
  41. if (Directory.GetFiles(songPath, "info.dat").Count() > 0)
  42. continue;
  43. string newPath = songPath;
  44. //If song is in a subfolder, move it to CustomSongs and correct the name
  45. var parent = Directory.GetParent(songPath);
  46. if (parent.Name != "CustomSongs")
  47. {
  48. try
  49. {
  50. // Logging.Log("SubFolder Song Found: " + songPath, LogSeverity.Notice);
  51. // Logging.Log("Moving Subfolder to CustomSongs", LogSeverity.Notice);
  52. newPath = oldFolderPath + "/" + parent.Name + " " + new DirectoryInfo(songPath).Name;
  53. if (Directory.Exists(newPath))
  54. {
  55. int pathNum = 1;
  56. while (Directory.Exists(newPath + $" ({pathNum})")) ++pathNum;
  57. newPath = newPath + $" ({pathNum})";
  58. }
  59. Directory.Move(songPath, newPath);
  60. if (Utils.IsDirectoryEmpty(parent.FullName))
  61. {
  62. // Logging.Log("Old parent folder empty, Deleting empty folder.");
  63. Directory.Delete(parent.FullName);
  64. }
  65. }
  66. catch (Exception ex)
  67. {
  68. Logging.Log($"Error attempting to correct Subfolder {songPath}: \n {ex}", LogSeverity.Error);
  69. }
  70. }
  71. ToConvert.Push(newPath);
  72. }
  73. }
  74. }
  75. if (File.Exists(oldFolderPath + "/../songe-converter.exe"))
  76. Loader.Instance.StartCoroutine(ConvertSongs());
  77. else
  78. {
  79. Logging.Log("Missing Songe converter, not converting", LogSeverity.Notice);
  80. Loader.Instance.RefreshSongs();
  81. }
  82. }
  83. internal static IEnumerator ConvertSongs()
  84. {
  85. int totalSongs = ToConvert.Count;
  86. Loader.Instance._progressBar.ShowMessage($"Converting {totalSongs} Existing Songs. Please Wait...");
  87. System.Diagnostics.Process process = new System.Diagnostics.Process();
  88. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  89. startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
  90. startInfo.FileName = "cmd.exe";
  91. startInfo.Arguments = "/C " + "songe-converter.exe" + " -k -a " + '"' + oldFolderPath + '"';
  92. process.StartInfo = startInfo;
  93. process.EnableRaisingEvents = true;
  94. process.Exited += Process_Exited;
  95. process.Start();
  96. yield return new WaitUntil((delegate { return doneConverting; }));
  97. /*
  98. while (ToConvert.Count > 0)
  99. {
  100. while (ActiveProcesses < ConcurrentProcesses)
  101. {
  102. ActiveProcesses++;
  103. System.Diagnostics.Process process = new System.Diagnostics.Process();
  104. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  105. if (ToConvert.Count == 0) break;
  106. string newPath = ToConvert.Pop();
  107. startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
  108. startInfo.FileName = "cmd.exe";
  109. startInfo.Arguments = "/C " + "songe-converter.exe" + " -k " + '"' + newPath + '"';
  110. process.StartInfo = startInfo;
  111. process.EnableRaisingEvents = true;
  112. process.Exited += Process_Exited;
  113. process.Start();
  114. }
  115. yield return new WaitUntil((delegate { return ActiveProcesses < ConcurrentProcesses; }));
  116. // if (ConvertedCount % 10 == 0)
  117. // {
  118. // Loader.Instance._progressBar.ShowMessage($"Converting {ToConvert.Count} Existing Songs");
  119. // }
  120. // else if(ToConvert.Count <= 10)
  121. Loader.Instance._progressBar.ShowMessage($"Converting {ToConvert.Count} Existing Songs");
  122. }
  123. */
  124. Logging.Log($"Converted {totalSongs} songs.");
  125. FinishConversion();
  126. }
  127. private static void Process_Exited(object sender, EventArgs e)
  128. {
  129. // Logging.Log("Ended");
  130. // ActiveProcesses--;
  131. // ConvertedCount++;
  132. doneConverting = true;
  133. }
  134. internal static void FinishConversion()
  135. {
  136. if (Directory.Exists(oldFolderPath))
  137. {
  138. // Logging.Log(CustomLevelPathHelper.customLevelsDirectoryPath);
  139. // Logging.Log((CustomLevelPathHelper.customLevelsDirectoryPath + System.DateTime.Now.ToFileTime().ToString()));
  140. // Logging.Log(oldFolderPath);
  141. Logging.Log("Moving CustomSongs folder to new Location");
  142. if (Directory.Exists(CustomLevelPathHelper.customLevelsDirectoryPath))
  143. {
  144. Utils.GrantAccess(CustomLevelPathHelper.customLevelsDirectoryPath);
  145. Directory.Move(CustomLevelPathHelper.customLevelsDirectoryPath, CustomLevelPathHelper.customLevelsDirectoryPath + System.DateTime.Now.ToFileTime().ToString());
  146. }
  147. Utils.GrantAccess(oldFolderPath);
  148. Directory.Move(oldFolderPath, CustomLevelPathHelper.customLevelsDirectoryPath);
  149. // Directory.Delete(oldFolderPath);
  150. }
  151. Logging.Log("Conversion Finished. Loading songs");
  152. Loader.Instance.RefreshSongs();
  153. }
  154. }
  155. }