Implemented new Path classes in SteamHandler

This commit is contained in:
erri120 2020-04-22 14:24:49 +02:00
parent 6ec0025245
commit 82d8939198
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135
4 changed files with 54 additions and 48 deletions

View File

@ -220,12 +220,13 @@ namespace Wabbajack.Common
/// Assuming the path is a folder, enumerate all the files in the folder
/// </summary>
/// <param name="recursive">if true, also returns files in sub-folders</param>
/// <param name="pattern">pattern to match against</param>
/// <returns></returns>
public IEnumerable<AbsolutePath> EnumerateFiles(bool recursive = true)
public IEnumerable<AbsolutePath> EnumerateFiles(bool recursive = true, string pattern = "*")
{
if (!IsDirectory) return new AbsolutePath[0];
return Directory
.EnumerateFiles(_path, "*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.EnumerateFiles(_path, pattern, recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.Select(path => new AbsolutePath(path, true));
}

View File

@ -1,9 +1,7 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using DynamicData;
using Microsoft.Win32;
#nullable enable
@ -14,7 +12,7 @@ namespace Wabbajack.Common.StoreHandlers
public override Game Game { get; internal set; }
public override StoreType Type { get; internal set; } = StoreType.STEAM;
public string Universe = string.Empty;
public AbsolutePath Universe;
public readonly List<SteamWorkshopItem> WorkshopItems = new List<SteamWorkshopItem>();
public int WorkshopItemsSizeOnDisk;
@ -38,10 +36,9 @@ namespace Wabbajack.Common.StoreHandlers
private const string SteamRegKey = @"Software\Valve\Steam";
public string SteamPath { get; set; } = string.Empty;
private List<string>? SteamUniverses { get; set; }
private string SteamConfig => Path.Combine(SteamPath, "config", "config.vdf");
public AbsolutePath SteamPath { get; set; }
private AbsolutePath SteamConfig => new RelativePath("config//config.vdf").RelativeTo(SteamPath);
private List<AbsolutePath>? SteamUniverses { get; set; }
public override bool Init()
{
@ -56,20 +53,22 @@ namespace Wabbajack.Common.StoreHandlers
return false;
}
SteamPath = steamPathKey.ToString() ?? string.Empty;
if (string.IsNullOrWhiteSpace(SteamPath))
var steamPath = steamPathKey.ToString() ?? string.Empty;
if (string.IsNullOrWhiteSpace(steamPath))
{
Utils.Error(new StoreException("Path to the Steam Directory from registry is Null or Empty!"));
return false;
}
if (!Directory.Exists(SteamPath))
SteamPath = new AbsolutePath(steamPath);
if (!SteamPath.Exists)
{
Utils.Error(new StoreException($"Path to the Steam Directory from registry does not exists: {SteamPath}"));
return false;
}
if (File.Exists(SteamConfig))
if (SteamConfig.Exists)
return true;
Utils.Error(new StoreException($"The Steam config file could not be read: {SteamConfig}"));
@ -88,38 +87,39 @@ namespace Wabbajack.Common.StoreHandlers
return false;
}
private List<string> LoadUniverses()
private List<AbsolutePath> LoadUniverses()
{
var ret = new List<string>();
var ret = new List<AbsolutePath>();
File.ReadAllLines(SteamConfig).Do(l =>
SteamConfig.ReadAllLines().Do(l =>
{
if (!l.ContainsCaseInsensitive("BaseInstallFolder_")) return;
var s = GetVdfValue(l);
s = Path.Combine(s, "steamapps");
if (!Directory.Exists(s))
var s = new AbsolutePath(GetVdfValue(l));
var path = new RelativePath("steamapps").RelativeTo(s);
if (!path.Exists)
{
Utils.Log($"Directory {s} does not exist, skipping");
Utils.Log($"Directory {path} does not exist, skipping");
return;
}
ret.Add(s);
Utils.Log($"Steam Library found at {s}");
ret.Add(path);
Utils.Log($"Steam Library found at {path}");
});
Utils.Log($"Total number of Steam Libraries found: {ret.Count}");
// Default path in the Steam folder isn't in the configs
if (Directory.Exists(Path.Combine(SteamPath, "steamapps")))
ret.Add(Path.Combine(SteamPath, "steamapps"));
var defaultPath = new RelativePath("steamapps").RelativeTo(SteamPath);
if(defaultPath.Exists)
ret.Add(defaultPath);
return ret;
}
public override bool LoadAllGames()
{
if (SteamUniverses == null)
SteamUniverses = LoadUniverses();
SteamUniverses ??= LoadUniverses();
if (SteamUniverses.Count == 0)
{
@ -131,12 +131,15 @@ namespace Wabbajack.Common.StoreHandlers
{
Utils.Log($"Searching for Steam Games in {u}");
Directory.EnumerateFiles(u, "*.acf", SearchOption.TopDirectoryOnly).Where(File.Exists).Do(f =>
u.EnumerateFiles(false, "*.acf")
.Where(a => a.Exists)
.Where(a => a.IsFile)
.Do(f =>
{
var game = new SteamGame();
var gotID = false;
File.ReadAllLines(f).Do(l =>
f.ReadAllLines().Do(l =>
{
if (l.ContainsCaseInsensitive("\"appid\""))
{
@ -152,9 +155,9 @@ namespace Wabbajack.Common.StoreHandlers
if (!l.ContainsCaseInsensitive("\"installdir\""))
return;
var path = Path.Combine(u, "common", GetVdfValue(l));
if (Directory.Exists(path))
game.Path = (AbsolutePath)path;
var path = new RelativePath($"common//{GetVdfValue(l)}").RelativeTo(u);
if (path.Exists)
game.Path = path;
});
if (!gotID || !game.Path.IsDirectory) return;
@ -189,18 +192,21 @@ namespace Wabbajack.Common.StoreHandlers
private static void LoadWorkshopItems(SteamGame game)
{
var workshop = Path.Combine(game.Universe, "workshop");
if (!Directory.Exists(workshop))
var workshop = new RelativePath("workshop").RelativeTo(game.Universe);
if (!workshop.Exists)
return;
Directory.EnumerateFiles(workshop, "*.acf", SearchOption.TopDirectoryOnly).Where(File.Exists).Do(f =>
workshop.EnumerateFiles(false, "*.acf")
.Where(f => f.Exists)
.Where(f => f.IsFile)
.Do(f =>
{
if (Path.GetFileName(f) != $"appworkshop{game.ID}.acf")
if (f.FileName.ToString() != $"appworkshop{game.ID}.acf")
return;
Utils.Log($"Found Steam Workshop item file {f} for \"{game.Name}\"");
var lines = File.ReadAllLines(f);
var lines = f.ReadAllLines().ToList();
var end = false;
var foundAppID = false;
var workshopItemsInstalled = 0;
@ -214,8 +220,8 @@ namespace Wabbajack.Common.StoreHandlers
{
if (end)
return;
if (currentItem == null)
currentItem = new SteamWorkshopItem(game);
currentItem ??= new SteamWorkshopItem(game);
var currentLine = lines.IndexOf(l);
if (l.ContainsCaseInsensitive("\"appid\"") && !foundAppID)

View File

@ -13,8 +13,8 @@ namespace Wabbajack.Common.StoreHandlers
public class StoreHandler
{
private static readonly Lazy<StoreHandler> instance = new Lazy<StoreHandler>(() => new StoreHandler(), true);
public static StoreHandler Instance => instance.Value;
private static readonly Lazy<StoreHandler> _instance = new Lazy<StoreHandler>(() => new StoreHandler(), true);
public static StoreHandler Instance => _instance.Value;
private static readonly Lazy<SteamHandler> _steamHandler = new Lazy<SteamHandler>(() => new SteamHandler());
public SteamHandler SteamHandler = _steamHandler.Value;

View File

@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Wabbajack.Common;
@ -60,13 +57,13 @@ namespace Wabbajack.Lib.Downloaders
{
var currentLib = Item.Game.Universe;
var downloadFolder = Path.Combine(currentLib, "workshop", "downloads", Item.Game.ID.ToString());
var contentFolder = Path.Combine(currentLib, "workshop", "content", Item.Game.ID.ToString());
var downloadFolder = new RelativePath($"workshop//downloads//{Item.Game.ID}").RelativeTo(currentLib);
var contentFolder = new RelativePath($"workshop//content//{Item.Game.ID}").RelativeTo(currentLib);
var p = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = Path.Combine(StoreHandler.Instance.SteamHandler.SteamPath, "steam.exe"),
FileName = new RelativePath("steam.exe").RelativeTo(StoreHandler.Instance.SteamHandler.SteamPath).ToString(),
CreateNoWindow = true,
Arguments = $"console +workshop_download_item {Item.Game.ID} {Item.ItemID}"
}
@ -76,10 +73,12 @@ namespace Wabbajack.Lib.Downloaders
//TODO: async
var finished = false;
var itemDownloadPath = new RelativePath(Item.ItemID.ToString()).RelativeTo(downloadFolder);
var itemContentPath = new RelativePath(Item.ItemID.ToString()).RelativeTo(contentFolder);
while (!finished)
{
if(!Directory.Exists(Path.Combine(downloadFolder, Item.ItemID.ToString())))
if (Directory.Exists(Path.Combine(contentFolder, Item.ItemID.ToString())))
if(!itemDownloadPath.Exists)
if(itemContentPath.Exists)
finished = true;
Thread.Sleep(1000);