@@ -1,43 +0,0 @@
|
|||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
using Lagrange.Core.Common;
|
|
||||||
|
|
||||||
namespace Shrink.Utility;
|
|
||||||
|
|
||||||
public static class Data
|
|
||||||
{
|
|
||||||
public static void SaveKeystore(BotKeystore keystore) =>
|
|
||||||
File.WriteAllText("Keystore.json", JsonSerializer.Serialize(keystore));
|
|
||||||
|
|
||||||
public static BotDeviceInfo GetDeviceInfo()
|
|
||||||
{
|
|
||||||
if (File.Exists("DeviceInfo.json"))
|
|
||||||
{
|
|
||||||
var info = JsonSerializer.Deserialize<BotDeviceInfo>(File.ReadAllText("DeviceInfo.json"));
|
|
||||||
if (info != null) return info;
|
|
||||||
|
|
||||||
info = BotDeviceInfo.GenerateInfo();
|
|
||||||
File.WriteAllText("DeviceInfo.json", JsonSerializer.Serialize(info));
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
|
|
||||||
var deviceInfo = BotDeviceInfo.GenerateInfo();
|
|
||||||
File.WriteAllText("DeviceInfo.json", JsonSerializer.Serialize(deviceInfo));
|
|
||||||
return deviceInfo;
|
|
||||||
}
|
|
||||||
public static BotKeystore? LoadKeystore()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var text = File.ReadAllText("Keystore.json");
|
|
||||||
return JsonSerializer.Deserialize<BotKeystore>(text, new JsonSerializerOptions()
|
|
||||||
{
|
|
||||||
ReferenceHandler = ReferenceHandler.Preserve
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
48
Utility/JsonUtility.cs
Normal file
48
Utility/JsonUtility.cs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace Shrink.Utility;
|
||||||
|
|
||||||
|
public static class JsonUtility
|
||||||
|
{
|
||||||
|
public static void WriteJsonToFile<T>(string filePath, T data)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var json = JsonSerializer.Serialize(data);
|
||||||
|
File.WriteAllText(filePath, json);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Console.WriteLine($"写入文件出错: {filePath}: {ex.Message}");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T ReadJsonFromFile<T>(string filePath)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var json = File.ReadAllText(filePath);
|
||||||
|
return JsonSerializer.Deserialize<T>(json,
|
||||||
|
new JsonSerializerOptions { ReferenceHandler = ReferenceHandler.Preserve })!;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Console.WriteLine($"读取文件出错: {filePath}: {ex.Message}");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T ReadOrCreateJsonFile<T>(string filePath, Func<T> createFunc)
|
||||||
|
{
|
||||||
|
if (File.Exists(filePath))
|
||||||
|
{
|
||||||
|
return ReadJsonFromFile<T>(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
var newData = createFunc();
|
||||||
|
WriteJsonToFile(filePath, newData);
|
||||||
|
return newData;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user