aboutsummaryrefslogtreecommitdiff
path: root/bindings/cs/libsurvive.net/LibSurViveAPI.cs
blob: 922a055d4fd3cbb808e377ac85d873c029b55202 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//using UnityEngine;
using System.Collections;
using System.Collections.Generic;

using libsurvive;
using System;
using System.Threading;
using System.Runtime.InteropServices;

public class LibSurViveAPI
{

    public struct SetupConfigs
    {
        public string playbaskFile;
        public int playbackFactor;
        public Disambiguator disambiguator;
        public Poser poser;
        public BoolConfig calibrate;
        public string configFile;
    }

    public enum Disambiguator
    {
        StateBased,
        Charles,
        Turvey,
        Default
    }

    public enum Poser
    {
        CharlesSlow,
        DaveOrtho,
        Dummy,
        EPNP,
        SBA
    }

    public enum BoolConfig
    {
        Yes,
        No,
        Default
    }


    private static LibSurViveAPI _instance;
    public static LibSurViveAPI instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new LibSurViveAPI();
            }

            return _instance;
        }
    }

    LibSurViveAPI()
    {
        CreateContext();

        CreateTread();

    }

    ~LibSurViveAPI()
    {
        running = false;
    }

    Thread internalPollTread;

    private void CreateTread()
    {
        internalPollTread = new Thread(InternalPoll);
        internalPollTread.Start();
    }

    bool running = true;

    void InternalPoll()
    {
        while (running)
        {
            cfunctions.Survive_poll(context);
        }
    }

    //private SurviveContext _context;
    public IntPtr context;

    light_process_func light_Process_Func;
    raw_pose_func raw_Pose_Func;
    lighthouse_pose_func lighthouse_Pose_Func;
    angle_process_func angle_Process_Func;
    button_process_func button_Process_Func;
    htc_config_func htc_Config_Func;
    imu_process_func imu_Process_Func;
    text_feedback_func error_func;
    text_feedback_func info_func;

    internal void CreateContext()
    {
        //Debug.Log("Start Init");

        SetupConfigs configs = new SetupConfigs
        {
            playbaskFile = @"C:\Users\justin\source\libsurvive-win-update\bindings\cs\Demo\lightcap-reformat.log",
            configFile = "survive_conf.json",
            playbackFactor = 1
        };

        string[] args = CreateStartParameters(configs);

        //string[] vs = new[] { "--playback", "P:/c/libsurvive-data/lightcap-reformat/lightcap-reformat.log", "--disambiguator", "StateBased", "--calibrate" };

        context = cfunctions.Survive_init_internal(args.Length, args);

        if (context == IntPtr.Zero)
        {
            throw new Exception("There was a problem initializing the lib!");
        }

        light_Process_Func = LightEvent;
        raw_Pose_Func = PoseEvent;
        lighthouse_Pose_Func = LightHouseEvent;
        angle_Process_Func = AngleEvent;
        button_Process_Func = ButtonEvent;
        htc_Config_Func = HTCConfigEvent;
        imu_Process_Func = IMUEvent;
        error_func = ErrorEvent;
        info_func = InfoEvent;

        cfunctions.Survive_install_raw_pose_fn(context, raw_Pose_Func);
        cfunctions.Survive_install_light_fn(context, light_Process_Func);
        cfunctions.Survive_install_lighthouse_pose_fn(context, lighthouse_Pose_Func);
        cfunctions.Survive_install_angle_fn(context, angle_Process_Func);
        cfunctions.Survive_install_button_fn(context, button_Process_Func);
        cfunctions.Survive_install_htc_config_fn(context, htc_Config_Func);
        cfunctions.Survive_install_imu_fn(context, imu_Process_Func);
        cfunctions.Survive_install_error_fn(context, error_func);
        cfunctions.Survive_install_info_fn(context, info_func);

        //Debug.Log("Finished Init");

        //Debug.Log("Start Startup");

        //Debug.LogError("ASD");

        int a = 0;
        try
        {
            a = cfunctions.Survive_startup(context);
        }
        catch (Exception)
        {
            throw;
        }

        if (a != 0)
        {
            throw new Exception("Error in startup");
        }

        //Debug.Log("Finished Startup");

    }


    static public string[] CreateStartParameters(SetupConfigs configs)
    {
        List<string> args = new List<string>();

        args.Add("unity");

        if (configs.playbaskFile != "")
        {
            args.AddRange(new[] { "--playback", configs.playbaskFile });
            args.AddRange(new[] { "--playback-factor", configs.playbackFactor.ToString() });
        }

        if(configs.disambiguator != Disambiguator.Default)
        args.AddRange(new[] { "--disambiguator", Enum.GetName(typeof(Disambiguator), configs.disambiguator) });

        if (configs.calibrate != BoolConfig.Default)
            args.Add(configs.calibrate == BoolConfig.Yes ? "--calibrate" : "--no-calibrate");

        if (configs.configFile != "")
            args.AddRange(new[] { "-c", configs.configFile });

        //args.AddRange(new[] { "--disambiguator", Enum.GetName(typeof(Poser), disambiguator) });



        return args.ToArray();
    }





    virtual protected void InfoEvent(IntPtr ctx, string fault)
    {
        //Debug.Log(fault);
    }

    virtual protected void ErrorEvent(IntPtr ctx, string fault)
    {
        //Debug.LogError(fault);
    }

    virtual protected void IMUEvent(IntPtr so, int mask, double[] accelgyro, uint timecode, int id)
    {
        cfunctions.Survive_default_imu_process(so, mask, accelgyro, timecode, id);
    }

    virtual protected int HTCConfigEvent(IntPtr so, string ct0conf, int len)
    {
        return cfunctions.Survive_default_htc_config_process(so, ct0conf, len);
    }

    virtual protected void ButtonEvent(IntPtr so, byte eventType, byte buttonId, byte axis1Id, ushort axis1Val, byte axis2Id, ushort axis2Val)
    {
        cfunctions.Survive_default_button_process(so, eventType, buttonId, axis1Id, axis1Val, axis2Id, axis2Val);
    }

    virtual protected void AngleEvent(IntPtr so, int sensor_id, int acode, uint timecode, double length, double angle, uint lh)
    {
        cfunctions.Survive_default_angle_process(so, sensor_id, acode, timecode, length, angle, lh);

        //Debug.Log("AngleEvent");
    }

    protected void LightHouseEvent(IntPtr ctx, byte lighthouse, SurvivePose lighthouse_pose, SurvivePose object_pose)
    {
        cfunctions.Survive_default_lighthouse_pose_process(ctx, lighthouse, lighthouse_pose, object_pose);

        //Debug.Log("LightHouseEvent");
    }

    virtual protected void LightEvent(IntPtr so, int sensor_id, int acode, int timeinsweep, UInt32 timecode, UInt32 length, UInt32 lighthouse)
    {
        cfunctions.Survive_default_light_process(so, sensor_id, acode, timeinsweep, timecode, length, lighthouse);

        //Console.WriteLine("LightEvent");
        //Debug.Log("LightEvent");
    }

    virtual protected void PoseEvent(IntPtr so, byte lighthouse, SurvivePose pose)
    {
        cfunctions.Survive_default_raw_pose_process(so, lighthouse, pose);

        //vaDebug.Log("PoseEvent");

        //poseUpdate(-1, new Vector3((float)pose.Pos[0], (float)pose.Pos[1], (float)pose.Pos[2]), new Quaternion((float)pose.Rot[0], (float)pose.Rot[1], (float)pose.Rot[2], (float)pose.Rot[3]));

        /*
        string a = cfunctions.Survive_object_codename(so);
        if (updates.ContainsKey(a))
        {
            Vector3 pos = new Vector3((float)pose.Pos[0], (float)pose.Pos[1], (float)pose.Pos[2]);
            Quaternion rot = new Quaternion((float)pose.Rot[0], (float)pose.Rot[1], (float)pose.Rot[2], (float)pose.Rot[3]);
            updates[a](pos, rot); 
        }
        */
    }


    public delegate void PoseUpdate(SurviveVector3 pos, SurviveQuaternion quat);

    public Dictionary<string, PoseUpdate> updates = new Dictionary<string, PoseUpdate>();

    public void AddCalback(string ID, PoseUpdate update)
    {
        if (!updates.ContainsKey(ID))
        {
            updates.Add(ID, update);
        }
        else
        {
            updates[ID] += update;
        }
    }

    /*
    public void Poll()
    {
        cfunctions.Survive_poll(context);
    }
    */

    public SurviveObject GetSurviveObjectByName(string name)
    {
        if (name == "")
        {
            throw new Exception("Empty string is not accepted");
        }

        if (context == IntPtr.Zero)
            throw new Exception("The context hasn't been initialsied yet");

        return new SurviveObject( cfunctions.Survive_get_so_by_name(context, name));
    }
}

public class SurviveObject
{
    private SurvivePose _pose;
    public SurvivePose Pose
    {
        get
        {
            var ptr1 = cfunctions.Survive_object_pose(ptr);
            return (SurvivePose)Marshal.PtrToStructure(ptr1, typeof(SurvivePose));
        }
    }

    private int _charge;
    public int Charge
    {
        get
        {
            return cfunctions.Survive_object_charge(ptr);
        }
    }
    public bool Charging
    {
        get
        {
            return cfunctions.Survive_object_charging(ptr);
        }
    }

    IntPtr ptr;
    private bool _charging;

    public SurviveObject(IntPtr obj_ptr)
    {
        if (obj_ptr == IntPtr.Zero)
        {
            throw new Exception("Can't create SurviveObject with 0 pointer");
        }
        ptr = obj_ptr;
    }
}

public class SurviveVector3
{
    float x;
    float y;
    float z;
}

public class SurviveQuaternion
{
    float x;
    float y;
    float z;
}