Skip to content

Commit 1c52352

Browse files
committed
Add support for scripts on SteamVR shutdown
Scripts moved into subfolders Example scripts folder added
1 parent 61f6501 commit 1c52352

7 files changed

Lines changed: 69 additions & 51 deletions

File tree

OpenVRStartup/OpenVRStartup.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,17 @@
8484
<None Include="app.vrmanifest">
8585
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
8686
</None>
87+
<None Include="start\demo_start.cmd">
88+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
89+
</None>
90+
<None Include="stop\demo_stop.cmd">
91+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
92+
</None>
8793
</ItemGroup>
8894
<ItemGroup>
95+
<None Include="examples\restart_voicemeeter_audio_engine.cmd">
96+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
97+
</None>
8998
<None Include="openvr_api.dll">
9099
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
91100
</None>

OpenVRStartup/Program.cs

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.IO;
45
using System.Runtime.InteropServices;
@@ -9,14 +10,17 @@ namespace OpenVRStartup
910
{
1011
class Program
1112
{
12-
static string LOG_FILE_PATH = "./OpenVRStartup.log";
13+
static readonly string PATH_LOGFILE = "./OpenVRStartup.log";
14+
static readonly string PATH_STARTFOLDER = "./start/";
15+
static readonly string PATH_STOPFOLDER = "./stop/";
16+
static readonly string FILE_PATTERN = "*.cmd";
1317

1418
[DllImport("user32.dll")]
1519
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
1620
public const int SW_SHOWMINIMIZED = 2;
1721
private volatile static bool _isReady = false;
1822

19-
static void Main(string[] args)
23+
static void Main(string[] _)
2024
{
2125
// Starting worker
2226
var t = new Thread(Worker);
@@ -25,7 +29,7 @@ static void Main(string[] args)
2529
else LogUtils.WriteLineToCache("Error: Could not start worker thread");
2630

2731
// Check if first run, if so do NOT minimize but write instructions.
28-
if (LogUtils.LogFileExists(LOG_FILE_PATH))
32+
if (LogUtils.LogFileExists(PATH_LOGFILE))
2933
{
3034
_isReady = true;
3135
IntPtr winHandle = Process.GetCurrentProcess().MainWindowHandle;
@@ -36,7 +40,8 @@ static void Main(string[] args)
3640
Utils.PrintInfo(" First Run Instructions ");
3741
Utils.PrintInfo("========================");
3842
Utils.Print("\nThis app automatically sets itself to auto-launch with SteamVR.");
39-
Utils.Print("\nWhen it runs it will in turn run all .cmd files in the same folder.");
43+
Utils.Print($"\nWhen it runs it will in turn run all {FILE_PATTERN} files in the {PATH_STARTFOLDER} folder.");
44+
Utils.Print($"\nIf there are {FILE_PATTERN} files in {PATH_STOPFOLDER} it will stay and run those on shutdown.");
4045
Utils.Print("\nThis message is only shown once, to see it again delete the log file.");
4146
Utils.Print("\nPress [Enter] in this window to continue execution.");
4247
Console.ReadLine();
@@ -64,24 +69,24 @@ private static void Worker()
6469
_isConnected = InitVR();
6570
}
6671
else if(_isReady)
67-
{
68-
RunStartupScripts();
69-
WaitForQuit();
72+
{
73+
RunScripts(PATH_STARTFOLDER);
74+
if(WeHaveScripts(PATH_STOPFOLDER)) WaitForQuit();
7075
OpenVR.System.AcknowledgeQuit_Exiting();
7176
OpenVR.Shutdown();
72-
RunShutdownScripts();
77+
RunScripts(PATH_STOPFOLDER);
7378
shouldRun = false;
7479
}
7580
if (!shouldRun)
7681
{
7782
LogUtils.WriteLineToCache("Application exiting, writing log");
78-
LogUtils.WriteCacheToLogFile(LOG_FILE_PATH, 100);
83+
LogUtils.WriteCacheToLogFile(PATH_LOGFILE, 100);
7984
Environment.Exit(0);
8085
}
8186
}
82-
}
83-
84-
// Initializing connection to OpenVR
87+
}
88+
89+
// Initializing connection to OpenVR
8590
private static bool InitVR()
8691
{
8792
var error = EVRInitError.None;
@@ -112,38 +117,15 @@ private static bool InitVR()
112117
}
113118

114119
// Scripts
115-
private static void RunStartupScripts()
116-
{
117-
try {
118-
var files = Directory.GetFiles("./", "*.cmd");
119-
LogUtils.WriteLineToCache($"Found: {files.Length} script(s)");
120-
foreach (var file in files)
121-
{
122-
LogUtils.WriteLineToCache($"Executing: {file}");
123-
var path = Path.Combine(Environment.CurrentDirectory, file);
124-
Process p = new Process();
125-
p.StartInfo.CreateNoWindow = true;
126-
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
127-
p.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
128-
p.StartInfo.Arguments = $"/C \"{path}\"";
129-
p.Start();
130-
}
131-
if(files.Length == 0) LogUtils.WriteLineToCache("Did not find any .cmd files to execute.");
132-
} catch(Exception e)
133-
{
134-
LogUtils.WriteLineToCache($"Error: Could not load scripts: {e.Message}");
135-
}
136-
}
137-
138-
private static void RunShutdownScripts()
139-
{
120+
private static void RunScripts(string folder) {
140121
try
141122
{
142-
var files = Directory.GetFiles("./shutdown/", "*.cmd");
143-
LogUtils.WriteLineToCache($"Found: {files.Length} script(s)");
123+
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
124+
var files = Directory.GetFiles(folder, FILE_PATTERN);
125+
LogUtils.WriteLineToCache($"Found: {files.Length} script(s) in {folder}");
144126
foreach (var file in files)
145127
{
146-
LogUtils.WriteLineToCache($"Executing: {file}");
128+
LogUtils.WriteLineToCache($"Executing: {file} from {folder}");
147129
var path = Path.Combine(Environment.CurrentDirectory, file);
148130
Process p = new Process();
149131
p.StartInfo.CreateNoWindow = true;
@@ -152,25 +134,49 @@ private static void RunShutdownScripts()
152134
p.StartInfo.Arguments = $"/C \"{path}\"";
153135
p.Start();
154136
}
155-
if (files.Length == 0) LogUtils.WriteLineToCache("Did not find any .cmd files to execute.");
137+
if (files.Length == 0) LogUtils.WriteLineToCache($"Did not find any .cmd files to execute in {folder}");
156138
}
157139
catch (Exception e)
158140
{
159-
LogUtils.WriteLineToCache($"Error: Could not load scripts: {e.Message}");
141+
LogUtils.WriteLineToCache($"Error: Could not load scripts from {folder}: {e.Message}");
160142
}
161143
}
162144

163145
private static void WaitForQuit()
164146
{
165-
VREvent_t ev = new VREvent_t();
166-
167-
var eventExists = OpenVR.System.PollNextEvent(ref ev, Utils.SizeOf(ev));
168-
169-
while (!eventExists || (EVREventType)ev.eventType != EVREventType.VREvent_Quit)
147+
var shouldRun = true;
148+
while(shouldRun)
170149
{
171-
Thread.Sleep(100);
172-
eventExists = OpenVR.System.PollNextEvent(ref ev, Utils.SizeOf(ev));
150+
var vrEvents = new List<VREvent_t>();
151+
var vrEvent = new VREvent_t();
152+
uint eventSize = (uint)Marshal.SizeOf(vrEvent);
153+
try
154+
{
155+
while (OpenVR.System.PollNextEvent(ref vrEvent, eventSize))
156+
{
157+
vrEvents.Add(vrEvent);
158+
}
159+
}
160+
catch (Exception e)
161+
{
162+
Utils.PrintError($"Could not get new events: {e.Message}");
163+
}
164+
165+
foreach (var e in vrEvents)
166+
{
167+
if ((EVREventType)e.eventType == EVREventType.VREvent_Quit)
168+
{
169+
shouldRun = false;
170+
}
171+
}
172+
Thread.Sleep(1000);
173173
}
174+
}
175+
176+
private static bool WeHaveScripts(string folder)
177+
{
178+
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
179+
return Directory.GetFiles(folder, FILE_PATTERN).Length > 0;
174180
}
175181
}
176182
}

OpenVRStartup/Properties/Resources.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

OpenVRStartup/Properties/Resources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,6 @@
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120120
<data name="Version" xml:space="preserve">
121-
<value>v0.0.5</value>
121+
<value>v0.9</value>
122122
</data>
123123
</root>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
start "Restart Voice Meeter Audio Engine" "C:\Program Files (x86)\VB\Voicemeeter\Voicemeeter.exe" -r

OpenVRStartup/start/demo_start.cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
msg %username% This dialog box was launched by demo_start.cmd in the ./start/ folder for OpenVRStartup. Edit this file or replace it with whatever scripts you want to launch with SteamVR.

OpenVRStartup/stop/demo_stop.cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
msg %username% This dialog box was launched by demo_stop.cmd in the ./stop/ folder for OpenVRStartup. Edit this file or replace it with whatever scripts you want to launch when SteamVR is shutdown.

0 commit comments

Comments
 (0)