Compare commits

..

3 Commits

Author SHA1 Message Date
2aeef0227c feat: 添加测试项目并更新解决方案配置 2025-01-21 17:47:23 +08:00
6b2c87c031 feat: 添加配置文件管理功能 2025-01-21 17:47:06 +08:00
539a0cb4a3 build: 添加 Newtonsoft.Json 依赖 2025-01-21 17:46:45 +08:00
6 changed files with 135 additions and 0 deletions

View File

@@ -8,6 +8,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Folder", "Solution
Readme.MD = Readme.MD
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -18,5 +20,9 @@ Global
{96DD0E6E-8CE2-42DE-87A9-111D9198CE76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96DD0E6E-8CE2-42DE-87A9-111D9198CE76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96DD0E6E-8CE2-42DE-87A9-111D9198CE76}.Release|Any CPU.Build.0 = Release|Any CPU
{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AExceptionDispatchInfo_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fbf9021a960b74107a7e141aa06bc9d8a0a53c929178c2fb95b1597be8af8dc_003FExceptionDispatchInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFileInfo_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F2610c78ec589e1aacd82c4428835c427b838e8156a6f346f517d28ef9ffbb9a2_003FFileInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFile_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F6d2342c247c64eb7a9b3702e481e4443f83e00_003Fa1_003F11a17405_003FFile_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>

View File

@@ -0,0 +1,82 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace JackCraft.Config;
public class Config
{
private readonly FileInfo _configFile;
public readonly string ConfigFileName;
public readonly string ConfigFilePath;
private Dictionary<string, object> _configData = null!;
public Config(FileInfo configFile)
{
_configFile = configFile;
ConfigFilePath = configFile.DirectoryName ?? string.Empty;
ConfigFileName = configFile.Name;
if (!configFile.Exists)
{
configFile.Directory?.Create();
using var stream = configFile.Create();
File.WriteAllText(configFile.FullName, "{}");
}
LoadConfigData();
}
private void LoadConfigData()
{
if (File.Exists(_configFile.FullName))
{
var existingJson = File.ReadAllText(_configFile.FullName);
_configData = JsonConvert.DeserializeObject<Dictionary<string, object>>(existingJson) ??
new Dictionary<string, object>();
}
else
{
_configData = new Dictionary<string, object>();
}
}
public T? Get<T>(string key, T? defaultValue)
{
var keys = key.Split('.');
var currentData = _configData;
for (var i = 0; i < keys.Length - 1; i++)
{
if (!currentData.TryGetValue(keys[i], out var tValue)) return defaultValue;
if (tValue is JObject jObject)
currentData = jObject.ToObject<Dictionary<string, object>>();
else if (tValue is Dictionary<string, object> nestedDict)
currentData = nestedDict;
else
return defaultValue;
}
if (currentData.TryGetValue(keys[^1], out var result) && result is T typedResult) return typedResult;
return defaultValue;
}
public void Set<T>(string key, T? value)
{
var keys = key.Split('.');
var currentData = _configData;
for (var i = 0; i < keys.Length - 1; i++)
{
if (!currentData.TryGetValue(keys[i], out var tValue) ||
tValue is not Dictionary<string, object> nestedDict)
{
nestedDict = new Dictionary<string, object>();
currentData[keys[i]] = nestedDict;
}
currentData = nestedDict;
}
currentData[keys[^1]] = (object?)value ?? string.Empty;
var json = JsonConvert.SerializeObject(_configData, Formatting.Indented);
File.WriteAllText(_configFile.FullName, json);
}
}

View File

@@ -7,4 +7,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
</ItemGroup>
</Project>

24
Test/Program.cs Normal file
View File

@@ -0,0 +1,24 @@
using JackCraft.Config;
namespace Test;
internal static class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
var testClass = new TestClass
{
Test = "test",
Abc = 123
};
var config = new Config(new FileInfo("./config.json"));
var t = config.Get("aaa.www.aaa.ccc", "1");
}
}
public class TestClass
{
public string? Test { get; set; }
public int Abc { get; set; }
}

15
Test/Test.csproj Normal file
View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latestmajor</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\JackCraft.Config\JackCraft.Config.csproj"/>
</ItemGroup>
</Project>