-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVarjoNativeInterface.cs
More file actions
176 lines (146 loc) · 6.23 KB
/
VarjoNativeInterface.cs
File metadata and controls
176 lines (146 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using VRCFaceTracking;
namespace VRCFTVarjoModule
{
class VarjoNativeInterface : VarjoInterface
{
private IntPtr _session;
public override bool Initialize()
{
if (!VarjoAvailable())
{
Logger.Error("Varjo headset isn't detected");
return false;
}
LoadLibrary();
_session = varjo_SessionInit();
if (_session == IntPtr.Zero)
{
return false;
}
if (!varjo_IsGazeAllowed(_session))
{
Logger.Error("Gaze tracking is not allowed! Please enable it in the Varjo Base!");
return false;
}
varjo_GazeInit(_session);
varjo_SyncProperties(_session);
return true;
}
public override void Teardown()
{
//no need to tear down anything right?
}
public override void Update()
{
if (_session == IntPtr.Zero)
return;
// Get's GazeData and EyeMeasurements from the Varjo SDK
// Return value states whether or not the request was successful (true = has Data; false = Error occured)
bool hasData = varjo_GetGazeData(_session, out gazeData, out eyeMeasurements);
if (!hasData)
Logger.Msg("Error while getting Gaze Data");
}
public override string GetName()
{
return "native DLL";
}
private bool LoadLibrary()
{
IEnumerable<string> dllPaths = ExtractAssemblies(new string[] { "Varjo.VarjoLib.dll" });
var path = dllPaths.First();
if (path == null)
{
Logger.Error(string.Concat("Couldn't extract the library ", path));
return false;
}
if (LoadLibrary(path) == IntPtr.Zero)
{
Logger.Error(string.Concat("Unable to load library ", path));
return false;
}
Logger.Msg(string.Concat("Loaded library ", path));
return true;
}
// I can't ask nicely to add my DLL into the dependency list so I had to steal code from the main repo :(
private static IEnumerable<string> ExtractAssemblies(IEnumerable<string> resourceNames)
{
var extractedPaths = new List<string>();
var dirName = Path.Combine(Utils.PersistentDataDirectory, "StockLibs");
if (!Directory.Exists(dirName))
Directory.CreateDirectory(dirName);
foreach (var dll in resourceNames)
{
var dllPath = Path.Combine(dirName, GetAssemblyNameFromPath(dll));
using (var stm = Assembly.GetAssembly(typeof(VarjoNativeInterface)).GetManifestResourceStream("VRCFTVarjoModule.TrackingLibs." + dll))
{
try
{
using (Stream outFile = File.Create(dllPath))
{
const int sz = 4096;
var buf = new byte[sz];
while (true)
{
if (stm == null) continue;
var nRead = stm.Read(buf, 0, sz);
if (nRead < 1)
break;
outFile.Write(buf, 0, nRead);
}
}
extractedPaths.Add(dllPath);
}
catch (Exception e)
{
Logger.Error($"Failed to get DLL: " + e.Message);
}
}
}
return extractedPaths;
}
private static string GetAssemblyNameFromPath(string path)
{
var splitPath = path.Split('.').ToList();
splitPath.Reverse();
return splitPath[1] + ".dll";
}
[DllImport("kernel32", CharSet = CharSet.Unicode, ExactSpelling = false, SetLastError = true)]
private static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern bool varjo_IsAvailable();
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern IntPtr varjo_SessionInit();
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern void varjo_SessionShutDown(IntPtr session);
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern void varjo_GazeInit(IntPtr session);
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern int varjo_GetError(IntPtr session);
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern string varjo_GetErrorDesc(int errorCode);
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern bool varjo_IsGazeAllowed(IntPtr session);
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern bool varjo_IsGazeCalibrated(IntPtr session);
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern GazeData varjo_GetGaze(IntPtr session);
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern bool varjo_GetGazeData(IntPtr session, out GazeData gaze, out EyeMeasurements eyeMeasurements);
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern void varjo_RequestGazeCalibration(IntPtr session);
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern bool varjo_GetPropertyBool(IntPtr session, int propertyKey);
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern int varjo_GetPropertyInt(IntPtr session, int propertyKey);
[DllImport("VarjoLib", CharSet = CharSet.Auto)]
private static extern void varjo_SyncProperties(IntPtr session);
}
}