Mod Tool/Scripting: Difference between revisions

m
no edit summary
(modify file via binary stream while patch data can be ASCII or byte array)
mNo edit summary
Line 140: Line 140:
===modify file===
===modify file===
====binary====
====binary====
[[Image:Before_and_after_binary_patching_with_vbs.png|thumb|400px|Before and after patching.]]
Change FilePath and in case of binary patching use function "StringToByteArray".
Change FilePath and in case of binary patching use function "StringToByteArray".


Line 180: Line 181:
  OutputStream.Close
  OutputStream.Close
  Set OutputStream = Nothing
  Set OutputStream = Nothing
===Conversions===
====String -> byte array====
function StringToByteArray(ThisString)
for i = 1 To Len(ThisString) Step 2
str = str & Chr("&h" & Mid(ThisString, i, 2))
next
Set stream = CreateObject("ADODB.Stream")
With stream
.Open
.CharSet = "Windows-1252"
.Type = 2
' ### 2 = text
.WriteText str
.Position = 0
.Type = 1
' ### 1 = binary
StringToByteArray = .Read
.Close
End With
end function
' ### usage
ByteArray = StringToByteArray(ThisString)
====Byte array -> string====
Function ByteArrayToString(Binary)
  'Antonin Foller, http://www.motobit.com
  'Optimized version of a simple BinaryToString algorithm.
 
  Dim cl1, cl2, cl3, pl1, pl2, pl3
  Dim L
  cl1 = 1
  cl2 = 1
  cl3 = 1
  L = LenB(Binary)
 
  Do While cl1<=L
    pl3 = pl3 & Chr(AscB(MidB(Binary,cl1,1)))
    cl1 = cl1 + 1
    cl3 = cl3 + 1
    If cl3>300 Then
      pl2 = pl2 & pl3
      pl3 = ""
      cl3 = 1
      cl2 = cl2 + 1
      If cl2>200 Then
        pl1 = pl1 & pl2
        pl2 = ""
        cl2 = 1
      End If
    End If
  Loop
  BinaryToString = pl1 & pl2 & pl3
End Function
' ### usage
MyString = ByteArrayToString(ByteArray)




Line 356: Line 418:
  'qW = ... * -1
  'qW = ... * -1
  'set qRotation = XSIMath.CreateQuaternion (qW, qX, qY, qZ)
  'set qRotation = XSIMath.CreateQuaternion (qW, qX, qY, qZ)
===Conversions===
====String -> byte array====
function StringToByteArray(ThisString)
for i = 1 To Len(ThisString) Step 2
str = str & Chr("&h" & Mid(ThisString, i, 2))
next
Set stream = CreateObject("ADODB.Stream")
With stream
.Open
.CharSet = "Windows-1252"
.Type = 2
' ### 2 = text
.WriteText str
.Position = 0
.Type = 1
' ### 1 = binary
StringToByteArray = .Read
.Close
End With
end function
' ### usage
ByteArray = StringToByteArray(ThisString)
====Byte array -> string====
Function ByteArrayToString(Binary)
  'Antonin Foller, http://www.motobit.com
  'Optimized version of a simple BinaryToString algorithm.
 
  Dim cl1, cl2, cl3, pl1, pl2, pl3
  Dim L
  cl1 = 1
  cl2 = 1
  cl3 = 1
  L = LenB(Binary)
 
  Do While cl1<=L
    pl3 = pl3 & Chr(AscB(MidB(Binary,cl1,1)))
    cl1 = cl1 + 1
    cl3 = cl3 + 1
    If cl3>300 Then
      pl2 = pl2 & pl3
      pl3 = ""
      cl3 = 1
      cl2 = cl2 + 1
      If cl2>200 Then
        pl1 = pl1 & pl2
        pl2 = ""
        cl2 = 1
      End If
    End If
  Loop
  BinaryToString = pl1 & pl2 & pl3
End Function
' ### usage
MyString = ByteArrayToString(ByteArray)




[[Category:Windows-only modding tools]]
[[Category:Windows-only modding tools]]
8,452

edits