Steam --> launcher, autolaunch support in progress, localization

This commit is contained in:
Norbi Peti 2021-05-16 00:29:11 +02:00
parent c43446e2cb
commit 0551535455
8 changed files with 130 additions and 202 deletions

View file

@ -0,0 +1,9 @@
namespace GCMM
{
public enum AutoPatchingState
{
Unspecified,
Disabled,
Enabled
}
}

19
GCMM/DialogUtils.cs Normal file
View file

@ -0,0 +1,19 @@
using System.Windows.Forms;
namespace GCMM
{
public static class DialogUtils
{
public static void ShowInfo(string message, string title) =>
MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
public static void ShowError(string message, string title) =>
MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
public static void ShowWarning(string message, string title) =>
MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
public static bool ShowYesNoQuestion(string message, string title) =>
MessageBox.Show(message, title, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes;
}
}

View file

@ -2,6 +2,7 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
@ -20,34 +21,33 @@ namespace GCMM
public MainForm()
{
InitializeComponent();
resources = new ComponentResourceManager(typeof(MainForm));
}
private readonly ComponentResourceManager resources;
private readonly Dictionary<string, ModInfo> mods = new Dictionary<string, ModInfo>();
private readonly ModInfo gcipa = new ModInfo { Author = "modtainers", Name = "GCIPA" };
private readonly ModInfo gcmm = new ModInfo { Author = "NorbiPeti", Name = "GCMM" };
private DateTime lastGameUpdateTime;
#if USING_STEAM
private string steamPath;
#endif
private const string defaultInfo = @"
Techblox Mod Manager
If you click on a mod it will show some info about it. The install instructions there are usually for manual installs.
To get started, click on a mod and select Install mod. Most mods need TechbloxModdingAPI as well so it'll be installed.
Then launch Techblox: if you enabled auto-patching then you can use Steam but if you didn't then you must use the Play button.
Then launch Techblox: if you enabled auto-patching then you can use the launcher but if you didn't then you must use the Play button.
This will first download and run the patcher (GCIPA) if needed. If all goes well, after some time a modded Techblox should launch.
After a Techblox update there's a good chance that mods will break. If this happens you may get errors when trying to start Techblox.
Until updated versions are released, use the ""Disable mods"" checkbox at the bottom to launch the game without mods.
If you enabled auto-patching you will get a warning about this.
If you don't have auto-patching enabled then you will need to run the mod manager each time Techblox updates and click ""Patch & Play"".
If you don't have auto-patching enabled then you will need to run the mod manager each time Techblox updates and click ""Patch & Play"" or the game may not function properly.
Disclaimer:
This mod manager and the mods in the list are made by the ExMods developers. We are not associated with Freejam or Techblox. Modify Techblox at your own risk.
If you encounter an issue while any mods are installed, report it to us. If you think it's an issue with the game, test again with the ""Disable mods"" option checked before reporting to Freejam.
You may also want to verify the game's files by clicking on the Validate game button.
You may also want to verify the game's files in the launcher.
";
private async void Form1_Load(object sender, EventArgs e)
@ -68,41 +68,28 @@ You may also want to verify the game's files by clicking on the Validate game bu
mods.Clear(); //This method may get called twice when ran from the command line
UpdateButton(installbtn, false);
modinfobox.Text = defaultInfo;
#if USING_STEAM
var (steamPath, user) = GetSteamLocationAndUser();
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 Techblox 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 Techblox until you change the launch options in Steam.",
"GCMM auto-patching", MessageBoxButtons.YesNo) == DialogResult.Yes)
DetectConfigLocationAndAutoStart(steamPath, ref user);
else
Settings.Default.AutoLaunch = false;
Settings.Default.Save();
}
#endif
if (string.IsNullOrWhiteSpace(Settings.Default.GamePath) || GetExe() == null)
{
Settings.Default.GamePath = GetGameFolder();
if (string.IsNullOrWhiteSpace(Settings.Default.GamePath))
{
DialogUtils.ShowWarning(resources.GetString("Game_not_found"), "");
Settings.Default.GamePath = SelectGameFolder();
}
else
MessageBox.Show("Found game at " + Settings.Default.GamePath);
DialogUtils.ShowInfo(string.Format(resources.GetString("Found_game_at"), Settings.Default.GamePath), "");
Settings.Default.Save();
}
if (Settings.Default.AutoLaunch == AutoPatchingState.Unspecified)
{
Settings.Default.AutoLaunch = AutoPatchingState.Disabled;
if (DialogUtils.ShowYesNoQuestion(resources.GetString("Change_launch_settings_question"),
resources.GetString("Change_launch_settings_title")))
EnableDisableAutoPatchingWithDialog(true);
}
if(string.IsNullOrWhiteSpace(Settings.Default.GamePath))
{
status.Text = "Status: Game not found";
status.Text = resources.GetString("Status_Game_not_found");
return;
}
DeleteEmptyPluginsDir(out bool pexists, out bool dexists);

View file

@ -1867,4 +1867,29 @@
APAIAADhCAAAw48AAMf/AAD//wAA//8AAA==
</value>
</data>
<data name="Found_game_at" xml:space="preserve">
<value>Found game at {0}</value>
</data>
<data name="Game_not_found" xml:space="preserve">
<value>Game not found. Is it installed correctly? Anyway, please locate the game.</value>
</data>
<data name="Status_Game_not_found" xml:space="preserve">
<value>Status: Game not found</value>
</data>
<data name="Change_launch_settings_question" xml:space="preserve">
<value>Do you want TBMM to change the game's launch settings so it can ensure the game is patched?
If you say yes, TBMM will do a quick check before the game is launched and patches if necessary. This way you (hopefully) won't see crashes after a Techblox update.
Note that this also means that if you (re)move TBMM without disabling this in the settings then you won't be able to launch Techblox until you reinstall TBMM or fix the launcher configuration.</value>
</data>
<data name="Change_launch_settings_title" xml:space="preserve">
<value>TBMM auto-patching</value>
</data>
<data name="Change_launch_settings_done" xml:space="preserve">
<value>Launcher settings updated.</value>
</data>
<data name="Change_launch_settings_error" xml:space="preserve">
<value>Failed to update launcher settings! You can still run TBMM manually or retry in settings.</value>
</data>
</root>

View file

@ -32,28 +32,53 @@ namespace GCMM
}
}
private bool EnableDisableAutoPatching(bool enable)
{
string launcherConfig =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Techblox Launcher", "launcher_settings.ini");
bool gamePathSet = false;
if (File.Exists(launcherConfig))
{
string[] lines = File.ReadLines(launcherConfig).Select(line =>
{
if (line.StartsWith("133062..GAME_PATH=") && line.Contains('/'))
{
gamePathSet = true;
return
$"{line.Substring(0, Math.Max(line.LastIndexOf("TBMM", StringComparison.Ordinal), line.Length))}{(enable ? "TBMM/" : "")}";
}
return line;
}).ToArray(); //Need to close the file
File.WriteAllLines(launcherConfig, lines);
}
return gamePathSet;
}
public void EnableDisableAutoPatchingWithDialog(bool enable)
{
if (EnableDisableAutoPatching(enable))
{
DialogUtils.ShowInfo(resources.GetString("Change_launch_settings_done"),
resources.GetString("Change_launch_settings_title"));
Settings.Default.AutoLaunch = enable ? AutoPatchingState.Enabled : AutoPatchingState.Disabled;
}
else
DialogUtils.ShowError(resources.GetString("Change_launch_settings_error"),
resources.GetString("Change_launch_settings_title"));
}
public string GetGameFolder()
{
#if USING_STEAM
string libs;
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
libs = steamPath + @"\steamapps\libraryfolders.vdf";
else
string launcherConfig =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Techblox Launcher", "launcher_settings.ini");
if (File.Exists(launcherConfig))
return File.ReadLines(launcherConfig).FirstOrDefault(line => line.StartsWith("133062..GAME_PATH="))
?.Substring("133062..GAME_PATH=".Length);
return null;
foreach (var line in File.ReadAllLines(libs).Concat(new[] { "\t\"19\"\t\t\"" + steamPath + "\"" }))
{
var regex = new Regex("\\t\"\\d+\"\\t\\t\"(.+)\"");
var match = regex.Match(line);
if (!match.Success)
continue;
string library = match.Groups[1].Value.Replace("\\\\", "\\");
library += @"\steamapps\common\";
if (GetExe(library + "Techblox") != null)
return library + "Techblox";
}
return null;
#endif
return null; //TODO
}
public string SelectGameFolder()
@ -61,129 +86,11 @@ namespace GCMM
var ofd = new OpenFileDialog();
ofd.Filter = "Techblox executable|Techblox.exe|Techblox Preview executable|TechbloxPreview.exe";
ofd.Title = "Game location";
#if USING_STEAM
ofd.InitialDirectory = steamPath + @"\steamapps\common\";
#endif //TODO
ofd.CheckFileExists = true;
ofd.ShowDialog();
if (string.IsNullOrWhiteSpace(ofd.FileName))
return null;
return Directory.GetParent(ofd.FileName).FullName;
return string.IsNullOrWhiteSpace(ofd.FileName) ? null : Directory.GetParent(ofd.FileName)?.FullName;
}
#if USING_STEAM
public (string, int) AskForSteamLogin()
{
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);
}
/// <summary>
/// Does not return the current value if setting it is not possible because Steam is running.
/// </summary>
/// <param name="autoLaunch">The value to set or null to keep as is</param>
/// <returns>The current value, unless setting it while Steam is running</returns>
public bool UpdateOrGetSteamConfigToAutoStart(bool? autoLaunch)
{
string commandToUse = Application.ExecutablePath + " -start %command%";
if (autoLaunch.HasValue && Process.GetProcessesByName("steam").Length > 0)
{ //Setting it while Steam is running
if (MessageBox.Show("Cannot set launch options while Steam is running." +
(autoLaunch.Value
? " Do you want to do it manually? If so, it will be copied on your clipboard." +
" Right click the game, select Properties and paste it in the launch options field."
: " Do you want to do it manually?" +
" If so, right click the game, select Properties and remove the text from the launch options field."),
"Launch options in Steam", MessageBoxButtons.YesNo) == DialogResult.Yes && autoLaunch.Value)
Clipboard.SetText(commandToUse);
return false;
}
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;
ret = match.Groups[2].Value.Contains("GCMM.exe");
string enabledCommand = match.Groups[1].Value + commandToUse.Replace("\\", "\\\\") + match.Groups[3].Value;
if (autoLaunch.HasValue)
{
if (autoLaunch.Value)
lines[i] = enabledCommand;
else
lines[i] = match.Groups[1].Value + match.Groups[3].Value;
File.WriteAllLines(path, lines);
}
else if (ret && lines[i] != enabledCommand) //GCMM got moved or something and it's only queried, not set
{
lines[i] = enabledCommand;
File.WriteAllLines(path, lines);
}
break;
}
}
return ret;
}
public (string, int) GetSteamLocationAndUser()
{
if (Environment.OSVersion.Platform != PlatformID.Win32NT) return (null, 0);
using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Valve\Steam\ActiveProcess"))
{
string path = (string)key?.GetValue("SteamClientDll");
path = path != null ? Directory.GetParent(path).FullName : null;
return (path, (int)(key?.GetValue("ActiveUser") ?? 0));
}
}
public 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;
}
#endif
private (EventHandler, Task) CheckStartGame(string command)
{
var tcs = new TaskCompletionSource<object>();
@ -204,15 +111,11 @@ namespace GCMM
await CheckModUpdatesAsync();
Process.Start(command);
}
#if USING_STEAM
else if (Environment.OSVersion.Platform == PlatformID.Win32NT)
Process.Start("steam://run/1078000/");
else
Process.Start("xdg-open", "steam://run/1078000/");
#else
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
Process.Start(GamePath("\\" + GetExe()));
#endif
Process.Start(new ProcessStartInfo(GamePath("\\" + GetExe()))
{
WorkingDirectory = GamePath("\\") //Mods are only loaded if the working directory is correct
});
EndWork(false);
tcs.SetResult(null);
};

View file

@ -85,10 +85,10 @@ namespace GCMM.Properties {
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool AutoLaunch {
[global::System.Configuration.DefaultSettingValueAttribute("0")]
public AutoPatchingState AutoLaunch {
get {
return ((bool)(this["AutoLaunch"]));
return ((AutoPatchingState)(this["AutoLaunch"]));
}
set {
this["AutoLaunch"] = value;

View file

@ -1,4 +1,4 @@
<?xml version='1.0' encoding='utf-8'?>
<?xml version="1.0" encoding="UTF-8"?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="GCMM.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
@ -17,8 +17,9 @@
<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>
<Setting Name="AutoLaunch" Type="GCMM.AutoPatchingState" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
</Settings>
</SettingsFile>

View file

@ -16,9 +16,7 @@ namespace GCMM
public partial class SettingsForm : Form
{
private MainForm mainForm;
#if USING_STEAM
private bool autopatchingEnabled;
#endif
public SettingsForm()
{
InitializeComponent();
@ -29,10 +27,8 @@ namespace GCMM
gamelocation.Text = Settings.Default.GamePath;
useProxy.Checked = Settings.Default.UseProxy;
mainForm = Owner as MainForm;
#if USING_STEAM
autopatchingEnabled = Settings.Default.SteamUserID != 0 && mainForm.UpdateOrGetSteamConfigToAutoStart(null);
autopatchingEnabled = Settings.Default.AutoLaunch == AutoPatchingState.Enabled;
autopatching.Checked = autopatchingEnabled;
#endif
}
private void browsebtn_Click(object sender, EventArgs e)
@ -44,20 +40,8 @@ namespace GCMM
{
Settings.Default.GamePath = gamelocation.Text;
Settings.Default.UseProxy = useProxy.Checked;
#if USING_STEAM
if (autopatching.Checked != autopatchingEnabled)
{
if (autopatching.Checked && Settings.Default.SteamUserID == 0)
{
var (steamPath, user) = mainForm.GetSteamLocationAndUser();
if (user != 0)
mainForm.DetectConfigLocationAndAutoStart(steamPath, ref user);
Settings.Default.SteamUserID = user; //If it's 0 then it's no change
}
else
mainForm.UpdateOrGetSteamConfigToAutoStart(autopatching.Checked);
}
#endif
mainForm.EnableDisableAutoPatchingWithDialog(autopatching.Checked);
Settings.Default.Save();
Close();
}