Skip to content

Instantly share code, notes, and snippets.

@Banane9
Created December 6, 2023 00:12
Show Gist options
  • Select an option

  • Save Banane9/9849550d7c7d8e20456161c61fdef5f9 to your computer and use it in GitHub Desktop.

Select an option

Save Banane9/9849550d7c7d8e20456161c61fdef5f9 to your computer and use it in GitHub Desktop.
Dynamically finds the install location of Neos through Steam and dynamically resolves references to its DLLs.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Win32;
namespace NLL
{
internal static class NeosLibraryLoader
{
public const string NeosSteamId = "740250";
/// <summary>
/// Contains the path to the root directory of the local Steam install based on the system registry.
/// </summary>
public static readonly string SteamInstallPath = (string)(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Valve\Steam", "InstallPath", null)
?? Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Valve\Steam", "InstallPath", null));
/// <summary>
/// Contains the path to the main Steam library games folder.
/// </summary>
public static readonly string SteamMainLibraryPath = Path.Combine(SteamInstallPath, "steamapps");
/// <summary>
/// Gets the path where Neos is installed through Steam.
/// </summary>
public static string NeosInstallPath
{
get
{
var neosManifest = "appmanifest_" + NeosSteamId + ".acf";
var neosLibrary = SteamLibraryPaths.First(library => File.Exists(Path.Combine(library, neosManifest)));
return Path.Combine(neosLibrary, "common", File.ReadAllLines(Path.Combine(neosLibrary, neosManifest))
.First(line => line.Contains("\"installdir\"", StringComparison.OrdinalIgnoreCase)).Split('"')[3]);
}
}
/// <summary>
/// Gets the paths to all existing Steam libraries.
/// </summary>
public static IEnumerable<string> SteamLibraryPaths
{
get
{
var libraryFile = Path.Combine(SteamMainLibraryPath, "libraryfolders.vdf");
return File.ReadAllLines(libraryFile)
.Where(line => line.Contains("\"path\"", StringComparison.OrdinalIgnoreCase))
.Select(line => line.Split('"')[3])
.Select(path => Path.Combine(path, "steamapps"))
.Where(path => Directory.Exists(path))
.Append(SteamMainLibraryPath);
}
}
/// <summary>
/// Sets up an Assembly loader that checks the Neos install folder.
/// </summary>
public static void SetupAssemblyLoader()
{
AppDomain.CurrentDomain.AssemblyResolve += neosAssemblyResolver;
}
private static Assembly neosAssemblyResolver(object sender, ResolveEventArgs eventArgs)
{
if (eventArgs.Name.Contains("resources", StringComparison.OrdinalIgnoreCase))
return null;
var assemblyFile = eventArgs.Name.Contains(',') ?
eventArgs.Name.Substring(0, eventArgs.Name.IndexOf(',')) : eventArgs.Name;
assemblyFile += ".dll";
//var targetPath = Path.Combine(NeosInstallPath, "Neos_Data", "Managed", assemblyFile);
var targetPath = Path.Combine(NeosInstallPath, "Tools", "AdminX", assemblyFile);
if (File.Exists(targetPath))
return Assembly.LoadFile(targetPath);
else
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment