@@ -28,10 +28,17 @@ public class SRanipalExtTrackingInterface : ExtTrackingModule
2828 internal static Process ? _process ;
2929 internal static IntPtr _processHandle ;
3030 internal static IntPtr _offset ;
31-
31+
32+ LipData_v2 lipData ;
33+ EyeData_v2 eyeData ;
34+
35+ private static byte [ ] eyeImageCache , lipImageCache ;
36+
37+ // Kernel32 SetDllDirectory
3238 [ DllImport ( "kernel32.dll" , CharSet = CharSet . Unicode ) ]
3339 private static extern bool SetDllDirectory ( string lpPathName ) ;
34- internal static bool Attach ( )
40+
41+ private static bool Attach ( )
3542 {
3643 var processes = Process . GetProcessesByName ( "sr_runtime" ) ;
3744 if ( processes . Length <= 0 ) return false ;
@@ -42,13 +49,13 @@ internal static bool Attach()
4249 return true ;
4350 }
4451
45- internal static byte [ ] ReadMemory ( int size ) {
46- var buffer = new byte [ size ] ;
47-
52+ private static byte [ ] ReadMemory ( IntPtr offset , ref byte [ ] buf ) {
4853 var bytesRead = 0 ;
49- Utils . ReadProcessMemory ( ( int ) _processHandle , _offset , buffer , size , ref bytesRead ) ;
54+ var size = buf . Length ;
55+
56+ Utils . ReadProcessMemory ( ( int ) _processHandle , offset , buf , size , ref bytesRead ) ;
5057
51- return bytesRead != size ? null : buffer ;
58+ return bytesRead != size ? null : buf ;
5259 }
5360
5461 public override ( bool SupportsEye , bool SupportsExpression ) Supported => ( true , true ) ;
@@ -65,6 +72,28 @@ public override (bool eyeSuccess, bool expressionSuccess) Initialize(bool eyeAva
6572 // Get the directory of the sr_runtime.exe program from our start menu shortcut. This is where the SRanipal dlls are located.
6673 var srInstallDir = ( string ) Registry . LocalMachine . OpenSubKey ( @"Software\VIVE\SRWorks\SRanipal" ) ? . GetValue ( "ModuleFileName" ) ;
6774
75+ // Dang you SRanipal
76+ var localAppData = Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) ;
77+ var srLogsDirectory = Path . Combine ( localAppData + @"Low\HTC Corporation\SR_Logs\SRAnipal_Logs" ) ;
78+
79+ // Get logs that should be yeeted.
80+ string [ ] srLogFiles = Directory . GetFiles ( srLogsDirectory ) ;
81+
82+ foreach ( string logFile in srLogFiles )
83+ {
84+ try {
85+ using ( var stream = File . Open ( logFile , FileMode . Open , FileAccess . Write , FileShare . ReadWrite ) )
86+ {
87+ Logger . LogDebug ( $ "Clearing \" { logFile } \" ") ;
88+ stream . SetLength ( 0 ) ;
89+ stream . Close ( ) ;
90+ }
91+ }
92+ catch {
93+ Logger . LogWarning ( $ "Failed to delete log file \" { logFile } \" ") ;
94+ }
95+ }
96+
6897 if ( srInstallDir == null )
6998 {
7099 Logger . LogError ( "Bruh, SRanipal not installed. Assuming default path" ) ;
@@ -128,17 +157,19 @@ public override (bool eyeSuccess, bool expressionSuccess) Initialize(bool eyeAva
128157 }
129158
130159 UnifiedTracking . EyeImageData . ImageSize = ( 200 , 100 ) ;
160+ UnifiedTracking . EyeImageData . ImageData = new byte [ 200 * 100 * 4 ] ;
161+ eyeImageCache = new byte [ 200 * 100 ] ;
131162 }
132163 }
133164
134165 if ( lipEnabled )
135166 {
136167 UnifiedTracking . LipImageData . SupportsImage = true ;
137168 UnifiedTracking . LipImageData . ImageSize = ( SRanipal_Lip_v2 . ImageWidth , SRanipal_Lip_v2 . ImageHeight ) ;
138- UnifiedTracking . LipImageData . ImageData = new byte [ UnifiedTracking . LipImageData . ImageSize . x *
139- UnifiedTracking . LipImageData . ImageSize . y ] ;
140169 lipData . image = Marshal . AllocCoTaskMem ( UnifiedTracking . LipImageData . ImageSize . x *
141170 UnifiedTracking . LipImageData . ImageSize . x ) ;
171+
172+ lipImageCache = new byte [ SRanipal_Lip_v2 . ImageWidth * SRanipal_Lip_v2 . ImageHeight ] ;
142173 }
143174
144175 ModuleInformation = new ModuleMetadata ( )
@@ -194,13 +225,13 @@ public override void Update()
194225 if ( lipEnabled && ! UpdateMouth ( ) )
195226 {
196227 Logger . LogError ( "An error has occured when updating tracking. Reinitializing needed runtimes." ) ;
197- // SRanipal_API.InitialRuntime();
228+ SRanipal_API . InitialRuntime ( ) ;
198229 InitTracker ( SRanipal_Lip_v2 . ANIPAL_TYPE_LIP_V2 , "Lip" ) ;
199230 }
200231 if ( eyeEnabled && ! UpdateEye ( ) )
201232 {
202233 Logger . LogError ( "An error has occured when updating tracking. Reinitializing needed runtimes." ) ;
203- // SRanipal_API.InitialRuntime();
234+ SRanipal_API . InitialRuntime ( ) ;
204235 InitTracker ( SRanipal_Eye_v2 . ANIPAL_TYPE_EYE_V2 , "Eye" ) ;
205236 }
206237 }
@@ -217,7 +248,7 @@ private bool UpdateEye()
217248 return true ;
218249
219250 // Read 20000 image bytes from the predefined offset. 10000 bytes per eye.
220- var imageBytes = ReadMemory ( 20000 ) ;
251+ var imageBytes = ReadMemory ( _offset , ref eyeImageCache ) ;
221252
222253 // Concatenate the two images side by side instead of one after the other
223254 byte [ ] leftEye = new byte [ 10000 ] ;
@@ -234,9 +265,21 @@ private bool UpdateEye()
234265 // Add 100 bytes from the right eye to the right side of the image
235266 Array . Copy ( rightEye , i * 100 , imageBytes , leftIndex + 100 , 100 ) ;
236267 }
237-
238- // Write the image to the latest eye data
239- UnifiedTracking . EyeImageData . ImageData = imageBytes ;
268+
269+ for ( int y = 0 ; y < 100 ; y ++ )
270+ {
271+ for ( int x = 0 ; x < 200 ; x ++ )
272+ {
273+ byte grayscaleValue = imageBytes [ y * 200 + x ] ;
274+
275+ // Set the R, G, B, and A channels to the grayscale value
276+ int index = ( y * 200 + x ) * 4 ;
277+ UnifiedTracking . EyeImageData . ImageData [ index + 0 ] = grayscaleValue ; // R
278+ UnifiedTracking . EyeImageData . ImageData [ index + 1 ] = grayscaleValue ; // G
279+ UnifiedTracking . EyeImageData . ImageData [ index + 2 ] = grayscaleValue ; // B
280+ UnifiedTracking . EyeImageData . ImageData [ index + 3 ] = 255 ; // A (fully opaque)
281+ }
282+ }
240283
241284 return true ;
242285 }
@@ -325,8 +368,23 @@ private bool UpdateMouth()
325368 if ( lipData . image == IntPtr . Zero || ! UnifiedTracking . LipImageData . SupportsImage )
326369 return true ;
327370
328- Marshal . Copy ( lipData . image , UnifiedTracking . LipImageData . ImageData , 0 , UnifiedTracking . LipImageData . ImageSize . x *
371+ Marshal . Copy ( lipData . image , lipImageCache , 0 , UnifiedTracking . LipImageData . ImageSize . x *
329372 UnifiedTracking . LipImageData . ImageSize . y ) ;
373+
374+ for ( int y = 0 ; y < 400 ; y ++ )
375+ {
376+ for ( int x = 0 ; x < 800 ; x ++ )
377+ {
378+ byte grayscaleValue = lipImageCache [ y * 800 + x ] ;
379+
380+ // Set the R, G, B, and A channels to the grayscale value
381+ int index = ( y * 800 + x ) * 4 ;
382+ UnifiedTracking . LipImageData . ImageData [ index + 0 ] = grayscaleValue ; // R
383+ UnifiedTracking . LipImageData . ImageData [ index + 1 ] = grayscaleValue ; // G
384+ UnifiedTracking . LipImageData . ImageData [ index + 2 ] = grayscaleValue ; // B
385+ UnifiedTracking . LipImageData . ImageData [ index + 3 ] = 255 ; // A (fully opaque)
386+ }
387+ }
330388
331389 return true ;
332390 }
0 commit comments