fix catching exception

This commit is contained in:
AlexZ 2020-02-03 13:13:35 +03:00
parent e33f0b6d12
commit 08659b463b

View File

@ -97,71 +97,83 @@ namespace EpicMorg.SteamPathsLib {
} }
} }
public static ConfigSteamData GetSteamConfig() { public static ConfigSteamData GetSteamConfig() {
var result = new ConfigSteamData(); try {
var libraryPaths = new List<string>(); var result = new ConfigSteamData();
var libraryPaths = new List<string>();
var steamPath = new DirectoryInfo(GetSteamData().SteamPath).FullName; var steamPath = new DirectoryInfo(GetSteamData().SteamPath).FullName;
var configPath = Path.Combine(steamPath, "config", "config.vdf"); var configPath = Path.Combine(steamPath, "config", "config.vdf");
libraryPaths.Add(Path.Combine(steamPath, "steamapps")); libraryPaths.Add(Path.Combine(steamPath, "steamapps"));
dynamic configObject = VdfConvert.Deserialize(File.ReadAllText(configPath)).Value; dynamic configObject = VdfConvert.Deserialize(File.ReadAllText(configPath)).Value;
var configLibraryPaths = ((VObject)configObject.Software.Valve.Steam) var configLibraryPaths = ((VObject)configObject.Software.Valve.Steam)
.Children() .Children()
.Where(item => item.Key.StartsWith("BaseInstallFolder")) .Where(item => item.Key.StartsWith("BaseInstallFolder"))
.Select(item => item.Value.ToString()) .Select(item => item.Value.ToString())
.Select(line => new DirectoryInfo(line).FullName) .Select(line => new DirectoryInfo(line).FullName)
.Select(line => Path.Combine(line, "steamapps")); .Select(line => Path.Combine(line, "steamapps"));
libraryPaths.AddRange(configLibraryPaths); libraryPaths.AddRange(configLibraryPaths);
result.SteamLibraryFolders = libraryPaths.ToArray(); result.SteamLibraryFolders = libraryPaths.ToArray();
return result; return result;
} catch (Exception) {
return null;
}
} }
public static List<LibrarySteamData> GetLibrarySteamDataList() { public static List<LibrarySteamData> GetLibrarySteamDataList() {
var configData = GetSteamConfig(); try {
var result = new List<LibrarySteamData>(); var configData = GetSteamConfig();
var result = new List<LibrarySteamData>();
foreach (var path in configData.SteamLibraryFolders) { foreach (var path in configData.SteamLibraryFolders) {
var libraryData = new LibrarySteamData(); var libraryData = new LibrarySteamData();
libraryData.Path = path; libraryData.Path = path;
libraryData.AppManifestDataList = new List<SteamAppManifestData>(); libraryData.AppManifestDataList = new List<SteamAppManifestData>();
var files = new DirectoryInfo(path).GetFiles("appmanifest_*.acf"); var files = new DirectoryInfo(path).GetFiles("appmanifest_*.acf");
foreach (var file in files) { foreach (var file in files) {
var appData = new SteamAppManifestData(); var appData = new SteamAppManifestData();
dynamic appManifestObject = VdfConvert.Deserialize(File.ReadAllText(file.FullName)).Value; dynamic appManifestObject = VdfConvert.Deserialize(File.ReadAllText(file.FullName)).Value;
appData.AppId = Convert.ToInt32(appManifestObject.appid.Value); appData.AppId = Convert.ToInt32(appManifestObject.appid.Value);
appData.Name = appManifestObject.name.Value; appData.Name = appManifestObject.name.Value;
appData.InstallDir = appManifestObject.installdir.Value; appData.InstallDir = appManifestObject.installdir.Value;
appData.Path = Path.Combine(path, "common", appData.InstallDir); appData.Path = Path.Combine(path, "common", appData.InstallDir);
libraryData.AppManifestDataList.Add(appData); libraryData.AppManifestDataList.Add(appData);
}
result.Add(libraryData);
} }
result.Add(libraryData);
}
return result; return result;
} catch (Exception) {
return null;
}
} }
public static List<SteamAppManifestData> GetAllSteamAppManifestData() { public static List<SteamAppManifestData> GetAllSteamAppManifestData() {
var result = new List<SteamAppManifestData>(); try {
var result = new List<SteamAppManifestData>();
var libraries = GetLibrarySteamDataList(); var libraries = GetLibrarySteamDataList();
foreach (var library in libraries) { foreach (var library in libraries) {
result.AddRange(library.AppManifestDataList); result.AddRange(library.AppManifestDataList);
}
return result;
} catch (Exception) {
return null;
} }
return result;
} }
public static SteamAppManifestData GetSteamAppManifestDataById(int appId) => GetAllSteamAppManifestData().FirstOrDefault(appData => appData.AppId == appId); public static SteamAppManifestData GetSteamAppManifestDataById(int appId) => GetAllSteamAppManifestData()?.FirstOrDefault(appData => appData.AppId == appId);
} }
} }