2024-10-03 13:27:26 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.ServiceProcess;
|
|
|
|
|
|
|
|
|
|
namespace WelsonJS.Service
|
|
|
|
|
{
|
|
|
|
|
public class UserVariables
|
|
|
|
|
{
|
|
|
|
|
private ServiceMain parent;
|
|
|
|
|
private Dictionary<string, string> userVariables;
|
|
|
|
|
private readonly string envFilePath = Path.Combine(Path.GetTempPath(), "welsonjs_default.env");
|
|
|
|
|
|
|
|
|
|
public UserVariables(ServiceBase parent)
|
|
|
|
|
{
|
|
|
|
|
this.parent = (ServiceMain)parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load user-defined variables from the temporary folder in .env format
|
|
|
|
|
public void Load()
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(envFilePath))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string fileContent = File.ReadAllText(envFilePath);
|
|
|
|
|
// Split based on new line characters
|
|
|
|
|
string[] keyValuePairs = fileContent.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
userVariables = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
foreach (string pair in keyValuePairs)
|
|
|
|
|
{
|
|
|
|
|
// Split by the first occurrence of '='
|
|
|
|
|
int indexOfEquals = pair.IndexOf('=');
|
|
|
|
|
if (indexOfEquals != -1)
|
|
|
|
|
{
|
|
|
|
|
string key = pair.Substring(0, indexOfEquals).Trim();
|
|
|
|
|
string value = pair.Substring(indexOfEquals + 1).Trim();
|
|
|
|
|
|
|
|
|
|
// Remove surrounding quotes if present
|
|
|
|
|
if (value.StartsWith("\"") && value.EndsWith("\""))
|
|
|
|
|
{
|
|
|
|
|
value = value.Substring(1, value.Length - 2); // Remove the first and last character
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unescape double quotes in the value
|
|
|
|
|
value = value.Replace("\\\"", "\"");
|
|
|
|
|
|
|
|
|
|
userVariables[key] = value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-11-01 18:51:07 +00:00
|
|
|
|
throw new Exception($"Error parsing line: '{pair}'.");
|
2024-10-03 13:27:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-11-01 18:51:07 +00:00
|
|
|
|
Console.WriteLine($"Error loading variable file: {ex.Message}");
|
2024-10-03 13:27:26 +00:00
|
|
|
|
userVariables = new Dictionary<string, string>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
userVariables = new Dictionary<string, string>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetValue(string name)
|
|
|
|
|
{
|
|
|
|
|
userVariables.TryGetValue(name, out string value);
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2024-10-03 13:56:04 +00:00
|
|
|
|
|
|
|
|
|
public string GetEnvFilePath()
|
|
|
|
|
{
|
|
|
|
|
return envFilePath;
|
|
|
|
|
}
|
2024-10-03 13:27:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|