2020-12-25 10:49:51 -05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using auto_creamapi.Models;
|
|
|
|
using auto_creamapi.Services;
|
|
|
|
using auto_creamapi.Utils;
|
|
|
|
using Microsoft.Win32;
|
|
|
|
using MvvmCross.Commands;
|
2020-12-28 09:27:37 -05:00
|
|
|
using MvvmCross.Navigation;
|
2020-12-25 10:49:51 -05:00
|
|
|
using MvvmCross.ViewModels;
|
|
|
|
|
|
|
|
namespace auto_creamapi.ViewModels
|
|
|
|
{
|
|
|
|
public class MainViewModel : MvxViewModel
|
|
|
|
{
|
|
|
|
private readonly ICacheService _cache;
|
|
|
|
private readonly ICreamConfigService _config;
|
2020-12-28 09:27:37 -05:00
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
private readonly ICreamDllService _dll;
|
2020-12-28 09:27:37 -05:00
|
|
|
private readonly IMvxNavigationService _navigationService;
|
|
|
|
private int _appId;
|
|
|
|
private bool _configExists;
|
|
|
|
private ObservableCollection<SteamApp> _dlcs;
|
|
|
|
private bool _dllApplied;
|
2020-12-25 10:49:51 -05:00
|
|
|
private string _dllPath;
|
2021-01-01 10:27:42 -05:00
|
|
|
private bool _extraProtection;
|
2020-12-25 10:49:51 -05:00
|
|
|
private string _gameName;
|
|
|
|
private string _lang;
|
2020-12-28 09:27:37 -05:00
|
|
|
private ObservableCollection<string> _languages;
|
|
|
|
|
|
|
|
//private readonly IDownloadCreamApiService _download;
|
|
|
|
private bool _mainWindowEnabled;
|
2020-12-25 10:49:51 -05:00
|
|
|
private bool _offline;
|
2020-12-28 09:27:37 -05:00
|
|
|
private string _status;
|
2021-01-01 10:27:42 -05:00
|
|
|
private bool _unlockAll;
|
2020-12-28 09:27:37 -05:00
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
private bool _useSteamDb;
|
2021-01-04 10:14:10 -05:00
|
|
|
|
|
|
|
private bool _ignoreUnknown;
|
2020-12-28 09:27:37 -05:00
|
|
|
//private const string DlcRegexPattern = @"(?<id>.*) *= *(?<name>.*)";
|
2020-12-25 10:49:51 -05:00
|
|
|
|
|
|
|
public MainViewModel(ICacheService cache, ICreamConfigService config, ICreamDllService dll,
|
2020-12-28 09:27:37 -05:00
|
|
|
IMvxNavigationService navigationService)
|
2020-12-25 10:49:51 -05:00
|
|
|
{
|
2020-12-28 09:27:37 -05:00
|
|
|
_navigationService = navigationService;
|
2020-12-25 10:49:51 -05:00
|
|
|
_cache = cache;
|
|
|
|
_config = config;
|
|
|
|
_dll = dll;
|
2020-12-28 09:27:37 -05:00
|
|
|
//_download = download;
|
2020-12-25 10:49:51 -05:00
|
|
|
}
|
|
|
|
|
2021-01-13 07:04:35 -05:00
|
|
|
public override async void Prepare()
|
2020-12-29 06:32:04 -05:00
|
|
|
{
|
2021-01-13 07:04:35 -05:00
|
|
|
base.Prepare();
|
2020-12-29 06:32:04 -05:00
|
|
|
_config.Initialize();
|
2021-01-13 07:04:35 -05:00
|
|
|
var tasks = new List<Task> {_cache.Initialize()};
|
2020-12-29 06:32:04 -05:00
|
|
|
if (!File.Exists("steam_api.dll") | !File.Exists("steam_api64.dll"))
|
|
|
|
tasks.Add(_navigationService.Navigate<DownloadViewModel>());
|
2021-01-13 07:04:35 -05:00
|
|
|
//tasks.Add(_navigationService.Navigate<DownloadViewModel>());
|
2020-12-29 06:32:04 -05:00
|
|
|
tasks.Add(_dll.Initialize());
|
2021-01-13 07:04:35 -05:00
|
|
|
await Task.WhenAll(tasks).ConfigureAwait(false);
|
2020-12-29 06:32:04 -05:00
|
|
|
Languages = new ObservableCollection<string>(Misc.DefaultLanguages);
|
|
|
|
ResetForm();
|
|
|
|
UseSteamDb = true;
|
|
|
|
MainWindowEnabled = true;
|
|
|
|
Status = "Ready.";
|
|
|
|
}
|
|
|
|
|
2021-01-13 07:04:35 -05:00
|
|
|
public override Task Initialize()
|
|
|
|
{
|
|
|
|
return base.Initialize();
|
|
|
|
}
|
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
// // COMMANDS // //
|
|
|
|
|
2021-01-01 11:03:46 -05:00
|
|
|
public IMvxCommand OpenFileCommand => new MvxAsyncCommand(OpenFile);
|
2020-12-25 10:49:51 -05:00
|
|
|
|
2021-01-13 07:04:35 -05:00
|
|
|
public IMvxCommand SearchCommand => new MvxAsyncCommand(async () => await Search().ConfigureAwait(false)); //Command(Search);
|
2020-12-25 10:49:51 -05:00
|
|
|
|
|
|
|
public IMvxCommand GetListOfDlcCommand => new MvxAsyncCommand(GetListOfDlc);
|
|
|
|
|
|
|
|
public IMvxCommand SaveCommand => new MvxCommand(Save);
|
|
|
|
|
|
|
|
public IMvxCommand ResetFormCommand => new MvxCommand(ResetForm);
|
|
|
|
|
|
|
|
public IMvxCommand GoToForumThreadCommand => new MvxCommand(GoToForumThread);
|
|
|
|
|
|
|
|
// // ATTRIBUTES // //
|
|
|
|
|
|
|
|
public bool MainWindowEnabled
|
|
|
|
{
|
|
|
|
get => _mainWindowEnabled;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_mainWindowEnabled = value;
|
|
|
|
RaisePropertyChanged(() => MainWindowEnabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string DllPath
|
|
|
|
{
|
|
|
|
get => _dllPath;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_dllPath = value;
|
|
|
|
RaisePropertyChanged(() => DllPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GameName
|
|
|
|
{
|
|
|
|
get => _gameName;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_gameName = value;
|
|
|
|
RaisePropertyChanged(() => GameName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int AppId
|
|
|
|
{
|
|
|
|
get => _appId;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_appId = value;
|
|
|
|
RaisePropertyChanged(() => AppId);
|
2020-12-29 06:32:04 -05:00
|
|
|
SetNameById();
|
2020-12-25 10:49:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Lang
|
|
|
|
{
|
|
|
|
get => _lang;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_lang = value;
|
|
|
|
RaisePropertyChanged(() => Lang);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Offline
|
|
|
|
{
|
|
|
|
get => _offline;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_offline = value;
|
|
|
|
RaisePropertyChanged(() => Offline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 10:27:42 -05:00
|
|
|
public bool ExtraProtection
|
2020-12-25 10:49:51 -05:00
|
|
|
{
|
2021-01-01 10:27:42 -05:00
|
|
|
get => _extraProtection;
|
2020-12-25 10:49:51 -05:00
|
|
|
set
|
|
|
|
{
|
2021-01-01 10:27:42 -05:00
|
|
|
_extraProtection = value;
|
|
|
|
RaisePropertyChanged(() => ExtraProtection);
|
2020-12-25 10:49:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 10:27:42 -05:00
|
|
|
public bool UnlockAll
|
2020-12-25 10:49:51 -05:00
|
|
|
{
|
2021-01-01 10:27:42 -05:00
|
|
|
get => _unlockAll;
|
2020-12-25 10:49:51 -05:00
|
|
|
set
|
|
|
|
{
|
2021-01-01 10:27:42 -05:00
|
|
|
_unlockAll = value;
|
|
|
|
RaisePropertyChanged(() => UnlockAll);
|
2020-12-25 10:49:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool UseSteamDb
|
|
|
|
{
|
|
|
|
get => _useSteamDb;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_useSteamDb = value;
|
|
|
|
RaisePropertyChanged(() => UseSteamDb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
public ObservableCollection<SteamApp> Dlcs
|
2020-12-25 10:49:51 -05:00
|
|
|
{
|
|
|
|
get => _dlcs;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_dlcs = value;
|
2020-12-28 09:27:37 -05:00
|
|
|
RaisePropertyChanged(() => Dlcs);
|
2020-12-25 10:49:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool DllApplied
|
|
|
|
{
|
|
|
|
get => _dllApplied;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_dllApplied = value;
|
|
|
|
RaisePropertyChanged(() => DllApplied);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool ConfigExists
|
|
|
|
{
|
|
|
|
get => _configExists;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_configExists = value;
|
|
|
|
RaisePropertyChanged(() => ConfigExists);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Status
|
|
|
|
{
|
|
|
|
get => _status;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_status = value;
|
|
|
|
RaisePropertyChanged(() => Status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ObservableCollection<string> Languages
|
|
|
|
{
|
|
|
|
get => _languages;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_languages = value;
|
|
|
|
RaisePropertyChanged(() => Languages);
|
|
|
|
}
|
|
|
|
}
|
2020-12-28 09:27:37 -05:00
|
|
|
|
2021-01-04 10:14:10 -05:00
|
|
|
public bool IgnoreUnknown
|
|
|
|
{
|
|
|
|
get => _ignoreUnknown;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_ignoreUnknown = value;
|
|
|
|
RaisePropertyChanged(() => IgnoreUnknown);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 11:03:46 -05:00
|
|
|
private async Task OpenFile()
|
2020-12-28 09:27:37 -05:00
|
|
|
{
|
|
|
|
Status = "Waiting for file...";
|
|
|
|
var dialog = new OpenFileDialog
|
|
|
|
{
|
|
|
|
Filter = "SteamAPI DLL|steam_api.dll;steam_api64.dll|" +
|
|
|
|
"All files (*.*)|*.*",
|
|
|
|
Multiselect = false,
|
|
|
|
Title = "Select SteamAPI DLL..."
|
|
|
|
};
|
|
|
|
if (dialog.ShowDialog() == true)
|
|
|
|
{
|
|
|
|
var filePath = dialog.FileName;
|
|
|
|
DllPath = filePath;
|
|
|
|
var dirPath = Path.GetDirectoryName(filePath);
|
|
|
|
if (dirPath != null)
|
|
|
|
{
|
|
|
|
_config.ReadFile(Path.Combine(dirPath, "cream_api.ini"));
|
|
|
|
ResetForm();
|
|
|
|
_dll.TargetPath = dirPath;
|
|
|
|
_dll.CheckIfDllExistsAtTarget();
|
2021-01-01 11:03:46 -05:00
|
|
|
CheckSetupStatus();
|
|
|
|
if (!ConfigExists)
|
|
|
|
{
|
|
|
|
var separator = Path.DirectorySeparatorChar;
|
|
|
|
var strings = new List<string>(dirPath.Split(separator));
|
|
|
|
var index = strings.Contains("common") ? strings.FindIndex(x => x.Equals("common")) + 1 : -1;
|
2021-01-04 10:14:10 -05:00
|
|
|
if (index == -1)
|
|
|
|
index = strings.Contains("steamapps")
|
|
|
|
? strings.FindIndex(x => x.Equals("steamapps")) + 2
|
|
|
|
: -1;
|
2021-01-01 11:03:46 -05:00
|
|
|
var s = index > -1 ? strings[index] : null;
|
|
|
|
if (s != null) GameName = s;
|
2021-01-13 07:04:35 -05:00
|
|
|
await Search().ConfigureAwait(false);
|
|
|
|
await GetListOfDlc().ConfigureAwait(false);
|
2021-01-01 11:03:46 -05:00
|
|
|
}
|
2021-01-04 10:14:10 -05:00
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
Status = "Ready.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Status = "File selection canceled.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task Search()
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(GameName))
|
|
|
|
{
|
|
|
|
var app = _cache.GetAppByName(GameName);
|
|
|
|
if (app != null)
|
|
|
|
{
|
|
|
|
GameName = app.Name;
|
|
|
|
AppId = app.AppId;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-04 06:54:28 -05:00
|
|
|
MainWindowEnabled = false;
|
2020-12-28 09:27:37 -05:00
|
|
|
var navigate = _navigationService.Navigate<SearchResultViewModel, IEnumerable<SteamApp>, SteamApp>(
|
|
|
|
_cache.GetListOfAppsByName(GameName));
|
2021-01-13 07:04:35 -05:00
|
|
|
await navigate.ConfigureAwait(false);
|
2020-12-28 09:27:37 -05:00
|
|
|
var navigateResult = navigate.Result;
|
|
|
|
if (navigateResult != null)
|
|
|
|
{
|
|
|
|
GameName = navigateResult.Name;
|
|
|
|
AppId = navigateResult.AppId;
|
|
|
|
}
|
|
|
|
}
|
2021-01-04 10:14:10 -05:00
|
|
|
|
2021-01-13 07:04:35 -05:00
|
|
|
await GetListOfDlc().ConfigureAwait(false);
|
2020-12-28 09:27:37 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MyLogger.Log.Warning("Empty game name, cannot initiate search!");
|
|
|
|
}
|
2021-01-04 10:14:10 -05:00
|
|
|
|
2021-01-04 06:54:28 -05:00
|
|
|
MainWindowEnabled = true;
|
2020-12-28 09:27:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task GetListOfDlc()
|
|
|
|
{
|
|
|
|
Status = "Trying to get DLC...";
|
|
|
|
if (AppId > 0)
|
|
|
|
{
|
|
|
|
var app = new SteamApp {AppId = AppId, Name = GameName};
|
2021-01-04 10:14:10 -05:00
|
|
|
var task = _cache.GetListOfDlc(app, UseSteamDb, IgnoreUnknown);
|
2020-12-28 09:27:37 -05:00
|
|
|
MainWindowEnabled = false;
|
2021-01-13 07:04:35 -05:00
|
|
|
var listOfDlc = await task.ConfigureAwait(false);
|
2020-12-28 09:27:37 -05:00
|
|
|
if (task.IsCompletedSuccessfully)
|
|
|
|
{
|
|
|
|
listOfDlc.Sort((app1, app2) => app1.AppId.CompareTo(app2.AppId));
|
|
|
|
Dlcs = new ObservableCollection<SteamApp>(listOfDlc);
|
2021-01-04 10:14:10 -05:00
|
|
|
Status = $"Got DLC for AppID {AppId} (Count: {Dlcs.Count})";
|
2020-12-28 09:27:37 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Status = $"Could not get DLC for AppID {AppId}";
|
|
|
|
}
|
|
|
|
|
|
|
|
MainWindowEnabled = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Status = $"Could not get DLC for AppID {AppId}";
|
|
|
|
MyLogger.Log.Error($"GetListOfDlc: Invalid AppID {AppId}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Save()
|
|
|
|
{
|
|
|
|
Status = "Saving...";
|
|
|
|
_config.SetConfigData(
|
|
|
|
AppId,
|
|
|
|
Lang,
|
2021-01-01 10:27:42 -05:00
|
|
|
UnlockAll,
|
|
|
|
ExtraProtection,
|
2020-12-28 09:27:37 -05:00
|
|
|
Offline,
|
|
|
|
Dlcs
|
|
|
|
);
|
|
|
|
_config.SaveFile();
|
|
|
|
_dll.Save();
|
2021-01-01 11:03:46 -05:00
|
|
|
CheckSetupStatus();
|
2020-12-28 09:27:37 -05:00
|
|
|
Status = "Saving successful.";
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ResetForm()
|
|
|
|
{
|
|
|
|
AppId = _config.Config.AppId;
|
|
|
|
Lang = _config.Config.Language;
|
2021-01-01 10:27:42 -05:00
|
|
|
UnlockAll = _config.Config.UnlockAll;
|
|
|
|
ExtraProtection = _config.Config.ExtraProtection;
|
2020-12-28 09:27:37 -05:00
|
|
|
Offline = _config.Config.ForceOffline;
|
|
|
|
Dlcs = new ObservableCollection<SteamApp>(_config.Config.DlcList);
|
|
|
|
Status = "Changes have been reset.";
|
|
|
|
}
|
|
|
|
|
|
|
|
private void GoToForumThread()
|
|
|
|
{
|
|
|
|
Status = "Opening URL...";
|
|
|
|
if (AppId > 0)
|
|
|
|
{
|
|
|
|
var searchTerm = AppId; //$"{GameName.Replace(" ", "+")}+{appId}";
|
|
|
|
var destinationUrl =
|
|
|
|
"https://cs.rin.ru/forum/search.php?keywords=" +
|
|
|
|
searchTerm +
|
|
|
|
"&terms=any&fid[]=10&sf=firstpost&sr=topics&submit=Search";
|
|
|
|
var uri = new Uri(destinationUrl);
|
|
|
|
var process = new ProcessStartInfo(uri.AbsoluteUri)
|
|
|
|
{
|
|
|
|
UseShellExecute = true
|
|
|
|
};
|
|
|
|
Process.Start(process);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MyLogger.Log.Error($"OpenURL: Invalid AppID {AppId}");
|
|
|
|
Status = $"Could not open URL: Invalid AppID {AppId}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 11:03:46 -05:00
|
|
|
private void CheckSetupStatus()
|
2020-12-28 09:27:37 -05:00
|
|
|
{
|
|
|
|
DllApplied = _dll.CreamApiApplied();
|
|
|
|
ConfigExists = _config.ConfigExists();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetNameById()
|
|
|
|
{
|
2020-12-29 06:32:04 -05:00
|
|
|
if (_appId > 0)
|
|
|
|
{
|
|
|
|
var appById = _cache.GetAppById(_appId);
|
|
|
|
GameName = appById != null ? appById.Name : "";
|
|
|
|
}
|
|
|
|
else GameName = "";
|
2020-12-28 09:27:37 -05:00
|
|
|
}
|
2020-12-25 10:49:51 -05:00
|
|
|
}
|
|
|
|
}
|