forked from bhoogter/VB6TocSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modConvert.bas
215 lines (178 loc) · 8.08 KB
/
modConvert.bas
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
Attribute VB_Name = "modConvert"
Option Explicit
Const WithMark As String = "_WithVar_"
Dim WithLevel As Long, MaxWithLevel As Long
Dim WithVars As String, WithTypes As String, WithAssign As String
Dim FormName As String
Dim CurrentModule As String
Dim CurrSub As String
Public Const CONVERTER_VERSION_1 As String = "v1"
Public Const CONVERTER_VERSION_2 As String = "v2"
Public Const CONVERTER_VERSION_DEFAULT As String = CONVERTER_VERSION_2
Public Function QuickConvertProject() As Boolean
QuickConvertProject = ConvertProject(vbpFile, CONVERTER_VERSION_2)
End Function
Public Function QuickConvert() As Boolean
QuickConvert = ConvertFile("modQuickConvert.bas", False, CONVERTER_VERSION_2)
End Function
Public Function ConvertProject(Optional ByVal vbpFile As String = "", Optional ByVal ConverterVersion As String = CONVERTER_VERSION_DEFAULT) As Boolean
If vbpFile = "" Then vbpFile = modConfig.vbpFile
Prg 0, 1, "Preparing..."
ScanRefs
CreateProjectFile vbpFile
CreateProjectSupportFiles
ConvertFileList FilePath(vbpFile), VBPModules(vbpFile) & vbCrLf & VBPClasses(vbpFile) & vbCrLf & VBPForms(vbpFile), vbCrLf, ConverterVersion
ConvertProject = True
End Function
Public Function ConvertFileList(ByVal Path As String, ByVal List As String, Optional ByVal Sep As String = vbCrLf, Optional ByVal ConverterVersion As String = CONVERTER_VERSION_DEFAULT) As Boolean
Dim L As Variant, V As Long, N As Long
V = StrCnt(List, Sep) + 1
Prg 0, V, N & "/" & V & "..."
For Each L In Split(List, Sep)
N = N + 1
If L = "" Then GoTo NextItem
If L = "modFunctionList.bas" Then GoTo NextItem
ConvertFile Path & L, False, ConverterVersion
NextItem:
Prg N, , N & "/" & V & ": " & L
DoEvents
Next
Prg
End Function
Public Function ConvertFile(ByVal SomeFile As String, Optional ByVal UIOnly As Boolean = False, Optional ByVal ConverterVersion As String = CONVERTER_VERSION_DEFAULT) As Boolean
If Not IsInStr(SomeFile, "\") Then SomeFile = vbpPath & SomeFile
CurrentModule = ""
Select Case LCase(FileExt(SomeFile))
Case ".bas": ConvertFile = ConvertModule(SomeFile, ConverterVersion)
Case ".cls": ConvertFile = ConvertClass(SomeFile, ConverterVersion)
Case ".frm": FormName = FileBaseName(SomeFile): ConvertFile = ConvertForm(SomeFile, UIOnly, ConverterVersion)
' Case ".ctl": ConvertModule someFile
Case Else: MsgBox "UNKNOWN VB TYPE: " & SomeFile: Exit Function
End Select
FormName = ""
ConvertFile = True
End Function
Public Function ConvertForm(ByVal frmFile As String, Optional ByVal UIOnly As Boolean = False, Optional ByVal ConverterVersion As String = CONVERTER_VERSION_DEFAULT) As Boolean
Dim S As String, J As Long, Preamble As String, Code As String, Globals As String, Functions As String
Dim X As String, fName As String
Dim F As String
If Not FileExists(frmFile) Then
MsgBox "File not found in ConvertForm: " & frmFile
Exit Function
End If
S = ReadEntireFile(frmFile)
fName = ModuleName(S)
CurrentModule = fName
F = fName & ".xaml.cs"
If IsConverted(F, frmFile) Then Debug.Print "Form Already Converted: " & F: Exit Function
J = CodeSectionLoc(S)
Preamble = Left(S, J - 1)
Code = Mid(S, J)
X = ConvertFormUi(Preamble, Code)
F = fName & ".xaml"
WriteOut F, X, frmFile
If UIOnly Then Exit Function
Dim ConvertedCode As String
If ConverterVersion = CONVERTER_VERSION_2 Then
ConvertedCode = ""
Dim ControlArrays As String, VV As Variant
ControlArrays = Replace(Replace(Replace(modConvertForm.FormControlArrays, "][", ";"), "[", ""), "]", "")
For Each VV In Split(ControlArrays, ";")
Dim ControlArrayParts() As String
ControlArrayParts = Split(VV, ",")
ConvertedCode = ConvertedCode & "public List<" & ControlArrayParts(1) & "> " & ControlArrayParts(0) & " { get => VBExtension.controlArray<" & ControlArrayParts(1) & ">(this, """ & ControlArrayParts(0) & """); }" & vbCrLf2
' ConvertedCode = ConvertedCode & "public ControlArrayList<" & ControlArrayParts(1) & "> " & ControlArrayParts(0) & "() => VBExtension.controlArray(this, """ & ControlArrayParts(1) & """).Cast<" & ControlArrayParts(1) & ">().ToList();" & vbCrLf
' ConvertedCode = ConvertedCode & "public " & ControlArrayParts(1) & " " & ControlArrayParts(0) & "(int i) => " & ControlArrayParts(0) & "()[i];" & vbCrLf2
Next
ConvertedCode = ConvertedCode & QuickConvertFile(frmFile)
Else
J = CodeSectionGlobalEndLoc(Code)
Globals = ConvertGlobals(Left(Code, J))
InitLocalFuncs FormControls(fName, Preamble) & ScanRefsFileToString(frmFile)
Functions = ConvertCodeSegment(Mid(Code, J))
ConvertedCode = Globals & vbCrLf2 & Functions
End If
X = ""
X = X & UsingEverything(fName) & vbCrLf
X = X & vbCrLf
X = X & "namespace " & AssemblyName & ".Forms" & vbCrLf
X = X & "{" & vbCrLf
X = X & "public partial class " & fName & " : Window {" & vbCrLf
X = X & " private static " & fName & " _instance;" & vbCrLf
X = X & " public static " & fName & " instance { set { _instance = null; } get { return _instance ?? (_instance = new " & fName & "()); }}"
X = X & " public static void Load() { if (_instance == null) { dynamic A = " + fName + ".instance; } }"
X = X & " public static void Unload() { if (_instance != null) instance.Close(); _instance = null; }"
X = X & " public " & fName & "() => InitializeComponent();" & vbCrLf
X = X & vbCrLf
X = X & vbCrLf
X = X & ConvertedCode
X = X & vbCrLf & "}"
X = X & vbCrLf & "}"
X = deWS(X)
F = fName & ".xaml.cs"
WriteOut F, X, frmFile
End Function
Public Function ConvertModule(ByVal basFile As String, Optional ByVal ConverterVersion As String = CONVERTER_VERSION_DEFAULT) As Boolean
Dim S As String, J As Long, Code As String, Globals As String, Functions As String
Dim F As String, X As String, fName As String
If Not FileExists(basFile) Then
MsgBox "File not found in ConvertModule: " & basFile
Exit Function
End If
S = ReadEntireFile(basFile)
fName = ModuleName(S)
CurrentModule = fName
F = fName & ".cs"
If IsConverted(F, basFile) Then Debug.Print "Module Already Converted: " & F: Exit Function
fName = ModuleName(S)
Code = Mid(S, CodeSectionLoc(S))
Dim UserCode As String
If ConverterVersion = CONVERTER_VERSION_2 Then
UserCode = QuickConvertFile(basFile)
Else
J = CodeSectionGlobalEndLoc(Code)
Globals = ConvertGlobals(Left(Code, J - 1), True)
Functions = ConvertCodeSegment(Mid(Code, J), True)
UserCode = nlTrim(Globals & vbCrLf & vbCrLf & Functions)
UserCode = deWS(UserCode)
End If
X = ""
X = X & UsingEverything(fName) & vbCrLf
X = X & vbCrLf
X = X & "static class " & fName & " {" & vbCrLf
X = X & UserCode
X = X & vbCrLf & "}"
WriteOut F, X, basFile
End Function
Public Function ConvertClass(ByVal clsFile As String, Optional ByVal ConverterVersion As String = CONVERTER_VERSION_DEFAULT) As Boolean
Dim S As String, J As Long, Code As String, Globals As String, Functions As String
Dim F As String, X As String, fName As String
Dim cName As String
If Not FileExists(clsFile) Then
MsgBox "File not found in ConvertModule: " & clsFile
Exit Function
End If
S = ReadEntireFile(clsFile)
fName = ModuleName(S)
CurrentModule = fName
F = fName & ".cs"
If IsConverted(F, clsFile) Then Debug.Print "Class Already Converted: " & F: Exit Function
Dim UserCode As String
If ConverterVersion = CONVERTER_VERSION_2 Then
UserCode = QuickConvertFile(clsFile)
Else
Code = Mid(S, CodeSectionLoc(S))
J = CodeSectionGlobalEndLoc(Code)
Globals = ConvertGlobals(Left(Code, J - 1))
Functions = ConvertCodeSegment(Mid(Code, J))
UserCode = deWS(Globals & vbCrLf & vbCrLf & Functions)
End If
X = ""
X = X & UsingEverything(fName) & vbCrLf
X = X & vbCrLf
X = X & "public class " & fName & " {" & vbCrLf
X = X & UserCode
X = X & vbCrLf & "}"
F = fName & ".cs"
WriteOut F, X, clsFile
End Function