Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of variables and relative paths when reading paths from env vars #537

Conversation

David1Socha
Copy link
Contributor

Should resolve #460 .

Some notes on the approach taken:

Dotnet has Environment.ExpandEnvironmentVariables which seems like it'd be suitable for expanding any nested environment variables, but by design that method only supports Windows-style %VARIABLES%. I could not find any ready-made .NET functions or util libraries that fully addressed the concerns flagged in issue 460, so I added some ad-hoc checks and updates to the GetCiv3Path method in Civ3Location.cs.

In Util.cs, I noticed that its GetCiv3Path method seems to be unused at the moment, with the Civ3Root property forwarding out to Civ3Location.GetCiv3Path. I thought it made sense to consolidate the similar GetCiv3Path methods in Util.cs and Civ3Location.cs to a single method, so I updated Util.GetCiv3Path to point to Civ3Location.GetCiv3Path (but first checking C7Settings for a saved civ3 location, since that seemed like a significant difference between the two methods). I'd be happy to either fully remove Util.GetCiv3Path or restore its original method body and then also add my changes to that method if either of those would be preferred.

I updated the GetHome function from using Environment.SpecialFolder.Personal to using Environment.SpecialFolder.UserProfile , this is because .NET 8 (which I see is on the roadmap) has a breaking change to SpecialFolder.Personal changing behavior for Linux systems from returning $HOME to returning "XDG_DOCUMENTS_DIR if available; otherwise $HOME/Documents". Environment.SpecialFolder.UserProfile returns $HOME under both .NET 7 and .NET 8.

Tested relative directory handling and environment variable replacement on a local Windows game build with these changes, both worked successfully. I tested the logic for relative directory handling, Unix-style environment variable replacement, and ~ home path replacement in a Console app in WSL and that also worked as expected. Haven't tested the full game build with these changes on Linux or OSX because I don't currently have setups for those operating systems, but if that's needed before merge I can setup a Linux VM and try to validate there as well.

using ConvertCiv3Media;
using C7GameData;
using Godot;
using QueryCiv3;

public partial class Util {
static public string Civ3Root = Civ3Location.GetCiv3Path();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly relevant to this PR, but I was wondering if this property is truly meant to use Civ3Location.GetCiv3Path() and not the class method Util.GetCiv3Path(). Currently Util.GetCiv3Path has no refs, and the C7Settings.GetSettingValue check seems like something that might be expected when calling Util.Civ3Root. From git history it looks like this property was updated 2 years ago, but I wanted to double check if this setup was intentional

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't say one way or another if it was intentional, but if it doesn't seem needed it might be worth a separate PR to change it.

Copy link
Contributor

@TomWerner TomWerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I confirmed this worked correctly on Linux when using a CIV3_HOME with ~ in it.

@TomWerner TomWerner merged commit 6cfb51e into C7-Game:Development Feb 7, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle variables in file paths
2 participants