Compare commits
11 Commits
5ddd421947
...
main
Author | SHA1 | Date | |
---|---|---|---|
7cea589294
|
|||
ad15422700
|
|||
6e3450fef2
|
|||
47e80bfebc
|
|||
17f78685fc
|
|||
bfe24d1e4b
|
|||
5cd01a6375
|
|||
a11b29a21d
|
|||
2aeef0227c
|
|||
6b2c87c031
|
|||
539a0cb4a3
|
@@ -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
|
||||
|
7
JackCraft.Config.sln.DotSettings.user
Normal file
7
JackCraft.Config.sln.DotSettings.user
Normal file
@@ -0,0 +1,7 @@
|
||||
<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_003ACastHelpers_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F6d2342c247c64eb7a9b3702e481e4443f83e00_003F54_003Ff2c78e92_003FCastHelpers_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<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>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJsonSerializerInternalReader_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F7e62198beab24380bbac29171862d1d8adf10_003F90_003F0d01fe57_003FJsonSerializerInternalReader_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJsonSerializerInternalReader_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F29ea32a76b2a6b8eb246f247df6a6a86d887384b4c31db2a35effccdd86c24_003FJsonSerializerInternalReader_002Ecs_002Fz_003A2_002D1/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>
|
135
JackCraft.Config/Config.cs
Normal file
135
JackCraft.Config/Config.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
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 = new();
|
||||
|
||||
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)
|
||||
{
|
||||
return Get<T>(key, default);
|
||||
}
|
||||
|
||||
public T? Get<T>(string key, T? defaultValue)
|
||||
{
|
||||
LoadConfigData();
|
||||
var keys = key.Split('.');
|
||||
var currentData = _configData;
|
||||
for (var i = 0; i < keys.Length - 1; i++)
|
||||
{
|
||||
if (!currentData.TryGetValue(keys[i], out var tValue))
|
||||
{
|
||||
Set(key, defaultValue);
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
switch (tValue)
|
||||
{
|
||||
case JObject jObject:
|
||||
currentData = jObject.ToObject<Dictionary<string, object>>() ?? new Dictionary<string, object>();
|
||||
break;
|
||||
case Dictionary<string, object> nestedDict:
|
||||
currentData = nestedDict;
|
||||
break;
|
||||
default:
|
||||
Set(key, defaultValue);
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentData.TryGetValue(keys[^1], out var result) && result is T typedResult) return typedResult;
|
||||
|
||||
Set(key, defaultValue);
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public void Set<T>(string key, T? value)
|
||||
{
|
||||
LoadConfigData();
|
||||
var keys = key.Split('.');
|
||||
var currentData = _configData;
|
||||
|
||||
for (var i = 0; i < keys.Length - 1; i++)
|
||||
{
|
||||
var currentKey = keys[i];
|
||||
|
||||
if (!currentData.TryGetValue(currentKey, out var tValue))
|
||||
{
|
||||
tValue = new Dictionary<string, object>();
|
||||
currentData[currentKey] = tValue;
|
||||
}
|
||||
|
||||
if (tValue is JObject jObject)
|
||||
currentData[currentKey] =
|
||||
jObject.ToObject<Dictionary<string, object>>() ?? new Dictionary<string, object>();
|
||||
|
||||
currentData = (Dictionary<string, object>)currentData[currentKey];
|
||||
}
|
||||
|
||||
var finalKey = keys[^1];
|
||||
currentData[finalKey] = value!;
|
||||
|
||||
var json = JsonConvert.SerializeObject(_configData, Formatting.Indented);
|
||||
File.WriteAllText(_configFile.FullName, json);
|
||||
}
|
||||
|
||||
public void Delete(string key)
|
||||
{
|
||||
LoadConfigData();
|
||||
var keys = key.Split('.');
|
||||
var currentData = _configData;
|
||||
|
||||
for (var i = 0; i < keys.Length - 1; i++)
|
||||
{
|
||||
var currentKey = keys[i];
|
||||
|
||||
if (!currentData.TryGetValue(currentKey, out var value)) return;
|
||||
|
||||
if (value is JObject jObject)
|
||||
currentData[currentKey] =
|
||||
jObject.ToObject<Dictionary<string, object>>() ?? new Dictionary<string, object>();
|
||||
|
||||
currentData = (Dictionary<string, object>)currentData[currentKey];
|
||||
}
|
||||
|
||||
var finalKey = keys[^1];
|
||||
currentData.Remove(finalKey);
|
||||
|
||||
var json = JsonConvert.SerializeObject(_configData, Formatting.Indented);
|
||||
File.WriteAllText(_configFile.FullName, json);
|
||||
}
|
||||
}
|
@@ -7,4 +7,8 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
46
Readme.MD
46
Readme.MD
@@ -1,3 +1,49 @@
|
||||
# JackCraft.Config
|
||||
|
||||
一个自己觉得怎么好用怎么写的配置文件库
|
||||
|
||||
## 使用方法
|
||||
|
||||
### 获取配置文件
|
||||
|
||||
> 配置文件不存在时会自动创建一个空 Json 文件,但是若配置文件不是 Json 标准格式将会在写入和读取键值时抛出异常
|
||||
|
||||
```csharp
|
||||
// 标准示例: var config = new Config(new FileInfo(配置文件路径));
|
||||
var config = new Config(new FileInfo("./config.json"));
|
||||
```
|
||||
|
||||
### 读取键值
|
||||
|
||||
> 若键不存在则返回默认值,并且同时也会写入键值(若没有提供默认值则写入空)
|
||||
|
||||
```csharp
|
||||
// 标准示例: var value = config.Get<T>(键, <可选>默认值);
|
||||
|
||||
// {
|
||||
// "114514": "1919810"
|
||||
// }
|
||||
var value = config.Get("114514", "1919810");
|
||||
var value = config.Get<string>("114514");
|
||||
|
||||
// {
|
||||
// "abc": {
|
||||
// "def": 1234
|
||||
// }
|
||||
// }
|
||||
var value = config.Get("abc.def", 1234);
|
||||
var value = config.Get<int>("abc.def");
|
||||
```
|
||||
|
||||
### 写入键值
|
||||
|
||||
```csharp
|
||||
// 标准示例: config.Set<T>(键, 值);
|
||||
|
||||
// {
|
||||
// "114514": "1919810"
|
||||
// }
|
||||
config.Set("114514", "1919810");
|
||||
|
||||
// 其他类型与 Get<T> 同理...
|
||||
```
|
||||
|
29
Test/Program.cs
Normal file
29
Test/Program.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
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 a = config.Get("aaa.www.www", "555");
|
||||
var b = config.Get("aaa.bbb", "1");
|
||||
var c = config.Get("aaa.sss", "1");
|
||||
var d = config.Get("bbb.sss", "1");
|
||||
|
||||
config.Delete("aaa.www");
|
||||
}
|
||||
}
|
||||
|
||||
public class TestClass
|
||||
{
|
||||
public string? Test { get; set; }
|
||||
public int Abc { get; set; }
|
||||
}
|
15
Test/Test.csproj
Normal file
15
Test/Test.csproj
Normal 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>
|
Reference in New Issue
Block a user