forked from vpinball/vpinball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeview.h
589 lines (465 loc) · 16.1 KB
/
codeview.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#pragma once
#include <commdlg.h>
#include <activscp.h>
#include <activdbg.h>
#include <atlcom.h>
#include "codeviewedit.h"
#include "ScriptErrorDialog.h"
#include "scintilla.h"
#define MAX_FIND_LENGTH 81
#define MAX_LINE_LENGTH 2048
enum SecurityLevelEnum
{
eSecurityNone = 0,
eSecurityWarnOnUnsafeType = 1,
eSecurityWarnOnType = 2,
eSecurityWarnOnAll = 3,
eSecurityNoControls = 4
};
class IScriptable
{
public:
IScriptable();
STDMETHOD(get_Name)(BSTR *pVal) = 0;
virtual IDispatch *GetDispatch() = 0;
virtual const IDispatch *GetDispatch() const = 0;
virtual ISelect *GetISelect() = 0;
virtual const ISelect *GetISelect() const = 0;
WCHAR m_wzName[MAXNAMEBUFFER];
};
class CodeViewer;
class DebuggerModule :
public CComObjectRootEx<CComSingleThreadModel>,
public IDispatchImpl<IVPDebug, &IID_IVPDebug, &LIBID_VPinballLib>,
public IScriptable
{
BEGIN_COM_MAP(DebuggerModule)
COM_INTERFACE_ENTRY(IVPDebug)
COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()
STDMETHOD(Print)(VARIANT *pvar);
public:
void Init(CodeViewer * const pcv);
IDispatch *GetDispatch() final { return (IDispatch *)this; }
const IDispatch *GetDispatch() const final { return (const IDispatch *)this; }
ISelect *GetISelect() final { return nullptr; }
const ISelect *GetISelect() const final { return nullptr; }
STDMETHOD(get_Name)(BSTR *pVal);
CodeViewer *m_pcv;
};
class IScriptableHost
{
public:
virtual void SelectItem(IScriptable *piscript) = 0;
virtual void SetDirtyScript(SaveDirtyState sds) = 0;
virtual void DoCodeViewCommand(int command) = 0;
};
class CodeViewDispatch
{
public:
CodeViewDispatch() {}
~CodeViewDispatch() {}
wstring m_wName;
IUnknown *m_punk;
IDispatch *m_pdisp;
IScriptable *m_piscript;
bool m_global;
// for VectorSortString
int SortAgainst(const CodeViewDispatch * const pcvd/*void *pvoid*/) const { return SortAgainstValue(pcvd->m_wName); }
int SortAgainstValue(const wstring &pv) const;
};
class CodeViewer :
public CWnd,
public CComObjectRoot,
//public IDispatchImpl<IDragPoint, &IID_IDragPoint, &LIBID_VPinballLib>,
//public CComCoClass<CodeViewer,&CLSID_DragPoint>,
//public CComObjectRootEx<CComSingleThreadModel>,
public IActiveScriptSite,
public IActiveScriptSiteDebug,
public IActiveScriptSiteWindow,
public IInternetHostSecurityManager,
public IServiceProvider
{
public:
CodeViewer() : m_haccel(nullptr), m_pProcessDebugManager(nullptr), m_parentLevel(0), m_lastErrorWidgetVisible(false), m_suppressErrorDialogs(false) {}
virtual ~CodeViewer();
void Init(IScriptableHost *psh);
void SetVisible(const bool visible);
void SetEnabled(const bool enabled);
void SetClean(const SaveDirtyState sds);
// Script Class
STDMETHOD(CleanUpScriptEngine)();
STDMETHOD(InitializeScriptEngine)();
HRESULT AddItem(IScriptable * const piscript, const bool global);
void RemoveItem(IScriptable * const piscript);
HRESULT ReplaceName(IScriptable * const piscript, const WCHAR * const wzNew);
void SelectItem(IScriptable * const piscript);
void Compile(const bool message);
void Start();
void EndSession();
HRESULT AddTemporaryItem(const BSTR bstr, IDispatch * const pdisp);
STDMETHOD(GetItemInfo)(LPCOLESTR pstrName, DWORD dwReturnMask,
IUnknown **ppiunkItem, ITypeInfo **ppti);
STDMETHOD(OnScriptError)(IActiveScriptError *pscripterror);
STDMETHOD(GetLCID)(LCID *plcid) override
{
*plcid = 9; return S_OK;
}
STDMETHOD(GetDocVersionString)(BSTR *pbstrVersion) override
{
*pbstrVersion = SysAllocString(L""); return S_OK;
}
STDMETHOD(OnScriptTerminate)(const VARIANT *pvr, const EXCEPINFO *pei) override
{
return S_OK;
}
STDMETHOD(OnStateChange)(SCRIPTSTATE ssScriptState) override
{
return S_OK;
}
STDMETHOD(OnEnterScript)();
STDMETHODIMP OnLeaveScript();
STDMETHODIMP GetWindow(HWND *phwnd) override
{
*phwnd = GetDesktopWindow(); return S_OK; //!! ?
}
STDMETHODIMP EnableModeless(BOOL) override
{
return S_OK;
}
// IActiveScriptSiteDebug interface
STDMETHOD(GetDocumentContextFromPosition)(
DWORD_PTR dwSourceContext,
ULONG uCharacterOffset,
ULONG uNumChars,
IDebugDocumentContext** ppsc
);
STDMETHOD(GetApplication)(
IDebugApplication** ppda
);
STDMETHOD(GetRootApplicationNode)(
IDebugApplicationNode** ppdanRoot
);
STDMETHOD(OnScriptErrorDebug)(
IActiveScriptErrorDebug* pscripterror,
BOOL* pfEnterDebugger,
BOOL* pfCallOnScriptErrorWhenContinuing
);
// Internet Security interface
virtual HRESULT STDMETHODCALLTYPE GetSecurityId(
/* [size_is][out] */ BYTE *pbSecurityId,
/* [out][in] */ DWORD *pcbSecurityId,
/* [in] */ DWORD_PTR dwReserved);
virtual HRESULT STDMETHODCALLTYPE ProcessUrlAction(
/* [in] */ DWORD dwAction,
/* [size_is][out] */ BYTE __RPC_FAR *pPolicy,
/* [in] */ DWORD cbPolicy,
/* [in] */ BYTE __RPC_FAR *pContext,
/* [in] */ DWORD cbContext,
/* [in] */ DWORD dwFlags,
/* [in] */ DWORD dwReserved);
virtual HRESULT STDMETHODCALLTYPE QueryCustomPolicy(
/* [in] */ REFGUID guidKey,
/* [size_is][size_is][out] */ BYTE __RPC_FAR *__RPC_FAR *ppPolicy,
/* [out] */ DWORD __RPC_FAR *pcbPolicy,
/* [in] */ BYTE __RPC_FAR *pContext,
/* [in] */ DWORD cbContext,
/* [in] */ DWORD dwReserved);
bool FControlAlreadyOkayed(const CONFIRMSAFETY *pcs);
void AddControlToOkayedList(const CONFIRMSAFETY *pcs);
bool FControlMarkedSafe(const CONFIRMSAFETY *pcs);
bool FUserManuallyOkaysControl(const CONFIRMSAFETY *pcs);
virtual HRESULT STDMETHODCALLTYPE QueryService(
REFGUID guidService,
REFIID riid,
void **ppv);
// Use CComObject to implement AddRef/Release/QI
BEGIN_COM_MAP(CodeViewer)
//COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IActiveScriptSite)
COM_INTERFACE_ENTRY(IActiveScriptSiteDebug)
COM_INTERFACE_ENTRY(IActiveScriptSiteWindow)
COM_INTERFACE_ENTRY(IInternetHostSecurityManager)
COM_INTERFACE_ENTRY(IServiceProvider)
END_COM_MAP()
void UncolorError();
void ParseForFunction();
void ShowFindDialog();
void ShowFindReplaceDialog();
void Find(const FINDREPLACE * const pfr);
void Replace(const FINDREPLACE * const pfr);
void SaveToStream(IStream *pistream, HCRYPTHASH const hcrypthash);
void SaveToFile(const string& filename);
void LoadFromStream(IStream *pistream, HCRYPTHASH const hcrypthash, const HCRYPTKEY hcryptkey); // incl. table protection
void LoadFromFile(const string& filename);
void SetCaption(const string& szCaption);
bool ShowTooltipOrGoToDefinition(const SCNotification *pSCN, const bool tooltip);
void ShowAutoComplete(const SCNotification *pSCN);
void UpdateRegWithPrefs();
void UpdatePrefsfromReg();
void GetWordUnderCaret();
void ListEventsFromItem();
void FindCodeFromEvent();
void TellHostToSelectItem();
void UpdateScinFromPrefs();
void MarginClick(const Sci_Position position, const int modifiers);
void EvaluateScriptStatement(const char * const szScript);
void AddToDebugOutput(const char * const szText);
bool PreTranslateMessage(MSG* msg);
IScriptableHost *m_psh;
IActiveScript* m_pScript;
VectorSortString<CodeViewDispatch*> m_vcvd;
COLORREF m_prefCols[16];
COLORREF m_bgColor;
COLORREF m_bgSelColor;
CVPreference *m_prefEverythingElse;
vector<CVPreference*> *m_lPrefsList;
int m_displayAutoCompleteLength;
SaveDirtyState m_sdsDirty;
bool m_ignoreDirty;
bool m_scriptError; // Whether a script error has occured - used for polling from the game
bool m_visible;
bool m_minimized;
bool m_displayAutoComplete;
bool m_toolTipActive;
bool m_stopErrorDisplay;
bool m_dwellHelp;
bool m_dwellDisplay;
int m_dwellDisplayTime;
fi_vector<UserData> m_pageConstructsDict;
Sci_TextRange m_wordUnderCaret;
CComObject<DebuggerModule> *m_pdm; // Object to expose to script for global functions
//ULONG m_cref;
HWND m_hwndMain;
HWND m_hwndScintilla;
HWND m_hwndFind;
HWND m_hwndStatus;
HWND m_hwndFunctionList;
HACCEL m_haccel; // Accelerator keys
int m_errorLineNumber;
FINDREPLACE m_findreplaceold; // the last thing found/replaced
string external_script_name; // loaded from external .vbs?
vector<char> original_table_script; // if yes, then this one stores the original table script
protected:
void PreCreate(CREATESTRUCT& cs) final;
void PreRegisterClass(WNDCLASS& wc) final;
int OnCreate(CREATESTRUCT& cs) final;
LRESULT WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam) final;
BOOL OnCommand(WPARAM wparam, LPARAM lparam) final;
LRESULT OnNotify(WPARAM wparam, LPARAM lparam) final;
void Destroy() final;
private:
CodeViewer* GetCodeViewerPtr();
BOOL ParseClickEvents(const int id, const SCNotification *pSCN);
BOOL ParseSelChangeEvent(const int id, const SCNotification *pSCN);
bool ParseOKLineLength(const size_t LineLen);
string ParseDelimtByColon(string &wholeline);
void ParseFindConstruct(size_t &Pos, const string &UCLine, WordType &Type, int &ConstructSize);
bool ParseStructureName(fi_vector<UserData> &ListIn, const UserData &ud, const string &UCline, const string &line, const int Lineno);
size_t SureFind(const string &LineIn, const string &ToFind);
void RemoveByVal(string &line);
void RemoveNonVBSChars(string &line);
string ExtractWordOperand(const string &line, const size_t StartPos);
void ColorLine(const int line);
void ColorError(const int line, const int nchar);
void ParseVPCore();
void ReadLineToParseBrain(string wholeline, const int linecount, fi_vector<UserData> &ListIn);
void GetMembers(const fi_vector<UserData> &ListIn, const string &StrIn);
void InitPreferences();
void GetParamsFromEvent(const UINT iEvent, char * const szParams);
/**
* Resizes the Scintilla widget (the text editor) and the last error widget (if it's visible)
*
* This is called when the window is resized (when we get a WM_SIZE message)
* or when the last error widget is toggled (since that appears below the text editor)
*/
void ResizeScintillaAndLastError();
/**
* Sets the visibility of the last error information, shown below the Scintilla text editor.
*/
void SetLastErrorVisibility(bool show);
void SetLastErrorTextW(const LPCWSTR text);
void AppendLastErrorTextW(const wstring& text);
IActiveScriptParse* m_pScriptParse;
IActiveScriptDebug* m_pScriptDebug;
/**
* Will be nullptr on systems that don't support debugging.
*
* For example, wine 6.9 says ...
* > no class object {78a51822-51f4-11d0-8f20-00805f2cd064} could be created for context 0x17
* ... if I try to create CLSID_PrrocessDebugManager
*/
IProcessDebugManager* m_pProcessDebugManager;
FINDREPLACE m_findreplacestruct;
char szFindString[MAX_FIND_LENGTH];
char szReplaceString[MAX_FIND_LENGTH];
VectorSortString<CodeViewDispatch*> m_vcvdTemp; // Objects added through script
string m_validChars;
// CodeViewer Preferences
CVPreference *prefDefault;
CVPreference *prefVBS;
CVPreference *prefComps;
CVPreference *prefSubs;
CVPreference *prefComments;
CVPreference *prefLiterals;
CVPreference *prefVPcore;
int m_parentLevel;
string m_currentParentKey; // always lower case
//bool m_parentTreeInvalid;
//TODO: int TabStop;
// Dictionaries
fi_vector<UserData> m_VBwordsDict;
fi_vector<UserData> m_componentsDict;
fi_vector<UserData> m_VPcoreDict;
fi_vector<UserData> m_currentMembers;
string m_autoCompString;
string m_autoCompMembersString;
Sci_TextRange m_currentConstruct;
HWND m_hwndItemList;
HWND m_hwndItemText;
HWND m_hwndEventList;
HWND m_hwndEventText;
HWND m_hwndFunctionText;
/**
* Whether the last error widget is visible
*/
bool m_lastErrorWidgetVisible;
/**
* If true, error dialogs will be suppressed for the play session
*
* This gets reset to false whenever the script is started
*/
bool m_suppressErrorDialogs;
/**
* Handle for the last error widget
*
* The last error widget is a read-only text area that appears below the Scintilla text editor, with the contents of
* the last reported compile or runtime error.
*/
HWND m_hwndLastErrorTextArea;
};
class Collection :
public IDispatchImpl<ICollection, &IID_ICollection, &LIBID_VPinballLib>,
public CComObjectRoot,
public CComCoClass<Collection, &CLSID_Collection>,
public EventProxy<Collection, &DIID_ICollectionEvents>,
public IConnectionPointContainerImpl<Collection>,
public IProvideClassInfo2Impl<&CLSID_Collection, &DIID_ICollectionEvents, &LIBID_VPinballLib>,
public IScriptable,
public ILoadable
{
public:
Collection();
// IScriptable
STDMETHOD(get_Name)(BSTR *pVal);
IDispatch *GetDispatch() final { return (IDispatch *)this; }
const IDispatch *GetDispatch() const final { return (const IDispatch *)this; }
ISelect *GetISelect() final { return nullptr; }
const ISelect *GetISelect() const final { return nullptr; }
//ILoadable
HRESULT SaveData(IStream *pstm, HCRYPTHASH hcrypthash, const bool saveForUndo);
HRESULT LoadData(IStream *pstm, PinTable *ppt, int version, HCRYPTHASH hcrypthash, HCRYPTKEY hcryptkey);
bool LoadToken(const int id, BiffReader * const pbr) final;
STDMETHOD(get_Count)(long __RPC_FAR *plCount);
STDMETHOD(get_Item)(long index, IDispatch __RPC_FAR * __RPC_FAR *ppidisp);
STDMETHOD(get__NewEnum)(IUnknown** ppunk);
BEGIN_COM_MAP(Collection)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(ICollection)
COM_INTERFACE_ENTRY_IMPL(IConnectionPointContainer)
COM_INTERFACE_ENTRY(IProvideClassInfo)
COM_INTERFACE_ENTRY(IProvideClassInfo2)
END_COM_MAP()
BEGIN_CONNECTION_POINT_MAP(Collection)
CONNECTION_POINT_ENTRY(DIID_ICollectionEvents)
END_CONNECTION_POINT_MAP()
VectorProtected<ISelect> m_visel;
bool m_fireEvents;
bool m_stopSingleEvents;
bool m_groupElements;
};
class OMCollectionEnum :
public CComObjectRootEx<CComSingleThreadModel>,
public IEnumVARIANT
{
public:
BEGIN_COM_MAP(OMCollectionEnum)
COM_INTERFACE_ENTRY(IEnumVARIANT)
END_COM_MAP()
OMCollectionEnum() {}
~OMCollectionEnum() {}
STDMETHOD(Init)(Collection *pcol);
STDMETHOD(Next)(ULONG celt, VARIANT __RPC_FAR *rgVar, ULONG __RPC_FAR *pCeltFetched);
STDMETHOD(Skip)(ULONG celt);
STDMETHOD(Reset)();
STDMETHOD(Clone)(IEnumVARIANT __RPC_FAR *__RPC_FAR *ppEnum);
private:
Collection *m_pcol;
int m_index;
};
// general string helpers:
constexpr __forceinline bool IsWhitespace(const char ch)
{
return (ch == ' ' || ch == 9/*tab*/);
}
__forceinline string upperCase(string input)
{
std::transform(input.begin(), input.end(), input.begin(), ::toupper);
return input;
}
__forceinline string lowerCase(string input)
{
std::transform(input.begin(), input.end(), input.begin(), ::tolower);
return input;
}
inline void RemovePadding(string &line)
{
const size_t LL = line.length();
size_t Pos = line.find_first_not_of("\n\r\t ,");
if (Pos == string::npos)
{
line.clear();
return;
}
if (Pos > 0)
{
if ((SSIZE_T)(LL - Pos) < 1) return;
line = line.substr(Pos, (LL - Pos));
}
Pos = line.find_last_not_of("\n\r\t ,");
if (Pos != string::npos)
{
if (Pos < 1) return;
line = line.erase(Pos + 1);
}
}
inline string ParseRemoveVBSLineComments(string &Line)
{
const size_t commentIdx = Line.find('\'');
if (commentIdx == string::npos) return string();
string RetVal = Line.substr(commentIdx + 1, string::npos);
RemovePadding(RetVal);
if (commentIdx > 0)
Line = Line.substr(0, commentIdx);
else
Line.clear();
return RetVal;
}
inline void szLower(char * pC)
{
while (*pC)
{
if (*pC >= 'A' && *pC <= 'Z')
*pC += ('a' - 'A');
pC++;
}
}
inline void szUpper(char * pC)
{
while (*pC)
{
if (*pC >= 'a' && *pC <= 'z')
*pC -= ('a' - 'A');
pC++;
}
}