1
0

LaunchUtils.cs 1.6 KB

123456789101112131415161718192021222324252627282930
  1. namespace PCLCS;
  2. public static class LaunchUtils {
  3. /// <summary>
  4. /// 释放补丁文件并返回完整文件路径。
  5. /// </summary>
  6. /// <param name="patchFilePath">补丁文件路径
  7. /// <param name="outputDirectory">补丁文件的释放目录。</param>
  8. public static string ExtractPatch(string patchFilePath, string outputDirectory) {
  9. Logger.Info($"选定的资源 {patchFilePath} 输出路径:{outputDirectory}");
  10. lock (ExtractPatchLock) { // 避免 OptiFine 和 Forge 安装时同时释放导致冲突
  11. try {
  12. FileUtils.ExtractResources(patchFilePath, Path.Combine(outputDirectory, PathUtils.GetLastPart(patchFilePath)), typeof(LaunchUtils));
  13. } catch (Exception ex) {
  14. if (!FileUtils.Exists(outputDirectory)) throw new FileNotFoundException($"释放 {patchFilePath} 失败", ex);
  15. Logger.Warn(ex, $"{patchFilePath} 文件重新释放失败,将尝试更换文件名重新生成");
  16. patchFilePath = Path.Combine(outputDirectory, $"{PathUtils.GetFileNameWithoutExtension(patchFilePath)}2.{PathUtils.GetExtension(patchFilePath)}");
  17. try {
  18. FileUtils.ExtractResources(patchFilePath, Path.Combine(outputDirectory, PathUtils.GetLastPart(patchFilePath)), typeof(LaunchUtils));
  19. } catch (Exception ex2) {
  20. throw new FileNotFoundException($"释放 {patchFilePath} 最终尝试失败", ex2);
  21. }
  22. }
  23. }
  24. return Path.Combine(outputDirectory, PathUtils.GetLastPart(patchFilePath));
  25. }
  26. private static readonly object ExtractPatchLock = new();
  27. }