support first placeholder & overwriting version

This commit is contained in:
2025-04-23 13:38:51 +02:00
parent 0285892f20
commit e14dedc924
7 changed files with 24 additions and 12 deletions

View File

@@ -41,11 +41,13 @@ public static class Extensions
}
}
public static string GetSourceUrl(this InstallAction @this, Version version)
public static string GetSourceUrl(this InstallAction @this, Version version, string? overwriteVersion = null)
{
if (version is null)
return @this.SourceUrl;
return @this.SourceUrl.Replace("{version}", version.ToString(3));
if (!string.IsNullOrWhiteSpace(overwriteVersion))
return @this.SourceUrl.Replace("{version}", overwriteVersion);
if (version is not null)
return @this.SourceUrl.Replace("{version}", version.ToString(3));
return @this.SourceUrl;
}
public static string GetInstallUrl(this ModpackConfig @this, string? overwriteRefTag = null)

View File

@@ -24,7 +24,7 @@ public class ModpackFactory
modrinth.Dispose();
}
public async Task<string> ResolveSourceUrl(InstallAction action, Version? targetVersion)
public async Task<string> ResolveSourceUrl(InstallAction action, Version? targetVersion = null, string? overwriteVersion = null)
{
if (action.SourceType == SourceType.GitHub)
{
@@ -53,7 +53,7 @@ public class ModpackFactory
return file.Url;
}
return action.GetSourceUrl(targetVersion);
return action.GetSourceUrl(targetVersion, overwriteVersion: overwriteVersion);
}
public async Task<KeyValuePair<string, string>[]> FindUpdates(InstallAction action, string? gameVersion, ModLoader modLoader)

View File

@@ -22,6 +22,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
public ILogger Log { get; set; } = NullLogger.Instance;
public string? OverwriteRefTag { get; set; }
public string? OverwriteVersion { get; set; }
private readonly HttpClient http = new();
private readonly ModpackFactory factory = new();
@@ -151,8 +152,8 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
{
var destFilePath = Path.Combine(modpackInfo.LocaLPath, iaction.DestPath);
var sourceUrl = updateConfig.PreferDirectLinks && !string.IsNullOrWhiteSpace(iaction.SourceUrl)
? iaction.GetSourceUrl(checkResult.LatestVersion)
: await factory.ResolveSourceUrl(iaction, checkResult.LatestVersion);
? iaction.GetSourceUrl(checkResult.LatestVersion, overwriteVersion: OverwriteVersion)
: await factory.ResolveSourceUrl(iaction, targetVersion: checkResult.LatestVersion, overwriteVersion: OverwriteVersion);
if (iaction is UpdateAction uaction)
{
@@ -278,6 +279,11 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
if (cachedZipInfo != null)
{
// Copy content
if (zipPath.StartsWith("{first}"))
{
var firstDirName = Path.GetFileName(Directory.GetDirectories(cachedZipInfo.ExtractedZipPath).First());
zipPath = zipPath.Replace("{first}", firstDirName);
}
var zipSrc = Path.Combine(cachedZipInfo.ExtractedZipPath, zipPath);
Extensions.CopyDirectory(zipSrc, destFilePath, true);
}