scs-sdk-plugin 0.1.1

Safe lifecycle and callback framework for SCS Telemetry and Input plugins
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Safe application framework for the SCS Input Device API.
//!
//! Input devices use a pull model: while a registered device is active, SCS
//! repeatedly asks the plugin for the next event in the current frame. The
//! public types in this module keep device declarations, callback identity,
//! event flags, indices, and values in safe Rust. Raw callback pointers and
//! the lifetime of their opaque contexts remain owned by the runtime.

use std::ffi::CString;
use std::fmt;

use scs_sdk::{InputApiVersion, InputGameVersion, LogLevel, ScopedLogger, SdkError};

use crate::{Game, PluginError, PluginMetadata, PluginResult, classify_game_id};

pub use scs_sdk::input::{
    InputAxisValue, InputAxisValueError, InputDeviceType, InputEvent, InputEventFlags, InputIndex,
    InputValue, InputValueType,
};

/// Stable identity assigned to one device declared during plugin initialization.
///
/// The value is intentionally not interchangeable with an input index. SCS
/// calls each device through a distinct opaque context, while [`InputIndex`]
/// selects one entry inside that device's registered input array.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct InputDeviceId(u32);

impl InputDeviceId {
    pub(crate) const fn from_ordinal(ordinal: u32) -> Self {
        Self(ordinal)
    }

    /// Zero-based declaration ordinal, useful for logs and application tables.
    #[must_use]
    pub const fn ordinal(self) -> u32 {
        self.0
    }
}

/// One bool or normalized float-axis input exposed by an input device.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InputSpec {
    name: &'static str,
    display_name: &'static str,
    value_type: InputValueType,
}

impl InputSpec {
    /// Describes one input using the exact names consumed by SCS.
    ///
    /// Syntax is validated when the containing device is registered. The
    /// configuration name accepts lowercase ASCII letters, digits, and
    /// underscores. The display name accepts ASCII letters, digits,
    /// underscores, spaces, and dots.
    #[must_use]
    pub const fn new(
        name: &'static str,
        display_name: &'static str,
        value_type: InputValueType,
    ) -> Self {
        Self {
            name,
            display_name,
            value_type,
        }
    }

    /// Name persisted by the game or interpreted as a semantical mix name.
    #[must_use]
    pub const fn name(self) -> &'static str {
        self.name
    }

    /// Human-facing name shown by the game's input UI.
    #[must_use]
    pub const fn display_name(self) -> &'static str {
        self.display_name
    }

    /// SDK representation expected for events targeting this input.
    #[must_use]
    pub const fn value_type(self) -> InputValueType {
        self.value_type
    }
}

/// Explicit declaration of one SCS input device.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InputDeviceSpec {
    name: &'static str,
    display_name: &'static str,
    device_type: InputDeviceType,
    inputs: &'static [InputSpec],
    activity_notifications: bool,
}

impl InputDeviceSpec {
    /// Creates a device declaration without the optional activity callback.
    ///
    /// Call [`InputDeviceSpec::with_activity_notifications`] when the product
    /// needs explicit activation and deactivation notifications.
    #[must_use]
    pub const fn new(
        name: &'static str,
        display_name: &'static str,
        device_type: InputDeviceType,
        inputs: &'static [InputSpec],
    ) -> Self {
        Self {
            name,
            display_name,
            device_type,
            inputs,
            activity_notifications: false,
        }
    }

    /// Explicitly requests the optional SCS device-activity callback.
    #[must_use]
    pub const fn with_activity_notifications(mut self) -> Self {
        self.activity_notifications = true;
        self
    }

    /// Unique device configuration name.
    #[must_use]
    pub const fn name(self) -> &'static str {
        self.name
    }

    /// Human-facing device name.
    #[must_use]
    pub const fn display_name(self) -> &'static str {
        self.display_name
    }

    /// Generic bindable or semantical device class.
    #[must_use]
    pub const fn device_type(self) -> InputDeviceType {
        self.device_type
    }

    /// Inputs in the exact zero-based order used by [`InputIndex`].
    #[must_use]
    pub const fn inputs(self) -> &'static [InputSpec] {
        self.inputs
    }

    /// Whether the runtime should register the optional activity callback.
    #[must_use]
    pub const fn activity_notifications(self) -> bool {
        self.activity_notifications
    }
}

/// Owned identity and input-specific version of the loading game.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InputGameInfo {
    name: String,
    id: String,
    kind: Game,
    version: InputGameVersion,
}

impl InputGameInfo {
    pub(crate) fn new(
        name: &std::ffi::CStr,
        id: &std::ffi::CStr,
        version: InputGameVersion,
    ) -> Self {
        Self {
            name: name.to_string_lossy().into_owned(),
            id: id.to_string_lossy().into_owned(),
            kind: classify_game_id(id),
            version,
        }
    }

    /// Human-facing name copied from the initialization parameters.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Stable textual game identifier such as `eut2` or `ats`.
    #[must_use]
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Recognized game classification.
    #[must_use]
    pub const fn kind(&self) -> Game {
        self.kind
    }

    /// Game-specific Input API version, separate from telemetry schema.
    #[must_use]
    pub const fn version(&self) -> InputGameVersion {
        self.version
    }
}

/// Minimum Input API game version required for one recognized game.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InputGameCompatibility {
    game: Game,
    minimum_version: InputGameVersion,
}

impl InputGameCompatibility {
    /// Declares one recognized game and its oldest accepted Input API version.
    #[must_use]
    pub const fn new(game: Game, minimum_version: InputGameVersion) -> Self {
        Self {
            game,
            minimum_version,
        }
    }

    /// Game governed by this compatibility entry.
    #[must_use]
    pub const fn game(self) -> Game {
        self.game
    }

    /// Oldest accepted game-specific Input API version.
    #[must_use]
    pub const fn minimum_version(self) -> InputGameVersion {
        self.minimum_version
    }
}

/// Explicit API and game requirements of one input plugin.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InputPluginCompatibility {
    minimum_input_api: InputApiVersion,
    games: &'static [InputGameCompatibility],
}

impl InputPluginCompatibility {
    /// Creates a product compatibility declaration validated before devices
    /// are registered with SCS.
    #[must_use]
    pub const fn new(
        minimum_input_api: InputApiVersion,
        games: &'static [InputGameCompatibility],
    ) -> Self {
        Self {
            minimum_input_api,
            games,
        }
    }

    /// Oldest Input API layout required by the product.
    #[must_use]
    pub const fn minimum_input_api(self) -> InputApiVersion {
        self.minimum_input_api
    }

    /// Per-game input-version requirements.
    #[must_use]
    pub const fn games(self) -> &'static [InputGameCompatibility] {
        self.games
    }
}

/// Data supplied each time SCS asks one device for its next event.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InputEventRequest {
    device: InputDeviceId,
    flags: InputEventFlags,
}

impl InputEventRequest {
    pub(crate) const fn new(device: InputDeviceId, flags: InputEventFlags) -> Self {
        Self { device, flags }
    }

    /// Device whose callback SCS is currently polling.
    #[must_use]
    pub const fn device(self) -> InputDeviceId {
        self.device
    }

    /// Frame and activation boundary flags supplied by SCS.
    #[must_use]
    pub const fn flags(self) -> InputEventFlags {
        self.flags
    }
}

/// Safe capabilities available during one input-plugin hook.
pub struct InputPluginContext<'scope> {
    logger: ScopedLogger<'scope>,
    api_version: InputApiVersion,
    game: InputGameInfo,
    devices: Option<&'scope mut Vec<InputDeviceSpec>>,
}

impl<'scope> InputPluginContext<'scope> {
    pub(crate) fn initializing(
        logger: ScopedLogger<'scope>,
        api_version: InputApiVersion,
        game: InputGameInfo,
        devices: &'scope mut Vec<InputDeviceSpec>,
    ) -> Self {
        Self {
            logger,
            api_version,
            game,
            devices: Some(devices),
        }
    }

    pub(crate) fn callback(
        logger: ScopedLogger<'scope>,
        api_version: InputApiVersion,
        game: InputGameInfo,
    ) -> Self {
        Self {
            logger,
            api_version,
            game,
            devices: None,
        }
    }

    /// Loading-game identity copied from the Input API initialization call.
    #[must_use]
    pub const fn game(&self) -> &InputGameInfo {
        &self.game
    }

    /// Input API version selected by the SCS loader.
    #[must_use]
    pub const fn input_api_version(&self) -> InputApiVersion {
        self.api_version
    }

    /// Declares one input device and returns its callback identity.
    ///
    /// Registration remains explicit: the runtime does not infer a device from
    /// hook implementations or automatically install activity callbacks.
    ///
    /// # Errors
    ///
    /// Returns [`SdkError::NotNow`] outside [`InputPlugin::initialize`].
    /// Invalid names, empty or oversized input arrays, duplicate device/input
    /// names, and an unrepresentable device ordinal return
    /// [`SdkError::InvalidParameter`] or [`SdkError::AlreadyRegistered`].
    pub fn register_device(&mut self, spec: InputDeviceSpec) -> PluginResult<InputDeviceId> {
        let Some(devices) = self.devices.as_deref_mut() else {
            return Err(PluginError::new(
                SdkError::NotNow,
                "input devices may only be registered during plugin initialization",
            ));
        };
        validate_device_spec(spec)?;
        if devices.iter().any(|device| device.name() == spec.name()) {
            return Err(PluginError::new(
                SdkError::AlreadyRegistered,
                format!("duplicate input device name {:?}", spec.name()),
            ));
        }
        let ordinal = u32::try_from(devices.len()).map_err(|_| {
            PluginError::new(
                SdkError::InvalidParameter,
                "input device count exceeds the framework identity range",
            )
        })?;
        let id = InputDeviceId(ordinal);
        devices.push(spec);
        Ok(id)
    }

    /// Formats and writes one message to the game log.
    pub fn log(&self, level: LogLevel, arguments: fmt::Arguments<'_>) {
        let rendered = format!("{arguments}").replace('\0', " ");
        if let Ok(message) = CString::new(rendered) {
            self.logger.log(level, &message);
        }
    }

    /// Logs an informational message.
    pub fn message(&self, arguments: fmt::Arguments<'_>) {
        self.log(LogLevel::Message, arguments);
    }

    /// Logs a warning message.
    pub fn warning(&self, arguments: fmt::Arguments<'_>) {
        self.log(LogLevel::Warning, arguments);
    }

    /// Logs an error message.
    pub fn error(&self, arguments: fmt::Arguments<'_>) {
        self.log(LogLevel::Error, arguments);
    }
}

fn validate_device_spec(spec: InputDeviceSpec) -> PluginResult {
    if !valid_configuration_name(spec.name()) {
        return Err(PluginError::new(
            SdkError::InvalidParameter,
            format!("invalid input device configuration name {:?}", spec.name()),
        ));
    }
    if !valid_display_name(spec.display_name()) {
        return Err(PluginError::new(
            SdkError::InvalidParameter,
            format!(
                "invalid input device display name {:?}",
                spec.display_name()
            ),
        ));
    }
    if spec.inputs().is_empty() || spec.inputs().len() > InputIndex::MAX_COUNT as usize {
        return Err(PluginError::new(
            SdkError::InvalidParameter,
            format!(
                "input device {:?} must declare between 1 and {} inputs",
                spec.name(),
                InputIndex::MAX_COUNT,
            ),
        ));
    }
    for (position, input) in spec.inputs().iter().copied().enumerate() {
        if !valid_configuration_name(input.name()) {
            return Err(PluginError::new(
                SdkError::InvalidParameter,
                format!(
                    "invalid input name {:?} at position {position} for device {:?}",
                    input.name(),
                    spec.name(),
                ),
            ));
        }
        if !valid_display_name(input.display_name()) {
            return Err(PluginError::new(
                SdkError::InvalidParameter,
                format!(
                    "invalid input display name {:?} at position {position} for device {:?}",
                    input.display_name(),
                    spec.name(),
                ),
            ));
        }
        if spec.inputs()[..position]
            .iter()
            .any(|previous| previous.name() == input.name())
        {
            return Err(PluginError::new(
                SdkError::AlreadyRegistered,
                format!(
                    "duplicate input name {:?} for device {:?}",
                    input.name(),
                    spec.name(),
                ),
            ));
        }
    }
    Ok(())
}

fn valid_configuration_name(value: &str) -> bool {
    !value.is_empty()
        && value
            .bytes()
            .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_')
}

fn valid_display_name(value: &str) -> bool {
    !value.is_empty()
        && value.bytes().all(|byte| {
            byte.is_ascii_alphabetic()
                || byte.is_ascii_digit()
                || matches!(byte, b'_' | b' ' | b'.')
        })
}

/// Application-facing lifecycle for one SCS input plugin.
pub trait InputPlugin: Send + 'static {
    /// Stable product identity used in runtime lifecycle logs.
    fn metadata(&self) -> PluginMetadata;

    /// Explicit Input API and per-game compatibility requirements.
    fn compatibility(&self) -> InputPluginCompatibility;

    /// Declares devices and initializes plugin-owned input state.
    ///
    /// # Errors
    ///
    /// Implementations may return [`PluginError`] to reject configuration or
    /// propagate a device-declaration failure before SCS registration begins.
    fn initialize(&mut self, context: &mut InputPluginContext<'_>) -> PluginResult;

    /// Receives an optional activation or deactivation notification.
    fn device_active(
        &mut self,
        _context: &mut InputPluginContext<'_>,
        _device: InputDeviceId,
        _active: bool,
    ) {
    }

    /// Returns the next event for the polled device, or `None` when the current
    /// frame has no more events.
    fn next_input_event(
        &mut self,
        _context: &mut InputPluginContext<'_>,
        _request: InputEventRequest,
    ) -> Option<InputEvent> {
        None
    }

    /// Releases plugin-owned resources after SCS has unregistered all devices.
    fn shutdown(&mut self, _context: &mut InputPluginContext<'_>) {}
}

#[cfg(test)]
mod tests {
    use super::*;

    const BOOL: InputSpec = InputSpec::new("button", "Button 1", InputValueType::Bool);
    const DUPLICATES: [InputSpec; 2] = [BOOL, BOOL];

    #[test]
    fn validates_the_exact_header_name_character_sets() {
        assert!(valid_configuration_name("device_01"));
        assert!(!valid_configuration_name("Device"));
        assert!(!valid_configuration_name("device-name"));
        assert!(!valid_configuration_name(""));

        assert!(valid_display_name("Example Device 1.0"));
        assert!(!valid_display_name("Example/Device"));
        assert!(!valid_display_name("设备"));
    }

    #[test]
    fn validates_device_input_count_and_duplicate_names() {
        let empty = InputDeviceSpec::new("empty", "Empty", InputDeviceType::Generic, &[]);
        assert_eq!(
            validate_device_spec(empty)
                .err()
                .map(|error| error.result()),
            Some(SdkError::InvalidParameter)
        );

        let duplicate = InputDeviceSpec::new(
            "duplicate",
            "Duplicate",
            InputDeviceType::Generic,
            &DUPLICATES,
        );
        assert_eq!(
            validate_device_spec(duplicate)
                .err()
                .map(|error| error.result()),
            Some(SdkError::AlreadyRegistered)
        );
    }
}