Skip to content

Commit

Permalink
Added logic to pull launching application to forground. Added delay p…
Browse files Browse the repository at this point in the history
…ost shortcut rename with app.config setting. Fixed shortcut rename function in Steam4NET to use UTF8 marshal.
  • Loading branch information
Nielk1 committed Sep 18, 2016
1 parent 899fd7f commit 57b131c
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 55 deletions.
9 changes: 6 additions & 3 deletions GameLaunchProxy/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<appSettings>
<add key="SteamShortcutAfterRenameDelay" value="1000" />
</appSettings>
</configuration>
2 changes: 2 additions & 0 deletions GameLaunchProxy/GameLaunchProxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Data.HashFunction.Core, Version=1.8.2.2, Culture=neutral, PublicKeyToken=80c9288e394c1322, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.HashFunction.Core.1.8.2.2\lib\net45\System.Data.HashFunction.Core.dll</HintPath>
Expand Down Expand Up @@ -172,6 +173,7 @@
<ProjectReference Include="..\Steam4NET2\Steam4NET.csproj">
<Project>{1a204257-fe82-4bbe-9ed4-40694821c31f}</Project>
<Name>Steam4NET</Name>
<Private>True</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
Expand Down
121 changes: 76 additions & 45 deletions GameLaunchProxy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
using System.Configuration;

namespace GameLaunchProxy
{
Expand Down Expand Up @@ -92,12 +93,22 @@ struct SteamProxyData
static Settings settings;


static int config_SteamShortcutAfterRenameDelay;

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
{
string string_SteamShortcutAfterRenameDelay = ConfigurationManager.AppSettings.Get("SteamShortcutAfterRenameDelay");
if (!int.TryParse(string_SteamShortcutAfterRenameDelay, out config_SteamShortcutAfterRenameDelay))
{
config_SteamShortcutAfterRenameDelay = 0;
}
}

LoadSettings();

LogMessage($"Start");
Expand Down Expand Up @@ -301,19 +312,31 @@ static void Main(string[] args)
UInt64 steamShortcutId = 0;
for (int x = 0; x < names.Count && steamShortcutId == 0; x++)
{
LogMessage($"Searching for item #{x}");

steamShortcutId = SteamContext.GetInstance().GetShortcutID(names[x].Item1, proxyPath, settings.Core.SteamShortcutFilePath);

LogMessage($"Shortcut ID Candidate:\t{steamShortcutId}");

if (steamShortcutId != 0 && names[x].Item2)
{
//UInt64 oldSteamShortcutId = steamShortcutId;
//string oldSteamShortcutName = names[x].Item1;
steamShortcutId = SteamContext.GetInstance().RenameLiveShortcut(steamShortcutId, name);
try {
//UInt64 oldSteamShortcutId = steamShortcutId;
//string oldSteamShortcutName = names[x].Item1;
steamShortcutId = SteamContext.GetInstance().RenameLiveShortcut(steamShortcutId, name);

if (steamShortcutId != 0)
if (steamShortcutId != 0)
{
gameRestoreData.SteamShortcutID = steamShortcutId;
gameRestoreData.OldSteamShortcutname = names[x].Item1;
File.WriteAllText("GameRestoreData.json", JsonConvert.SerializeObject(gameRestoreData));
break; // loop will terminate anyway but why not
}
}
catch
{
gameRestoreData.SteamShortcutID = steamShortcutId;
gameRestoreData.OldSteamShortcutname = names[x].Item1;
File.WriteAllText("GameRestoreData.json", JsonConvert.SerializeObject(gameRestoreData));
break; // loop will terminate anyway but why not
LogMessage($"Failed to rename {steamShortcutId}");
steamShortcutId = 0;
}
}
}
Expand Down Expand Up @@ -352,6 +375,14 @@ static void Main(string[] args)
bool AlreadyInBigPicture = SteamContext.GetInstance().BigPicturePID != 0;
SetupBigPicture(bigpicture && !AlreadyInBigPicture);

if (gameRestoreData.SteamShortcutID != 0) // did a rename
{
if (config_SteamShortcutAfterRenameDelay > 0)
{
Thread.Sleep(config_SteamShortcutAfterRenameDelay);
}
}

LogMessage($"start\tsteam://rungameid/{steamShortcutId}");
Process.Start($"steam://rungameid/{steamShortcutId}");

Expand Down Expand Up @@ -399,6 +430,43 @@ static void Main(string[] args)
CleanupBigPicture(bigpicture, AlreadyInBigPicture);

CleanupLaunchedGame();

// force parent process to front
try
{
var myId = Process.GetCurrentProcess().Id;
var query = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", myId);
var search = new ManagementObjectSearcher("root\\CIMV2", query);
var results = search.Get().GetEnumerator();
results.MoveNext();
var queryObj = results.Current;
var parentId = (uint)queryObj["ParentProcessId"];
var parent = Process.GetProcessById((int)parentId);

if (parent != null)
{
Thread.Sleep(1000);

int secondsOfAggression = 0;
do
{
{
IntPtr hwnd = parent.MainWindowHandle;
if (hwnd == IntPtr.Zero)
{
//the window is hidden so try to restore it before setting focus.
ShowWindow(parent.Handle, ShowWindowEnum.Restore);
}

//set user the focus to the window
SetForegroundWindow(parent.MainWindowHandle);
}
Thread.Sleep(1000);
secondsOfAggression++;
} while (secondsOfAggression <= 1);
}
}
catch { }
}
else
{
Expand Down Expand Up @@ -656,43 +724,6 @@ private static void CleanupBigPicture(bool performAction, bool minimize = false)

Keyboard.Key key = new Keyboard.Key(Keyboard.Messaging.VKeys.KEY_F4);
Keyboard.Messaging.ForegroundKeyPressAll(WindowToFind, key, true, false, false);



// force parent process to front
try
{
var myId = Process.GetCurrentProcess().Id;
var query = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", myId);
var search = new ManagementObjectSearcher("root\\CIMV2", query);
var results = search.Get().GetEnumerator();
results.MoveNext();
var queryObj = results.Current;
var parentId = (uint)queryObj["ParentProcessId"];
var parent = Process.GetProcessById((int)parentId);




int secondsOfAggression = 0;
do
{
{
IntPtr hwnd = parent.MainWindowHandle;
if (hwnd == IntPtr.Zero)
{
//the window is hidden so try to restore it before setting focus.
ShowWindow(parent.Handle, ShowWindowEnum.Restore);
}

//set user the focus to the window
SetForegroundWindow(parent.MainWindowHandle);
}
Thread.Sleep(1000);
secondsOfAggression++;
} while (secondsOfAggression <= 1);
}
catch { }
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions GameLaunchProxy/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.3.1")]
[assembly: AssemblyFileVersion("0.0.3.1")]
[assembly: AssemblyVersion("0.0.3.2")]
[assembly: AssemblyFileVersion("0.0.3.2")]
11 changes: 11 additions & 0 deletions GameLaunchProxy/SteamShortcutDataFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,13 @@ public VPropertyCollection()
public void Add(VProperty vProperty)
{
Properties.Add(vProperty);
NumericMemo = null; // we don't know anymore
}

public void Add(string key, VToken token)
{
Properties.Add(new VProperty(key, token));
NumericMemo = null; // we don't know anymore
}

public void Add(VToken token)
Expand All @@ -194,10 +196,12 @@ public int Remove(string key)
dr.Key = (int.Parse(dr.Key) - 1).ToString();
});
}
NumericMemo = true; // we were numeric and we removed numerics and shifted
return countRemoved;
}
else
{
NumericMemo = null; // we don't know anymore
return Properties.RemoveAll(dr => dr.Key == key);
}
}
Expand All @@ -215,21 +219,28 @@ public int Remove(VToken value)
{
dr.Key = (int.Parse(dr.Key) - 1).ToString();
});
NumericMemo = true; // we're still numeric
return countRemoved;
}
NumericMemo = true; // we're still numeric
return 0;
}
else
{
NumericMemo = null; // we don't know, we might have removed the thing that made us not numeric
return Properties.RemoveAll(dr => dr.Value == value);
}
}

private bool? NumericMemo;
public bool IsNumeric()
{
if (Properties.Count == 0)
return true;

if (NumericMemo.HasValue)
return NumericMemo.Value;

int tmp;
if (!Properties.All(dr => int.TryParse(dr.Key, out tmp)))
return false;
Expand Down
23 changes: 18 additions & 5 deletions Steam4NET2/autogen/IClientShortcuts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,27 @@ public UInt32 AddOpenVRShortcut( string arg0, string arg1, string arg2, string a
[UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate void NativeSetShortcutFromFullpathUS( IntPtr thisptr, UInt32 arg0, string arg1 );
public void SetShortcutFromFullpath( UInt32 arg0, string arg1 )
{
this.GetFunction<NativeSetShortcutFromFullpathUS>( this.Functions.SetShortcutFromFullpath27 )( this.ObjectAddress, arg0, arg1 );
}
this.GetFunction<NativeSetShortcutFromFullpathUS>( this.Functions.SetShortcutFromFullpath27 )( this.ObjectAddress, arg0, arg1 );
}

[UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate void NativeSetShortcutAppNameUS( IntPtr thisptr, UInt32 arg0, string arg1 );
//[UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate void NativeSetShortcutAppNameUS( IntPtr thisptr, UInt32 arg0, string arg1 );
[UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate void NativeSetShortcutAppNameUS( IntPtr thisptr, UInt32 arg0, IntPtr arg1 );
public void SetShortcutAppName( UInt32 arg0, string arg1 )
{
this.GetFunction<NativeSetShortcutAppNameUS>( this.Functions.SetShortcutAppName28 )( this.ObjectAddress, arg0, arg1 );
}
//this.GetFunction<NativeSetShortcutAppNameUS>( this.Functions.SetShortcutAppName28 )( this.ObjectAddress, arg0, arg1 );

byte[] _arg1 = Encoding.UTF8.GetBytes(arg1);

IntPtr i_arg1 = Marshal.AllocHGlobal(_arg1.Length + 1);

Marshal.Copy(_arg1, 0, i_arg1, _arg1.Length);

Marshal.WriteByte(i_arg1, _arg1.Length, 0x00);

this.GetFunction<NativeSetShortcutAppNameUS>(this.Functions.SetShortcutAppName28)(this.ObjectAddress, arg0, i_arg1);

Marshal.FreeHGlobal(i_arg1);
}

[UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate void NativeSetShortcutExeUS( IntPtr thisptr, UInt32 arg0, string arg1 );
public void SetShortcutExe( UInt32 arg0, string arg1 )
Expand Down

0 comments on commit 57b131c

Please sign in to comment.