IdentifyingYourAircraft

From X-Plane SDK
Jump to: navigation, search

If you have an Aircraft related plugin, you can use this code to identify that your aircraft is loaded.

bool TheFlyingPig = false;
XPLMDataRef gAcfDescription = NULL;

PLUGIN_API int XPluginStart(
					char *		outName,
					char *		outSig,
					char *		outDesc)
{
	gAcfDescription = XPLMFindDataRef("sim/aircraft/view/acf_descrip");
	...
	...
	...
}

PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFrom, long inMsg, void * inParam)
{
	char AircraftDescription[500];
	int ByteVals[500];

	if (inFrom == XPLM_PLUGIN_XPLANE)
	{
		switch(inMsg) {
		case XPLM_MSG_PLANE_LOADED:
			TheFlyingPig = false;
			if (gAcfDescription != NULL)
			{
				XPLMGetDatab(gAcfDescription, ByteVals, 0, 500);
				strcpy(AircraftDescription, (char*) &ByteVals);
				if (strstr(AircraftDescription, "The Flying Pig") != NULL)
					TheFlyingPig = true;
			}
			break;
		}
	}
}

If your plugin includes flight loop callbacks and or menu items specific to your aircraft you will want to disable your flight loop call backs and menu items upon the load of an aircraft other than yours. The following routine will accomplish this:

PLUGIN_API  int   XPluginStart(char *  outName, char *  outSig, char *  outDesc)
{
	.
	.
	.
	//             Menu and SubMenu items.

	MySubMenuItem = XPLMAppendMenuItem(
				XPLMFindPluginsMenu(),
				"My Menu Name",
				0,
				1);

	MyMenu = XPLMCreateMenu(
				"My Menu Name",
				XPLMFindPluginsMenu(),
				FuelTransferSubMenuItem,
				FuelTransferMenuCallback,
				0);

	gFormerAircraftOurs = false;                     // Our Aircraft is unknown at this time
	.
	.
	gAircraftDescriptionDataRef = XPLMFindDataRef("sim/aircraft/view/acf_descrip");
	.
	.
	return 1;
}

PLUGIN_API  void   XPluginReceiveMessage(XPLMPluginID   inFromWho, long	 inMessage, void *  inParam)
{
	char    PresentAircraftDescription(500)

	if (inFromWho == XPLM_PLUGIN_XPLANE)
	{
		switch(inMessage) {
		case XPLM_MSG_PLANE_LOADED:
			if ((int)inParam == XPLM_PLUGIN_XPLANE)
			{
				XPLMGetDatab(gAircraftDescriptionDataRef, PresentAircraftDescription, 0, 500);
				if( strcmp(PresentAircraftDescription, "MyAirplaneDescription") == 0)
				{
					if (gFormerAircraftOurs == false)     // Former acf not ours or first pass
					{
						gFormerAircraftOurs = true;

						XPLMEnableMenuItem(XPLMFindPluginsMenu(), MySubMenuItem, 1);

						XPLMAppendMenuItem(      // This paragraph required for each menu item.
						MyMenu,
						"First Item in My Menu",
						(void *) +1,
						1);
						.
						.
						XPLMSetFlightLoopCallbackInterval(MyFlightLoopCallback,  0.5, 1, NULL);
						.
						.
					}
				}
				else
				{
					gFormerAircraftOurs = false;

					XPLMEnableMenuItem(XPLMFindPluginsMenu(), MySubMenuItem, 0);
					XPLMClearAllMenuItems(MyMenu);

					XPLMSetFlightLoopCallbackInterval(MyFlightLoopCallBack, 0, 1, NULL);
				}
			}
			break;
		}
	}
}

An alternative technique is to identify aircraft by seeing if a plugin installed in that aircraft's folder has activated. This avoids the problem of a third party's aircraft having slightly different descriptions depending on when it was obtained by your users. For example: a plugin that unifies the commands for landing-light switches will operate the normal X-Plane landing light switch command if an unidentified aircraft is loaded, but if the Foo Software Spitfire is loaded it will instead operate the Spitfire's landing light switch, which uses a different set of commands. But the user's Spitfire description is unknown, because Foo Software changed the description at least once. A more reliable way to positively identify a Foo Software Spitfire is to see if the Spitfire's plugin has loaded. One method is to try and find one of the datarefs published by the Spitfire's plugin. If XPLMFindDataRef("/foo/spitfire/some_known_dataref") returns NULL, the current aircraft is not the Foo Spitfire.