191 lines
11 KiB
Plaintext
191 lines
11 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 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
|
|
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, "")
|
|
Body = Header & vbCrLf & "Dim QP_NR_Selected, QP_NR_Text, QP_NR_Code, QP_NR_Source, QP_NR_Description, QP_NR_Safe" & 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 & _
|
|
"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" & 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 & "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
|