Use detected Steam locations, update launch options

This commit is contained in:
Norbi Peti 2021-01-20 18:31:34 +01:00
parent 5aa0f788c3
commit b2d9b1073f
4 changed files with 102 additions and 51 deletions

View file

@ -26,6 +26,7 @@ namespace GCMM
private readonly ModInfo gcipa = new ModInfo { Author = "modtainers", Name = "GCIPA" };
private readonly ModInfo gcmm = new ModInfo { Author = "NorbiPeti", Name = "GCMM" };
private DateTime lastGameUpdateTime;
private string steamPath;
private const string defaultInfo = @"
Gamecraft Mod Manager
@ -64,7 +65,27 @@ You may also want to verify the game's files by right clicking the game in Steam
mods.Clear(); //This method may get called twice when ran from the command line
UpdateButton(installbtn, false);
modinfobox.Text = defaultInfo;
GetSteamLocationAndUser(); //TODO: If user is null then start Steam
var (steamPath, user) = GetSteamLocationAndUser(); //TODO: If user is 0 then start Steam
if (steamPath != null)
this.steamPath = steamPath;
else
{
MessageBox.Show("Steam not found! If you have Steam installed, please report this to ExMods.\n\nThe Steam install is checked to autodetect where the game is installed and to optionally configure auto-patching.", "Steam not found");
status.Text = "Status: Steam not found";
return;
}
if (Settings.Default.SteamUserID == 0 && Settings.Default.AutoLaunch)
{
if (MessageBox.Show("Do you want GCMM to change the game's launch settings so it can ensure the game is patched?\n\n" +
"If you say yes, GCMM will do a quick check before the game is launched and patches if necessary. " +
"This way you (hopefully) won't see crashes after a Gamecraft update.\n\n" +
"Note that this also means that if you (re)move GCMM without disabling this in the settings then you won't be able to launch Gamecraft.",
"GCMM auto-patching", MessageBoxButtons.YesNo) == DialogResult.Yes)
DetectConfigLocationAndAutoStart(steamPath, ref user);
else
Settings.Default.AutoLaunch = false;
Settings.Default.Save();
}
if (string.IsNullOrWhiteSpace(Settings.Default.GamePath) || GetExe() == null)
{
Settings.Default.GamePath = GetGameFolder();
@ -82,34 +103,6 @@ You may also want to verify the game's files by right clicking the game in Steam
DeleteEmptyPluginsDir(out bool pexists, out bool dexists);
if (!pexists && dexists)
unpatched.Checked = true; //It will call the event but that won't do anything
if(Settings.Default.AutoLaunch && string.IsNullOrWhiteSpace(Settings.Default.SteamConfigFileForAutoLaunch))
{
string path = @"C:\Program Files (x86)\Steam\userdata";
if(MessageBox.Show("Do you want GCMM to change the game's launch settings so it can ensure the game is patched?\n\n" +
"If you say yes, GCMM will do a quick check before the game is launched and updates if necessary. " +
"This way you (hopefully) won't see crashes after a Gamecraft update.\n\n" +
"Note that this also means that if you (re)move GCMM without disabling this then you won't be able to launch Gamecraft.",
"GCMM auto-patching", MessageBoxButtons.YesNo)==DialogResult.Yes)
{
var dirs = Directory.GetDirectories(path);
var goodPaths = (from dir in dirs
where File.Exists(dir + @"\config\localconfig.vdf")
select dir + @"\config\localconfig.vdf").ToArray();
if (goodPaths.Length != 1)
path = SelectSteamConfigFile();
else
path = goodPaths[0];
//if (path is not null)
if (path != null)
{
Settings.Default.SteamConfigFileForAutoLaunch = path;
UpdateSteamConfigToAutoStart(true);
}
else
Settings.Default.AutoLaunch = false;
Settings.Default.Save();
}
}
await RefreshEverything();
}

View file

@ -36,7 +36,7 @@ namespace GCMM
{ //TODO
string libs;
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
libs = Settings.Default.SteamConfigFileForAutoLaunch + @"\steamapps\libraryfolders.vdf"; //TODO: Not the Steam folder anymore!
libs = Settings.Default.SteamUserID + @"\steamapps\libraryfolders.vdf"; //TODO: Not the Steam folder anymore!
else
return null;
foreach (var line in File.ReadAllLines(libs).Concat(new[] {@"C:\Program Files (x86)\Steam\"}))
@ -68,23 +68,48 @@ namespace GCMM
return Directory.GetParent(ofd.FileName).FullName;
}
public string SelectSteamConfigFile()
public (string, int) AskForSteamLogin()
{
MessageBox.Show("Please select your Steam config location in the next dialog. It's at Steam\\userdata\\<YourID>\\config\\localconfig.vdf");
var ofd = new OpenFileDialog();
ofd.Filter = "Steam config|localconfig.vdf";
ofd.Title = "Steam config location (Steam\\userdata\\<YourID>\\config\\localconfig.vdf)";
ofd.InitialDirectory = @"C:\Program Files (x86)\Steam\\userdata\"; //TODO
ofd.CheckFileExists = true;
ofd.ShowDialog();
if (string.IsNullOrWhiteSpace(ofd.FileName))
return null;
return ofd.FileName;
while (MessageBox.Show("Couid not find your Steam configuration to set launch options.\n\n" +
"Please make sure you are logged into Steam and click Retry or click Cancel to skip setting this up.",
"Steam config not found", MessageBoxButtons.RetryCancel) != DialogResult.Cancel)
{
var ret = GetSteamLocationAndUser();
if (ret != (null, 0))
return ret;
}
return (null, 0);
}
private void UpdateSteamConfigToAutoStart(bool autoLaunch)
private bool UpdateOrGetSteamConfigToAutoStart(bool? autoLaunch)
{
//TODO
var regex = new Regex(@"(\t{6}""LaunchOptions""\t+"")(.*)("")");
string path = steamPath + @"\userdata\" + Settings.Default.SteamUserID + @"\config\localconfig.vdf";
var lines = File.ReadAllLines(path);
bool shouldMatch = false;
bool ret = false;
for (int i = 0; i < lines.Length; i++)
{
if (lines[i] == "\t\t\t\t\t\"1078000\"")
shouldMatch = true; //Found the game
else if(shouldMatch)
{
var match = regex.Match(lines[i]);
if (!match.Success)
continue;
if (autoLaunch.HasValue)
{
if (autoLaunch.Value)
lines[i] = match.Groups[1].Value + Application.ExecutablePath + " -start %command%" + match.Groups[3].Value;
else
lines[i] = match.Groups[1].Value + match.Groups[2].Value;
File.WriteAllLines(path, lines);
}
ret = match.Groups[2].Value.Contains("GCMM.exe");
break;
}
}
return ret;
}
private (string, int) GetSteamLocationAndUser()
@ -92,11 +117,44 @@ namespace GCMM
if (Environment.OSVersion.Platform != PlatformID.Win32NT) return (null, 0);
using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Valve\Steam\ActiveProcess"))
{
string path = Directory.GetParent((string)key.GetValue("SteamClientDll")).FullName;
return (path, (int)key.GetValue("ActiveUser"));
string path = (string)key?.GetValue("SteamClientDll");
path = path != null ? Directory.GetParent(path).FullName : null;
return (path, (int)(key.GetValue("ActiveUser") ?? 0));
}
}
private void DetectConfigLocationAndAutoStart(string steamPath, ref int user)
{
string path = steamPath + @"\userdata";
if (user == 0)
{
var dirs = Directory.GetDirectories(path);
var goodPaths = (from dir in dirs
where File.Exists(dir + @"\config\localconfig.vdf")
select dir).ToArray();
if (goodPaths.Length != 1)
{
(_, user) = AskForSteamLogin();
path += user;
}
else
{
path = goodPaths[0];
user = int.Parse(Path.GetFileName(path));
}
}
else
path += user;
path += @"\config\localconfig.vdf";
if (path != null && user != 0 && File.Exists(path))
{
Settings.Default.SteamUserID = user;
UpdateOrGetSteamConfigToAutoStart(true);
}
else
Settings.Default.AutoLaunch = false;
}
private void CheckStartGame(object sender, EventArgs e)
{
Action act = () =>

View file

@ -73,13 +73,13 @@ namespace GCMM.Properties {
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string SteamConfigFileForAutoLaunch {
[global::System.Configuration.DefaultSettingValueAttribute("0")]
public int SteamUserID {
get {
return ((string)(this["SteamConfigFileForAutoLaunch"]));
return ((int)(this["SteamUserID"]));
}
set {
this["SteamConfigFileForAutoLaunch"] = value;
this["SteamUserID"] = value;
}
}

View file

@ -14,8 +14,8 @@
<Setting Name="NeedsUpdate" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="SteamConfigFileForAutoLaunch" Type="System.String" Scope="User">
<Value Profile="(Default)" />
<Setting Name="SteamUserID" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="AutoLaunch" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>