(svn r25652) -Fix: Improve text caret movement for complex scripts.
[openttd/fttd.git] / projects / generate.vbs
blob87c27ab38762ff5719e8c0457196d1a8fecce63c
1 Option Explicit
3 ' $Id$
5 ' This file is part of OpenTTD.
6 ' OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
7 ' OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 ' See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 Dim FSO
11 Set FSO = CreateObject("Scripting.FileSystemObject")
13 ' openttd_vs100.sln is for MSVC 2010
14 ' openttd_vs100.vcxproj is for MSVC 2010
15 ' openttd_vs100.vcxproj.filters is for MSVC 2010
16 ' langs_vs100.vcxproj is for MSVC 2010
17 ' strgen_vs100.vcxproj is for MSVC 2010
18 ' strgen_vs100.vcxproj.filters is for MSVC 2010
19 ' generate_vs100.vcxproj is for MSVC 2010
20 ' version_vs100.vcxproj is for MSVC 2010
22 ' openttd_vs90.sln is for MSVC 2008
23 ' openttd_vs90.vcproj is for MSVC 2008
24 ' langs_vs90.vcproj is for MSVC 2008
25 ' strgen_vs90.vcproj is for MSVC 2008
26 ' generate_vs90.vcproj is for MSVC 2008
27 ' version_vs90.vcproj is for MSVC 2008
29 ' openttd_vs80.sln is for MSVC 2005
30 ' openttd_vs80.vcproj is for MSVC 2005
31 ' langs_vs80.vcproj is for MSVC 2005
32 ' strgen_vs80.vcproj is for MSVC 2005
33 ' generate_vs80.vcproj is for MSVC 2005
34 ' version_vs80.vcproj is for MSVC 2005
36 Sub safety_check(filename)
37 Dim file, line, regexp, list
39 ' Define regexp
40 Set regexp = New RegExp
41 regexp.Pattern = "#|ottdres.rc|win32.cpp|win32_v.cpp"
42 regexp.Global = True
44 ' We use a dictionary to check duplicates
45 Set list = CreateObject("Scripting.Dictionary")
47 Set file = FSO.OpenTextFile(filename, 1, 0, 0)
48 While Not file.AtEndOfStream
49 line = Replace(file.ReadLine, Chr(9), "") ' Remove tabs
50 If Len(line) > 0 And Not regexp.Test(line) Then
51 line = FSO.GetFileName(line)
52 if list.Exists(line) Then
53 WScript.Echo " !! ERROR !!" _
54 & vbCrLf & "" _
55 & vbCrLf & "The filename '" & line & "' is already used in this project." _
56 & vbCrLf & "Because MSVC uses one single directory for all object files, it" _
57 & vbCrLf & "cannot handle filenames with the same name inside the same project." _
58 & vbCrLf & "Please rename either one of the file and try generating again." _
59 & vbCrLf & "" _
60 & vbCrLf & " !! ERROR !!"
61 WScript.Quit(1)
62 End If
63 list.Add line, line
64 End If
65 Wend
66 file.Close
67 End Sub
69 Sub get_files(srcdir, dir, list)
70 Dim file, filename
71 Dim rekeep, reskip
73 ' pattern for files to keep
74 Set rekeep = New RegExp
75 rekeep.Pattern = "\.h(pp)?$"
76 rekeep.Global = True
78 ' pattern for files to exclude
79 Set reskip = New RegExp
80 reskip.Pattern = "\.svn"
81 reskip.Global = True
83 For Each file in dir.Files
84 filename = Replace(file.path, srcdir, "") ' Remove */src/
85 filename = Replace(filename, "\", "/") ' Replace separators
86 If rekeep.Test(filename) And Not reskip.Test(filename) Then
87 list.Add filename, filename
88 End If
89 Next
90 End Sub
92 Sub get_dir_files(srcdir, dir, list)
93 Dim folder
94 ' Get files
95 get_files srcdir, dir, list
97 ' Recurse in subfolders
98 For Each folder in dir.SubFolders
99 get_dir_files srcdir, folder, list
100 Next
101 End Sub
103 Sub headers_check(filename, dir)
104 Dim source_list_headers, src_dir_headers, regexp, line, file, str
106 ' Define regexp for source.list parsing
107 Set regexp = New RegExp
108 regexp.Pattern = "\.h"
109 regexp.Global = True
111 ' Parse source.list and store headers in a dictionary
112 Set source_list_headers = CreateObject("Scripting.Dictionary")
113 Set file = FSO.OpenTextFile(filename, 1, 0, 0)
114 While Not file.AtEndOfStream
115 line = Replace(file.ReadLine, Chr(9), "") ' Remove tabs
116 If Len(line) > 0 And regexp.Test(line) And line <> "../objs/langs/table/strings.h" And line <> "../objs/settings/table/settings.h" Then
117 source_list_headers.Add line, line
118 End If
119 Wend
120 file.Close()
122 ' Get header files in /src/
123 Set src_dir_headers = CreateObject("Scripting.Dictionary")
124 get_dir_files dir, FSO.GetFolder(dir), src_dir_headers
126 ' Finding files in source.list but not in /src/
127 For Each line In source_list_headers
128 If Not src_dir_headers.Exists(line) Then
129 str = str & "< " & line & vbCrLf
130 End If
131 Next
133 ' Finding files in /src/ but not in source.list
134 For Each line In src_dir_headers
135 If Not source_list_headers.Exists(line) Then
136 str = str & "> " & line & vbCrLf
137 End If
138 Next
140 ' Display the missing files if any
141 If str <> "" Then
142 str = "The following headers are missing in source.list and not in /src/ or vice versa." _
143 & vbCrLf & str
144 WScript.Echo str
145 End If
146 End Sub
148 Function load_main_data(filename, ByRef vcxproj, ByRef filters, ByRef files)
149 Dim res, file, line, deep, skip, first_filter, first_file, filter, cltype, index
150 res = ""
151 index = 0
152 ' Read the source.list and process it
153 Set file = FSO.OpenTextFile(filename, 1, 0, 0)
154 While Not file.AtEndOfStream
155 line = Replace(file.ReadLine, Chr(9), "") ' Remove tabs
156 If Len(line) > 0 Then
157 Select Case Split(line, " ")(0)
158 Case "#end"
159 If deep = skip Then skip = skip - 1
160 deep = deep - 1
161 Case "#else"
162 If deep = skip Then
163 skip = skip - 1
164 ElseIf deep - 1 = skip Then
165 skip = skip + 1
166 End If
167 Case "#if"
168 line = Replace(line, "#if ", "")
169 If deep = skip And ( _
170 line = "SDL" Or _
171 line = "PNG" Or _
172 line = "WIN32" Or _
173 line = "MSVC" Or _
174 line = "DIRECTMUSIC" Or _
175 line = "AI" Or _
176 line = "HAVE_THREAD" _
177 ) Then skip = skip + 1
178 deep = deep + 1
179 Case "#"
180 if deep = skip Then
181 line = Replace(line, "# ", "")
182 if first_filter <> 0 Then
183 res = res & " </Filter>" & vbCrLf
184 filters = filters & vbCrLf
185 Else
186 first_filter = 1
187 End If
188 filter = line
189 res = res & _
190 " <Filter" & vbCrLf & _
191 " Name=" & Chr(34) & filter & Chr(34) & vbCrLf & _
192 " >" & vbCrLf
193 filters = filters & _
194 " <Filter Include="& Chr(34) & filter & Chr(34) & ">" & vbCrLf & _
195 " <UniqueIdentifier>{c76ff9f1-1e62-46d8-8d55-" & String(12 - Len(CStr(index)), "0") & index & "}</UniqueIdentifier>" & vbCrLf & _
196 " </Filter>"
197 index = index + 1
198 End If
199 Case Else
200 If deep = skip Then
201 line = Replace(line, "/" ,"\")
202 if first_file <> 0 Then
203 vcxproj = vcxproj & vbCrLf
204 files = files & vbCrLf
205 Else
206 first_file = 1
207 End If
208 res = res & _
209 " <File" & vbCrLf & _
210 " RelativePath=" & Chr(34) & ".\..\src\" & line & Chr(34) & vbCrLf & _
211 " >" & vbCrLf & _
212 " </File>" & vbCrLf
213 Select Case Split(Line, ".")(1)
214 Case "cpp"
215 cltype = "ClCompile"
216 Case "rc"
217 cltype = "ResourceCompile"
218 Case Else
219 cltype = "ClInclude"
220 End Select
221 vcxproj = vcxproj & " <" & cltype & " Include="& Chr(34) & "..\src\" & line & Chr(34) & " />"
222 files = files & _
223 " <" & cltype & " Include="& Chr(34) & "..\src\" & line & Chr(34) & ">" & vbCrLf & _
224 " <Filter>" & filter & "</Filter>" & vbCrLf & _
225 " </" & cltype & ">"
226 End If
227 End Select
228 End If
229 Wend
230 res = res & " </Filter>"
231 file.Close()
232 load_main_data = res
233 End Function
235 Function load_lang_data(dir, ByRef vcxproj, ByRef files)
236 Dim res, folder, file, first_time
237 res = ""
238 Set folder = FSO.GetFolder(dir)
239 For Each file In folder.Files
240 file = FSO.GetFileName(file)
241 If file <> "english.txt" And FSO.GetExtensionName(file) = "txt" Then
242 file = Left(file, Len(file) - 4)
243 If first_time <> 0 Then
244 res = res & vbCrLf
245 vcxproj = vcxproj & vbCrLf
246 files = files & vbCrLf
247 Else
248 first_time = 1
249 End If
250 res = res & _
251 " <File" & vbCrLf & _
252 " RelativePath=" & Chr(34) & "..\src\lang\" & file & ".txt" & Chr(34) & vbCrLf & _
253 " >" & vbCrLf & _
254 " <FileConfiguration" & vbCrLf & _
255 " Name=" & Chr(34) & "Debug|Win32" & Chr(34) & vbCrLf & _
256 " >" & vbCrLf & _
257 " <Tool" & vbCrLf & _
258 " Name=" & Chr(34) & "VCCustomBuildTool" & Chr(34) & vbCrLf & _
259 " Description=" & Chr(34) & "Generating " & file & " language file" & Chr(34) & vbCrLf & _
260 " CommandLine=" & Chr(34) & "..\objs\strgen\strgen.exe -s ..\src\lang -d ..\bin\lang &quot;$(InputPath)&quot;&#x0D;&#x0A;exit 0&#x0D;&#x0A;" & Chr(34) & vbCrLf & _
261 " AdditionalDependencies=" & Chr(34) & "..\src\lang\english.txt;..\objs\strgen\strgen.exe" & Chr(34) & vbCrLf & _
262 " Outputs=" & Chr(34) & "..\bin\lang\" & file & ".lng" & Chr(34) & vbCrLf & _
263 " />" & vbCrLf & _
264 " </FileConfiguration>" & vbCrLf & _
265 " </File>"
266 vcxproj = vcxproj & _
267 " <CustomBuild Include=" & Chr(34) & "..\src\lang\" & file & ".txt" & Chr(34) & ">" & vbCrLf & _
268 " <Message Condition=" & Chr(34) & "'$(Configuration)|$(Platform)'=='Debug|Win32'" & Chr(34) & ">Generating " & file & " language file</Message>" & vbCrLf & _
269 " <Command Condition=" & Chr(34) & "'$(Configuration)|$(Platform)'=='Debug|Win32'" & Chr(34) & ">..\objs\strgen\strgen.exe -s ..\src\lang -d ..\bin\lang " & Chr(34) & "%(FullPath)" & Chr(34) & "</Command>" & vbCrLf & _
270 " <AdditionalInputs Condition=" & Chr(34) & "'$(Configuration)|$(Platform)'=='Debug|Win32'" & Chr(34) & ">..\src\lang\english.txt;..\objs\strgen\strgen.exe;%(AdditionalInputs)</AdditionalInputs>" & vbCrLf & _
271 " <Outputs Condition=" & Chr(34) & "'$(Configuration)|$(Platform)'=='Debug|Win32'" & Chr(34) & ">..\bin\lang\" & file & ".lng;%(Outputs)</Outputs>" & vbCrLf & _
272 " </CustomBuild>"
273 files = files & _
274 " <CustomBuild Include=" & Chr(34) & "..\src\lang\" & file & ".txt" & Chr(34) & ">" & vbCrLf & _
275 " <Filter>Translations</Filter>" & vbCrLf & _
276 " </CustomBuild>"
277 End If
278 Next
279 load_lang_data = res
280 End Function
282 Function load_settings_data(dir, ByRef vcxproj, ByRef command, ByRef files)
283 Dim res, folder, file, first_time
284 res = ""
285 command = "..\objs\settings\settings_gen.exe -o ..\objs\settings\table\settings.h -b ..\src\table\settings.h.preamble -a ..\src\table\settings.h.postamble"
286 Set folder = FSO.GetFolder(dir)
287 For Each file In folder.Files
288 file = FSO.GetFileName(file)
289 If FSO.GetExtensionName(file) = "ini" Then
290 if first_time <> 0 Then
291 res = res & vbCrLf
292 vcxproj = vcxproj & vbCrLf
293 files = files & vbCrLf
294 Else
295 first_time = 1
296 End If
297 res = res & _
298 " <File" & vbCrLf & _
299 " RelativePath=" & Chr(34) & "..\src\table\" & file & Chr(34) & vbCrLf & _
300 " >" & vbCrLf & _
301 " </File>"
302 vcxproj = vcxproj & _
303 " <None Include=" & Chr(34) & "..\src\table\" & file & Chr(34) & " />"
304 command = command & " ..\src\table\" & file
305 files = files & _
306 " <None Include=" & Chr(34) & "..\src\table\" & file & Chr(34) & ">" & vbCrLf & _
307 " <Filter>INI</Filter>" & vbCrLf & _
308 " </None>"
309 End If
310 Next
311 load_settings_data = res
312 End Function
314 Sub generate(data, dest, data2)
315 Dim srcfile, destfile, line
316 WScript.Echo "Generating " & FSO.GetFileName(dest) & "..."
317 Set srcfile = FSO.OpenTextFile(dest & ".in", 1, 0, 0)
318 Set destfile = FSO.CreateTextFile(dest, -1, 0)
320 If Not IsNull(data2) Then
321 ' Everything above the !!FILTERS!! marker
322 line = srcfile.ReadLine()
323 While line <> "!!FILTERS!!"
324 If len(line) > 0 Then destfile.WriteLine(line)
325 line = srcfile.ReadLine()
326 Wend
328 ' Our generated content
329 destfile.WriteLine(data2)
330 End If
332 ' Everything above the !!FILES!! marker
333 line = srcfile.ReadLine()
334 While line <> "!!FILES!!"
335 If len(line) > 0 Then destfile.WriteLine(line)
336 line = srcfile.ReadLine()
337 Wend
339 ' Our generated content
340 destfile.WriteLine(data)
342 ' Everything below the !!FILES!! marker
343 While Not srcfile.AtEndOfStream
344 line = srcfile.ReadLine()
345 If len(line) > 0 Then destfile.WriteLine(line)
346 Wend
347 srcfile.Close()
348 destfile.Close()
349 End Sub
351 Dim ROOT_DIR
352 ROOT_DIR = FSO.GetFolder("..").Path
353 If Not FSO.FileExists(ROOT_DIR & "/source.list") Then
354 ROOT_DIR = FSO.GetFolder(".").Path
355 End If
356 If Not FSO.FileExists(ROOT_DIR & "/source.list") Then
357 WScript.Echo "Can't find source.list, needed in order to make this run." _
358 & vbCrLf & "Please go to either the project dir, or the root dir of a clean SVN checkout."
359 WScript.Quit(1)
360 End If
362 safety_check ROOT_DIR & "/source.list"
363 headers_check ROOT_DIR & "/source.list", ROOT_DIR & "\src\" ' Backslashes needed for DoFiles
365 Dim openttd, openttdvcxproj, openttdfilters, openttdfiles
366 openttd = load_main_data(ROOT_DIR & "/source.list", openttdvcxproj, openttdfilters, openttdfiles)
367 generate openttd, ROOT_DIR & "/projects/openttd_vs80.vcproj", Null
368 generate openttd, ROOT_DIR & "/projects/openttd_vs90.vcproj", Null
369 generate openttdvcxproj, ROOT_DIR & "/projects/openttd_vs100.vcxproj", Null
370 generate openttdfiles, ROOT_DIR & "/projects/openttd_vs100.vcxproj.filters", openttdfilters
372 Dim lang, langvcxproj, langfiles
373 lang = load_lang_data(ROOT_DIR & "/src/lang", langvcxproj, langfiles)
374 generate lang, ROOT_DIR & "/projects/langs_vs80.vcproj", Null
375 generate lang, ROOT_DIR & "/projects/langs_vs90.vcproj", Null
376 generate langvcxproj, ROOT_DIR & "/projects/langs_vs100.vcxproj", Null
377 generate langfiles, ROOT_DIR & "/projects/langs_vs100.vcxproj.filters", Null
379 Dim settings, settingsvcxproj, settingscommand, settingsfiles
380 settings = load_settings_data(ROOT_DIR & "/src/table", settingsvcxproj, settingscommand, settingsfiles)
381 generate settings, ROOT_DIR & "/projects/settings_vs80.vcproj", settingscommand
382 generate settings, ROOT_DIR & "/projects/settings_vs90.vcproj", settingscommand
383 generate settingsvcxproj, ROOT_DIR & "/projects/settings_vs100.vcxproj", settingscommand
384 generate settingsfiles, ROOT_DIR & "/projects/settings_vs100.vcxproj.filters", Null