2020-12-25 10:49:51 -05:00
|
|
|
using System;
|
2020-12-19 19:14:40 -05:00
|
|
|
using System.Collections.Generic;
|
2020-12-28 09:27:37 -05:00
|
|
|
using System.Collections.ObjectModel;
|
2020-12-19 19:14:40 -05:00
|
|
|
using System.IO;
|
2020-12-25 10:49:51 -05:00
|
|
|
using System.Linq;
|
2020-12-19 19:14:40 -05:00
|
|
|
using System.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
2020-12-25 10:49:51 -05:00
|
|
|
using auto_creamapi.Models;
|
2020-12-19 19:14:40 -05:00
|
|
|
using auto_creamapi.Utils;
|
|
|
|
using IniParser;
|
|
|
|
using IniParser.Model;
|
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
namespace auto_creamapi.Services
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2020-12-25 10:49:51 -05:00
|
|
|
public interface ICreamConfigService
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2020-12-25 10:49:51 -05:00
|
|
|
public CreamConfig Config { get; }
|
2020-12-28 09:27:37 -05:00
|
|
|
public void Initialize();
|
2020-12-25 10:49:51 -05:00
|
|
|
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);
|
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
public void SetConfigData(int appId,
|
|
|
|
string language,
|
|
|
|
bool unlockAll,
|
|
|
|
bool extraProtection,
|
|
|
|
bool forceOffline,
|
2021-01-13 07:04:35 -05:00
|
|
|
IEnumerable<SteamApp> dlcList);
|
2020-12-25 10:49:51 -05:00
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
public bool ConfigExists();
|
2020-12-23 06:34:31 -05:00
|
|
|
}
|
2020-12-25 10:49:51 -05:00
|
|
|
|
|
|
|
public class CreamConfigService : ICreamConfigService
|
2020-12-23 06:34:31 -05:00
|
|
|
{
|
2020-12-19 19:14:40 -05:00
|
|
|
private string _configFilePath;
|
|
|
|
|
2021-01-13 07:04:35 -05:00
|
|
|
public CreamConfig Config { get; private set; }
|
2020-12-28 09:27:37 -05:00
|
|
|
|
|
|
|
public void Initialize()
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2020-12-28 09:27:37 -05:00
|
|
|
//await Task.Run(() =>
|
|
|
|
//{
|
|
|
|
//MyLogger.Log.Debug("CreamConfigService: init start");
|
2020-12-23 06:34:31 -05:00
|
|
|
Config = new CreamConfig();
|
|
|
|
ResetConfigData();
|
2020-12-28 09:27:37 -05:00
|
|
|
//MyLogger.Log.Debug("CreamConfigService: init end");
|
|
|
|
//});
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ReadFile(string configFilePath)
|
|
|
|
{
|
|
|
|
_configFilePath = configFilePath;
|
2020-12-25 10:49:51 -05:00
|
|
|
if (File.Exists(configFilePath))
|
|
|
|
{
|
2020-12-19 19:14:40 -05:00
|
|
|
MyLogger.Log.Information($"Config file found @ {configFilePath}, parsing...");
|
|
|
|
var parser = new FileIniDataParser();
|
|
|
|
var data = parser.ReadFile(_configFilePath, Encoding.UTF8);
|
|
|
|
|
2020-12-23 06:34:31 -05:00
|
|
|
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)
|
2020-12-25 10:49:51 -05:00
|
|
|
//Config.DlcList.Add(int.Parse(item.KeyName), item.Value);
|
2020-12-28 09:27:37 -05:00
|
|
|
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...");
|
2020-12-23 06:34:31 -05:00
|
|
|
ResetConfigData();
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SaveFile()
|
|
|
|
{
|
|
|
|
var parser = new FileIniDataParser();
|
|
|
|
var data = new IniData();
|
|
|
|
|
2020-12-23 06:34:31 -05:00
|
|
|
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");
|
2020-12-25 10:49:51 -05:00
|
|
|
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-25 10:49:51 -05:00
|
|
|
}*/
|
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)
|
|
|
|
{
|
2020-12-23 06:34:31 -05:00
|
|
|
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);
|
|
|
|
}
|
2020-12-28 09:27:37 -05:00
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
public void SetConfigData(int appId,
|
2020-12-19 19:14:40 -05:00
|
|
|
string language,
|
|
|
|
bool unlockAll,
|
|
|
|
bool extraProtection,
|
|
|
|
bool forceOffline,
|
2020-12-25 10:49:51 -05:00
|
|
|
List<SteamApp> dlcList)
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2020-12-25 10:49:51 -05:00
|
|
|
Config.AppId = appId;
|
|
|
|
Config.Language = language;
|
|
|
|
Config.UnlockAll = unlockAll;
|
|
|
|
Config.ExtraProtection = extraProtection;
|
|
|
|
Config.ForceOffline = forceOffline;
|
|
|
|
Config.DlcList = dlcList;
|
|
|
|
}
|
2020-12-28 09:27:37 -05:00
|
|
|
|
|
|
|
public void SetConfigData(int appId,
|
|
|
|
string language,
|
|
|
|
bool unlockAll,
|
|
|
|
bool extraProtection,
|
|
|
|
bool forceOffline,
|
2021-01-13 07:04:35 -05:00
|
|
|
IEnumerable<SteamApp> dlcList)
|
2020-12-28 09:27:37 -05:00
|
|
|
{
|
|
|
|
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;
|
2020-12-29 06:32:04 -05:00
|
|
|
Config.Language = Misc.DefaultLanguageSelection;
|
2020-12-28 09:27:37 -05:00
|
|
|
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)
|
|
|
|
{
|
2020-12-23 06:34:31 -05:00
|
|
|
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)
|
2020-12-25 10:49:51 -05:00
|
|
|
Config.DlcList.Add(
|
2020-12-28 09:27:37 -05:00
|
|
|
new SteamApp {AppId = int.Parse(match.Groups["id"].Value), Name = match.Groups["name"].Value});
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-25 10:49:51 -05:00
|
|
|
/*private void SetDlcFromAppList(List<SteamApp> dlcList)
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2020-12-25 10:49:51 -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()
|
|
|
|
{
|
2020-12-29 06:32:04 -05:00
|
|
|
var str = $"INI file: {_configFilePath}\n{Config}";
|
2020-12-19 19:14:40 -05:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
2020-12-25 10:49:51 -05:00
|
|
|
}
|