-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_luac.vbs
78 lines (67 loc) · 3.18 KB
/
convert_luac.vbs
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
Option Explicit
' -------------------------------------------------------------------------------------
' Script: convert_luac.vbs
' Description:
' Reads a compiled Lua bytecode file (initScript.luac) and the original Lua source
' (initScript.lua), then generates a C++ header file (luaEmbedded.h).
'
' Expected input:
' - initScript.luac (compiled Lua bytecode)
' - initScript.lua (original Lua source)
'
' Output:
' - luaEmbedded.h (C++ header file with both bytecode and source)
' -------------------------------------------------------------------------------------
Dim stream, byteArray, i, hexStr, outStr, fso, outFile
Dim sourceStream, sourceStr, sourceArray, line
' --- Read Lua bytecode file ---
Set stream = CreateObject("ADODB.Stream")
stream.Type = 1 ' Binary mode
stream.Open
stream.LoadFromFile "initScript.luac"
byteArray = stream.Read
stream.Close
' --- Read Lua source file ---
Set sourceStream = CreateObject("ADODB.Stream")
sourceStream.Type = 2 ' Text mode
sourceStream.Charset = "UTF-8"
sourceStream.Open
sourceStream.LoadFromFile "initScript.lua"
sourceStr = sourceStream.ReadText
sourceStream.Close
' --- Start generating the C++ header file ---
outStr = "#ifndef LUA_EMBEDDED_H" & vbCrLf & _
"#define LUA_EMBEDDED_H" & vbCrLf & vbCrLf & _
"// ==========================================================================================" & vbCrLf & _
"// This file is auto-generated by lua-bytecode-builder." & vbCrLf & _
"// Project: https://github.com/daddel80/lua-bytecode-builder" & vbCrLf & _
"// The bytecode below is generated from the Lua source code and used at runtime." & vbCrLf & _
"// The original Lua source is provided below for documentation and fallback purposes." & vbCrLf & _
"// ==========================================================================================" & vbCrLf & vbCrLf & _
"// ---------- Precompiled Lua Bytecode (Used at Runtime) ----------" & vbCrLf & _
"static const unsigned char luaBytecode[] = {" & vbCrLf
' --- Store bytecode as a C++ array ---
For i = 1 To LenB(byteArray)
hexStr = Right("0" & Hex(AscB(MidB(byteArray, i, 1))), 2)
outStr = outStr & "0x" & hexStr & ", "
If (i Mod 16) = 0 Then outStr = outStr & vbCrLf
Next
outStr = outStr & "};" & vbCrLf & _
"static const size_t luaBytecodeSize = sizeof(luaBytecode);" & vbCrLf & vbCrLf
' --- Store Lua source code as a C++ raw string literal ---
outStr = outStr & "// ---------- Original Lua Source Code (For Documentation/Fallback) ----------" & vbCrLf & _
"static const char* luaSourceCode = R""(" & vbCrLf
' --- Append Lua source code as a valid C++ raw string ---
sourceArray = Split(sourceStr, vbCrLf)
For i = 0 To UBound(sourceArray)
outStr = outStr & sourceArray(i) & vbCrLf
Next
outStr = outStr & ")"";" & vbCrLf & _
"static const size_t luaSourceSize = sizeof(luaSourceCode);" & vbCrLf & vbCrLf & _
"#endif // LUA_EMBEDDED_H" & vbCrLf
' --- Save the header file ---
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("luaEmbedded.h", True)
outFile.Write outStr
outFile.Close
WScript.Echo "C++ header file has been saved as luaEmbedded.h!"