2016-08-27 09:39:14 +12:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2022-01-12 05:32:17 +13:00
|
|
|
|
Copyright (c) 2007-2022 ShareX Team
|
2016-08-27 09:39:14 +12:00
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
using Steamworks;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace ShareX.Steam
|
|
|
|
|
{
|
|
|
|
|
public static class Launcher
|
|
|
|
|
{
|
2021-10-25 02:38:11 +13:00
|
|
|
|
private static string ContentFolderPath = GetContentFolderPath();
|
|
|
|
|
private static string ContentExecutablePath = Path.Combine(ContentFolderPath, "ShareX.exe");
|
|
|
|
|
private static string ContentSteamFilePath = Path.Combine(ContentFolderPath, "Steam");
|
|
|
|
|
private static string UpdatingTempFilePath = Path.Combine(ContentFolderPath, "Updating");
|
2016-08-27 09:39:14 +12:00
|
|
|
|
|
2021-10-25 02:38:11 +13:00
|
|
|
|
private static string UpdateFolderPath = Helpers.GetAbsolutePath("Updates");
|
|
|
|
|
private static string UpdateExecutablePath = Path.Combine(UpdateFolderPath, "ShareX.exe");
|
|
|
|
|
|
2021-10-26 23:39:58 +13:00
|
|
|
|
private static bool IsFirstTimeRunning, IsStartupRun, ShowInApp, IsSteamInit;
|
2021-10-27 00:15:49 +13:00
|
|
|
|
private static Stopwatch SteamInitStopwatch;
|
2016-08-27 09:39:14 +12:00
|
|
|
|
|
|
|
|
|
public static void Run(string[] args)
|
|
|
|
|
{
|
|
|
|
|
if (Helpers.IsCommandExist(args, "-uninstall"))
|
|
|
|
|
{
|
|
|
|
|
UninstallShareX();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsStartupRun = Helpers.IsCommandExist(args, "-silent");
|
|
|
|
|
|
2021-10-25 02:38:11 +13:00
|
|
|
|
#if DEBUG
|
|
|
|
|
ShowInApp = true;
|
|
|
|
|
#else
|
|
|
|
|
ShowInApp = File.Exists(ContentSteamFilePath);
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-08-27 09:39:14 +12:00
|
|
|
|
if (!IsShareXRunning())
|
|
|
|
|
{
|
2021-10-26 23:39:58 +13:00
|
|
|
|
// If running on startup and need to show "In-app" then wait for Steam to run.
|
2016-08-27 09:39:14 +12:00
|
|
|
|
if (IsStartupRun && ShowInApp)
|
|
|
|
|
{
|
2021-10-25 01:34:11 +13:00
|
|
|
|
for (int i = 0; i < 30 && !SteamAPI.IsSteamRunning(); i++)
|
2016-08-27 09:39:14 +12:00
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SteamAPI.IsSteamRunning())
|
|
|
|
|
{
|
2021-10-26 23:39:58 +13:00
|
|
|
|
// Even "IsSteamRunning" is true still Steam API init can fail, therefore need to give more time for Steam to launch.
|
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
|
{
|
|
|
|
|
IsSteamInit = SteamAPI.Init();
|
|
|
|
|
|
|
|
|
|
if (IsSteamInit)
|
|
|
|
|
{
|
2021-10-27 00:15:49 +13:00
|
|
|
|
SteamInitStopwatch = Stopwatch.StartNew();
|
2021-10-26 23:39:58 +13:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
}
|
2016-08-27 09:39:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IsUpdateRequired())
|
|
|
|
|
{
|
2021-10-27 00:15:49 +13:00
|
|
|
|
UpdateShareX();
|
2016-08-27 09:39:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 23:39:58 +13:00
|
|
|
|
if (IsSteamInit)
|
2016-08-27 09:39:14 +12:00
|
|
|
|
{
|
|
|
|
|
SteamAPI.Shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (File.Exists(ContentExecutablePath))
|
|
|
|
|
{
|
|
|
|
|
string arguments = "";
|
|
|
|
|
|
|
|
|
|
if (IsFirstTimeRunning)
|
|
|
|
|
{
|
2021-10-26 23:39:58 +13:00
|
|
|
|
// Show first time config window.
|
2016-08-27 09:39:14 +12:00
|
|
|
|
arguments = "-SteamConfig";
|
|
|
|
|
}
|
|
|
|
|
else if (IsStartupRun)
|
|
|
|
|
{
|
2021-10-26 23:39:58 +13:00
|
|
|
|
// Don't show ShareX main window.
|
2016-08-27 09:39:14 +12:00
|
|
|
|
arguments = "-silent";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RunShareX(arguments);
|
|
|
|
|
|
2021-10-26 23:39:58 +13:00
|
|
|
|
if (IsSteamInit)
|
2016-08-27 09:39:14 +12:00
|
|
|
|
{
|
2021-10-26 23:39:58 +13:00
|
|
|
|
// Reason for this workaround is because Steam only allows writing review if user is played the game at least 5 minutes.
|
|
|
|
|
// For this reason ShareX launcher will stay on for at least 10 seconds to let users eventually reach 5 minutes play time.
|
2021-10-27 00:15:49 +13:00
|
|
|
|
int waitTime = 10000;
|
|
|
|
|
|
|
|
|
|
if (SteamInitStopwatch != null)
|
|
|
|
|
{
|
|
|
|
|
waitTime -= (int)SteamInitStopwatch.ElapsedMilliseconds;
|
|
|
|
|
}
|
2016-08-27 09:39:14 +12:00
|
|
|
|
|
|
|
|
|
if (waitTime > 0)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(waitTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-25 02:38:11 +13:00
|
|
|
|
private static string GetContentFolderPath()
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
string path = Helpers.GetAbsolutePath(@"..\..\..\ShareX\bin\Debug");
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return Helpers.GetAbsolutePath("ShareX");
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-27 09:39:14 +12:00
|
|
|
|
private static bool IsShareXRunning()
|
|
|
|
|
{
|
2021-10-26 23:39:58 +13:00
|
|
|
|
// Check ShareX mutex.
|
2016-08-27 09:39:14 +12:00
|
|
|
|
return Helpers.IsRunning("82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool IsUpdateRequired()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-10-25 02:38:11 +13:00
|
|
|
|
// Update not exists?
|
|
|
|
|
if (!File.Exists(UpdateExecutablePath))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-27 09:39:14 +12:00
|
|
|
|
// First time running?
|
2021-10-25 02:38:11 +13:00
|
|
|
|
if (!File.Exists(ContentExecutablePath))
|
2016-08-27 09:39:14 +12:00
|
|
|
|
{
|
|
|
|
|
IsFirstTimeRunning = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Need repair?
|
|
|
|
|
if (File.Exists(UpdatingTempFilePath))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Need update?
|
|
|
|
|
FileVersionInfo contentVersionInfo = FileVersionInfo.GetVersionInfo(ContentExecutablePath);
|
|
|
|
|
FileVersionInfo updateVersionInfo = FileVersionInfo.GetVersionInfo(UpdateExecutablePath);
|
|
|
|
|
|
|
|
|
|
return Helpers.CompareVersion(contentVersionInfo.FileVersion, updateVersionInfo.FileVersion) < 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Helpers.ShowError(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 00:15:49 +13:00
|
|
|
|
private static void UpdateShareX()
|
2016-08-27 09:39:14 +12:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(ContentFolderPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(ContentFolderPath);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 23:39:58 +13:00
|
|
|
|
// In case updating process terminate middle of it, allow launcher to repair ShareX.
|
2017-04-25 04:29:19 +12:00
|
|
|
|
Helpers.CreateEmptyFile(UpdatingTempFilePath);
|
2016-08-27 09:39:14 +12:00
|
|
|
|
Helpers.CopyAll(UpdateFolderPath, ContentFolderPath);
|
|
|
|
|
File.Delete(UpdatingTempFilePath);
|
2017-04-25 04:29:19 +12:00
|
|
|
|
|
|
|
|
|
if (IsFirstTimeRunning)
|
|
|
|
|
{
|
|
|
|
|
Helpers.CreateEmptyFile(ContentSteamFilePath);
|
|
|
|
|
}
|
2016-08-27 09:39:14 +12:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Helpers.ShowError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void RunShareX(string arguments = "")
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (ShowInApp)
|
|
|
|
|
{
|
2018-12-07 07:51:41 +13:00
|
|
|
|
using (Process process = new Process())
|
2016-08-27 09:39:14 +12:00
|
|
|
|
{
|
2018-12-07 07:51:41 +13:00
|
|
|
|
ProcessStartInfo psi = new ProcessStartInfo()
|
|
|
|
|
{
|
|
|
|
|
FileName = ContentExecutablePath,
|
|
|
|
|
Arguments = arguments,
|
|
|
|
|
UseShellExecute = true
|
|
|
|
|
};
|
2016-08-27 09:39:14 +12:00
|
|
|
|
|
2018-12-07 07:51:41 +13:00
|
|
|
|
process.StartInfo = psi;
|
|
|
|
|
process.Start();
|
|
|
|
|
}
|
2016-08-27 09:39:14 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-10-26 23:39:58 +13:00
|
|
|
|
// Workaround to don't show "In-app".
|
2016-08-27 09:39:14 +12:00
|
|
|
|
uint result = Helpers.WinExec($"\"{ContentExecutablePath}\" {arguments}", 5);
|
|
|
|
|
|
2021-10-26 23:39:58 +13:00
|
|
|
|
// If the function succeeds, the return value is greater than 31.
|
2016-08-27 09:39:14 +12:00
|
|
|
|
if (result > 31)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-16 20:09:01 +12:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
2016-08-27 09:39:14 +12:00
|
|
|
|
|
2021-10-26 23:39:58 +13:00
|
|
|
|
// Workaround 2.
|
2016-08-27 09:39:14 +12:00
|
|
|
|
string path = Path.Combine(Environment.SystemDirectory, "cmd.exe");
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
path = "cmd.exe";
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-07 07:51:41 +13:00
|
|
|
|
using (Process process = new Process())
|
2016-08-27 09:39:14 +12:00
|
|
|
|
{
|
2018-12-07 07:51:41 +13:00
|
|
|
|
ProcessStartInfo psi = new ProcessStartInfo()
|
|
|
|
|
{
|
|
|
|
|
FileName = path,
|
|
|
|
|
Arguments = $"/C start \"\" \"{ContentExecutablePath}\" {arguments}",
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
CreateNoWindow = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
process.StartInfo = psi;
|
|
|
|
|
process.Start();
|
|
|
|
|
}
|
2016-08-27 09:39:14 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Helpers.ShowError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void UninstallShareX()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
while (IsShareXRunning())
|
|
|
|
|
{
|
|
|
|
|
if (MessageBox.Show("ShareX is currently running.\r\n\r\nPlease close ShareX and press \"Retry\" button after it is closed.", "ShareX - Uninstaller",
|
|
|
|
|
MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(ContentFolderPath))
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(ContentExecutablePath))
|
|
|
|
|
{
|
2018-12-07 07:51:41 +13:00
|
|
|
|
using (Process process = new Process())
|
2016-08-27 09:39:14 +12:00
|
|
|
|
{
|
2018-12-07 07:51:41 +13:00
|
|
|
|
ProcessStartInfo psi = new ProcessStartInfo()
|
|
|
|
|
{
|
|
|
|
|
FileName = ContentExecutablePath,
|
|
|
|
|
Arguments = "-uninstall"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
process.StartInfo = psi;
|
|
|
|
|
process.Start();
|
2016-08-27 09:39:14 +12:00
|
|
|
|
process.WaitForExit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Directory.Delete(ContentFolderPath, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Helpers.ShowError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|