Files

229 lines
6.9 KiB
Plaintext

' Shared DxDesigner 7.9.5 bridge helpers. Keep this file ASCII-only.
Function DxToolsQuote(value)
Dim text
Dim expression
text = CStr(value)
' Double any trailing backslashes so they cannot escape the closing quote.
Set expression = CreateObject("VBScript.RegExp")
expression.Pattern = "\\+$"
expression.Global = False
text = expression.Replace(text, "$&$&")
DxToolsQuote = Chr(34) & Replace(text, Chr(34), _
Chr(92) & Chr(34)) & Chr(34)
End Function
Function DxToolsJson(value)
Dim text
text = CStr(value)
text = Replace(text, Chr(92), Chr(92) & Chr(92))
text = Replace(text, Chr(34), Chr(92) & Chr(34))
text = Replace(text, vbCr, Chr(92) & "r")
text = Replace(text, vbLf, Chr(92) & "n")
text = Replace(text, vbTab, Chr(92) & "t")
DxToolsJson = text
End Function
Function DxToolsReadUtf8(path)
Dim stream
Set stream = CreateObject("ADODB.Stream")
stream.Type = 2
stream.Charset = "utf-8"
stream.Open
stream.LoadFromFile CStr(path)
DxToolsReadUtf8 = stream.ReadText
stream.Close
End Function
Sub DxToolsWriteUtf8Atomic(path, text)
Dim stream
Dim temporaryPath
Dim shell
Dim commandLine
Dim exitCode
temporaryPath = CStr(path) & ".tmp"
Set stream = CreateObject("ADODB.Stream")
stream.Type = 2
stream.Charset = "utf-8"
stream.Open
stream.WriteText CStr(text)
stream.SaveToFile temporaryPath, 2
stream.Close
Set shell = CreateObject("WScript.Shell")
commandLine = DxToolsQuote(shell.ExpandEnvironmentStrings("%ComSpec%")) & _
" /d /s /c " & Chr(34) & "move /y " & _
DxToolsQuote(temporaryPath) & " " & DxToolsQuote(path) & _
" >nul" & Chr(34)
exitCode = shell.Run(commandLine, 0, True)
If exitCode <> 0 Then
Err.Raise vbObjectError + 795, "DxToolsWriteUtf8Atomic", _
"Cannot replace bridge file: " & CStr(path)
End If
End Sub
Function DxToolsParseScheduleRow(line, ByRef sequence, ByRef op, _
ByRef bridgeId, ByRef propertyName, ByRef expectedValue, _
ByRef newValue, ByRef finalValue)
Dim fields
Dim field
DxToolsParseScheduleRow = False
fields = Split(CStr(line), vbTab)
If UBound(fields) <> 6 Then Exit Function
If Not IsNumeric(fields(0)) Then Exit Function
If CLng(fields(0)) < 1 Then Exit Function
If Len(fields(2)) = 0 Then Exit Function
For Each field In fields
If InStr(field, vbCr) > 0 Or InStr(field, vbLf) > 0 Then Exit Function
Next
sequence = CLng(fields(0))
op = CStr(fields(1))
bridgeId = CStr(fields(2))
propertyName = CStr(fields(3))
expectedValue = CStr(fields(4))
newValue = CStr(fields(5))
finalValue = CStr(fields(6))
DxToolsParseScheduleRow = True
End Function
Function DxToolsSha256File(path)
Dim shell
Dim fso
Dim certutilPath
Dim outputPath
Dim commandLine
Dim exitCode
Dim output
Dim expression
Dim matches
Set shell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
certutilPath = shell.ExpandEnvironmentStrings( _
"%SystemRoot%\System32\certutil.exe")
outputPath = CStr(path) & ".sha256.txt"
commandLine = DxToolsQuote(certutilPath) & " -hashfile " & _
DxToolsQuote(path) & " SHA256 > " & DxToolsQuote(outputPath)
exitCode = shell.Run(DxToolsQuote( _
shell.ExpandEnvironmentStrings("%ComSpec%")) & _
" /d /s /c " & Chr(34) & commandLine & Chr(34), 0, True)
If exitCode <> 0 Then
Err.Raise vbObjectError + 796, "DxToolsSha256File", _
"Cannot calculate result digest"
End If
output = fso.OpenTextFile(outputPath, 1, False, 0).ReadAll
fso.DeleteFile outputPath, True
Set expression = CreateObject("VBScript.RegExp")
expression.Pattern = "[0-9A-Fa-f]{64}"
expression.Global = False
Set matches = expression.Execute(output)
If matches.Count <> 1 Then
Err.Raise vbObjectError + 797, "DxToolsSha256File", _
"SHA-256 output is invalid"
End If
DxToolsSha256File = LCase(matches.Item(0).Value)
End Function
Function DxToolsJsonString(jsonText, key, ByRef value)
Dim marker
Dim markerAt
Dim colonAt
Dim quoteAt
Dim index
Dim character
Dim escaped
DxToolsJsonString = False
marker = Chr(34) & CStr(key) & Chr(34)
markerAt = InStr(1, CStr(jsonText), marker, vbBinaryCompare)
If markerAt = 0 Then Exit Function
colonAt = InStr(markerAt + Len(marker), CStr(jsonText), ":")
If colonAt = 0 Then Exit Function
quoteAt = InStr(colonAt + 1, CStr(jsonText), Chr(34))
If quoteAt = 0 Then Exit Function
escaped = False
value = ""
For index = quoteAt + 1 To Len(CStr(jsonText))
character = Mid(CStr(jsonText), index, 1)
If escaped Then
Select Case character
Case Chr(34), Chr(92), "/"
value = value & character
Case "r"
value = value & vbCr
Case "n"
value = value & vbLf
Case "t"
value = value & vbTab
Case Else
Exit Function
End Select
escaped = False
ElseIf character = Chr(92) Then
escaped = True
ElseIf character = Chr(34) Then
DxToolsJsonString = True
Exit Function
Else
value = value & character
End If
Next
End Function
Sub DxBridgeEnsureFolder(path)
Dim fso
Dim parent
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(CStr(path)) Then Exit Sub
parent = fso.GetParentFolderName(CStr(path))
If parent <> "" And Not fso.FolderExists(parent) Then
Call DxBridgeEnsureFolder(parent)
End If
On Error Resume Next
fso.CreateFolder CStr(path)
If Err.Number <> 0 And Err.Number <> 58 Then
On Error GoTo 0
Err.Raise vbObjectError + 798, "DxBridgeEnsureFolder", _
"Cannot create folder: " & CStr(path)
End If
Err.Clear
On Error GoTo 0
End Sub
Function DxBridgeNormalizeVisible(value)
Dim text
If IsEmpty(value) Or IsNull(value) Then
DxBridgeNormalizeVisible = "0"
Exit Function
End If
If VarType(value) = vbBoolean Then
If CBool(value) Then
DxBridgeNormalizeVisible = "1"
Else
DxBridgeNormalizeVisible = "0"
End If
Exit Function
End If
text = LCase(Trim(CStr(value)))
If text = "0" Or text = "" Or text = "false" Then
DxBridgeNormalizeVisible = "0"
Else
DxBridgeNormalizeVisible = "1"
End If
End Function
Sub DxBridgeIdleWait()
Dim shell
On Error Resume Next
Set shell = CreateObject("WScript.Shell")
shell.Run "%ComSpec% /d /s /c ping -n 2 -w 400 127.0.0.1 > nul", 0, True
Err.Clear
On Error GoTo 0
Call DxBridgeYield()
End Sub