/*****************************************************************************
 * This is the main code of a DeleD import plugin template for C++.          *
 * Author: Salvatore Russo "Il Buzzo"                                        *
 * Version: 1.2.0                                                            *
 * DeleD version: 1.1.0                                                      *
 * License: GNU General Public License.                                      *
 * Made with: Dev-Cpp 4.9.9.2 and MinGW (Tested with GCC 3.3.1, GCC 3.2.2    *
 *            and GCC 3.4.2)                                                 *
 * Link Libraries: libcomdlg32.a (it includes calling to OpenDialog etc..)   *
 * Linker parameters: --no-export-all-symbols --add-stdcall-alias            *
 * Additional Compiler Parameters: -DBUILDING_DLL=1                          *
 * End Notes: This is an adaptation to new plugin system, so there are some  *
 *            changes from the old code, but as result I'm still adding to   *
 *            to code try{}catch() blocks for a better error handling.       *
 *            It's not so difficult so if you don't want to wait for another *
 *            version you can implement it without difficulty.               *
 *****************************************************************************/

    //Main header
#include "dll.h"
    //Necessary for DLL to work
#include <windows.h>

    //support.h contains some classes used, defined by me
    //like String and StringList and some defined for DeleD
    //in Jeroen sample code.
    //Look at that file for major details.
#include "support.h"


    //TCallBack variable used to receive a pointer by DeleD
TCallBack callback;

    /* EXPORTED FUNCTIONS NECESSARY TO DELED
    
    PCHAR PluginName() : returns name of Plugin
    PCHAR PluginDescription() : returns a small description of Plugin
    PCHAR PluginDeleDVersion() : returns version of DeleD
    PCHAR PluginVersion() : returns version of Plugin
    PCHAR PluginAuthor() : returns Author's name
    PCHAR PluginEmail() : returns Author's e-mail
    void PluginSetCallback(void*cp) : pass callback pointer from DeleD to callback
    void PluginExecute() : it executes plugin code
    
    notes:
        PCHAR is the same as char* so it's a null terminated c string.
        Each function is defined as DLLEXPORT for major details see
        the DLLEXPORT definition in dll.h file.
        As you can notice the function PluginType() is no longer used.
    */


DLLEXPORT PCHAR PluginName(){
    return "DeleD Import Plugin Sample";
}    

DLLEXPORT PCHAR PluginDescription(){
    return "This is just a sample to test main plugin system functionality.";
}

DLLEXPORT PCHAR PluginDeleDVersion(){
    return "1.1";
}    

DLLEXPORT PCHAR PluginVersion(){
    return "1.0";
}    

DLLEXPORT PCHAR PluginAuthor(){
    return "Your Name";
}    

DLLEXPORT PCHAR PluginEmail(){
    return "youremail@yourdomain";
}
    
DLLEXPORT void PluginSetCallback(void *cp){
    callback=TCallBack(cp);
}

DLLEXPORT void PluginExecute(void){
   //variables used
    TCbData cbData;//callback data
    String MyChars;//a string containing scene info
    
    //clear the string to store cube data
    MyChars.clear();
    
    //First of all we'll try to get DeleD directory to show new plugin
    //system enhancements
    try{
        unsigned error;// errors returned by callback
        cbData.Reason = PR_GETINSTALLDIR;//reason of callback
        cbData.Data = new char[512];//allocate new space for string
        error=callback(&cbData);//pass Data to callback retrieving eventual errors
        if(!error){//if no errors detected then go on
            String myStr = String(cbData.Data);//get directory
            MessageBox ( NULL, myStr.c_str(),"This is DeleD main directory:",MB_ICONINFORMATION);//show it
            myStr.clear();
        }
        else throw error;//if errors generate an exception    
        delete cbData.Data;//destroy previously allocated memory
    }
    catch( unsigned exception) {
        ErrorMessage(exception);//show error message (you can use this block to handle errors)    
    }    
    
    //Now we do the real job of passing a cube to DeleD
    try{
        unsigned errors;// errors returned by callback
        //we're now creating string to pass to DeleD
        MyChars=CreateDeleDCube();//create a basic cube
        String MyData=String("1;091;1,1,0,");//Number of Headers,Version,Header Item (in this case object), Number of items,Offset from end of header,
        MyData = MyData + toString(MyChars.length()) + String(";");//we must now add total length of object data as the last header field;
        MyData = AddLengthToHeader(MyData);//We add in front of header total lenght of it
        MyData = MyData + MyChars;//Final string made by header+data
    

        cbData.Reason = PR_SETDATA;//reason of callback
        cbData.DataSize = (MyData.length()+1);//size of data to be sent, a null terminated string
        cbData.Data = new char[(MyData.length()+1)];//allocates memory for string
        sprintf(cbData.Data,"%s",MyData.c_str());//copy memory to Data
        MyData.clear();//clear Data strings
        MyChars.clear();
        errors=callback(&cbData);//pass Data to callback retrieving eventual errors
        delete cbData.Data;//destroy previously allocated memory
        if(!errors) MessageBox(NULL,"Cube sent to DeleD correctly","Information",0);
        else throw errors;
    }
    catch(unsigned exception){
        ErrorMessage(exception);//show error message (you can use this block to handle errors)    
    }        
    //We have finished our works so a message of information.
    MessageBox(NULL,"DeleD C++ templates version 1.1 written by:\n Salvatore Russo.\n\nLot of thanks to Jeroen and Paul Jan\nfor this new improved version of DeleD!!!","Information",0);
}