mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-10-27 19:11:18 +00:00
Updated ICompatibleLogger to accept params object[] for flexible logging. Refactored TraceLogger to support the new interface and improved formatting. Added JsNative.cs to encapsulate ChakraCore P/Invoke interop, and updated JsCore to use JsNative for all native calls. Modified all resource tools to accept and use ICompatibleLogger for consistent logging. Updated project file to include new and updated sources.
86 lines
2.8 KiB
C#
86 lines
2.8 KiB
C#
// Settings.cs
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// SPDX-FileCopyrightText: 2025 Catswords OSS and WelsonJS Contributors
|
|
// https://github.com/gnh1201/welsonjs
|
|
//
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Reflection;
|
|
using System.Resources;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
|
|
namespace WelsonJS.Launcher.ResourceTools
|
|
{
|
|
public class Settings : IResourceTool
|
|
{
|
|
private readonly ResourceServer Server;
|
|
private readonly HttpClient _httpClient;
|
|
private readonly ICompatibleLogger _logger;
|
|
private const string Prefix = "settings";
|
|
|
|
public Settings(ResourceServer server, HttpClient httpClient, ICompatibleLogger logger)
|
|
{
|
|
Server = server;
|
|
|
|
_httpClient = httpClient;
|
|
_logger = logger;
|
|
}
|
|
|
|
public bool CanHandle(string path)
|
|
{
|
|
return path.Equals(Prefix, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
public async Task HandleAsync(HttpListenerContext context, string path)
|
|
{
|
|
await Task.Delay(0);
|
|
|
|
// Get current namespace (e.g., WelsonJS.Launcher)
|
|
string ns = typeof(Program).Namespace;
|
|
|
|
// Build resource base name (e.g., WelsonJS.Launcher.Properties.Resources)
|
|
string resourceBaseName = ns + ".Properties.Resources";
|
|
|
|
// Load resource strings using ResourceManager
|
|
var resourceManager = new ResourceManager(resourceBaseName, Assembly.GetExecutingAssembly());
|
|
var resourceSet = resourceManager.GetResourceSet(
|
|
System.Globalization.CultureInfo.CurrentCulture,
|
|
true,
|
|
true
|
|
);
|
|
|
|
var resourceStrings = new Dictionary<string, string>();
|
|
foreach (System.Collections.DictionaryEntry entry in resourceSet)
|
|
{
|
|
if (entry.Value is string strValue)
|
|
{
|
|
resourceStrings[(string)entry.Key] = strValue;
|
|
}
|
|
}
|
|
|
|
// Load settings from app.config
|
|
var appConfig = ConfigurationManager.AppSettings.AllKeys
|
|
.ToDictionary(k => k, k => ConfigurationManager.AppSettings[k]);
|
|
|
|
// Merge by starting with resourceStrings and letting app.config override
|
|
var finalConfig = new Dictionary<string, string>(resourceStrings);
|
|
foreach (var kv in appConfig)
|
|
{
|
|
finalConfig[kv.Key] = kv.Value;
|
|
}
|
|
|
|
// Generate XML using XElement
|
|
XElement xml = new XElement("settings",
|
|
finalConfig.Select(kv => new XElement(kv.Key, kv.Value))
|
|
);
|
|
|
|
Server.ServeResource(context, xml.ToString(), "application/xml");
|
|
}
|
|
}
|
|
}
|