aexlo 0.3.0

Run After Effects plugins without Adobe dependencies.
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
//! Host suites handed to plugins through `SPBasicSuite::AcquireSuite`.
//!
//! # Ownership model
//!
//! Every suite is a **stateless vtable** - a table of `extern "C"` function
//! pointers with no per-instance state; any mutable state lives behind the
//! plugin-provided pointers those callbacks receive, not in the suite struct.
//! Because of that, a single **process-wide** instance is shared by every
//! [`PluginInstance`](crate::PluginInstance), and across threads, soundly.
//!
//! Nearly all suites live in the `const` [`SUITE_CONTAINER`] static, so
//! acquiring one just hands back a pointer into it and releasing it is a
//! no-op; nothing is allocated or freed. Suites served at several versions
//! with different layouts are separate `static` tables in the same model.
//!
//! # Safety model
//!
//! Every callback here is `unsafe extern "C"` and trusts the plugin the way
//! After Effects does. Rather than repeat it at each of the hundreds of
//! `unsafe` blocks, these premises cover them (`undocumented_unsafe_blocks`
//! is allowed in this module tree; blocks that rely on anything more say so
//! with their own `SAFETY:` comment):
//!
//! - **Out-pointers** are written only after a null check. The plugin owns
//!   them and sizes them as the SDK documents for that call.
//! - **In-pointers** (worlds, rects, C strings, handles) are null-checked or
//!   come straight from the SDK contract for the call; strings are
//!   NUL-terminated and live for the duration of the call.
//! - **`effect_ref`** is the `PluginInstance` aexlo passed in `in_data`, and
//!   the plugin calls back during one of that instance's commands
//!   (see `PluginInstance::get_instance_ptr`).
//! - **Handles and worlds** the plugin passes back were created by these
//!   suites; their bookkeeping (`handle`, `world`, `aegp_memory`, ...) rejects
//!   ones it does not know.
//! - **Unions** (`PF_ParamDef::u`, `PF_EventExtra::u`, ...) are read only in
//!   the variant their tag (`param_type`, `e_type`, ...) selects.
#![allow(clippy::undocumented_unsafe_blocks)]

mod adv_app;
mod adv_item;
mod adv_time;
mod ae_app;
mod aegp_item;
mod aegp_memory;
mod aegp_render;
mod aegp_utility;
mod aegp_world;
mod angle_param;
pub mod ansi;
mod cache_on_load;
mod channel;
pub mod color_callbacks;
mod color_param;
mod color_settings;
mod comp;
pub(crate) mod drawbot;
pub(crate) use drawbot::system_font;
mod dynamic_stream;
mod effect;
mod effect_ui;
mod ffi;
pub mod fill_matte;
pub mod gpu_device;
pub mod handle;
mod helper;
pub mod interface;
pub mod iterate;
mod keyframe;
mod layer;
pub(crate) mod layer_mask;
pub(crate) use layer::enter_effect;
pub mod param_utils;
mod path;
pub mod persistent_data;
pub mod pixel_data;
mod pixel_format;
mod pixel_norm;
mod point_param;
pub(crate) mod register;
pub mod sampling;
pub(crate) mod stream;
mod text_layer;
pub mod transform;
pub mod utility;
pub mod world;

use crate::core::diagnostics::diag;
use crate::sys::*;
static COMP_SUITE_4: AEGP_CompSuite4 = comp::create_comp_suite_4();
static AEGP_WORLD_1: AEGP_WorldSuite1 = aegp_world::suite_1();
static AEGP_MASK_5: AEGP_MaskSuite5 = layer_mask::mask_suite_5();
static AEGP_MASK_6: AEGP_MaskSuite6 = layer_mask::mask_suite_6();
static AEGP_MASK_OUTLINE_3: AEGP_MaskOutlineSuite3 = layer_mask::mask_outline_suite_3();
static AEGP_WORLD_2: AEGP_WorldSuite2 = aegp_world::suite_2();
static AEGP_WORLD_3: AEGP_WorldSuite3 = aegp_world::suite_3();
static AEGP_MEMORY_1: AEGP_MemorySuite1 = aegp_memory::suite_1();
static AEGP_UTILITY_3: AEGP_UtilitySuite3 = aegp_utility::suite_3();
static AEGP_UTILITY_5: AEGP_UtilitySuite5 = aegp_utility::suite_5();
static AEGP_UTILITY_6: AEGP_UtilitySuite6 = aegp_utility::suite_6();
static AEGP_ITEM_6: AEGP_ItemSuite6 = aegp_item::suite_6();
static AEGP_STREAM_3: AEGP_StreamSuite3 = stream::create_stream_suite_3();
static AEGP_LAYER_3: AEGP_LayerSuite3 = layer::create_layer_suite_3();
static AEGP_DYNAMIC_STREAM_1: AEGP_DynamicStreamSuite1 = dynamic_stream::suite_1();
static AEGP_DYNAMIC_STREAM_2: AEGP_DynamicStreamSuite2 = dynamic_stream::suite_2();
static AEGP_DYNAMIC_STREAM_3: AEGP_DynamicStreamSuite3 = dynamic_stream::suite_3();
static AEGP_DYNAMIC_STREAM_4: AEGP_DynamicStreamSuite4 = dynamic_stream::suite_4();
static AEGP_ITEM_8: AEGP_ItemSuite8 = aegp_item::suite_8();
static AEGP_LAYER_RENDER_OPTIONS_2: AEGP_LayerRenderOptionsSuite2 = aegp_render::layer_render_options_suite_2();
static AEGP_RENDER_OPTIONS_4: AEGP_RenderOptionsSuite4 = aegp_render::render_options_suite_4();
static AEGP_RENDER_5: AEGP_RenderSuite5 = aegp_render::render_suite_5();
static COLOR_SETTINGS_5: AEGP_ColorSettingsSuite5 = color_settings::create_color_settings_suite_5();
use std::ffi::CStr;
use std::os::raw::c_void;

pub static SUITE_CONTAINER: SuiteContainer = SuiteContainer {
	ansi: PF_ANSICallbacksBlock {
		atan: Some(ansi::atan_sys),
		atan2: Some(ansi::atan2_sys),
		ceil: Some(ansi::ceil_sys),
		cos: Some(ansi::cos_sys),
		exp: Some(ansi::exp_sys),
		fabs: Some(ansi::fabs_sys),
		floor: Some(ansi::floor_sys),
		fmod: Some(ansi::fmod_sys),
		hypot: Some(ansi::hypot_sys),
		log: Some(ansi::log_sys),
		log10: Some(ansi::log10_sys),
		pow: Some(ansi::pow_sys),
		sin: Some(ansi::sin_sys),
		sqrt: Some(ansi::sqrt_sys),
		tan: Some(ansi::tan_sys),
		sprintf: Some(ansi::sprintf_sys),
		strcpy: Some(ansi::strcpy_sys),
		asin: Some(ansi::asin_sys),
		acos: Some(ansi::acos_sys),
		unused_longA: [0; 1],
	},
	effect_ui: effect_ui::create_effect_ui_suite_1(),
	effect_custom_ui1: effect_ui::create_effect_custom_ui_suite_1(),
	effect_custom_ui2: effect_ui::create_effect_custom_ui_suite_2(),
	overlay_theme: effect_ui::create_overlay_theme_suite_1(),
	handle: handle::create_handle_suite(),
	world_transform: transform::create_world_transform_suite_1(),
	world: world::create_world_suite(),
	iterate8: iterate::create_iterate_8_suite_2(),
	iterate16: iterate::create_iterate_16_suite_2(),
	iterate_float: iterate::create_iterate_float_suite_2(),
	aegp_iterate: iterate::create_aegp_iterate_suite_1(),
	utility: utility::create_utility_suite(),
	aegp_interface: interface::create_aegp_pf_interface_suite(),
	angle_param: angle_param::create_angle_param_suite(),
	color_param: color_param::create_color_param_suite_1(),
	point_param: point_param::create_point_param_suite_1(),
	color_callbacks8: color_callbacks::create_color_callbacks_suite_1(),
	color_callbacks16: color_callbacks::create_color_callbacks_16_suite_1(),
	color_callbacks_float: color_callbacks::create_color_callbacks_float_suite_1(),
	fill_matte: fill_matte::create_fill_matte_suite_2(),
	batch_sampling: sampling::create_batch_sampling_suite_1(),
	sampling8: sampling::create_sampling_8_suite_1(),
	sampling16: sampling::create_sampling_16_suite_1(),
	sampling_float: sampling::create_sampling_float_suite_1(),
	pixel_data: pixel_data::create_pixel_data_suite_2(),
	pixel_format1: pixel_format::create_pixel_format_suite_1(),
	pixel_format2: pixel_format::create_pixel_format_suite_2(),
	cache_on_load: cache_on_load::create_cache_on_load_suite_1(),
	channel: channel::create_channel_suite_1(),
	ae_app4: ae_app::create_ae_app_suite_4(),
	ae_app5: ae_app::create_ae_app_suite_5(),
	ae_app6: ae_app::create_ae_app_suite_6(),
	adv_app1: adv_app::create_adv_app_suite_1(),
	adv_app2: adv_app::create_adv_app_suite_2(),
	adv_item: adv_item::create_adv_item_suite_1(),
	adv_time1: adv_time::create_adv_time_suite_1(),
	adv_time2: adv_time::create_adv_time_suite_2(),
	adv_time3: adv_time::create_adv_time_suite_3(),
	adv_time4: adv_time::create_adv_time_suite_4(),
	helper1: helper::create_helper_suite_1(),
	helper2: helper::create_helper_suite_2(),
	gpu_device: gpu_device::create_gpu_device_suite_1(),
	param_utils: param_utils::create_param_utils_suite_3(),
	path_query: path::create_path_query_suite_1(),
	path_data: path::create_path_data_suite_1(),
	persistent_data: persistent_data::create_persistent_data_suite_3(),
	stream2: stream::create_stream_suite_2(),
	stream4: stream::create_stream_suite_4(),
	stream5: stream::create_stream_suite_5(),
	stream6: stream::create_stream_suite_6(),
	keyframe: keyframe::create_keyframe_suite_4(),
	keyframe5: keyframe::create_keyframe_suite_5(),
	effect1: effect::create_effect_suite_1(),
	effect2: effect::create_effect_suite_2(),
	effect3: effect::create_effect_suite_3(),
	effect4: effect::create_effect_suite_4(),
	color_settings: color_settings::create_color_settings_suite_2(),
	light: layer::create_light_suite_1(),
	text_layer: text_layer::suite_1(),
	comp: comp::create_comp_suite_10(),
	layer5: layer::create_layer_suite_5(),
	layer6: layer::create_layer_suite_6(),
	layer7: layer::create_layer_suite_7(),
	layer8: layer::create_layer_suite_8(),
	layer9: layer::create_layer_suite_9(),
};

/// Process-wide storage for the stateless suite vtables handed to plugins.
///
/// Every field is a plain table of `extern "C"` function pointers with no
/// per-instance state, so a single shared `static` instance serves every
/// [`PluginInstance`](crate::PluginInstance) - see the module-level ownership
/// notes. Suites live for the program's lifetime; there is nothing to allocate
/// or free.
pub struct SuiteContainer {
	pub ansi: PF_ANSICallbacksBlock,
	pub effect_ui: PF_EffectUISuite1,
	pub effect_custom_ui1: PF_EffectCustomUISuite1,
	pub effect_custom_ui2: PF_EffectCustomUISuite2,
	pub overlay_theme: PF_EffectCustomUIOverlayThemeSuite1,
	pub handle: PF_HandleSuite1,
	pub world_transform: PF_WorldTransformSuite1,
	pub world: PF_WorldSuite2,
	pub iterate8: PF_Iterate8Suite2,
	pub iterate16: PF_iterate16Suite2,
	pub iterate_float: PF_iterateFloatSuite2,
	pub aegp_iterate: AEGP_IterateSuite1,
	pub utility: PF_UtilitySuite,
	pub aegp_interface: AEGP_PFInterfaceSuite1,
	pub angle_param: PF_AngleParamSuite1,
	pub color_param: PF_ColorParamSuite1,
	pub point_param: PF_PointParamSuite1,
	pub color_callbacks8: PF_ColorCallbacksSuite1,
	pub color_callbacks16: PF_ColorCallbacks16Suite1,
	pub color_callbacks_float: PF_ColorCallbacksFloatSuite1,
	pub fill_matte: PF_FillMatteSuite2,
	pub batch_sampling: PF_BatchSamplingSuite1,
	pub sampling8: PF_Sampling8Suite1,
	pub sampling16: PF_Sampling16Suite1,
	pub sampling_float: PF_SamplingFloatSuite1,
	pub pixel_data: PF_PixelDataSuite2,
	pub pixel_format1: PF_PixelFormatSuite,
	pub pixel_format2: PF_PixelFormatSuite2,
	pub cache_on_load: PF_CacheOnLoadSuite1,
	pub channel: PF_ChannelSuite1,
	pub ae_app4: PFAppSuite4,
	pub ae_app5: PFAppSuite5,
	pub ae_app6: PFAppSuite6,
	pub adv_app1: PF_AdvAppSuite1,
	pub adv_app2: PF_AdvAppSuite2,
	pub adv_item: PF_AdvItemSuite1,
	pub adv_time1: PF_AdvTimeSuite1,
	pub adv_time2: PF_AdvTimeSuite2,
	pub adv_time3: PF_AdvTimeSuite3,
	pub adv_time4: PF_AdvTimeSuite4,
	pub helper1: PF_HelperSuite1,
	pub helper2: PF_HelperSuite2,
	pub gpu_device: PF_GPUDeviceSuite1,
	pub param_utils: PF_ParamUtilsSuite3,
	pub path_query: PF_PathQuerySuite1,
	pub path_data: PF_PathDataSuite1,
	pub persistent_data: AEGP_PersistentDataSuite3,
	pub stream2: AEGP_StreamSuite2,
	pub stream4: AEGP_StreamSuite4,
	pub stream5: AEGP_StreamSuite5,
	pub stream6: AEGP_StreamSuite6,
	pub keyframe: AEGP_KeyframeSuite4,
	pub keyframe5: AEGP_KeyframeSuite5,
	pub effect1: AEGP_EffectSuite1,
	pub effect2: AEGP_EffectSuite2,
	pub effect3: AEGP_EffectSuite3,
	pub effect4: AEGP_EffectSuite4,
	pub color_settings: AEGP_ColorSettingsSuite2,
	pub light: AEGP_LightSuite1,
	pub text_layer: AEGP_TextLayerSuite1,
	pub comp: AEGP_CompSuite10,
	pub layer5: AEGP_LayerSuite5,
	pub layer6: AEGP_LayerSuite6,
	pub layer7: AEGP_LayerSuite7,
	pub layer8: AEGP_LayerSuite8,
	pub layer9: AEGP_LayerSuite9,
}

/// Hand back a pointer to one of the shared [`SUITE_CONTAINER`] vtables.
///
/// Writes `&SUITE_CONTAINER.$field` into the `*suite` out-param, logs it, and
/// returns `PF_Err_NONE`. The pointer is valid for the program's lifetime, so
/// there is no matching release step.
macro_rules! dispatch_static {
	($suite:expr, $name:expr, $version:expr, $field:ident $(,)?) => {{
		// SAFETY: `rusty_acquire_suite` returns early when `suite` is null,
		// so the out-param is a valid place to write here.
		unsafe { *$suite = &SUITE_CONTAINER.$field as *const _ as *const c_void };
		// debug, not info: some plugins re-acquire suites on every render call.
		log::debug!("Acquired {} v{}", $name, $version);
		PF_Err_NONE
	}};
}

/// Emulates `SPBasicSuite::AcquireSuite` function
/// # Safety
/// This function is unsafe because it handles raw pointers.
#[allow(non_snake_case)]
pub unsafe extern "C" fn rusty_acquire_suite(name: *const i8, version: i32, suite: *mut *const c_void) -> i32 {
	if suite.is_null() || name.is_null() {
		return PF_Err_BAD_CALLBACK_PARAM;
	}

	let suite_name = unsafe {
		match CStr::from_ptr(name).to_str() {
			Ok(s) => s,
			Err(_) => return PF_Err_INTERNAL_STRUCT_DAMAGED,
		}
	};

	diag!("SPBasicSuite/AcquireSuite",
		"name" => format!("{suite_name:?}"),
		"version" => version,
		"suite" => format!("{suite:?}"),
	);

	match (suite_name, version) {
		// Static suites: pointers into the shared SUITE_CONTAINER.
		("PF ANSI Suite", 1) => dispatch_static!(suite, suite_name, version, ansi),
		("PF Effect UI Suite", 1) => dispatch_static!(suite, suite_name, version, effect_ui),
		("PF Effect Custom UI Suite", 1) => dispatch_static!(suite, suite_name, version, effect_custom_ui1),
		("PF Effect Custom UI Suite", 2) => dispatch_static!(suite, suite_name, version, effect_custom_ui2),
		("PF Effect Custom UI Overlay Theme Suite", 1) => {
			dispatch_static!(suite, suite_name, version, overlay_theme)
		}
		("PF Handle Suite", 2) => dispatch_static!(suite, suite_name, version, handle),
		("PF World Transform Suite", 1) => dispatch_static!(suite, suite_name, version, world_transform),
		("PF World Suite", 2) => dispatch_static!(suite, suite_name, version, world),
		// Iterate suites are append-only, so the v2 tables also satisfy v1 requests.
		("PF Iterate8 Suite", 1..=2) => dispatch_static!(suite, suite_name, version, iterate8),
		("PF iterate16 Suite", 1..=2) => dispatch_static!(suite, suite_name, version, iterate16),
		("PF iterateFloat Suite", 1..=2) => dispatch_static!(suite, suite_name, version, iterate_float),
		("AEGP Iterate Suite", 1) => dispatch_static!(suite, suite_name, version, aegp_iterate),
		("PF Utility Suite", 1..=18) => dispatch_static!(suite, suite_name, version, utility),
		("AEGP PF Interface Suite", 1) => dispatch_static!(suite, suite_name, version, aegp_interface),
		("PF AngleParamSuite", 1) => dispatch_static!(suite, suite_name, version, angle_param),
		("PF ColorParamSuite", 1) => dispatch_static!(suite, suite_name, version, color_param),
		("PF PointParamSuite", 1) => dispatch_static!(suite, suite_name, version, point_param),
		("PF Color Suite", 1) => dispatch_static!(suite, suite_name, version, color_callbacks8),
		("PF Color16 Suite", 1) => dispatch_static!(suite, suite_name, version, color_callbacks16),
		("PF ColorFloat Suite", 1) => dispatch_static!(suite, suite_name, version, color_callbacks_float),
		("PF Fill Matte Suite", 2) => dispatch_static!(suite, suite_name, version, fill_matte),
		("PF Batch Sampling Suite", 1) => dispatch_static!(suite, suite_name, version, batch_sampling),
		("PF Sampling8 Suite", 1) => dispatch_static!(suite, suite_name, version, sampling8),
		("PF Sampling16 Suite", 1) => dispatch_static!(suite, suite_name, version, sampling16),
		("PF SamplingFloat Suite", 1) => dispatch_static!(suite, suite_name, version, sampling_float),
		// PixelData suites are append-only (v2 adds the GPU accessor), so the v2
		// table also satisfies v1 requests.
		("PF Pixel Data Suite", 1..=2) => dispatch_static!(suite, suite_name, version, pixel_data),
		// v1 is Premiere's full `PF_PixelFormatSuite`; v2 is AE's two-entry
		// registration-only table. Same name, unrelated layouts.
		("PF Pixel Format Suite", 1) => dispatch_static!(suite, suite_name, version, pixel_format1),
		("PF Pixel Format Suite", 2) => dispatch_static!(suite, suite_name, version, pixel_format2),
		("PF Cache On Load Suite", 1) => dispatch_static!(suite, suite_name, version, cache_on_load),
		("PF AE Channel Suite", 1) => dispatch_static!(suite, suite_name, version, channel),
		// The App Suite's wire versions do NOT follow the struct names, and the
		// layouts are not append-only (v5 inserts `PF_AppGetLanguage` mid-table),
		// so each wire version must get its own exactly-shaped table.
		("PF AE App Suite", v) if v == kPFAppSuiteVersion6 as i32 => {
			dispatch_static!(suite, suite_name, version, ae_app6)
		}
		("PF AE App Suite", v) if v == kPFAppSuiteVersion5 as i32 => {
			dispatch_static!(suite, suite_name, version, ae_app5)
		}
		("PF AE App Suite", v) if v == kPFAppSuiteVersion4 as i32 => {
			dispatch_static!(suite, suite_name, version, ae_app4)
		}
		// Adv App v2 only appends `PF_AppendInfoText`; Adv Time versions differ
		// in their preference struct, so each gets an exactly-shaped table.
		("PF AE Adv App Suite", 1) => dispatch_static!(suite, suite_name, version, adv_app1),
		("PF AE Adv App Suite", 2) => dispatch_static!(suite, suite_name, version, adv_app2),
		("PF AE Adv Item Suite", 1) => dispatch_static!(suite, suite_name, version, adv_item),
		("PF AE Adv Time Suite", 1) => dispatch_static!(suite, suite_name, version, adv_time1),
		("PF AE Adv Time Suite", 2) => dispatch_static!(suite, suite_name, version, adv_time2),
		("PF AE Adv Time Suite", 3) => dispatch_static!(suite, suite_name, version, adv_time3),
		("PF AE Adv Time Suite", 4) => dispatch_static!(suite, suite_name, version, adv_time4),
		("AE Plugin Helper Suite", 1) => dispatch_static!(suite, suite_name, version, helper1),
		("AE Plugin Helper Suite2", 1..=2) => dispatch_static!(suite, suite_name, version, helper2),
		("PF GPU Device Suite", 1) => dispatch_static!(suite, suite_name, version, gpu_device),
		// ParamUtils suites are append-only, so the v3 table also satisfies v1/v2 requests.
		("PF Param Utils Suite", 1..=3) => dispatch_static!(suite, suite_name, version, param_utils),
		("PF Path Query Suite", 1) => dispatch_static!(suite, suite_name, version, path_query),
		("PF Path Data Suite", 1) => dispatch_static!(suite, suite_name, version, path_data),
		("AEGP Persistent Data Suite", 3) => {
			dispatch_static!(suite, suite_name, version, persistent_data)
		}
		// Stream Suite wire versions 7, 8, 9, 10 and 11 are AEGP_StreamSuite2, 3,
		// 4, 5 and 6. They differ in their stream value structs and in whether names
		// and expression text are `char` or UTF-16.
		("AEGP Stream Suite", v) if v == kAEGPStreamSuiteVersion2 as i32 => {
			dispatch_static!(suite, suite_name, version, stream2)
		}
		("AEGP Stream Suite", v) if v == kAEGPStreamSuiteVersion3 as i32 => {
			unsafe { *suite = &AEGP_STREAM_3 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Stream Suite", v) if v == kAEGPStreamSuiteVersion4 as i32 => {
			dispatch_static!(suite, suite_name, version, stream4)
		}
		("AEGP Stream Suite", v) if v == kAEGPStreamSuiteVersion5 as i32 => {
			dispatch_static!(suite, suite_name, version, stream5)
		}
		("AEGP Stream Suite", v) if v == kAEGPStreamSuiteVersion6 as i32 => {
			dispatch_static!(suite, suite_name, version, stream6)
		}
		// Dynamic Stream wire versions 1, 2, 3 and 5 are suites 1, 2, 3 and 4.
		// `kAEGPDynamicStreamSuiteVersion4` is 5; there is no wire version 4.
		// Suite 4 adds mask refs, UTF-16 names and dimension separation.
		("AEGP Dynamic Stream Suite", v) if v == kAEGPDynamicStreamSuiteVersion1 as i32 => {
			unsafe { *suite = &AEGP_DYNAMIC_STREAM_1 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Dynamic Stream Suite", v) if v == kAEGPDynamicStreamSuiteVersion2 as i32 => {
			unsafe { *suite = &AEGP_DYNAMIC_STREAM_2 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Dynamic Stream Suite", v) if v == kAEGPDynamicStreamSuiteVersion3 as i32 => {
			unsafe { *suite = &AEGP_DYNAMIC_STREAM_3 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Dynamic Stream Suite", v) if v == kAEGPDynamicStreamSuiteVersion4 as i32 => {
			unsafe { *suite = &AEGP_DYNAMIC_STREAM_4 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Keyframe Suite", 4) => dispatch_static!(suite, suite_name, version, keyframe),
		("AEGP Keyframe Suite", 5) => dispatch_static!(suite, suite_name, version, keyframe5),
		// Effect Suite v1 is v2 without DuplicateEffect. v3 adds the command
		// argument to EffectCallGeneric; v4 appends the effect-mask calls.
		("AEGP Effect Suite", 1) => dispatch_static!(suite, suite_name, version, effect1),
		("AEGP Effect Suite", 2) => dispatch_static!(suite, suite_name, version, effect2),
		("AEGP Effect Suite", 3) => dispatch_static!(suite, suite_name, version, effect3),
		("AEGP Effect Suite", 4) => dispatch_static!(suite, suite_name, version, effect4),
		("PF Color Settings Suite", v) if v == kAEGPColorSettingsSuiteVersion2 as i32 => {
			dispatch_static!(suite, suite_name, version, color_settings)
		}
		("PF Color Settings Suite", v) if v == kAEGPColorSettingsSuiteVersion5 as i32 => {
			unsafe { *suite = &COLOR_SETTINGS_5 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP World Suite", v) if v == kAEGPWorldSuiteVersion1 as i32 => {
			unsafe { *suite = &AEGP_WORLD_1 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP World Suite", v) if v == kAEGPWorldSuiteVersion2 as i32 => {
			unsafe { *suite = &AEGP_WORLD_2 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP World Suite", v) if v == kAEGPWorldSuiteVersion3 as i32 => {
			unsafe { *suite = &AEGP_WORLD_3 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Memory Suite", v) if v == kAEGPMemorySuiteVersion1 as i32 => {
			unsafe { *suite = &AEGP_MEMORY_1 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Item Suite", v) if v == kAEGPItemSuiteVersion6 as i32 => {
			unsafe { *suite = &AEGP_ITEM_6 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Item Suite", v) if v == kAEGPItemSuiteVersion8 as i32 => {
			unsafe { *suite = &AEGP_ITEM_8 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Layer Render Options Suite", v) if v == kAEGPLayerRenderOptionsSuiteVersion2 as i32 => {
			unsafe { *suite = &AEGP_LAYER_RENDER_OPTIONS_2 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Render Options Suite", v) if v == kAEGPRenderOptionsSuiteVersion4 as i32 => {
			unsafe { *suite = &AEGP_RENDER_OPTIONS_4 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Render Suite", v) if v == kAEGPRenderSuiteVersion5 as i32 => {
			unsafe { *suite = &AEGP_RENDER_5 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Light Suite", 1) => dispatch_static!(suite, suite_name, version, light),
		("AEGP Text Layer Suite", 1) => dispatch_static!(suite, suite_name, version, text_layer),
		// Layer Mask wire versions 6 and 7 are AEGP_MaskSuite5 and 6.
		("AEGP Layer Mask Suite", v) if v == kAEGPMaskSuiteVersion5 as i32 => {
			unsafe { *suite = &AEGP_MASK_5 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Layer Mask Suite", v) if v == kAEGPMaskSuiteVersion6 as i32 => {
			unsafe { *suite = &AEGP_MASK_6 as *const _ as *const c_void };
			PF_Err_NONE
		}
		// Mask Outline wire versions 3 and 5 are AEGP_MaskOutlineSuite2 and 3;
		// suite 2 is the first seven entries of suite 3.
		("AEGP Mask Outline Suite", v)
			if v == kAEGPMaskOutlineSuiteVersion2 as i32 || v == kAEGPMaskOutlineSuiteVersion3 as i32 =>
		{
			unsafe { *suite = &AEGP_MASK_OUTLINE_3 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Comp Suite", v) if v == kAEGPCompSuiteVersion4 as i32 => {
			unsafe {
				*suite = &COMP_SUITE_4 as *const _ as *const std::ffi::c_void;
			}
			PF_Err_NONE
		}
		("AEGP Comp Suite", v) if v == kAEGPCompSuiteVersion10 as i32 => {
			dispatch_static!(suite, suite_name, version, comp)
		}
		// Layer Suite v6 switches names to UTF-16 handles; later versions append.
		// Wire version 8 is `AEGP_LayerSuite3`. It is not a prefix of suite 5:
		// suite 5 inserts `GetLayerSourceItemID` and the duplicate/lookup calls.
		("AEGP Layer Suite", v) if v == kAEGPLayerSuiteVersion3 as i32 => {
			unsafe { *suite = &AEGP_LAYER_3 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Layer Suite", v) if v == kAEGPLayerSuiteVersion5 as i32 => {
			dispatch_static!(suite, suite_name, version, layer5)
		}
		("AEGP Layer Suite", v) if v == kAEGPLayerSuiteVersion6 as i32 => {
			dispatch_static!(suite, suite_name, version, layer6)
		}
		("AEGP Layer Suite", v) if v == kAEGPLayerSuiteVersion7 as i32 => {
			dispatch_static!(suite, suite_name, version, layer7)
		}
		("AEGP Layer Suite", v) if v == kAEGPLayerSuiteVersion8 as i32 => {
			dispatch_static!(suite, suite_name, version, layer8)
		}
		("AEGP Layer Suite", v) if v == kAEGPLayerSuiteVersion9 as i32 => {
			dispatch_static!(suite, suite_name, version, layer9)
		}
		("DRAWBOT Draw Suite", 1) => {
			unsafe { *suite = &drawbot::DRAW_SUITE as *const _ as *const c_void };
			PF_Err_NONE
		}
		("DRAWBOT Supplier Suite", 1) => {
			unsafe { *suite = &drawbot::SUPPLIER_SUITE as *const _ as *const c_void };
			PF_Err_NONE
		}
		("DRAWBOT Surface Suite", 1 | 2) => {
			unsafe { *suite = &drawbot::SURFACE_SUITE as *const _ as *const c_void };
			PF_Err_NONE
		}
		("DRAWBOT Path Suite", 1) => {
			unsafe { *suite = &drawbot::PATH_SUITE as *const _ as *const c_void };
			PF_Err_NONE
		}
		("DRAWBOT Pen Suite", 1) => {
			unsafe { *suite = &drawbot::PEN_SUITE as *const _ as *const c_void };
			PF_Err_NONE
		}
		("DRAWBOT Image Suite", 1) => {
			unsafe { *suite = &drawbot::IMAGE_SUITE as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Register Suite", 6) => {
			unsafe {
				*suite = &register::REGISTER as *const _ as *const std::ffi::c_void;
			}
			PF_Err_NONE
		}
		// Utility Suite 1 and 2 (wire 3 and 5) are prefixes of 3 (wire 7), and 4
		// (wire 10) of 5 (wire 11). 5 inserts AEGP_GetLastErrorMessage and 6
		// (wire 13) AEGP_ReportInfoUnicode, moving every later slot.
		("AEGP Utility Suite", v)
			if v == kAEGPUtilitySuiteVersion1 as i32
				|| v == kAEGPUtilitySuiteVersion2 as i32
				|| v == kAEGPUtilitySuiteVersion3 as i32 =>
		{
			unsafe { *suite = &AEGP_UTILITY_3 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Utility Suite", v) if v == kAEGPUtilitySuiteVersion4 as i32 || v == kAEGPUtilitySuiteVersion5 as i32 => {
			unsafe { *suite = &AEGP_UTILITY_5 as *const _ as *const c_void };
			PF_Err_NONE
		}
		("AEGP Utility Suite", v) if v == kAEGPUtilitySuiteVersion6 as i32 => {
			unsafe { *suite = &AEGP_UTILITY_6 as *const _ as *const c_void };
			PF_Err_NONE
		}
		_ => {
			crate::observe::unsupported(
				crate::observe::UnsupportedKind::MissingSuite,
				suite_name,
				format!("v{version}"),
			);
			PF_Err_OUT_OF_MEMORY
		}
	}
}

/// Emulates `SPBasicSuite::ReleaseSuite` function
/// # Safety
/// This function is unsafe because it handles raw pointers.
#[allow(non_snake_case)]
pub unsafe extern "C" fn rusty_release_suite(name: *const ::std::os::raw::c_char, version: i32) -> PF_Err {
	crate::core::ffi::guard("rusty_release_suite", || {
		if name.is_null() {
			diag!("SPBasicSuite/ReleaseSuite", "name" => "(null)", "version" => version);
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		diag!("SPBasicSuite/ReleaseSuite",
			// SAFETY: `name` is non-null, and the plugin passes a C string.
			"name" => format!("{:?}", unsafe { CStr::from_ptr(name) }),
			"version" => version,
		);

		// Every suite is a process-wide shared static (see the module docs); nothing
		// is allocated per acquire, so releasing one is a no-op.
		PF_Err_NONE
	})
}

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

	fn acquire(name: &str, version: i32) -> (PF_Err, *const c_void) {
		let cname = CString::new(name).unwrap();
		let mut out: *const c_void = std::ptr::null();
		let err = unsafe { rusty_acquire_suite(cname.as_ptr(), version, &mut out) };
		(err, out)
	}

	#[test]
	fn acquire_serves_every_registered_suite() {
		for (name, version) in [
			("PF ANSI Suite", 1),
			("PF Iterate8 Suite", 2),
			("PF iterate16 Suite", 1),
			("PF iterate16 Suite", 2),
			("PF iterateFloat Suite", 2),
			("PF AngleParamSuite", 1),
			("PF ColorParamSuite", 1),
			("PF PointParamSuite", 1),
			("PF Color Suite", 1),
			("PF Color16 Suite", 1),
			("PF ColorFloat Suite", 1),
			("PF Fill Matte Suite", 2),
			("PF Pixel Data Suite", 1),
			("PF Pixel Data Suite", 2),
			("PF Pixel Format Suite", 1),
			("PF Pixel Format Suite", 2),
			("PF Batch Sampling Suite", 1),
			("PF Sampling8 Suite", 1),
			("PF Sampling16 Suite", 1),
			("PF SamplingFloat Suite", 1),
			("PF Effect UI Suite", 1),
			("PF Effect Custom UI Suite", 1),
			("PF Effect Custom UI Suite", 2),
			("PF Effect Custom UI Overlay Theme Suite", 1),
			("PF Path Query Suite", 1),
			("PF Path Data Suite", 1),
			("AEGP Text Layer Suite", 1),
			("AEGP Layer Mask Suite", 6),
			("AEGP Layer Mask Suite", 7),
			("AEGP Mask Outline Suite", 3),
			("AEGP Mask Outline Suite", 5),
			("PF AE Adv App Suite", 1),
			("PF AE Adv App Suite", 2),
			("PF AE Adv Item Suite", 1),
			("PF AE Adv Time Suite", 1),
			("PF AE Adv Time Suite", 4),
			("PF AE Channel Suite", 1),
			("PF Cache On Load Suite", 1),
			("AE Plugin Helper Suite", 1),
			("AE Plugin Helper Suite2", 2),
			("AEGP Stream Suite", 7),
			("AEGP Stream Suite", 8),
			("AEGP Stream Suite", 9),
			("AEGP Stream Suite", 10),
			("AEGP Stream Suite", 11),
			("AEGP Dynamic Stream Suite", 1),
			("AEGP Dynamic Stream Suite", 2),
			("AEGP Dynamic Stream Suite", 3),
			("AEGP Dynamic Stream Suite", 5),
			("AEGP Keyframe Suite", 4),
			("AEGP Keyframe Suite", 5),
			("AEGP Effect Suite", 1),
			("AEGP Effect Suite", 2),
			("AEGP Effect Suite", 3),
			("AEGP Effect Suite", 4),
			("AEGP Iterate Suite", 1),
			("AEGP Layer Suite", 8),
			("AEGP Layer Suite", 11),
			("AEGP Layer Suite", 12),
			("AEGP Layer Suite", 13),
			("AEGP Layer Suite", 14),
			("AEGP Layer Suite", 15),
			("AEGP Comp Suite", 9),
			("AEGP Comp Suite", 21),
			("AEGP Light Suite", 1),
			("PF Color Settings Suite", 3),
			("PF Color Settings Suite", 6),
			("AEGP Memory Suite", 1),
			("AEGP Item Suite", 10),
			("AEGP Item Suite", 13),
			("AEGP Layer Render Options Suite", 2),
			("AEGP Render Options Suite", 4),
			("AEGP Render Suite", 8),
			("AEGP World Suite", 3),
			("AEGP Utility Suite", 3),
			("AEGP Utility Suite", 5),
			("AEGP Utility Suite", 7),
			("AEGP Utility Suite", 10),
			("AEGP Utility Suite", 11),
			("AEGP Utility Suite", 13),
		] {
			let (err, ptr) = acquire(name, version);
			assert_eq!(err, PF_Err_NONE, "'{name}' v{version} should be served");
			assert!(!ptr.is_null(), "'{name}' v{version} returned a null suite");
		}
	}

	#[test]
	fn acquire_rejects_unknown_suites_and_versions() {
		let (err, ptr) = acquire("PF Nonexistent Suite", 1);
		assert_ne!(err, PF_Err_NONE);
		assert!(ptr.is_null());

		// Fill Matte is only served at its known v2 layout.
		let (err, _) = acquire("PF Fill Matte Suite", 3);
		assert_ne!(err, PF_Err_NONE);

		// Dynamic Stream suite 4 is wire version 5, not 4.
		let (err, _) = acquire("AEGP Dynamic Stream Suite", 4);
		assert_ne!(err, PF_Err_NONE);

		// Utility Suite versions that do not exist get no table to guess at.
		let (err, _) = acquire("AEGP Utility Suite", 14);
		assert_ne!(err, PF_Err_NONE);
	}

	#[test]
	fn utility_versions_get_their_own_layout() {
		let (_, v7) = acquire("AEGP Utility Suite", 7);
		let (_, v11) = acquire("AEGP Utility Suite", 11);
		let (_, v13) = acquire("AEGP Utility Suite", 13);
		assert_eq!(v7, &AEGP_UTILITY_3 as *const _ as *const c_void);
		assert_eq!(v11, &AEGP_UTILITY_5 as *const _ as *const c_void);
		assert_eq!(v13, &AEGP_UTILITY_6 as *const _ as *const c_void);
	}
}