Skip to content

Commit

Permalink
Install DLLs to PersistentDataDirectory
Browse files Browse the repository at this point in the history
Don't know why the DLLs are present but never read from
  • Loading branch information
200Tigersbloxed committed Jun 17, 2024
1 parent 925249d commit 3fccdcc
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions SRanipalExtTrackingModule/SRanipalTrackingInterface.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using ViveSR;
Expand Down Expand Up @@ -64,7 +65,7 @@ public override (bool eyeSuccess, bool expressionSuccess) Initialize(bool eyeAva
var lipStream = a.GetManifestResourceStream("SRanipalExtTrackingModule.Assets.vive_face_tracker.png");

// Look for SRanipal assemblies here. Placeholder for unmanaged assemblies not being embedded in the dll.
var currentDllDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
//var currentDllDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

// Get the directory of the sr_runtime.exe program from our start menu shortcut. This is where the SRanipal dlls are located.
var srInstallDir = (string) Registry.LocalMachine.OpenSubKey(@"Software\VIVE\SRWorks\SRanipal")?.GetValue("ModuleFileName");
Expand Down Expand Up @@ -124,7 +125,8 @@ public override (bool eyeSuccess, bool expressionSuccess) Initialize(bool eyeAva

Logger.LogInformation($"SRanipalExtTrackingModule: SRanipal version: {srRuntimeVer}");

SetDllDirectory(currentDllDirectory + "\\ModuleLibs\\" + (srRuntimeVer.StartsWith("1.3.6") ? "New" : "Old"));
//SetDllDirectory(currentDllDirectory + "\\ModuleLibs\\" + (srRuntimeVer.StartsWith("1.3.6") ? "New" : "Old"));
SetDllDirectory(WriteAndLoadAssemblies(a, Utils.PersistentDataDirectory, srRuntimeVer));

SRanipal_API.InitialRuntime(); // hack to unblock sranipal!!!

Expand Down Expand Up @@ -221,6 +223,33 @@ private bool InitTracker(int anipalType, string name)
Logger.LogInformation($"{name} failed to initialize: {error}");
return false;
}

// We have the ModuleLibs, but they're never saved anywhere. The directory is just kinda, assumed?
private string WriteAndLoadAssemblies(Assembly assembly, string persistentData, string srRuntimeVer)
{
string dir = Path.Combine(persistentData, "ModuleLibs", srRuntimeVer.StartsWith("1.3.6") ? "New" : "Old");
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
foreach (string manifestResourceName in assembly.GetManifestResourceNames())
{
if(!manifestResourceName.EndsWith(".dll")) continue;
string[] dllNameSplit = manifestResourceName.Split('.');
// Yes, I know this won't work if the name contains multiple dots. I don't care.
string dllName = dllNameSplit[dllNameSplit.Length - 2] + ".dll";
string newOutput = Path.Combine(dir, dllName);
if(File.Exists(newOutput)) continue;
using Stream? nativeDll = assembly.GetManifestResourceStream(manifestResourceName);
if (nativeDll == null)
{
Logger.LogWarning($"Could not get stream from Manifest {manifestResourceName}");
continue;
}
using FileStream fs = new FileStream(newOutput, FileMode.Create, FileAccess.ReadWrite,
FileShare.ReadWrite | FileShare.Delete);
nativeDll.CopyTo(fs);
fs.Close();
}
return dir;
}

public override void Teardown()
{
Expand Down

0 comments on commit 3fccdcc

Please sign in to comment.