GME  13
ScriptHost.cpp
Go to the documentation of this file.
00001 // ScriptHost.cpp : Implementation of CScriptHost
00002 #include "stdafx.h"
00003 #include "Console.h"
00004 #include "ScriptHost.h"
00005 #include "ConsoleCtl.h"
00006 
00008 // CScriptHost
00009 
00010 
00011 STDMETHODIMP CScriptHost::InitEngine(IDispatch* console, BSTR engineProgid)
00012 {
00013         // register app and project 
00014         try
00015         {
00016                 if (console == NULL) {
00017                         m_iscript = NULL;
00018                         m_iscriptParse = NULL;
00019                         m_console = NULL;
00020                         return S_OK;
00021                 }
00022                 m_console = NULL;
00023                 m_console = console;
00024                 _bstr_t newprogid(engineProgid);
00025                 if (m_enginePID == newprogid)
00026                         return S_OK;
00027                 if (m_iscript != NULL)
00028                 {
00029                         COMTHROW(m_iscript->Close());
00030                         m_iscript.Release();
00031                 }
00032                 // FIXME: throw a better error message
00033                 COMTHROW(m_iscript.CreateInstance((BSTR)newprogid, NULL, CLSCTX_ALL));
00034                 m_enginePID = newprogid;
00035                 m_iscriptParse = m_iscript;
00036                 IActiveScriptSitePtr scs(this);
00037                 COMTHROW(m_iscript->SetScriptSite(scs));
00038                 COMTHROW(m_iscriptParse->InitNew());
00039                 if (m_gmeptr != NULL)
00040                 {
00041                         _bstr_t project(L"project");
00042                         _bstr_t gme(L"gme");
00043                         _bstr_t this_model(L"it");
00044                         COMTHROW(m_iscript->AddNamedItem(gme, SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE));
00045                         if( m_mgaproj != NULL) COMTHROW(m_iscript->AddNamedItem(project, SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE));
00046                         if( m_actMod  != NULL) COMTHROW(m_iscript->AddNamedItem(this_model, SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE));
00047                 }
00048                 COMTHROW(m_iscript->SetScriptState(SCRIPTSTATE_CONNECTED));
00049         }
00050         COMCATCH(;)
00051 
00052         return S_OK;
00053 }
00054 
00055 STDMETHODIMP CScriptHost::ProcessString(BSTR input)
00056 {
00057 // app = new ActiveXObject("Gme.Application"); app.ConsoleContents = "kaka maka";
00058 //      _bstr_t str1("GMEapp = new ActiveXObject(\"Gme.Application\"); ");
00059 //      _bstr_t str2("GMEapp.ConsoleContents = \"kaka maka\"");
00060 //      _bstr_t str2("GMEapp.ConsoleClear();");
00061 //      _bstr_t str2("GMEapp.CloseProject();");
00062 //      _bstr_t strx("app = new ActiveXObject(\"GME.Application\"); app.CreateProject(\"MetaGME\", \"MGA=C:\\OCL_TEST\\kakamaka.mga\");");
00063 //      _bstr_t str = str1 + str2;
00064 
00065         try
00066         {
00067                 COMTHROW(m_iscriptParse->ParseScriptText(input, NULL, NULL, NULL, 0, 0, 
00068                         SCRIPTTEXT_ISPERSISTENT, NULL, NULL));
00069         }
00070         catch(hresult_exception &e) 
00071         { 
00072                 Message( _bstr_t(_T("Input parsing failed!")), MSG_ERROR);
00073                 return e.hr;
00074         }
00075         return S_OK;
00076 }
00077 
00078 void CScriptHost::Message(BSTR message, msgtype_enum level)
00079 {
00080         static BYTE params[] = VTS_BSTR VTS_I2;
00081         COleDispatchDriver disp(m_console, false);
00082         disp.InvokeHelper(2, DISPATCH_METHOD, VT_EMPTY, 0, params, message, level);
00083 }
00084 
00085 STDMETHODIMP CScriptHost::GetLCID(/*[out]*/ LCID *plcid)
00086 {
00087         return S_OK;
00088 }
00089 
00090 STDMETHODIMP CScriptHost::GetItemInfo(
00091                 /*[in]*/  LPCOLESTR pstrName,
00092                 /*[in]*/  DWORD     dwReturnMask,
00093                 /*[out]*/ IUnknown  **ppiunkItem,
00094                 /*[out]*/ ITypeInfo **ppti)
00095 {
00096         if (!m_gmeptr)
00097                 return E_FAIL;
00098         if (dwReturnMask&SCRIPTINFO_IUNKNOWN)
00099         {
00100                 if (m_gmeptr && (_bstr_t)pstrName == _bstr_t(L"gme"))
00101                 {
00102                         CComPtr<IUnknown> punk(m_gmeptr);
00103                         ((IUnknown*)punk)->AddRef(); 
00104                         *ppiunkItem = punk;
00105                 }
00106                 else if (m_mgaproj && (_bstr_t)pstrName == _bstr_t(L"project"))
00107                 {
00108                         CComPtr<IUnknown> punk(m_mgaproj);
00109                         ((IUnknown*)punk)->AddRef(); 
00110                         *ppiunkItem = punk;
00111                 }
00112                 else if (m_actMod && (_bstr_t)pstrName == _bstr_t(L"it"))
00113                 {
00114                         CComPtr<IUnknown> punk(m_actMod);
00115                         ((IUnknown*)punk)->AddRef(); 
00116                         *ppiunkItem = punk;
00117                 }
00118         }
00119         if (dwReturnMask&SCRIPTINFO_ITYPEINFO)
00120         {
00121         }
00122 
00123         return S_OK;
00124 }
00125 
00126 STDMETHODIMP CScriptHost::GetDocVersionString(/*[out]*/ BSTR      *pbstrVersion)
00127 {
00128         return S_OK;
00129 }
00130 
00131 STDMETHODIMP CScriptHost::OnScriptTerminate(
00132                 /*[in]*/  const VARIANT     *pvarResult,
00133                 /*[in]*/  const EXCEPINFO   *pexcepinfo)
00134 {
00135         return S_OK;
00136 }
00137 
00138 STDMETHODIMP CScriptHost::OnStateChange(
00139                 /*[in]*/  SCRIPTSTATE       ssScriptState)
00140 {
00141         return S_OK;
00142 }
00143 
00144 STDMETHODIMP CScriptHost::OnScriptError(
00145                 /*[in]*/  IActiveScriptError *pscripterror)
00146 {
00147         try
00148         {
00149                 IActiveScriptErrorPtr perr(pscripterror);
00150 
00151                 EXCEPINFO expinfo;
00152                 COMTHROW(perr->GetExceptionInfo(&expinfo));
00153                 DWORD   sc;
00154                 ULONG   line = 0;
00155                 LONG    ch = 0;
00156                 COMTHROW(perr->GetSourcePosition(&sc, &line, &ch));
00157 
00158                 // write to console 
00159                 CString desc = expinfo.bstrDescription;
00160                 // make the error description html readable
00161                 desc.Replace( _T("<"), _T("&lt;"));             // replacing <
00162                 desc.Replace( _T(">"), _T("&gt;"));             // >
00163                 desc.Replace(_T("\r\n"), _T("<br>"));   // 0d0a newlines
00164                 desc.Replace(_T("\n"), _T("<br>"));             // 0a newlines
00165 
00166                 TCHAR err[5000];
00167                 _stprintf_s(err, _T("Scripting Error at Position: %ld Line: %lu<br>%s"), ch, line, (const TCHAR*)desc);
00168                 Message(_bstr_t(err), MSG_ERROR);
00169         }
00170         catch(hresult_exception &e) 
00171         { 
00172                 return e.hr;
00173         }
00174         catch(_com_error &e) 
00175         { 
00176                 return e.Error();
00177         }
00178 
00179         return S_OK;
00180 }
00181 
00182 STDMETHODIMP CScriptHost::OnEnterScript(void)
00183 {
00184         return S_OK;
00185 }
00186 
00187 STDMETHODIMP CScriptHost::OnLeaveScript(void)
00188 {
00189         return S_OK;
00190 }
00191 
00192 STDMETHODIMP CScriptHost::SetGMEApp(IDispatch *gmeapp)
00193 {
00194 
00195         if (m_gmeptr == NULL  &&  gmeapp == NULL)
00196                 return S_OK;
00197         try
00198         {
00199                 m_gmeptr.Release();
00200                 if (gmeapp == NULL)
00201                 {
00202                         // release other pointers
00203                         m_mgaproj.Release();
00204                         m_actMod.Release();
00205 
00206                         // delete names 
00207                         if (m_iscript != NULL)
00208                         {
00209                                 COMTHROW(m_iscript->Close());
00210                                 m_iscript.Release();
00211                         }
00212                         COMTHROW(m_iscript.CreateInstance((BSTR)m_enginePID));
00213                         m_iscriptParse = m_iscript;
00214                         IActiveScriptSitePtr scs(this);
00215                         COMTHROW(m_iscript->SetScriptSite(scs));
00216                         COMTHROW(m_iscriptParse->InitNew());
00217                         COMTHROW(m_iscript->SetScriptState(SCRIPTSTATE_CONNECTED));
00218                         return S_OK;
00219                 }
00220 
00221                 CComPtr<IDispatch> gip(gmeapp);
00222                 COMTHROW(gip.QueryInterface(&m_gmeptr));
00223 
00224                 _bstr_t gme(L"gme");
00225                 COMTHROW(m_iscript->AddNamedItem(gme, SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE));
00226                 COMTHROW(m_iscript->SetScriptState(SCRIPTSTATE_CONNECTED));
00227         }
00228         catch(hresult_exception &e) 
00229         { 
00230                 return e.hr;
00231         }
00232         catch(_com_error &e) 
00233         { 
00234                 return e.Error();
00235         }
00236         return S_OK;
00237 }
00238 
00239 STDMETHODIMP CScriptHost::SetGMEProj(IDispatch *gmeapp)
00240 {
00241 
00242         if (m_gmeptr == NULL  &&  gmeapp == NULL)
00243                 return S_OK;
00244         try
00245         {
00246                 m_mgaproj.Release();
00247                 m_actMod.Release();
00248 
00249                 if (gmeapp == NULL) // means close project
00250                 {
00251                         return S_OK;
00252                 }
00253 
00254                 COMTHROW(m_gmeptr->get_MgaProject(&m_mgaproj));
00255 
00256                 COMTHROW( m_gmeptr->get_OleIt( &m_actMod ));
00257 
00258                 _bstr_t project(L"project");
00259                 _bstr_t this_model(L"it");
00260                 COMTHROW(m_iscript->AddNamedItem(project, SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE));
00261                 COMTHROW(m_iscript->AddNamedItem(this_model, SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE));
00262                 COMTHROW(m_iscript->SetScriptState(SCRIPTSTATE_CONNECTED));
00263         }
00264         catch(hresult_exception &e) 
00265         { 
00266                 return e.hr;
00267         }
00268 
00269         return S_OK;
00270 }