Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include version in useragent string, make it readonly #4184

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Cmdline/Action/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,19 @@ public int RunCommand(CKAN.GameInstance instance, object options)
{
log.InfoFormat("Importing {0} files", toImport.Count);
var toInstall = new List<CkanModule>();
var installer = new ModuleInstaller(instance, manager.Cache, user);
var installer = new ModuleInstaller(instance, manager.Cache, user, opts?.NetUserAgent);
var regMgr = RegistryManager.Instance(instance, repoData);
installer.ImportFiles(toImport, user, toInstall.Add, regMgr.registry, !opts?.Headless ?? false);
ModuleInstaller.ImportFiles(toImport, user, toInstall.Add,
regMgr.registry, instance, manager.Cache,
!opts?.Headless ?? false);
if (toInstall.Count > 0)
{
HashSet<string>? possibleConfigOnlyDirs = null;
installer.InstallList(toInstall,
new RelationshipResolverOptions(),
regMgr,
ref possibleConfigOnlyDirs);
ref possibleConfigOnlyDirs,
opts?.NetUserAgent);
}
return Exit.OK;
}
Expand Down
6 changes: 3 additions & 3 deletions Cmdline/Action/Install.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public int RunCommand(CKAN.GameInstance instance, object raw_options)
.Select(arg => new NetAsyncDownloader.DownloadTargetFile(getUri(arg)))
.ToArray();
log.DebugFormat("Urls: {0}", targets.SelectMany(t => t.urls));
new NetAsyncDownloader(new NullUser()).DownloadAndWait(targets);
new NetAsyncDownloader(new NullUser(), options.NetUserAgent).DownloadAndWait(targets);
log.DebugFormat("Files: {0}", targets.Select(t => t.filename));
modules = targets.Select(t => MainClass.LoadCkanFromFile(t.filename))
.ToList();
Expand Down Expand Up @@ -101,7 +101,7 @@ public int RunCommand(CKAN.GameInstance instance, object raw_options)
return Exit.ERROR;
}

var installer = new ModuleInstaller(instance, manager.Cache, user);
var installer = new ModuleInstaller(instance, manager.Cache, user, options?.NetUserAgent);
var install_ops = new RelationshipResolverOptions
{
with_all_suggests = options?.with_all_suggests ?? false,
Expand All @@ -119,7 +119,7 @@ public int RunCommand(CKAN.GameInstance instance, object raw_options)
{
HashSet<string>? possibleConfigOnlyDirs = null;
installer.InstallList(modules, install_ops, regMgr,
ref possibleConfigOnlyDirs);
ref possibleConfigOnlyDirs, options?.NetUserAgent);
user.RaiseMessage("");
done = true;
}
Expand Down
2 changes: 1 addition & 1 deletion Cmdline/Action/Remove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public int RunCommand(CKAN.GameInstance instance, object raw_options)
try
{
HashSet<string>? possibleConfigOnlyDirs = null;
var installer = new ModuleInstaller(instance, manager.Cache, user);
var installer = new ModuleInstaller(instance, manager.Cache, user, options.NetUserAgent);
Search.AdjustModulesCase(instance, regMgr.registry, options.modules);
installer.UninstallList(options.modules, ref possibleConfigOnlyDirs, regMgr);
user.RaiseMessage("");
Expand Down
6 changes: 5 additions & 1 deletion Cmdline/Action/Replace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ public int RunCommand(CKAN.GameInstance instance, object raw_options)
try
{
HashSet<string>? possibleConfigOnlyDirs = null;
new ModuleInstaller(instance, manager.Cache, user).Replace(to_replace, replace_ops, new NetAsyncModulesDownloader(user, manager.Cache), ref possibleConfigOnlyDirs, regMgr);
new ModuleInstaller(instance, manager.Cache, user, options.NetUserAgent)
.Replace(to_replace, replace_ops,
new NetAsyncModulesDownloader(user, manager.Cache, options.NetUserAgent),
ref possibleConfigOnlyDirs,
regMgr);
user.RaiseMessage("");
}
catch (DependencyNotSatisfiedKraken ex)
Expand Down
8 changes: 4 additions & 4 deletions Cmdline/Action/Repo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public int RunSubCommand(GameInstanceManager? manager,
switch (option)
{
case "available":
exitCode = AvailableRepositories();
exitCode = AvailableRepositories(options.NetUserAgent);
break;

case "list":
Expand Down Expand Up @@ -189,14 +189,14 @@ public int RunSubCommand(GameInstanceManager? manager,
return exitCode;
}

private int AvailableRepositories()
private int AvailableRepositories(string? userAgent)
{
user?.RaiseMessage(Properties.Resources.RepoAvailableHeader);

try
{
if (RepositoryList.DefaultRepositories(
MainClass.GetGameInstance(Manager).game)
MainClass.GetGameInstance(Manager).game, userAgent)
is RepositoryList repoList)
{
var maxNameLen = repoList.repositories
Expand Down Expand Up @@ -270,7 +270,7 @@ private int AddRepository(RepoAddOptions options)
if (options.uri == null)
{
if (RepositoryList.DefaultRepositories(
MainClass.GetGameInstance(Manager).game)
MainClass.GetGameInstance(Manager).game, options.NetUserAgent)
is RepositoryList repoList)
{
foreach (var candidate in repoList.repositories)
Expand Down
16 changes: 8 additions & 8 deletions Cmdline/Action/Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public int RunCommand(object raw_options)
.Select(am => am.Latest())
.OfType<CkanModule>()
.ToList();
UpdateRepositories(game, repos, options.force);
UpdateRepositories(game, repos, options.NetUserAgent, options.force);
PrintChanges(availablePrior,
repoData.GetAllAvailableModules(repos)
.Select(am => am.Latest())
Expand All @@ -71,7 +71,7 @@ public int RunCommand(object raw_options)
}
else
{
UpdateRepositories(game, repos, options.force);
UpdateRepositories(game, repos, options.NetUserAgent, options.force);
}
}
else
Expand All @@ -83,13 +83,13 @@ public int RunCommand(object raw_options)
var registry = RegistryManager.Instance(instance, repoData).registry;
var crit = instance.VersionCriteria();
var compatible_prior = registry.CompatibleModules(crit).ToList();
UpdateRepositories(instance, options.force);
UpdateRepositories(instance, options.NetUserAgent, options.force);
PrintChanges(compatible_prior,
registry.CompatibleModules(crit).ToList());
}
else
{
UpdateRepositories(instance, options.force);
UpdateRepositories(instance, options.NetUserAgent, options.force);
}
}
}
Expand Down Expand Up @@ -177,12 +177,12 @@ private void PrintModules(string message, IEnumerable<CkanModule> modules)
/// </summary>
/// <param name="instance">The game instance to work on.</param>
/// <param name="repository">Repository to update. If null all repositories are used.</param>
private void UpdateRepositories(CKAN.GameInstance instance, bool force = false)
private void UpdateRepositories(CKAN.GameInstance instance, string? userAgent, bool force = false)
{
var registry = RegistryManager.Instance(instance, repoData).registry;
var result = repoData.Update(registry.Repositories.Values.ToArray(),
instance.game, force,
new NetAsyncDownloader(user), user);
new NetAsyncDownloader(user, userAgent), user, userAgent);
if (result == RepositoryDataManager.UpdateResult.Updated)
{
user.RaiseMessage(Properties.Resources.UpdateSummary,
Expand All @@ -195,9 +195,9 @@ private void UpdateRepositories(CKAN.GameInstance instance, bool force = false)
/// </summary>
/// <param name="game">The game for which the URLs contain metadata</param>
/// <param name="repos">Repositories to update</param>
private void UpdateRepositories(IGame game, Repository[] repos, bool force = false)
private void UpdateRepositories(IGame game, Repository[] repos, string? userAgent, bool force = false)
{
var result = repoData.Update(repos, game, force, new NetAsyncDownloader(user), user);
var result = repoData.Update(repos, game, force, new NetAsyncDownloader(user, userAgent), user, userAgent);
if (result == RepositoryDataManager.UpdateResult.Updated)
{
user.RaiseMessage(Properties.Resources.UpdateSummary,
Expand Down
29 changes: 16 additions & 13 deletions Cmdline/Action/Upgrade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public int RunCommand(CKAN.GameInstance instance, object raw_options)
try
{
var upd = new AutoUpdate();
var update = upd.GetUpdate(config.DevBuilds ?? false);
var update = upd.GetUpdate(config.DevBuilds ?? false, options.NetUserAgent);
var latestVersion = update.Version;
var currentVersion = new ModuleVersion(Meta.GetVersion());

Expand All @@ -107,7 +107,7 @@ public int RunCommand(CKAN.GameInstance instance, object raw_options)
if (user.RaiseYesNoDialog(Properties.Resources.UpgradeProceed))
{
user.RaiseMessage(Properties.Resources.UpgradePleaseWait);
upd.StartUpdateProcess(false, config.DevBuilds ?? false, user);
upd.StartUpdateProcess(false, options.NetUserAgent, config.DevBuilds ?? false, user);
}
}
else
Expand Down Expand Up @@ -138,15 +138,15 @@ public int RunCommand(CKAN.GameInstance instance, object raw_options)
}
else if (manager.Cache != null)
{
UpgradeModules(manager.Cache, user, instance, to_upgrade);
UpgradeModules(manager.Cache, options.NetUserAgent, user, instance, to_upgrade);
}
}
else
{
if (options.modules != null && manager.Cache != null)
{
Search.AdjustModulesCase(instance, registry, options.modules);
UpgradeModules(manager.Cache, user, instance, options.modules);
UpgradeModules(manager.Cache, options.NetUserAgent, user, instance, options.modules);
}
}
user.RaiseMessage("");
Expand Down Expand Up @@ -191,12 +191,13 @@ public int RunCommand(CKAN.GameInstance instance, object raw_options)
/// <param name="instance">Game instance to use</param>
/// <param name="modules">List of modules to upgrade</param>
private void UpgradeModules(NetModuleCache cache,
string? userAgent,
IUser user,
CKAN.GameInstance instance,
List<CkanModule> modules)
{
UpgradeModules(
cache, user, instance, repoData,
cache, userAgent, user, instance, repoData,
(ModuleInstaller installer, NetAsyncModulesDownloader downloader, RegistryManager regMgr, ref HashSet<string>? possibleConfigOnlyDirs) =>
installer.Upgrade(modules, downloader,
ref possibleConfigOnlyDirs,
Expand All @@ -212,12 +213,13 @@ private void UpgradeModules(NetModuleCache cache,
/// <param name="instance">Game instance to use</param>
/// <param name="identsAndVersions">List of identifier[=version] to upgrade</param>
private void UpgradeModules(NetModuleCache cache,
string? userAgent,
IUser user,
CKAN.GameInstance instance,
List<string> identsAndVersions)
{
UpgradeModules(
cache, user, instance, repoData,
cache, userAgent, user, instance, repoData,
(ModuleInstaller installer, NetAsyncModulesDownloader downloader, RegistryManager regMgr, ref HashSet<string>? possibleConfigOnlyDirs) =>
{
var crit = instance.VersionCriteria();
Expand Down Expand Up @@ -288,15 +290,16 @@ private static string UpTo(string orig, int pos)
/// <param name="attemptUpgradeCallback">Function to call to try to perform the actual upgrade, may throw TooManyModsProvideKraken</param>
/// <param name="addUserChoiceCallback">Function to call when the user has requested a new module added to the change set in response to TooManyModsProvideKraken</param>
private static void UpgradeModules(NetModuleCache cache,
IUser user,
CKAN.GameInstance instance,
RepositoryDataManager repoData,
AttemptUpgradeAction attemptUpgradeCallback,
Action<CkanModule> addUserChoiceCallback)
string? userAgent,
IUser user,
CKAN.GameInstance instance,
RepositoryDataManager repoData,
AttemptUpgradeAction attemptUpgradeCallback,
Action<CkanModule> addUserChoiceCallback)
{
using (TransactionScope transact = CkanTransaction.CreateTransactionScope()) {
var installer = new ModuleInstaller(instance, cache, user);
var downloader = new NetAsyncModulesDownloader(user, cache);
var installer = new ModuleInstaller(instance, cache, user, userAgent);
var downloader = new NetAsyncModulesDownloader(user, cache, userAgent);
var regMgr = RegistryManager.Instance(instance, repoData);
HashSet<string>? possibleConfigOnlyDirs = null;
bool done = false;
Expand Down
4 changes: 2 additions & 2 deletions Cmdline/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private static int Gui(GameInstanceManager manager, GuiOptions options, string[]
// GUI expects its first param to be an identifier, don't confuse it
GUI.GUI.Main_(args.Except(new string[] {"--verbose", "--debug", "--show-console", "--asroot"})
.ToArray(),
manager, options.ShowConsole);
options.NetUserAgent, manager, options.ShowConsole);

return Exit.OK;
}
Expand All @@ -312,7 +312,7 @@ private static int ConsoleUi(GameInstanceManager manager, ConsoleUIOptions opts)
LogManager.GetRepository().Threshold = Level.Warn;
return ConsoleUI.ConsoleUI.Main_(manager,
opts.Theme ?? Environment.GetEnvironmentVariable("CKAN_CONSOLEUI_THEME") ?? "default",
opts.Debug);
opts.NetUserAgent, opts.Debug);
}

private static int Version(IUser user)
Expand Down
6 changes: 0 additions & 6 deletions Cmdline/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,6 @@ public virtual int Handle(GameInstanceManager manager, IUser user)
Headless = true;
}

// Assign user-agent string if user has given us one
if (NetUserAgent != null)
{
Net.UserAgentString = NetUserAgent;
}

return Exit.OK;
}

Expand Down
8 changes: 6 additions & 2 deletions ConsoleUI/ConsoleCKAN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public class ConsoleCKAN {
/// Starts with a splash screen, then instance selection if no default,
/// then list of mods.
/// </summary>
public ConsoleCKAN(GameInstanceManager? mgr, string? themeName, bool debug)
public ConsoleCKAN(GameInstanceManager? mgr,
string? themeName,
string? userAgent,
bool debug)
{
if (ConsoleTheme.Themes.TryGetValue(themeName ?? "default", out ConsoleTheme? theme))
{
Expand All @@ -40,12 +43,13 @@ public ConsoleCKAN(GameInstanceManager? mgr, string? themeName, bool debug)
manager.GetPreferredInstance();
} else {
// Multiple instances, no default, pick one
new GameInstanceListScreen(theme, manager, repoData).Run();
new GameInstanceListScreen(theme, manager, repoData, userAgent).Run();
}
}
if (manager.CurrentInstance != null) {
new ModListScreen(theme, manager, repoData,
RegistryManager.Instance(manager.CurrentInstance, repoData),
userAgent,
manager.CurrentInstance.game,
debug).Run();
}
Expand Down
Loading