auto-creamapi-2/auto-creamapi/Services/CreamConfigService.cs

199 lines
6.6 KiB
C#
Raw Normal View History

using System;
2020-12-19 19:14:40 -05:00
using System.Collections.Generic;
using System.Collections.ObjectModel;
2020-12-19 19:14:40 -05:00
using System.IO;
using System.Linq;
2020-12-19 19:14:40 -05:00
using System.Text;
using System.Text.RegularExpressions;
using auto_creamapi.Models;
2020-12-19 19:14:40 -05:00
using auto_creamapi.Utils;
using IniParser;
using IniParser.Model;
namespace auto_creamapi.Services
2020-12-19 19:14:40 -05:00
{
public interface ICreamConfigService
2020-12-19 19:14:40 -05:00
{
public CreamConfig Config { get; }
public void Initialize();
public void ReadFile(string configFilePath);
public void SaveFile();
public void SetConfigData(int appId,
string language,
bool unlockAll,
bool extraProtection,
bool forceOffline,
string dlcList);
public void SetConfigData(int appId,
string language,
bool unlockAll,
bool extraProtection,
bool forceOffline,
List<SteamApp> dlcList);
public void SetConfigData(int appId,
string language,
bool unlockAll,
bool extraProtection,
bool forceOffline,
IEnumerable<SteamApp> dlcList);
public bool ConfigExists();
}
public class CreamConfigService : ICreamConfigService
{
2020-12-19 19:14:40 -05:00
private string _configFilePath;
public CreamConfig Config { get; private set; }
public void Initialize()
2020-12-19 19:14:40 -05:00
{
//await Task.Run(() =>
//{
//MyLogger.Log.Debug("CreamConfigService: init start");
Config = new CreamConfig();
ResetConfigData();
//MyLogger.Log.Debug("CreamConfigService: init end");
//});
2020-12-19 19:14:40 -05:00
}
public void ReadFile(string configFilePath)
{
_configFilePath = configFilePath;
if (File.Exists(configFilePath))
{
MyLogger.Log.Information("Config file found @ {ConfigFilePath}, parsing...", configFilePath);
2020-12-19 19:14:40 -05:00
var parser = new FileIniDataParser();
var data = parser.ReadFile(_configFilePath, Encoding.UTF8);
ResetConfigData(); // clear previous config data
Config.AppId = Convert.ToInt32(data["steam"]["appid"]);
Config.Language = data["steam"]["language"];
Config.UnlockAll = Convert.ToBoolean(data["steam"]["unlockall"]);
Config.ExtraProtection = Convert.ToBoolean(data["steam"]["extraprotection"]);
Config.ForceOffline = Convert.ToBoolean(data["steam"]["forceoffline"]);
2020-12-19 19:14:40 -05:00
var dlcCollection = data["dlc"];
foreach (var item in dlcCollection)
//Config.DlcList.Add(int.Parse(item.KeyName), item.Value);
Config.DlcList.Add(new SteamApp {AppId = int.Parse(item.KeyName), Name = item.Value});
2020-12-19 19:14:40 -05:00
}
else
{
MyLogger.Log.Information("Config file does not exist @ {ConfigFilePath}, skipping...", configFilePath);
ResetConfigData();
2020-12-19 19:14:40 -05:00
}
}
public void SaveFile()
{
var parser = new FileIniDataParser();
var data = new IniData();
data["steam"]["appid"] = Config.AppId.ToString();
data["steam"]["language"] = Config.Language;
data["steam"]["unlockall"] = Config.UnlockAll.ToString();
data["steam"]["extraprotection"] = Config.ExtraProtection.ToString();
data["steam"]["forceoffline"] = Config.ForceOffline.ToString();
2020-12-19 19:14:40 -05:00
data.Sections.AddSection("dlc");
Config.DlcList.ForEach(x => data["dlc"].AddKey(x.AppId.ToString(), x.Name));
/*foreach (var steamApp in Config.DlcList)
2020-12-19 19:14:40 -05:00
{
data["dlc"].AddKey(key.ToString(), value);
}*/
2020-12-19 19:14:40 -05:00
parser.WriteFile(_configFilePath, data, Encoding.UTF8);
}
public void SetConfigData(int appId,
string language,
bool unlockAll,
bool extraProtection,
bool forceOffline,
string dlcList)
{
Config.AppId = appId;
Config.Language = language;
Config.UnlockAll = unlockAll;
Config.ExtraProtection = extraProtection;
Config.ForceOffline = forceOffline;
2020-12-19 19:14:40 -05:00
SetDlcFromString(dlcList);
}
public void SetConfigData(int appId,
2020-12-19 19:14:40 -05:00
string language,
bool unlockAll,
bool extraProtection,
bool forceOffline,
List<SteamApp> dlcList)
2020-12-19 19:14:40 -05:00
{
Config.AppId = appId;
Config.Language = language;
Config.UnlockAll = unlockAll;
Config.ExtraProtection = extraProtection;
Config.ForceOffline = forceOffline;
Config.DlcList = dlcList;
}
public void SetConfigData(int appId,
string language,
bool unlockAll,
bool extraProtection,
bool forceOffline,
IEnumerable<SteamApp> dlcList)
{
Config.AppId = appId;
Config.Language = language;
Config.UnlockAll = unlockAll;
Config.ExtraProtection = extraProtection;
Config.ForceOffline = forceOffline;
Config.DlcList = new List<SteamApp>(dlcList);
}
public bool ConfigExists()
{
return File.Exists(_configFilePath);
}
private void ResetConfigData()
{
Config.AppId = -1;
Config.Language = Misc.DefaultLanguageSelection;
Config.UnlockAll = false;
Config.ExtraProtection = false;
Config.ForceOffline = false;
Config.DlcList.Clear();
}
2020-12-19 19:14:40 -05:00
private void SetDlcFromString(string dlcList)
{
Config.DlcList.Clear();
2020-12-19 19:14:40 -05:00
var expression = new Regex(@"(?<id>.*) *= *(?<name>.*)");
using var reader = new StringReader(dlcList);
string line;
while ((line = reader.ReadLine()) != null)
{
var match = expression.Match(line);
if (match.Success)
Config.DlcList.Add(
new SteamApp {AppId = int.Parse(match.Groups["id"].Value), Name = match.Groups["name"].Value});
2020-12-19 19:14:40 -05:00
}
}
/*private void SetDlcFromAppList(List<SteamApp> dlcList)
2020-12-19 19:14:40 -05:00
{
Config.DlcList.Clear();
dlcList.ForEach(x => Config.DlcList.Add(x.AppId, x.Name));
2020-12-19 19:14:40 -05:00
}*/
public override string ToString()
{
var str = $"INI file: {_configFilePath}\n{Config}";
2020-12-19 19:14:40 -05:00
return str;
}
}
}