Files
UnrealEngineUWP/Engine/Source/Programs/Shared/EpicGames.Core/RegistryConfigurationSource.cs
ben marsh 16989e491d Horde: Various agent configuration changes.
* Agent config now contains an "Installed" setting similar to the server, which defaults to true but is forced to false by appsettings.Local.json.
* In installed builds, we now read a config file called agent.json from C:\ProgramData\Epic\Horde\Agent.
* Config settings are now also read from the registry, under "HKLM\Software\Epic Games\Horde\Agent". The agent installer allows configuring the sandbox directory during installation, and stores the working directory here.
* FileReference and DirectoryReference now have type converters to allow serializing to/from strings.

#jira UE-206047
#jira UE-206049

[CL 31267867 by ben marsh in ue5-main branch]
2024-02-07 14:57:18 -05:00

87 lines
2.5 KiB
C#

// Copyright Epic Games, Inc. All Rights Reserved.
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using Microsoft.Extensions.Configuration;
using Microsoft.Win32;
namespace EpicGames.Core
{
/// <summary>
/// Configuration source which reads values from the Registry on Windows
/// </summary>
[SupportedOSPlatform("windows")]
public class RegistryConfigurationSource : IConfigurationSource
{
readonly RegistryKey _baseKey;
readonly string _keyPath;
readonly string _baseConfigName;
/// <summary>
/// Constructor
/// </summary>
/// <param name="baseKey">Hive to resolve keyPath relative to</param>
/// <param name="keyPath">Path within the registry to enumerate</param>
/// <param name="baseConfigName">Prefix for returned configuration values</param>
public RegistryConfigurationSource(RegistryKey baseKey, string keyPath, string baseConfigName)
{
_baseKey = baseKey;
_keyPath = keyPath;
_baseConfigName = baseConfigName;
}
/// <inheritdoc/>
public IConfigurationProvider Build(IConfigurationBuilder builder)
=> new RegistryConfigProvider(_baseKey, _keyPath, _baseConfigName);
}
class RegistryConfigProvider : ConfigurationProvider
{
readonly RegistryKey _baseKey;
readonly string _keyPath;
readonly string _baseConfigName;
public RegistryConfigProvider(RegistryKey baseKey, string keyPath, string baseConfigName)
{
_baseKey = baseKey;
_keyPath = keyPath;
_baseConfigName = baseConfigName;
}
public override void Load()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Dictionary<string, string?> data = new Dictionary<string, string?>();
GetValues(_baseKey, _keyPath, _baseConfigName, data);
Data = data;
}
}
[SupportedOSPlatform("windows")]
static void GetValues(RegistryKey baseKey, string keyPath, string baseConfigName, Dictionary<string, string?> data)
{
using RegistryKey? registryKey = baseKey.OpenSubKey(keyPath);
if (registryKey != null)
{
string[] subKeyNames = registryKey.GetSubKeyNames();
foreach (string subKeyName in subKeyNames)
{
GetValues(registryKey, subKeyName, $"{baseConfigName}:{subKeyName}", data);
}
string[] valueNames = registryKey.GetValueNames();
foreach (string valueName in valueNames)
{
object? value = registryKey.GetValue(valueName);
if (value != null)
{
data[$"{baseConfigName}:{valueName}"] = value.ToString();
}
}
}
}
}
}