Skip to content

XP event5x XPListening, games, ratings, comments and chat earn five times the XP

leftLeaderboard

juicewrld-desktopsrc-taurihosttestplugjwtestplug.cpp

1.4.2Released Nov 21, 2026

jwtestplug.cpp

1,052 lines36.1 KBC++
// A small VST 3 module for the plugin host's tests, built by build.rs in debug builds only and never shipped. Plain// pluginterfaces, no SDK helper classes, so it also shows the host copes with a plugin written straight to the API.////   JW Test Gain    an effect with a separate controller: Gain and Bypass parameters, 64 samples of latency (a delay//                   line, also while bypassed), component and controller states of its own, and a resizable editor that//                   draws the gain and raises it by 1 dB per click (an edit made in the plugin's own window).//   JW Test Crash   one object: setting Crash makes the next process() write through a null pointer; Hang makes it//                   never return; Hang editor makes the editor's attached() never return.//   JW Test Synth   an instrument (marked as one in the scan): a quiet 440 Hz sine, no input.//// The module's own file name changes how it loads, for the scan's tests: "scan-hang" opens a window, as an expired// licence's activation check would, and never returns from GetPluginFactory; "scan-spawn" starts a helper process (as// a licence checker that shows its window from a process of its own would; its id goes to the file JW_TEST_HELPER_PID// names) and never returns either; "scan-crash" crashes in it; "noclasses" has a factory with nothing in it;// "scan-exit" calls ExitProcess(0xFFFFFFFF) as the host reads its third class (what some Waves and Antares// modules looked like to the scan); "scan-stdio" does the same in GetPluginFactory if any standard handle is a pipe;// "shell" is a factory of 719 plugins, as Waves' WaveShell is (see ShellFactory).#ifndef NOMINMAX#define NOMINMAX#endif#include <windows.h>#include "pluginterfaces/base/ibstream.h"#include "pluginterfaces/base/ipluginbase.h"#include "pluginterfaces/gui/iplugview.h"#include "pluginterfaces/gui/iplugviewcontentscalesupport.h"#include "pluginterfaces/vst/ivstaudioprocessor.h"#include "pluginterfaces/vst/ivstcomponent.h"#include "pluginterfaces/vst/ivsteditcontroller.h"#include "pluginterfaces/vst/ivstevents.h"#include "pluginterfaces/vst/ivstmessage.h"#include "pluginterfaces/vst/ivstparameterchanges.h"#include "pluginterfaces/vst/vsttypes.h"#include <atomic>#include <cmath>#include <cstdio>#include <cstring>#include <string>#include <vector>#pragma comment(lib, "user32")#pragma comment(lib, "gdi32")#pragma comment(lib, "ole32")using namespace Steinberg;using namespace Steinberg::Vst;namespace {const FUID kGainProcessorUID (0x4A575447, 0x41494E50, 0x524F4331, 0x00000001);const FUID kGainControllerUID (0x4A575447, 0x41494E43, 0x54524C31, 0x00000002);const FUID kCrashUID (0x4A574352, 0x41534831, 0x00000000, 0x00000003);const FUID kSynthUID (0x4A575359, 0x4E544831, 0x00000000, 0x00000004);constexpr int32 kLatency = 64;constexpr uint32 kGainMagic = 0x4754574A;       // "JWTG"constexpr uint32 kControllerMagic = 0x4354574A; // "JWTC"/// This DLL's own file name, lower case.std::wstring moduleName (){	HMODULE self = nullptr;	GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,	                    reinterpret_cast<LPCWSTR> (&moduleName), &self);	wchar_t path[MAX_PATH] {};	GetModuleFileNameW (self, path, MAX_PATH);	std::wstring s (path);	for (auto& c : s)		c = static_cast<wchar_t> (towlower (c));	return s;}bool nameHas (const wchar_t* part) { return moduleName ().find (part) != std::wstring::npos; }void toString128 (String128 dst, const char* src){	int i = 0;	for (; src[i] && i < 127; i++)		dst[i] = static_cast<TChar> (src[i]);	dst[i] = 0;}bool readU32 (IBStream* s, uint32& v){	int32 n = 0;	return s && s->read (&v, 4, &n) == kResultOk && n == 4;}bool readF32 (IBStream* s, float& v){	int32 n = 0;	return s && s->read (&v, 4, &n) == kResultOk && n == 4;}void writeU32 (IBStream* s, uint32 v) { s->write (&v, 4, nullptr); }void writeF32 (IBStream* s, float v) { s->write (&v, 4, nullptr); }/// The last value each parameter got in this block.template <typename F>void eachParamChange (ProcessData& data, F f){	if (!data.inputParameterChanges)		return;	int32 n = data.inputParameterChanges->getParameterCount ();	for (int32 i = 0; i < n; i++)	{		auto q = data.inputParameterChanges->getParameterData (i);		if (!q || q->getPointCount () <= 0)			continue;		int32 offset = 0;		ParamValue v = 0;		if (q->getPoint (q->getPointCount () - 1, offset, v) == kResultTrue)			f (q->getParameterId (), v);	}}#define JW_REFCOUNT                                                   \	std::atomic<uint32> refs {1};                                       \	uint32 PLUGIN_API addRef () override { return ++refs; }            \	uint32 PLUGIN_API release () override                              \	{                                                                   \		uint32 r = --refs;                                                \		if (r == 0)                                                       \			delete this;                                                  \		return r;                                                         \	}// ---------------------------------------------------------------------------------------------------------------------// The editor: a child window with a line of text; clicks raise the gain through the component handler.class Editable{public:	virtual ~Editable () = default;	virtual std::string describe () = 0;	virtual void clicked () = 0;};const wchar_t* kViewClass = L"JWTestPluginView";LRESULT CALLBACK viewProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){	auto target = reinterpret_cast<Editable*> (GetWindowLongPtrW (hwnd, GWLP_USERDATA));	switch (msg)	{		case WM_PAINT:		{			PAINTSTRUCT ps;			HDC dc = BeginPaint (hwnd, &ps);			RECT r;			GetClientRect (hwnd, &r);			HBRUSH bg = CreateSolidBrush (RGB (24, 24, 30));			FillRect (dc, &r, bg);			DeleteObject (bg);			SetBkMode (dc, TRANSPARENT);			SetTextColor (dc, RGB (230, 230, 236));			std::string text = target ? target->describe () : "JW Test";			DrawTextA (dc, text.c_str (), -1, &r, DT_CENTER | DT_VCENTER | DT_WORDBREAK);			EndPaint (hwnd, &ps);			return 0;		}		case WM_LBUTTONDOWN:			if (target)				target->clicked ();			InvalidateRect (hwnd, nullptr, TRUE);			return 0;	}	return DefWindowProcW (hwnd, msg, wParam, lParam);}class TestView : public IPlugView, public IPlugViewContentScaleSupport{public:	TestView (Editable* target, bool hangOnAttach) : target (target), hang (hangOnAttach) {}	tresult PLUGIN_API isPlatformTypeSupported (FIDString type) override	{		return strcmp (type, kPlatformTypeHWND) == 0 ? kResultTrue : kResultFalse;	}	tresult PLUGIN_API attached (void* parent, FIDString type) override	{		if (hang)			for (;;)				Sleep (10);		static bool registered = false;		if (!registered)		{			WNDCLASSW wc {};			wc.lpfnWndProc = viewProc;			wc.hInstance = GetModuleHandleW (nullptr);			wc.hCursor = LoadCursorW (nullptr, IDC_HAND);			wc.lpszClassName = kViewClass;			RegisterClassW (&wc);			registered = true;		}		child = CreateWindowExW (0, kViewClass, L"", WS_CHILD | WS_VISIBLE, 0, 0, rect.getWidth (), rect.getHeight (),		                         static_cast<HWND> (parent), nullptr, GetModuleHandleW (nullptr), nullptr);		SetWindowLongPtrW (child, GWLP_USERDATA, reinterpret_cast<LONG_PTR> (target));		return child ? kResultTrue : kResultFalse;	}	tresult PLUGIN_API removed () override	{		if (child)			DestroyWindow (child);		child = nullptr;		return kResultTrue;	}	tresult PLUGIN_API onWheel (float) override { return kResultFalse; }	tresult PLUGIN_API onKeyDown (char16, int16, int16) override { return kResultFalse; }	tresult PLUGIN_API onKeyUp (char16, int16, int16) override { return kResultFalse; }	tresult PLUGIN_API getSize (ViewRect* size) override	{		*size = rect;		return kResultTrue;	}	tresult PLUGIN_API onSize (ViewRect* newSize) override	{		rect = *newSize;		if (child)			MoveWindow (child, 0, 0, rect.getWidth (), rect.getHeight (), TRUE);		return kResultTrue;	}	tresult PLUGIN_API onFocus (TBool) override { return kResultTrue; }	tresult PLUGIN_API setFrame (IPlugFrame* f) override	{		frame = f;		return kResultTrue;	}	tresult PLUGIN_API canResize () override { return kResultTrue; }	tresult PLUGIN_API checkSizeConstraint (ViewRect* r) override	{		int32 w = r->getWidth (), h = r->getHeight ();		int32 minW = static_cast<int32> (240 * scale), minH = static_cast<int32> (120 * scale);		int32 maxW = static_cast<int32> (1200 * scale), maxH = static_cast<int32> (800 * scale);		w = w < minW ? minW : (w > maxW ? maxW : w);		h = h < minH ? minH : (h > maxH ? maxH : h);		r->right = r->left + w;		r->bottom = r->top + h;		return kResultTrue;	}	tresult PLUGIN_API setContentScaleFactor (ScaleFactor factor) override	{		// Grows with the monitor's scale, as a DPI-aware plugin does, and tells the host.		if (factor <= 0)			return kResultFalse;		ViewRect r (0, 0, static_cast<int32> (rect.getWidth () / scale * factor), static_cast<int32> (rect.getHeight () / scale * factor));		scale = factor;		if (frame && (r.getWidth () != rect.getWidth () || r.getHeight () != rect.getHeight ()))			frame->resizeView (this, &r);		else			rect = r;		return kResultTrue;	}	tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override	{		QUERY_INTERFACE (iid, obj, FUnknown::iid, IPlugView)		QUERY_INTERFACE (iid, obj, IPlugView::iid, IPlugView)		QUERY_INTERFACE (iid, obj, IPlugViewContentScaleSupport::iid, IPlugViewContentScaleSupport)		*obj = nullptr;		return kNoInterface;	}	JW_REFCOUNTprivate:	virtual ~TestView () { removed (); }	Editable* target;	bool hang;	ViewRect rect {0, 0, 360, 180};	HWND child = nullptr;	IPlugFrame* frame = nullptr;	float scale = 1;};// ---------------------------------------------------------------------------------------------------------------------// Stereo-bus plumbing shared by the processors.struct Buses{	SpeakerArrangement in = SpeakerArr::kStereo;	SpeakerArrangement out = SpeakerArr::kStereo;	bool hasInput = true;};tresult busInfo (const Buses& b, MediaType type, BusDirection dir, int32 index, BusInfo& info, bool events){	memset (&info, 0, sizeof (info));	info.mediaType = type;	info.direction = dir;	info.flags = BusInfo::kDefaultActive;	if (type == kEvent)	{		if (!events || dir != kInput || index != 0)			return kInvalidArgument;		info.channelCount = 16;		toString128 (info.name, "Notes");		info.busType = kMain;		return kResultTrue;	}	if (index != 0 || (dir == kInput && !b.hasInput))		return kInvalidArgument;	info.channelCount = SpeakerArr::getChannelCount (dir == kInput ? b.in : b.out);	toString128 (info.name, dir == kInput ? "Input" : "Output");	info.busType = kMain;	return kResultTrue;}// ---------------------------------------------------------------------------------------------------------------------// JW Test Gain: the processor.class GainProcessor : public IComponent, public IAudioProcessor, public IConnectionPoint{public:	tresult PLUGIN_API initialize (FUnknown*) override { return kResultOk; }	tresult PLUGIN_API terminate () override { return kResultOk; }	tresult PLUGIN_API getControllerClassId (TUID classId) override	{		kGainControllerUID.toTUID (classId);		return kResultTrue;	}	tresult PLUGIN_API setIoMode (IoMode) override { return kResultOk; }	int32 PLUGIN_API getBusCount (MediaType type, BusDirection) override { return type == kAudio ? 1 : 0; }	tresult PLUGIN_API getBusInfo (MediaType type, BusDirection dir, int32 index, BusInfo& info) override	{		return busInfo (buses, type, dir, index, info, false);	}	tresult PLUGIN_API getRoutingInfo (RoutingInfo&, RoutingInfo&) override { return kNotImplemented; }	tresult PLUGIN_API activateBus (MediaType, BusDirection, int32, TBool) override { return kResultTrue; }	tresult PLUGIN_API setActive (TBool state) override	{		if (state)		{			delay[0].assign (kLatency, 0.f);			delay[1].assign (kLatency, 0.f);			pos = 0;		}		return kResultOk;	}	tresult PLUGIN_API setState (IBStream* s) override	{		uint32 magic = 0, bypassed = 0;		float g = 0;		if (!readU32 (s, magic) || magic != kGainMagic || !readF32 (s, g) || !readU32 (s, bypassed))			return kResultFalse;		gain = g;		bypass = bypassed != 0;		return kResultOk;	}	tresult PLUGIN_API getState (IBStream* s) override	{		writeU32 (s, kGainMagic);		writeF32 (s, gain);		writeU32 (s, bypass ? 1 : 0);		return kResultOk;	}	tresult PLUGIN_API setBusArrangements (SpeakerArrangement* inputs, int32 numIns, SpeakerArrangement* outputs, int32 numOuts) override	{		if (numIns != 1 || numOuts != 1)			return kResultFalse;		if (inputs[0] == SpeakerArr::kStereo && outputs[0] == SpeakerArr::kStereo)		{			buses.in = buses.out = SpeakerArr::kStereo;			return kResultTrue;		}		return kResultFalse;	}	tresult PLUGIN_API getBusArrangement (BusDirection dir, int32 index, SpeakerArrangement& arr) override	{		if (index != 0)			return kInvalidArgument;		arr = dir == kInput ? buses.in : buses.out;		return kResultTrue;	}	tresult PLUGIN_API canProcessSampleSize (int32 size) override { return size == kSample32 ? kResultTrue : kResultFalse; }	uint32 PLUGIN_API getLatencySamples () override { return kLatency; }	tresult PLUGIN_API setupProcessing (ProcessSetup& setup) override	{		rate = setup.sampleRate;		return kResultOk;	}	tresult PLUGIN_API setProcessing (TBool) override { return kResultOk; }	tresult PLUGIN_API process (ProcessData& data) override	{		eachParamChange (data, [this] (ParamID id, ParamValue v) {			if (id == 0)				gain = static_cast<float> (v * 2.0);			else if (id == 1)				bypass = v >= 0.5;		});		if (data.numSamples <= 0 || data.numInputs < 1 || data.numOutputs < 1)			return kResultOk;		float* in[2] = {data.inputs[0].channelBuffers32[0], data.inputs[0].channelBuffers32[1]};		float* out[2] = {data.outputs[0].channelBuffers32[0], data.outputs[0].channelBuffers32[1]};		float g = bypass ? 1.f : gain;		for (int32 i = 0; i < data.numSamples; i++)		{			for (int c = 0; c < 2; c++)			{				float d = delay[c][pos];				delay[c][pos] = in[c][i];				out[c][i] = d * g;			}			pos = (pos + 1) % kLatency;		}		data.outputs[0].silenceFlags = 0;		return kResultOk;	}	uint32 PLUGIN_API getTailSamples () override { return kLatency; }	tresult PLUGIN_API connect (IConnectionPoint*) override { return kResultTrue; }	tresult PLUGIN_API disconnect (IConnectionPoint*) override { return kResultTrue; }	tresult PLUGIN_API notify (IMessage*) override { return kResultOk; }	tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override	{		QUERY_INTERFACE (iid, obj, FUnknown::iid, IComponent)		QUERY_INTERFACE (iid, obj, IPluginBase::iid, IComponent)		QUERY_INTERFACE (iid, obj, IComponent::iid, IComponent)		QUERY_INTERFACE (iid, obj, IAudioProcessor::iid, IAudioProcessor)		QUERY_INTERFACE (iid, obj, IConnectionPoint::iid, IConnectionPoint)		*obj = nullptr;		return kNoInterface;	}	JW_REFCOUNTprivate:	virtual ~GainProcessor () = default;	Buses buses;	float gain = 1.f;	bool bypass = false;	double rate = 48000;	std::vector<float> delay[2] {std::vector<float> (kLatency), std::vector<float> (kLatency)};	int32 pos = 0;};// ---------------------------------------------------------------------------------------------------------------------// JW Test Gain: the controller and its editor.class GainController : public IEditController, public IConnectionPoint, public Editable{public:	tresult PLUGIN_API initialize (FUnknown*) override { return kResultOk; }	tresult PLUGIN_API terminate () override	{		handler = nullptr;		return kResultOk;	}	tresult PLUGIN_API setComponentState (IBStream* s) override	{		uint32 magic = 0, bypassed = 0;		float g = 0;		if (!readU32 (s, magic) || magic != kGainMagic || !readF32 (s, g) || !readU32 (s, bypassed))			return kResultFalse;		values[0] = g / 2.0;		values[1] = bypassed ? 1 : 0;		return kResultOk;	}	tresult PLUGIN_API setState (IBStream* s) override	{		uint32 magic = 0, n = 0;		if (!readU32 (s, magic) || magic != kControllerMagic || !readU32 (s, n))			return kResultFalse;		clicks = n;		return kResultOk;	}	tresult PLUGIN_API getState (IBStream* s) override	{		writeU32 (s, kControllerMagic);		writeU32 (s, clicks);		return kResultOk;	}	int32 PLUGIN_API getParameterCount () override { return 2; }	tresult PLUGIN_API getParameterInfo (int32 index, ParameterInfo& info) override	{		memset (&info, 0, sizeof (info));		if (index == 0)		{			info.id = 0;			toString128 (info.title, "Gain");			toString128 (info.shortTitle, "Gain");			toString128 (info.units, "dB");			info.defaultNormalizedValue = 0.5;			info.flags = ParameterInfo::kCanAutomate;			return kResultOk;		}		if (index == 1)		{			info.id = 1;			toString128 (info.title, "Bypass");			toString128 (info.shortTitle, "Bypass");			info.stepCount = 1;			info.flags = ParameterInfo::kCanAutomate | ParameterInfo::kIsBypass;			return kResultOk;		}		return kInvalidArgument;	}	tresult PLUGIN_API getParamStringByValue (ParamID id, ParamValue v, String128 out) override	{		char text[64];		if (id == 0)		{			double g = v * 2.0;			if (g <= 0)				snprintf (text, sizeof (text), "-inf dB");			else				snprintf (text, sizeof (text), "%.1f dB", 20.0 * std::log10 (g));		}		else			snprintf (text, sizeof (text), "%s", v >= 0.5 ? "On" : "Off");		toString128 (out, text);		return kResultOk;	}	tresult PLUGIN_API getParamValueByString (ParamID, TChar*, ParamValue&) override { return kNotImplemented; }	ParamValue PLUGIN_API normalizedParamToPlain (ParamID, ParamValue v) override { return v; }	ParamValue PLUGIN_API plainParamToNormalized (ParamID, ParamValue v) override { return v; }	ParamValue PLUGIN_API getParamNormalized (ParamID id) override { return id < 2 ? values[id] : 0; }	tresult PLUGIN_API setParamNormalized (ParamID id, ParamValue v) override	{		if (id >= 2)			return kInvalidArgument;		values[id] = v;		return kResultOk;	}	tresult PLUGIN_API setComponentHandler (IComponentHandler* h) override	{		handler = h;		return kResultOk;	}	IPlugView* PLUGIN_API createView (FIDString name) override	{		if (strcmp (name, ViewType::kEditor) != 0)			return nullptr;		return new TestView (this, false);	}	std::string describe () override	{		char text[160];		double g = values[0] * 2.0;		snprintf (text, sizeof (text), "JW Test Gain\n%.1f dB%s\nclick to raise it 1 dB", g > 0 ? 20.0 * std::log10 (g) : -99.0,		          values[1] >= 0.5 ? " (bypassed)" : "");		return text;	}	void clicked () override	{		double g = values[0] * 2.0 * std::pow (10.0, 1.0 / 20.0);		double v = g / 2.0 > 1.0 ? 1.0 : g / 2.0;		clicks++;		values[0] = v;		if (handler)		{			handler->beginEdit (0);			handler->performEdit (0, v);			handler->endEdit (0);		}	}	tresult PLUGIN_API connect (IConnectionPoint*) override { return kResultTrue; }	tresult PLUGIN_API disconnect (IConnectionPoint*) override { return kResultTrue; }	tresult PLUGIN_API notify (IMessage*) override { return kResultOk; }	tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override	{		QUERY_INTERFACE (iid, obj, FUnknown::iid, IEditController)		QUERY_INTERFACE (iid, obj, IPluginBase::iid, IEditController)		QUERY_INTERFACE (iid, obj, IEditController::iid, IEditController)		QUERY_INTERFACE (iid, obj, IConnectionPoint::iid, IConnectionPoint)		*obj = nullptr;		return kNoInterface;	}	JW_REFCOUNTprivate:	virtual ~GainController () = default;	double values[2] {0.5, 0};	uint32 clicks = 0;	IComponentHandler* handler = nullptr;};// ---------------------------------------------------------------------------------------------------------------------// JW Test Crash and JW Test Synth: single-component plugins.class SingleEffect : public IComponent, public IAudioProcessor, public IEditController, public Editable{public:	explicit SingleEffect (bool synth) : synth (synth) { buses.hasInput = !synth; }	tresult PLUGIN_API initialize (FUnknown*) override { return kResultOk; }	tresult PLUGIN_API terminate () override { return kResultOk; }	tresult PLUGIN_API getControllerClassId (TUID) override { return kNotImplemented; }	tresult PLUGIN_API setIoMode (IoMode) override { return kResultOk; }	int32 PLUGIN_API getBusCount (MediaType type, BusDirection dir) override	{		if (type == kEvent)			return synth && dir == kInput ? 1 : 0;		return dir == kInput && synth ? 0 : 1;	}	tresult PLUGIN_API getBusInfo (MediaType type, BusDirection dir, int32 index, BusInfo& info) override	{		return busInfo (buses, type, dir, index, info, synth);	}	tresult PLUGIN_API getRoutingInfo (RoutingInfo&, RoutingInfo&) override { return kNotImplemented; }	tresult PLUGIN_API activateBus (MediaType, BusDirection, int32, TBool) override { return kResultTrue; }	tresult PLUGIN_API setActive (TBool) override	{		phase = 0;		return kResultOk;	}	tresult PLUGIN_API setState (IBStream* s) override	{		uint32 magic = 0;		return readU32 (s, magic) && magic == 0x5354574A ? kResultOk : kResultFalse;	}	tresult PLUGIN_API getState (IBStream* s) override	{		writeU32 (s, 0x5354574A);		return kResultOk;	}	tresult PLUGIN_API setBusArrangements (SpeakerArrangement* inputs, int32 numIns, SpeakerArrangement* outputs, int32 numOuts) override	{		if (numOuts != 1 || outputs[0] != SpeakerArr::kStereo)			return kResultFalse;		if (!synth && (numIns != 1 || inputs[0] != SpeakerArr::kStereo))			return kResultFalse;		return kResultTrue;	}	tresult PLUGIN_API getBusArrangement (BusDirection dir, int32 index, SpeakerArrangement& arr) override	{		if (index != 0 || (dir == kInput && synth))			return kInvalidArgument;		arr = SpeakerArr::kStereo;		return kResultTrue;	}	tresult PLUGIN_API canProcessSampleSize (int32 size) override { return size == kSample32 ? kResultTrue : kResultFalse; }	uint32 PLUGIN_API getLatencySamples () override { return 0; }	tresult PLUGIN_API setupProcessing (ProcessSetup& setup) override	{		rate = setup.sampleRate;		return kResultOk;	}	tresult PLUGIN_API setProcessing (TBool) override { return kResultOk; }	tresult PLUGIN_API process (ProcessData& data) override	{		eachParamChange (data, [this] (ParamID id, ParamValue v) { setParamNormalized (id, v); });		if (values[0] >= 0.5)		{			// Crash: a write through a null pointer, as a buggy plugin would.			volatile int* nowhere = nullptr;			*nowhere = 42;		}		if (values[1] >= 0.5)			for (;;)				Sleep (10); // Hang: never returns.		if (data.numSamples <= 0 || data.numOutputs < 1)			return kResultOk;		float* out[2] = {data.outputs[0].channelBuffers32[0], data.outputs[0].channelBuffers32[1]};		if (synth)		{			for (int32 i = 0; i < data.numSamples; i++)			{				float v = 0.25f * static_cast<float> (std::sin (phase));				phase += 2.0 * 3.14159265358979 * 440.0 / rate;				out[0][i] = out[1][i] = v;			}		}		else if (data.numInputs > 0)		{			for (int c = 0; c < 2; c++)				memcpy (out[c], data.inputs[0].channelBuffers32[c], sizeof (float) * data.numSamples);		}		return kResultOk;	}	uint32 PLUGIN_API getTailSamples () override { return 0; }	tresult PLUGIN_API setComponentState (IBStream*) override { return kResultOk; }	int32 PLUGIN_API getParameterCount () override { return synth ? 0 : 3; }	tresult PLUGIN_API getParameterInfo (int32 index, ParameterInfo& info) override	{		static const char* names[3] = {"Crash", "Hang", "Hang editor"};		if (synth || index < 0 || index > 2)			return kInvalidArgument;		memset (&info, 0, sizeof (info));		info.id = index;		toString128 (info.title, names[index]);		toString128 (info.shortTitle, names[index]);		info.stepCount = 1;		info.flags = ParameterInfo::kCanAutomate;		return kResultOk;	}	tresult PLUGIN_API getParamStringByValue (ParamID, ParamValue v, String128 out) override	{		toString128 (out, v >= 0.5 ? "On" : "Off");		return kResultOk;	}	tresult PLUGIN_API getParamValueByString (ParamID, TChar*, ParamValue&) override { return kNotImplemented; }	ParamValue PLUGIN_API normalizedParamToPlain (ParamID, ParamValue v) override { return v; }	ParamValue PLUGIN_API plainParamToNormalized (ParamID, ParamValue v) override { return v; }	ParamValue PLUGIN_API getParamNormalized (ParamID id) override { return id < 3 ? values[id] : 0; }	tresult PLUGIN_API setParamNormalized (ParamID id, ParamValue v) override	{		if (id >= 3)			return kInvalidArgument;		values[id] = v;		return kResultOk;	}	tresult PLUGIN_API setComponentHandler (IComponentHandler*) override { return kResultOk; }	IPlugView* PLUGIN_API createView (FIDString name) override	{		if (synth || strcmp (name, ViewType::kEditor) != 0)			return nullptr;		return new TestView (this, values[2] >= 0.5);	}	std::string describe () override { return "JW Test Crash\nsets of Crash or Hang break the next block"; }	void clicked () override {}	tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override	{		QUERY_INTERFACE (iid, obj, FUnknown::iid, IComponent)		QUERY_INTERFACE (iid, obj, IPluginBase::iid, IComponent)		QUERY_INTERFACE (iid, obj, IComponent::iid, IComponent)		QUERY_INTERFACE (iid, obj, IAudioProcessor::iid, IAudioProcessor)		QUERY_INTERFACE (iid, obj, IEditController::iid, IEditController)		*obj = nullptr;		return kNoInterface;	}	// Both IComponent and IEditController are IPluginBase: one pair of methods serves both.	JW_REFCOUNTprivate:	virtual ~SingleEffect () = default;	bool synth;	Buses buses;	double rate = 48000;	double phase = 0;	double values[3] {0, 0, 0};};// ---------------------------------------------------------------------------------------------------------------------// The factory.struct ClassEntry{	const FUID* uid;	const char* category;	const char* name;	const char* sub;};const ClassEntry kClasses[] = {	{&kGainProcessorUID, kVstAudioEffectClass, "JW Test Gain", "Fx|Tools"},	{&kGainControllerUID, kVstComponentControllerClass, "JW Test Gain", "Fx|Tools"},	{&kCrashUID, kVstAudioEffectClass, "JW Test Crash", "Fx"},	{&kSynthUID, kVstAudioEffectClass, "JW Test Synth", "Instrument|Synth"},};class Factory : public IPluginFactory2{public:	tresult PLUGIN_API getFactoryInfo (PFactoryInfo* info) override	{		memset (info, 0, sizeof (*info));		strcpy_s (info->vendor, "juicewrld.io tests");		strcpy_s (info->url, "https://juicewrld.io");		strcpy_s (info->email, "[email protected]");		info->flags = PFactoryInfo::kNoFlags;		return kResultOk;	}	int32 PLUGIN_API countClasses () override { return empty ? 0 : static_cast<int32> (sizeof (kClasses) / sizeof (kClasses[0])); }	tresult PLUGIN_API getClassInfo (int32 index, PClassInfo* info) override	{		if (index < 0 || index >= countClasses ())			return kInvalidArgument;		const auto& c = kClasses[index];		memset (info, 0, sizeof (*info));		c.uid->toTUID (info->cid);		info->cardinality = PClassInfo::kManyInstances;		strcpy_s (info->category, c.category);		strcpy_s (info->name, c.name);		return kResultOk;	}	tresult PLUGIN_API getClassInfo2 (int32 index, PClassInfo2* info) override	{		if (index < 0 || index >= countClasses ())			return kInvalidArgument;		// What some WaveShell and new Antares modules did in the app's scan: end the process with exit code -1		// part way through, with no crash (here as the host reads class 3 of 4).		if (exitAt == index)			ExitProcess (0xFFFFFFFF);		const auto& c = kClasses[index];		memset (info, 0, sizeof (*info));		c.uid->toTUID (info->cid);		info->cardinality = PClassInfo::kManyInstances;		strcpy_s (info->category, c.category);		strcpy_s (info->name, c.name);		strcpy_s (info->subCategories, c.sub);		strcpy_s (info->vendor, "juicewrld.io tests");		strcpy_s (info->version, "1.0.0");		strcpy_s (info->sdkVersion, kVstVersionString);		return kResultOk;	}	tresult PLUGIN_API createInstance (FIDString cid, FIDString iid, void** obj) override	{		*obj = nullptr;		FUID id = FUID::fromTUID (*reinterpret_cast<const TUID*> (cid));		FUnknown* made = nullptr;		if (id == kGainProcessorUID)			made = static_cast<IComponent*> (new GainProcessor ());		else if (id == kGainControllerUID)			made = static_cast<IEditController*> (new GainController ());		else if (id == kCrashUID)			made = static_cast<IComponent*> (new SingleEffect (false));		else if (id == kSynthUID)			made = static_cast<IComponent*> (new SingleEffect (true));		if (!made)			return kNoInterface;		tresult r = made->queryInterface (*reinterpret_cast<const TUID*> (iid), obj);		made->release ();		return r == kResultOk ? kResultOk : kNoInterface;	}	tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override	{		QUERY_INTERFACE (iid, obj, FUnknown::iid, IPluginFactory)		QUERY_INTERFACE (iid, obj, IPluginFactory::iid, IPluginFactory)		QUERY_INTERFACE (iid, obj, IPluginFactory2::iid, IPluginFactory2)		*obj = nullptr;		return kNoInterface;	}	JW_REFCOUNT	bool empty = false;	int32 exitAt = -1;private:	virtual ~Factory () = default;};// ---------------------------------------------------------------------------------------------------------------------// A shell: one module whose factory lists hundreds of plugins, as Waves' WaveShell does (718 in WaveShell1-VST3 17.1),// through IPluginFactory3's Unicode class info, with class IDs that read as ASCII the way Waves' do ("VST" + a code +// a name: "VST76CScla-76 st" is CLA-76 Stereo's). Class 1 can't be read at all (every getClassInfo call fails), which// the host skips; class 8's name has a character outside the BMP (an emoji), which the SDK's own converter breaks.constexpr int32 kShellClasses = 720;/// Class `index`'s ID, "VST" + a 4-digit code + M or S + "jwshell ", as Waves' INLINE_UID-made IDs are laid out.FUID shellUid (int32 index){	char id[17];	sprintf_s (id, "VST%04d%cjwshell ", static_cast<int> (index), index % 2 ? 'S' : 'M');	auto word = [&] (int at) {		return (uint32 (uint8 (id[at])) << 24) | (uint32 (uint8 (id[at + 1])) << 16) | (uint32 (uint8 (id[at + 2])) << 8) | uint32 (uint8 (id[at + 3]));	};	return FUID (word (0), word (4), word (8), word (12));}void copy16 (char16* dst, const std::u16string& s, size_t max){	size_t n = s.size () < max - 1 ? s.size () : max - 1;	memcpy (dst, s.data (), n * sizeof (char16));	dst[n] = 0;}std::u16string ascii16 (const std::string& s) { return std::u16string (s.begin (), s.end ()); }std::string shellName (int32 index){	char name[64];	sprintf_s (name, "JW Shell %04d %s", static_cast<int> (index), index % 2 ? "Stereo" : "Mono");	return name;}class ShellFactory : public IPluginFactory3{public:	tresult PLUGIN_API getFactoryInfo (PFactoryInfo* info) override	{		memset (info, 0, sizeof (*info));		strcpy_s (info->vendor, "juicewrld.io tests");		info->flags = PFactoryInfo::kUnicode;		return kResultOk;	}	int32 PLUGIN_API countClasses () override { return kShellClasses; }	tresult PLUGIN_API getClassInfo (int32 index, PClassInfo* info) override	{		if (index <= 0 || index >= kShellClasses)			return kInvalidArgument;		memset (info, 0, sizeof (*info));		shellUid (index).toTUID (info->cid);		info->cardinality = PClassInfo::kManyInstances;		strcpy_s (info->category, kVstAudioEffectClass);		strcpy_s (info->name, shellName (index).c_str ());		return kResultOk;	}	tresult PLUGIN_API getClassInfo2 (int32 index, PClassInfo2* info) override	{		if (index <= 0 || index >= kShellClasses)			return kInvalidArgument;		memset (info, 0, sizeof (*info));		shellUid (index).toTUID (info->cid);		info->cardinality = PClassInfo::kManyInstances;		strcpy_s (info->category, kVstAudioEffectClass);		strcpy_s (info->name, shellName (index).c_str ());		info->classFlags = kDistributable;		strcpy_s (info->subCategories, "Fx|Dynamics");		strcpy_s (info->vendor, "juicewrld.io tests");		strcpy_s (info->version, "17.1.0");		strcpy_s (info->sdkVersion, kVstVersionString);		return kResultOk;	}	tresult PLUGIN_API getClassInfoUnicode (int32 index, PClassInfoW* info) override	{		if (index <= 0 || index >= kShellClasses)			return kInvalidArgument;		memset (info, 0, sizeof (*info));		shellUid (index).toTUID (info->cid);		info->cardinality = PClassInfo::kManyInstances;		strcpy_s (info->category, kVstAudioEffectClass);		std::u16string name = index == 8 ? u"JW Shell \U0001F39B 0008 Mono" : ascii16 (shellName (index));		copy16 (info->name, name, PClassInfo::kNameSize);		info->classFlags = kDistributable;		strcpy_s (info->subCategories, "Fx|Dynamics");		copy16 (info->vendor, u"juicewrld.io tests", PClassInfo2::kVendorSize);		copy16 (info->version, u"17.1.0", PClassInfo2::kVersionSize);		copy16 (info->sdkVersion, ascii16 (kVstVersionString), PClassInfo2::kVersionSize);		return kResultOk;	}	tresult PLUGIN_API setHostContext (FUnknown*) override { return kResultOk; }	tresult PLUGIN_API createInstance (FIDString cid, FIDString iid, void** obj) override	{		*obj = nullptr;		FUID id = FUID::fromTUID (*reinterpret_cast<const TUID*> (cid));		bool known = false;		for (int32 i = 1; i < kShellClasses && !known; i++)			known = id == shellUid (i);		if (!known)			return kNoInterface;		FUnknown* made = static_cast<IComponent*> (new SingleEffect (false));		tresult r = made->queryInterface (*reinterpret_cast<const TUID*> (iid), obj);		made->release ();		return r == kResultOk ? kResultOk : kNoInterface;	}	tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override	{		QUERY_INTERFACE (iid, obj, FUnknown::iid, IPluginFactory)		QUERY_INTERFACE (iid, obj, IPluginFactory::iid, IPluginFactory)		QUERY_INTERFACE (iid, obj, IPluginFactory2::iid, IPluginFactory2)		QUERY_INTERFACE (iid, obj, IPluginFactory3::iid, IPluginFactory3)		*obj = nullptr;		return kNoInterface;	}	JW_REFCOUNTprivate:	virtual ~ShellFactory () = default;};} // namespaceextern "C" {__declspec (dllexport) bool PLUGIN_API InitDll () { return true; }__declspec (dllexport) bool PLUGIN_API ExitDll () { return true; }/// What a copy-protected plugin with an expired licence does as it loads: a window of its own, waiting for the user,/// so loading never finishes. Put off-screen and never activated, so a test run doesn't get in anyone's way; the tests/// find it by its title and check it goes when the scan child is ended.void activationWindow (){	WNDCLASSW wc {};	wc.lpfnWndProc = DefWindowProcW;	wc.hInstance = GetModuleHandleW (nullptr);	wc.lpszClassName = L"JuiceWRLDTestActivation";	RegisterClassW (&wc);	CreateWindowExW (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE, wc.lpszClassName, L"JW Test Activation: all visible licenses expired",	                 WS_POPUP | WS_CAPTION | WS_VISIBLE, -4000, -4000, 420, 200, nullptr, nullptr, wc.hInstance, nullptr);	MSG msg;	for (;;)	{		while (PeekMessageW (&msg, nullptr, 0, 0, PM_REMOVE))		{			TranslateMessage (&msg);			DispatchMessageW (&msg);		}		Sleep (10);	}}/// What a plugin whose licence check runs in a process of its own does: starts it (a quiet wait here, no window, no/// console), and waits for it. The tests check the helper goes when the scan child is ended.void helperProcess (){	wchar_t cmd[] = L"cmd.exe /c ping -n 120 127.0.0.1 > nul";	STARTUPINFOW si {};	si.cb = sizeof (si);	PROCESS_INFORMATION pi {};	if (CreateProcessW (nullptr, cmd, nullptr, nullptr, FALSE, CREATE_NO_WINDOW, nullptr, nullptr, &si, &pi))	{		wchar_t file[MAX_PATH] {};		if (GetEnvironmentVariableW (L"JW_TEST_HELPER_PID", file, MAX_PATH))		{			HANDLE h = CreateFileW (file, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);			if (h != INVALID_HANDLE_VALUE)			{				char text[32];				int n = wsprintfA (text, "%lu", pi.dwProcessId);				DWORD written = 0;				WriteFile (h, text, n, &written, nullptr);				CloseHandle (h);			}		}		CloseHandle (pi.hThread);		CloseHandle (pi.hProcess);	}	for (;;)		Sleep (100);}__declspec (dllexport) IPluginFactory* PLUGIN_API GetPluginFactory (){	if (nameHas (L"scan-hang"))		activationWindow ();	if (nameHas (L"scan-spawn"))		helperProcess ();	if (nameHas (L"scan-crash"))	{		volatile int* nowhere = nullptr;		*nowhere = 1;	}	// A plugin that can't stand a pipe for a standard handle (what the app's pipes looked like to plugins until the	// host gave its standard handles up): it ends the process, as some Waves and Antares modules did.	if (nameHas (L"scan-stdio"))		for (DWORD which : {STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE})		{			HANDLE h = GetStdHandle (which);			if (h && h != INVALID_HANDLE_VALUE && GetFileType (h) == FILE_TYPE_PIPE)				ExitProcess (0xFFFFFFFF);		}	if (nameHas (L"shell"))		return new ShellFactory ();	auto f = new Factory ();	f->empty = nameHas (L"noclasses");	f->exitAt = nameHas (L"scan-exit") ? 2 : -1;	return f;}} // extern "C"