<commit_before>﻿using System;
using System.IO;
using System.Linq;
using Microsoft.Win32;

namespace SteamHelper
{
    internal class AppIdInfo
    {
        private AppIdInfo(int appId)
        {
            AppId = appId;
        }

        public static AppIdInfo FromAppId(int appId)
        {
            var output = new AppIdInfo(appId);

            var appIdStr = appId.ToString("G");

            var key = Registry.LocalMachine.OpenSubKey($@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App {appIdStr}") ??
                      Registry.LocalMachine.OpenSubKey($@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Steam App {appIdStr}");
            if (key == null)
                throw new ArgumentException("AppID is not registered properly");

            using (key)
            {
                output.UninstallString = key.GetValue(@"UninstallString") as string;
                output.InstallDirectory = key.GetValue(@"InstallLocation") as string;
                if (output.InstallDirectory == null || !Directory.Exists(output.InstallDirectory))
                    throw new ArgumentException($"Install location is invalid or doesn't exist: {output.InstallDirectory}");
            }

            var dir = new DirectoryInfo(output.InstallDirectory);
            while (!dir.Name.Equals(@"steamapps", StringComparison.InvariantCultureIgnoreCase))
            {
                dir = dir.Parent;
                if (dir == null)
                    throw new ArgumentException("Could not find the SteamApps directory");
            }

            output.ManifestPath = dir.GetFiles($@"appmanifest_{appIdStr}.acf").Single().FullName;
            output.DownloadDirectory = dir.GetDirectories(@"downloading").SingleOrDefault()?.GetDirectories(appIdStr).SingleOrDefault()?.FullName;

            var workshopDir = dir.GetDirectories(@"workshop").SingleOrDefault();
            if (workshopDir != null)
            {
                output.WorkshopManifestPath = dir.GetFiles($@"appworkshop_{appIdStr}.acf").SingleOrDefault()?.FullName;
                output.WorkshopDirectory = dir.GetDirectories(@"content").SingleOrDefault()?.GetDirectories(appIdStr).SingleOrDefault()?.FullName;
            }

            return output;
        }

        public int AppId { get; }
        public string UninstallString { get; private set; }
        public string ManifestPath { get; private set; }
        public string InstallDirectory { get; private set; }
        public string DownloadDirectory { get; private set; }
        public string WorkshopManifestPath { get; private set; }
        public string WorkshopDirectory { get; private set; }
    }
}<commit_msg>Fixed working on steamapps directory when looking for workshop items<commit_after>﻿using System;
using System.IO;
using System.Linq;
using Microsoft.Win32;

namespace SteamHelper
{
    internal class AppIdInfo
    {
        private AppIdInfo(int appId)
        {
            AppId = appId;
        }

        public static AppIdInfo FromAppId(int appId)
        {
            var output = new AppIdInfo(appId);

            var appIdStr = appId.ToString("G");

            var key = Registry.LocalMachine.OpenSubKey($@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App {appIdStr}") ??
                      Registry.LocalMachine.OpenSubKey($@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Steam App {appIdStr}");
            if (key == null)
                throw new ArgumentException("AppID is not registered properly");

            using (key)
            {
                output.UninstallString = key.GetValue(@"UninstallString") as string;
                output.InstallDirectory = key.GetValue(@"InstallLocation") as string;
                if (output.InstallDirectory == null || !Directory.Exists(output.InstallDirectory))
                    throw new ArgumentException($"Install location is invalid or doesn't exist: {output.InstallDirectory}");
            }

            var dir = new DirectoryInfo(output.InstallDirectory);
            while (!dir.Name.Equals(@"steamapps", StringComparison.InvariantCultureIgnoreCase))
            {
                dir = dir.Parent;
                if (dir == null)
                    throw new ArgumentException("Could not find the SteamApps directory");
            }

            output.ManifestPath = dir.GetFiles($@"appmanifest_{appIdStr}.acf").Single().FullName;
            output.DownloadDirectory = dir.GetDirectories(@"downloading").SingleOrDefault()?.GetDirectories(appIdStr).SingleOrDefault()?.FullName;

            var workshopDir = dir.GetDirectories(@"workshop").SingleOrDefault();
            if (workshopDir != null)
            {
                output.WorkshopManifestPath = workshopDir.GetFiles($@"appworkshop_{appIdStr}.acf").SingleOrDefault()?.FullName;
                output.WorkshopDirectory = workshopDir.GetDirectories(@"content").SingleOrDefault()?.GetDirectories(appIdStr).SingleOrDefault()?.FullName;
            }

            return output;
        }

        public int AppId { get; }
        public string UninstallString { get; private set; }
        public string ManifestPath { get; private set; }
        public string InstallDirectory { get; private set; }
        public string DownloadDirectory { get; private set; }
        public string WorkshopManifestPath { get; private set; }
        public string WorkshopDirectory { get; private set; }
    }
}
{'fraction_non_alphanumeric': 0.10155251141552511, 'fraction_numerical': 0.002191780821917808, 'mean_word_length': 2.4267834793491865, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2064951', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1343, 'n_words': 305}