using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gameloop.Vdf;
using Gameloop.Vdf.Linq;
using SteamTool.Core.Common;
namespace SteamTool.Core
{
public class VdfService
{
///
/// 获取VDF类型内容
///
/// vdf文件内容
///
public VProperty GetVdfModel(string text)
{
return VdfConvert.Deserialize(text);
}
///
/// 根据路径获取VDF类型内容
///
/// vdf文件路径
///
public dynamic GetVdfModelByPath(string path)
{
string text = File.ReadAllText(path, Encoding.UTF8);
return GetVdfModel(text);
}
public void UpdateVdfValueByReplace(string path, string oldVaule, string newValue)
{
string text = File.ReadAllText(path, Encoding.UTF8);
text = text.RemovePattern().Replace(
oldVaule.RemovePattern().Replace(" ",string.Empty), newValue);
//Debug.WriteLine(text);
File.WriteAllText(path, text, Encoding.UTF8);
}
public void DeleteVdfValueByKey(string path, string key)
{
string text = File.ReadAllText(path, Encoding.UTF8);
//如此实现的功能非常简单,但是应付嵌套的格式时会有问题
var index = text.IndexOf($"\"{key}\"");
var lastIndex = text.IndexOf('}', index);
text = text.Remove(index, lastIndex - index + 1);
//Debug.WriteLine(text);
File.WriteAllText(path, text, Encoding.UTF8);
}
public void DeleteVdfValueByReplace(string path, string oldVaule)
{
string text = File.ReadAllText(path, Encoding.UTF8);
text = text.Replace(oldVaule, string.Empty);
//Debug.WriteLine(text);
File.WriteAllText(path, text, Encoding.UTF8);
}
}
}