Skip to content

Commit 9a58041

Browse files
committed
Switch to log writing, add first run instructions, auto-minimize, version
1 parent ec75498 commit 9a58041

5 files changed

Lines changed: 293 additions & 18 deletions

File tree

OpenVRStartup/LogUtils.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
6+
namespace OpenVRStartup
7+
{
8+
9+
static class LogUtils
10+
{
11+
static List<string> cache = new List<string>();
12+
13+
static public void WriteLineToCache(string line) {
14+
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
15+
cache.Add($"{time} {line}");
16+
}
17+
18+
static public void FlushCache() {
19+
cache.Clear();
20+
}
21+
22+
static public bool LogFileExists(string filePath) {
23+
return File.Exists(filePath);
24+
}
25+
26+
static public void WriteCacheToLogFile(string filePath, int lineLimit) {
27+
var linesArr = File.Exists(filePath) ? File.ReadAllLines(filePath) : new string[0];
28+
var linesList = linesArr.ToList();
29+
linesList.AddRange(cache);
30+
if (linesList.Count > lineLimit)
31+
{
32+
linesList.RemoveRange(0, linesList.Count-lineLimit);
33+
linesList.Insert(0, $"(Log is limited to {lineLimit} lines and has been truncated)");
34+
}
35+
File.WriteAllText(filePath, string.Join("\n", linesList));
36+
FlushCache();
37+
}
38+
}
39+
}

OpenVRStartup/OpenVRStartup.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
5555
<Prefer32Bit>true</Prefer32Bit>
5656
</PropertyGroup>
57+
<PropertyGroup>
58+
<StartupObject />
59+
</PropertyGroup>
5760
<ItemGroup>
5861
<Reference Include="System" />
5962
<Reference Include="System.Core" />
@@ -65,9 +68,15 @@
6568
<Reference Include="System.Xml" />
6669
</ItemGroup>
6770
<ItemGroup>
71+
<Compile Include="LogUtils.cs" />
6872
<Compile Include="openvr_api.cs" />
6973
<Compile Include="Program.cs" />
7074
<Compile Include="Properties\AssemblyInfo.cs" />
75+
<Compile Include="Properties\Resources.Designer.cs">
76+
<AutoGen>True</AutoGen>
77+
<DesignTime>True</DesignTime>
78+
<DependentUpon>Resources.resx</DependentUpon>
79+
</Compile>
7180
<Compile Include="Utils.cs" />
7281
</ItemGroup>
7382
<ItemGroup>
@@ -81,5 +90,11 @@
8190
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
8291
</None>
8392
</ItemGroup>
93+
<ItemGroup>
94+
<EmbeddedResource Include="Properties\Resources.resx">
95+
<Generator>ResXFileCodeGenerator</Generator>
96+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
97+
</EmbeddedResource>
98+
</ItemGroup>
8499
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
85100
</Project>

OpenVRStartup/Program.cs

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,52 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Diagnostics;
43
using System.IO;
54
using System.Runtime.InteropServices;
6-
using System.Text;
75
using System.Threading;
86
using Valve.VR;
97

108
namespace OpenVRStartup
119
{
1210
class Program
1311
{
14-
// TODO: Write log files of which command files we execute every launch.
15-
// TODO: If no log file, stop execution with instruction of SteamVR startup toggle.
12+
static string LOG_FILE_PATH = "./OpenVRStartup.log";
13+
14+
[DllImport("user32.dll")]
15+
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
16+
public const int SW_SHOWMINIMIZED = 2;
17+
private volatile static bool _isReady = false;
1618

1719
static void Main(string[] args)
1820
{
1921
// Starting worker
2022
var t = new Thread(Worker);
21-
Utils.PrintDebug("Starting worker thread.");
23+
LogUtils.WriteLineToCache($"Application starting ({Properties.Resources.Version})");
2224
if (!t.IsAlive) t.Start();
23-
else Utils.PrintError("Could not start worker thread.");
25+
else LogUtils.WriteLineToCache("Error: Could not start worker thread");
26+
27+
// Check if first run, if so do NOT minimize but write instructions.
28+
if (LogUtils.LogFileExists(LOG_FILE_PATH))
29+
{
30+
_isReady = true;
31+
IntPtr winHandle = Process.GetCurrentProcess().MainWindowHandle;
32+
ShowWindow(winHandle, SW_SHOWMINIMIZED);
33+
}
34+
else {
35+
Utils.PrintInfo("\n========================");
36+
Utils.PrintInfo(" First Run Instructions ");
37+
Utils.PrintInfo("========================\n");
38+
Utils.Print("To get this to launch every time you start SteamVR, do the following steps:");
39+
Utils.Print(" 1. Launch SteamVR.");
40+
Utils.Print(" 2. Open [Settings] from the hamburger menu in the SteamVR status window.");
41+
Utils.Print(" 3. Select the [Startup / Shutdown] section in the menu to the left.");
42+
Utils.Print(" 4. Click [Choose startup overlay apps].");
43+
Utils.Print(" 5. Locate OpenVRStartup and toggle the switch to [On].");
44+
Utils.Print("\nThe next time this program runs it will be minimized and terminate as soon as scripts have been launched.");
45+
Utils.Print("\nTo see this message again, delete the log file that is in the same folder.");
46+
Utils.Print("\nPress [Enter] in this window to continue execution.");
47+
Console.ReadLine();
48+
_isReady = true;
49+
}
2450

2551
Console.ReadLine();
2652
t.Abort();
@@ -42,14 +68,16 @@ private static void Worker()
4268
Thread.Sleep(1000);
4369
_isConnected = InitVR();
4470
}
45-
else
46-
{
71+
else if(_isReady)
72+
{
4773
RunScripts();
4874
OpenVR.Shutdown();
4975
shouldRun = false;
5076
}
5177
if (!shouldRun)
5278
{
79+
LogUtils.WriteLineToCache("Application exiting, writing log");
80+
LogUtils.WriteCacheToLogFile(LOG_FILE_PATH, 100);
5381
Environment.Exit(0);
5482
}
5583
}
@@ -62,18 +90,16 @@ private static bool InitVR()
6290
OpenVR.Init(ref error, EVRApplicationType.VRApplication_Overlay);
6391
if (error != EVRInitError.None)
6492
{
65-
Utils.PrintError($"OpenVR initialization errored: {Enum.GetName(typeof(EVRInitError), error)}");
93+
LogUtils.WriteLineToCache($"Error: OpenVR init failed: {Enum.GetName(typeof(EVRInitError), error)}");
6694
return false;
6795
}
6896
else
6997
{
70-
Utils.PrintInfo("OpenVR initialized successfully.");
98+
LogUtils.WriteLineToCache("OpenVR init success");
7199

72-
// Load app manifest, I think this is needed for the application to show up in the input bindings at all
73-
Utils.PrintVerbose("Loading app.vrmanifest");
100+
// Load app manifest
74101
var appError = OpenVR.Applications.AddApplicationManifest(Path.GetFullPath("./app.vrmanifest"), false);
75-
if (appError != EVRApplicationError.None) Utils.PrintError($"Failed to load Application Manifest: {Enum.GetName(typeof(EVRApplicationError), appError)}");
76-
else Utils.PrintInfo("Application manifest loaded successfully.");
102+
if (appError != EVRApplicationError.None) LogUtils.WriteLineToCache($"Error: Failed to load app manifest: {Enum.GetName(typeof(EVRApplicationError), appError)}");
77103
return true;
78104
}
79105
}
@@ -83,21 +109,21 @@ private static void RunScripts()
83109
{
84110
try {
85111
var files = Directory.GetFiles("./", "*.cmd");
86-
Utils.Print($"Found {files.Length} file(s)");
112+
LogUtils.WriteLineToCache($"Found: {files.Length} script(s)");
87113
foreach (var file in files)
88114
{
89-
Utils.PrintInfo($"Running {file}");
115+
LogUtils.WriteLineToCache($"Executing: {file}");
90116
Process p = new Process();
91117
p.StartInfo.CreateNoWindow = true;
92118
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
93119
p.StartInfo.FileName = Path.Combine(Environment.CurrentDirectory, file);
94120
p.Start();
95121
}
122+
if(files.Length == 0) LogUtils.WriteLineToCache($"Did not find any .cmd files to execute.");
96123
} catch(Exception e)
97124
{
98-
Utils.PrintError($"Error loading scripts: {e.Message}");
125+
LogUtils.WriteLineToCache($"Error: Could not load scripts: {e.Message}");
99126
}
100-
101127
}
102128
}
103129
}

OpenVRStartup/Properties/Resources.Designer.cs

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)