Files
dxdesigner-part-replacement/integration/dxdesigner795/NativeSnapshotReader.vbs
T

243 lines
14 KiB
Plaintext

' Read-only native-host adapter. Appended to the external worker at preparation.
Function QPNativeReadUntouched(ByRef SelectedUids)
Dim Token, Base, ScriptPath, ResultPath, Stream, CommandPath, Text
Dim Project, Sheet, Guid, Safe, Result
Set QPNativeReadUntouched = Nothing
Project = QPProjectPath()
Sheet = QPSheetIdentity()
Guid = CStr(QP_App.Guid())
Token = CStr(QP_AttemptToken) & "-" & CStr(QP_ProgressSequence) & "-" & QP_Fso.GetTempName()
Base = QP_Fso.BuildPath(QP_Root, "native-read-" & Token)
ScriptPath = Base & ".vbs"
ResultPath = Base & ".tsv"
Safe = Not CBool(QP_NativeMutationPossible) And Not CBool(QP_RestoreTransactionOpen) And Not CBool(QP_NativeCallActive)
Text = QPBuildNativeSnapshotScript(QPReadUtf8(WScript.ScriptFullName), SelectedUids, _
Token, Project, Sheet, Guid, ResultPath, CStr(QP_AttemptToken), Safe)
' Generated source is ASCII; Unicode values are escaped with QPEncode.
Set Stream = QP_Fso.CreateTextFile(ScriptPath, False, False)
Stream.Write Text
Stream.Close
CommandPath = QP_Fso.GetFile(ScriptPath).ShortPath
If Not QPNativeCommandPathSupported(CommandPath) Then
QPLog QP_Root, "INFO", "native_read_path_unavailable; using external full read"
Exit Function
End If
QPProgress "native_snapshot_read", 0, 0
If Not QP_App.ExecuteCommand("run " & CommandPath) Then _
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "native script command failed"
If Not QP_Fso.FileExists(ResultPath) Then _
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "native script returned no complete result"
Set Result = QPParseNativeSnapshot(QPReadUtf8(ResultPath), Token, Project, Sheet, SelectedUids)
If CStr(QP_App.Guid()) <> Guid Or QPProjectPath() <> Project Or QPSheetIdentity() <> Sheet Then _
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "context changed after native read"
QPProgress "native_snapshot_read", Result("total"), Result("total")
QPLog QP_Root, "INFO", "native_snapshot_verified components=" & CStr(Result("total")) & " token=" & Token
Set QPNativeReadUntouched = Result
End Function
Function QPNativeCommandPathSupported(ByVal Path)
Dim Index, Code
QPNativeCommandPathSupported = False
For Index = 1 To Len(Path)
Code = AscW(Mid(Path, Index, 1))
If Code <= 32 Or Code > 126 Or Code = 34 Then Exit Function
Next
QPNativeCommandPathSupported = (Len(Path) > 0)
End Function
Function QPTryNativeVerifyUntouched(ByRef Records)
Dim Selected, Record, Index, Actual
QPTryNativeVerifyUntouched = False
QPRequireRecordsContext Records
Set Selected = CreateObject("Scripting.Dictionary")
For Index = 0 To Records("components").Count - 1
Set Record = Records("components").Item(CStr(Index))
Selected.Add CStr(Record("uid")), True
Next
Set Actual = QPNativeReadUntouched(Selected)
If Actual Is Nothing Then Exit Function
QPRequireRecordsContext Records
QPCompareNativeUntouched Records, Actual
QPTryNativeVerifyUntouched = True
End Function
Sub QPCompareNativeUntouched(ByRef Records, ByRef Actual)
Dim Baseline, FullBaseline, Rows, Uid, Expected
Set Baseline = Records("untouched")
Set FullBaseline = Records("untouched_full")
Set Rows = Actual("rows")
If Actual("total") <> Baseline.Count + Records("components").Count Or Rows.Count <> Baseline.Count Then _
QPNativeReadInvalid "component count changed outside selected targets"
For Each Uid In Baseline.Keys
If Not Rows.Exists(Uid) Or Not FullBaseline.Exists(Uid) Then QPNativeReadInvalid "unselected component missing"
Expected = CStr(Baseline(Uid)) & vbCrLf & "G" & vbTab & QPEncode(Uid) & vbTab & _
QPEncode(CStr(FullBaseline(Uid))) & vbCrLf
If Rows(Uid) <> Expected Then QPNativeReadInvalid "unselected component changed"
Next
End Sub
Function QPParseNativeSnapshot(ByVal Text, ByVal Token, ByVal Project, ByVal Sheet, ByRef SelectedUids)
Dim Lines, Fields, Last, Index, Uid, Identity, State, Pair, Rows, Result, Total
Set Rows = CreateObject("Scripting.Dictionary")
Lines = Split(Text, vbCrLf)
Last = UBound(Lines)
If Last > 0 Then
If Lines(Last) = "" Then Last = Last - 1
End If
Fields = Split(Lines(0), vbTab)
If Fields(0) = "ERROR" Then
If UBound(Fields) <> 4 Then QPNativeReadInvalid "malformed error frame"
If Fields(1) <> Token Then QPNativeReadInvalid "stale error token"
Err.Raise CLng(Fields(2)), QPDecode(Fields(3)), QPDecode(Fields(4))
End If
If UBound(Fields) <> 3 Then QPNativeReadInvalid "missing native header"
If Fields(0) <> "NR1" Or Fields(1) <> Token Or QPDecode(Fields(2)) <> Project Or _
QPDecode(Fields(3)) <> Sheet Then QPNativeReadInvalid "native header context mismatch"
If Last < 1 Then QPNativeReadInvalid "truncated native result"
Fields = Split(Lines(Last), vbTab)
If UBound(Fields) <> 2 Then QPNativeReadInvalid "missing completion frame"
If Fields(0) <> "END" Or Fields(1) <> Token Then QPNativeReadInvalid "missing completion token"
If Len(Fields(2)) = 0 Then QPNativeReadInvalid "invalid component count"
For Index = 1 To Len(Fields(2))
If InStr(1, "0123456789", Mid(Fields(2), Index, 1), vbBinaryCompare) = 0 Then _
QPNativeReadInvalid "invalid component count"
Next
Total = CLng(Fields(2))
Index = 1
Do While Index < Last
If Index + 1 >= Last Then QPNativeReadInvalid "incomplete U/G pair"
Fields = Split(Lines(Index), vbTab)
If UBound(Fields) <> 6 Then QPNativeReadInvalid "malformed identity row"
If Fields(0) <> "U" Then QPNativeReadInvalid "unexpected native row"
Uid = QPDecode(Fields(1))
If Len(Uid) = 0 Then QPNativeReadInvalid "empty component UID"
If SelectedUids.Exists(Uid) Or Rows.Exists(Uid) Then QPNativeReadInvalid "duplicate or selected UID in unselected result"
Identity = Lines(Index)
Fields = Split(Lines(Index + 1), vbTab)
If UBound(Fields) <> 2 Then QPNativeReadInvalid "malformed full-state row"
If Fields(0) <> "G" Or QPDecode(Fields(1)) <> Uid Then QPNativeReadInvalid "full-state owner mismatch"
State = QPDecode(Fields(2))
If Len(State) = 0 Or QPUntouchedIdentity(State) <> Identity Then QPNativeReadInvalid "full-state identity mismatch"
Pair = Identity & vbCrLf & Lines(Index + 1) & vbCrLf
Rows.Add Uid, Pair
Index = Index + 2
Loop
If Rows.Count + SelectedUids.Count <> Total Then QPNativeReadInvalid "component total mismatch"
Set Result = CreateObject("Scripting.Dictionary")
Result.Add "total", Total
Result.Add "rows", Rows
Text = ""
For Each Uid In Rows.Keys
Text = Text & Rows(Uid)
Next
Result.Add "text", Text
Set QPParseNativeSnapshot = Result
End Function
Sub QPNativeReadInvalid(ByVal Description)
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", Description
End Sub
Function QPNativeCollect(ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid)
Dim Components, Comp, Uid, Seen, Found, Text, Count
QPNativeRequireContext Project, Sheet, Guid
QPInvalidatePinIndex
Set Components = QP_App.ActiveView.Query(VDM_COMP, VD_ALL)
Set Seen = CreateObject("Scripting.Dictionary")
Set Found = CreateObject("Scripting.Dictionary")
Text = "NR1" & vbTab & Token & vbTab & QPEncode(Project) & vbTab & QPEncode(Sheet) & vbCrLf
Count = 0
QPProgress "native_snapshot_read", 0, Components.Count
For Each Comp In Components
Uid = CStr(Comp.UID)
If Len(Uid) = 0 Or Seen.Exists(Uid) Then QPNativeReadInvalid "duplicate or empty component UID"
Seen.Add Uid, True
If SelectedUids.Exists(Uid) Then
Found.Add Uid, True
Else
Text = Text & QPUntouchedComponent(Comp)
End If
Count = Count + 1
QPProgress "native_snapshot_read", Count, Components.Count
Next
If Found.Count <> SelectedUids.Count Then QPNativeReadInvalid "selected component missing from native read"
QPNativeRequireContext Project, Sheet, Guid
QPNativeCollect = Text & "END" & vbTab & Token & vbTab & CStr(Count) & vbCrLf
End Function
Sub QPNativeRequireContext(ByVal Project, ByVal Sheet, ByVal Guid)
If CStr(QP_App.Guid()) <> Guid Then QPNativeReadInvalid "foreign CAD GUID"
If QPProjectPath() <> Project Or QPSheetIdentity() <> Sheet Then QPNativeReadInvalid "native CAD context mismatch"
End Sub
Function QPBuildNativeSnapshotScript(ByVal Source, ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid, ByVal ResultPath, ByVal StopToken, ByVal Safe)
Dim Header, Body, Names, Name, Uid, Pattern, Matches, MonitorJob
Header = Left(Source, InStr(Source, "Set QP_Fso =") - 1)
Set Pattern = CreateObject("VBScript.RegExp")
Pattern.Global = True
Pattern.MultiLine = True
Pattern.Pattern = "^Const (VDM_COMP|VDM_COMPPIN|VD_ALL) = [^\r\n]*\r?\n"
Header = Pattern.Replace(Header, "")
MonitorJob = QP_ExpectedJobId
If QP_SessionMode Then MonitorJob = QP_MonitorJobId
Body = Header & vbCrLf & "Dim QP_NR_Selected, QP_NR_Text, QP_NR_Code, QP_NR_Source, QP_NR_Description, QP_NR_Safe" & vbCrLf & _
"Dim QP_NR_MonitorJob, QP_NR_CurrentJob, QP_NR_ParentSequence, QP_NR_Sequence, QP_NR_LastProgress" & vbCrLf & _
"Set QP_Fso=CreateObject(""Scripting.FileSystemObject"")" & vbCrLf & _
"QP_Root=" & QPNativeExpression(QP_Root) & vbCrLf & _
"QP_AttemptToken=" & QPNativeExpression(StopToken) & vbCrLf & _
"QP_NR_Safe=" & CStr(CBool(Safe)) & vbCrLf & _
"QP_NR_MonitorJob=" & QPNativeExpression(MonitorJob) & vbCrLf & _
"QP_NR_CurrentJob=" & QPNativeExpression(QP_ExpectedJobId) & vbCrLf & _
"QP_NR_ParentSequence=" & CStr(CLng(QP_ProgressSequence) + 1) & vbCrLf & _
"QP_NR_Sequence=0:QP_NR_LastProgress=-1" & vbCrLf & _
"Set QP_NR_Selected=CreateObject(""Scripting.Dictionary"")" & vbCrLf
For Each Uid In SelectedUids.Keys
Body = Body & "QP_NR_Selected.Add " & QPNativeExpression(Uid) & ",True" & vbCrLf
Next
Body = Body & "On Error Resume Next" & vbCrLf & _
"Set QP_App=GetObject(,""Viewdraw.Application"")" & vbCrLf & _
"If Err.Number=0 Then QP_NR_Text=QPNativeCollect(QP_NR_Selected," & _
QPNativeExpression(Token) & "," & QPNativeExpression(Project) & "," & QPNativeExpression(Sheet) & "," & QPNativeExpression(Guid) & ")" & vbCrLf & _
"QP_NR_Code=Err.Number: QP_NR_Source=Err.Source: QP_NR_Description=Err.Description" & vbCrLf & _
"Err.Clear" & vbCrLf & _
"If QP_NR_Code<>0 Then QP_NR_Text=""ERROR"" & vbTab & " & QPNativeExpression(Token) & _
" & vbTab & CStr(QP_NR_Code) & vbTab & QPEncode(QP_NR_Source) & vbTab & QPEncode(QP_NR_Description) & vbCrLf" & vbCrLf & _
"QPWriteUtf8 " & QPNativeExpression(ResultPath) & ",QP_NR_Text" & vbCrLf & "On Error GoTo 0" & vbCrLf
Names = Split("QPNativeCollect QPNativeRequireContext QPNativeReadInvalid QPUntouchedComponent QPUntouchedFullState QPUntouchedIdentity QPComponentPins QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPOptionalAttributeField QPSortSnapshotLines QPEncode QPDecode QPProjectPath QPSheetIdentity QPCanonicalPath QPEnsureProjectAccess QPReadUtf8 QPWriteUtf8", " ")
Pattern.Global = False
For Each Name In Names
Pattern.Pattern = "^(Function|Sub) " & Name & "\([^\n]*\n[\s\S]*?^End \1[ \t]*"
Set Matches = Pattern.Execute(Source)
If Matches.Count <> 1 Then QPNativeReadInvalid "missing native read routine: " & Name
Body = Body & vbCrLf & Matches(0).Value & vbCrLf
Next
Body = Body & vbCrLf & "Sub QPLog(ByVal Root,ByVal Level,ByVal Message)" & vbCrLf & "End Sub" & vbCrLf & _
"Sub QPProgress(ByVal Stage,ByVal Done,ByVal Total)" & vbCrLf & _
" Dim StopPath,Elapsed,CurrentTime,ProgressPath,Stream,SafeFlag" & vbCrLf & _
" If QP_NR_Safe And Len(QP_AttemptToken)>0 Then" & vbCrLf & _
" StopPath=QP_Fso.BuildPath(QP_Root,""monitor-"" & QP_AttemptToken & "".stop"")" & vbCrLf & _
" If QP_Fso.FileExists(StopPath) Then" & vbCrLf & _
" If Trim(QPReadUtf8(StopPath))=QP_AttemptToken Then Err.Raise vbObjectError+860,""QuickPartReplace.SafeStop"",""native read stopped at safe boundary""" & vbCrLf & _
" End If" & vbCrLf & " End If" & vbCrLf & _
" If Len(QP_AttemptToken)=0 Then Exit Sub" & vbCrLf & _
" CurrentTime=Timer:Elapsed=CurrentTime-QP_NR_LastProgress" & vbCrLf & _
" If Elapsed<0 Then Elapsed=Elapsed+86400" & vbCrLf & _
" If QP_NR_LastProgress>=0 And Elapsed<1 And Not (Total>0 And Done=Total) Then Exit Sub" & vbCrLf & _
" SafeFlag=0:If QP_NR_Safe Then SafeFlag=1" & vbCrLf & _
" QP_NR_Sequence=QP_NR_Sequence+1" & vbCrLf & _
" ProgressPath=QP_Fso.BuildPath(QP_Root,""monitor-"" & QP_AttemptToken & "".native-progress"")" & vbCrLf & _
" On Error Resume Next" & vbCrLf & _
" Set Stream=QP_Fso.CreateTextFile(ProgressPath,True,False)" & vbCrLf & _
" Stream.WriteLine ""DX_NATIVE_PROGRESS"" & vbTab & QP_AttemptToken & vbTab & QP_NR_MonitorJob & vbTab & _" & vbCrLf & _
" CStr(QP_NR_ParentSequence) & vbTab & QP_NR_CurrentJob & vbTab & CStr(QP_NR_Sequence) & vbTab & _" & vbCrLf & _
" CStr(Done) & vbTab & CStr(Total) & vbTab & CStr(SafeFlag)" & vbCrLf & _
" Stream.Close:Err.Clear" & vbCrLf & " On Error GoTo 0" & vbCrLf & _
" QP_NR_LastProgress=CurrentTime" & vbCrLf & "End Sub" & vbCrLf
QPBuildNativeSnapshotScript = Body
End Function
Function QPNativeExpression(ByVal Value)
QPNativeExpression = "QPDecode(" & Chr(34) & Replace(QPEncode(Value), Chr(34), Chr(34) & Chr(34)) & Chr(34) & ")"
End Function