LANCommander/LANCommander.SDK/ActionService.cs
Pat Hartl 0732e5d564
Some checks failed
LANCommander Playnite Plugin / build (push) Has been cancelled
LANCommander Release / build (push) Has been cancelled
LANCommander / build (push) Has been cancelled
Don't replace slashes in action arguments as some may use something other than the path separator char
2024-01-26 01:56:21 -06:00

104 lines
2.8 KiB
C#

using LANCommander.SDK.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Net;
using System.Text;
namespace LANCommander.SDK
{
internal class ActionVariables
{
private readonly Client Client;
public ActionVariables(Client client) {
Client = client;
}
public string ServerAddress
{
get
{
return Client.GetServerAddress();
}
}
public string IPXRelayHost
{
get
{
var host = Client.Settings.IPXRelayHost;
if (String.IsNullOrWhiteSpace(host))
{
var serverAddress = new Uri(Client.GetServerAddress());
host = serverAddress.DnsSafeHost;
}
var entry = Dns.GetHostEntry(host);
if (entry.AddressList.Length > 0)
host = entry.AddressList.First().ToString();
return host;
}
}
public int IPXRelayPort
{
get
{
return Client.Settings.IPXRelayPort;
}
}
}
public class ActionService
{
private readonly Client Client;
private readonly ActionVariables ActionVariables;
private Dictionary<string, string> CustomVariables { get; set; }
public ActionService(Client client)
{
Client = client;
ActionVariables = new ActionVariables(Client);
CustomVariables = new Dictionary<string, string>();
}
public void AddVariable(string key, string value)
{
CustomVariables[key] = value;
}
public string ExpandVariables(string input, string installDirectory, Dictionary<string, string> additionalVariables = null, bool skipSlashes = false)
{
if (input == null)
return input;
var variables = typeof(ActionVariables).GetProperties();
foreach (var variable in variables)
{
if (input.Contains($"{{{variable.Name}}}"))
input = input.Replace($"{{{variable.Name}}}", $"{variable.GetValue(ActionVariables)}");
}
foreach (var variable in CustomVariables)
{
input = input.Replace($"{{{variable.Key}}}", variable.Value);
}
if (additionalVariables != null)
foreach (var variable in additionalVariables)
{
input = input.Replace($"{{{variable.Key}}}", variable.Value);
}
return input.ExpandEnvironmentVariables(installDirectory, skipSlashes);
}
}
}