Alice
Alice
CC#
Created by Alice on 4/7/2025 in #help
Extracting values from external data files for a property in `.csproj`?
we are working on mod setup for a unity game installed through steam so we need to import game DLLs from steam library. we wrote a C# snippet that seems to perform that task correctly:
var SteamInstallPathFromRegistry = OS == "Windows_NT" ? $"{MSBuild.GetRegistryValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Valve\Steam", "InstallPath")}" : "";
var SteamDir = OS == "Windows_NT" ? SteamInstallPathFromRegistry != "" ? $"{SteamInstallPathFromRegistry}/steamapps": "C:/Program Files (x86)/Steam/steamapps" : $"{Environment.GetEnvironmentVariable("HOME")}/.steam/steam/steamapps";

var HasteAppId = "1796470";
var Libraries = System.IO.File.ReadAllText($"{SteamDir}/libraryfolders.vdf");
var InstallLibrary = Libraries;
// var InstallLibrary = Regex.Replace(InstallLibrary, $"\"{HasteAppId}\".*", "");
// InstallLibrary = InstallLibrary[(InstallLibrary.LastIndexOf("\"path\"") + 9)..];
// InstallLibrary = $"{InstallLibrary[..(InstallLibrary.IndexOfAny("\r\n".ToCharArray()) - 1)]}/steamapps";
InstallLibrary = Regex.Replace(InstallLibrary, $"\"{HasteAppId}\"(.|[\\n\\r])*", "");
InstallLibrary = Regex.Replace(InstallLibrary, "(.|[\\n\\r])*(?=\"path\").{9}", "");
InstallLibrary = Regex.Replace(InstallLibrary, "\"[\\r\\n](.|[\\n\\r])*", "/steamapps");
Console.Out.Write(InstallLibrary);
var SteamInstallPathFromRegistry = OS == "Windows_NT" ? $"{MSBuild.GetRegistryValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Valve\Steam", "InstallPath")}" : "";
var SteamDir = OS == "Windows_NT" ? SteamInstallPathFromRegistry != "" ? $"{SteamInstallPathFromRegistry}/steamapps": "C:/Program Files (x86)/Steam/steamapps" : $"{Environment.GetEnvironmentVariable("HOME")}/.steam/steam/steamapps";

var HasteAppId = "1796470";
var Libraries = System.IO.File.ReadAllText($"{SteamDir}/libraryfolders.vdf");
var InstallLibrary = Libraries;
// var InstallLibrary = Regex.Replace(InstallLibrary, $"\"{HasteAppId}\".*", "");
// InstallLibrary = InstallLibrary[(InstallLibrary.LastIndexOf("\"path\"") + 9)..];
// InstallLibrary = $"{InstallLibrary[..(InstallLibrary.IndexOfAny("\r\n".ToCharArray()) - 1)]}/steamapps";
InstallLibrary = Regex.Replace(InstallLibrary, $"\"{HasteAppId}\"(.|[\\n\\r])*", "");
InstallLibrary = Regex.Replace(InstallLibrary, "(.|[\\n\\r])*(?=\"path\").{9}", "");
InstallLibrary = Regex.Replace(InstallLibrary, "\"[\\r\\n](.|[\\n\\r])*", "/steamapps");
Console.Out.Write(InstallLibrary);
but porting it to the <PropertyGroup> is proving to be a challenge, as trying to use that code in an inline task throws an error on input parameter property as soon as we add output property, and using repeated re-definition of <InstallLibrary> does not yield desired result either (and we don't know how to see what we are actually getting, as adding a target before Build with a Message with property value does not seem to output anything)
12 replies