1987WEB视界-分享互联网热点话题和事件

您现在的位置是:首页 > 人工智能AI > 正文

人工智能AI

illustrator插件开发-AI插件-aip格式-第一章第二小节入口点函数

1987web2024-01-18人工智能AI53
1.入口点函数

1.入口点函数

插件作为illustrator软件的一部分,需要与主体程序进行通信,必然需要约定调用函数。而入口点函数就是其中关键一环,实例代码如下:

/** Basic suite-access information provided with every call. */structSPMessageData{ifdef __cplusplusSPMessageData(ai::int32SPCheck_=0,structSPPlugin*self_=0,void*globals_=0,structSPBasicSuite*basic_=0):SPCheck(SPCheck_),self(self_),globals(globals_),basic(basic_){}endif/** \c kSPValidSPMessageData if this is a valid PICA message. */ai::int32SPCheck;/** This plug-in, an \c SPPluginRef. */structSPPlugin*self;/** An array of application-wide global variables. */void*globals;/** A pointer to the basic PICA suite, which you use to obtain all other suites. */structSPBasicSuite*basic;};extern"C"ASAPIASErrPluginMain(char*caller,char*selector,void*message){//caller指示消息的发送者(PICA,宿主程序或插件)和动作的通用类型。//selector指定动作类型的执行动作。所有插件收到至少4类消息动作:reload(重新加载),unload(卸载),startup(启动)和shutdown(关系)。此外,插件可能收到特定插件类型的附加消息。//message指针是一个消息数据结构体,它包括这个消息动作的相关信息。如,当一个鼠标点击消息接收到时,消息结构体包括鼠标的位置。消息结构体的内容取决于消息,在你的插件定义这个后,才能知道。由于消息内容不同,按照约定,所有消息结构体由相同字段组成,组合到SPMessageData结构体中。ASErrerror=kNoErr;SPMessageData*msgData=(SPMessageData*)message;Plugin*plugin=(Plugin*)msgData->globals;sSPBasic=msgData->basic;/** PICA messaging system caller; see \c SPInterfaceSuite. *///define kSPInterfaceCaller                "SP Interface"if(strcmp(caller,kSPInterfaceCaller)==0)//是否是消息系统调用{/** PICA messaging system startup; see \c SPInterfaceSuite.  *///define kSPInterfaceStartupSelector       "Startup"if(strcmp(selector,kSPInterfaceStartupSelector)==0)//插件启动{plugin=AllocatePlugin(msgData->self);//全局插件if(plugin){msgData->globals=(void*)plugin;error=plugin->StartupPlugin((SPInterfaceMessage*)message);//插件启动if(error!=kNoErr){// Make sure to delete in case startup faileddeleteplugin;plugin=nil;msgData->globals=nil;}}else{error=kOutOfMemoryErr;}}elseif(strcmp(selector,kSPInterfaceShutdownSelector)==0)// 插件关闭{if(plugin){error=plugin->ShutdownPlugin((SPInterfaceMessage*)message);//关闭函数deleteplugin;plugin=nil;msgData->globals=nil;}}}if(plugin){if(Plugin::IsReloadMsg(caller,selector))//重载插件{// Call this before calling any virtual functions (like Message)FixupReload(plugin);error=plugin->ReloadPlugin((SPInterfaceMessage*)message);}else{// If a load or reload failed because the suites could not be acquired, we released// any partially acquired suites and returned an error.  However, SuitePea still might// call us, so protect against this situation.if(plugin->SuitesAcquired())error=plugin->Message(caller,selector,message);//消息路由elseerror=kNoErr;}if(error==kUnhandledMsgErr){error=kNoErr;ifndef NDEBUGifdef MAC_ENVfprintf(stderr,"Warning: Unhandled plugin message: caller\"%s\"selector\"%s\"\n",caller,selector);elsecharbuf[1024];sprintf(buf+1,"Warning: Unhandled plugin message: caller\"%s\"selector\"%s\"\n",caller,selector);OutputDebugStringA(buf+1);endifendif}}if(error){if(plugin)plugin->ReportError(error,caller,selector,message);//错误elsePlugin::DefaultError(msgData->self,error);}returnerror;}

功能解析都写在注释里,方便读者阅读,在上述入口函数中,涉及到了两个类一个是SPMessageData,它里面有几个参数,见注释,另一个是SPBasicSuite类,代码如下所示:

/*********************************************************************************** Suite****//** @ingroup SuitesThis suite provides basic memory management for PICA (the Adobe plug-in manager)and defines the basic functions for acquiring and releasing other suites.A suite consists of a list of function pointers. The application, or aplug-in that loads a suite, provides valid pointers when the suite isacquired. When a suite is not available, the pointers are set to theaddress of the \c Undefined() function.Do not attempt to acquire a suite (other than the \c SPBlocksSuite)in response to a PICA access (\c kSPAccessCaller) or property(\c kSPPropertiesCaller) message. Most suites are unavailableduring these load and unload operations.You can acquire all the suites you will need when your plug-in is firstloaded, as long as you release them before your plug-in is unloaded.At shutdown, however, it is most efficient to acquire only thosesuites explicitly needed to shut down; for example, to free memoryand save preferences.The \c SPBasicSuite itself is a part of the message data passedto your plug-in with any call. To access it from the message data structure:@codeSPBasicSuite sBasic = message->d.basic;sBasic->function( )@endcode*/typedefstructSPBasicSuite{/** Acquires a function suite. Loads the suite if necessary,and increments its reference count. For example:@codeSPErr error;SPBasicSuite *sBasic = message->d.basic;AIRandomSuite *sRandom;sBasic->AcquireSuite( kAIRandomSuite, kAIRandomVersion, &sRandom );@endcode@param name The suite name.@param version The suite version number.@param suite [out] A buffer in which to return the suite pointer.@see \c SPSuitesSuite::AcquireSuite()*/SPAPISPErr(*AcquireSuite)(constchar*name,ai::int32version,constvoid**suite);/** Decrements the reference count of a suite and unloads it when thereference count reaches 0.@param name The suite name.@param version The suite version number.*/SPAPISPErr(*ReleaseSuite)(constchar*name,ai::int32version);/** Compares two strings for equality.@param token1 The first null-terminated string.@param token2 The second null-terminated string.@return True if the strings are the same, false otherwise.*/SPAPISPBoolean(*IsEqual)(constchar*token1,constchar*token2);/** Allocates a block of memory.@param size The number of bytes.@param block [out] A buffer in which to return the block pointer.@see \c SPBlocksSuite::AllocateBlock()*/SPAPISPErr(*AllocateBlock)(size_tsize,void**block);/** Frees a block of memory allocated with \c AllocateBlock().@param block The block pointer.@see \c SPBlocksSuite::FreeBlock()*/SPAPISPErr(*FreeBlock)(void*block);/** Reallocates a block previously allocated with \c AllocateBlock().Increases the size without changing the location, if possible.@param block The block pointer.@param newSize The new number of bytes.@param newblock [out] A buffer in which to return the new block pointer.@see \c SPBlocksSuite::ReallocateBlock()*/SPAPISPErr(*ReallocateBlock)(void*block,size_tnewSize,void**newblock);/** A function pointer for unloaded suites. This is a protective measureagainst other plug-ins that may mistakenly use the suite after they havereleased it.A plug-in that exports a suite should unload the suites procedure pointerswhen it is unloaded, and restore them when the plug-in is reloaded.\li On unload, replace the suites procedure pointerswith the address of this function.\li On reload, restore the suites procedurepointers with the updated addresses of their functions.For example:@codeSPErr UnloadSuite( MySuite *mySuite, SPAccessMessage *message ) {mySuite->functionA = (void *) message->d.basic->Undefined;mySuite->functionB = (void *) message->d.basic->Undefined;}SPErr ReloadSuite( MySuite *mySuite, SPAccessMessage *message ) {mySuite->functionA = functionA;mySuite->functionB = functionB;}@endcode*/SPAPISPErr(*Undefined)(void);}SPBasicSuite;

这是一个加载模块,它实现了模块的加载和卸载,还有内存分配、释放和重新分配功能。

2.作者答疑


代码长度过长,如需全部项目或有疑问,请留言。

提示:作者知了-联系方式1提示:作者知了-联系方式2