Implemented .WAgame import support

This commit is contained in:
Luxferre
2024-10-12 15:29:51 +03:00
parent d9ef9e5062
commit 2babede737
2 changed files with 34 additions and 9 deletions
+30 -7
View File
@@ -375,13 +375,34 @@ proc fullscanscheme {version} {
# load a scheme file into the internal structure
proc loadscheme {fname} {
global rawschemedata wsc_data
set fp [open $fname r]
fconfigure $fp -translation binary
set rawschemedata [read $fp]
close $fp
if [string match *.wagame [string tolower $fname]] { # extract from the replay file
set fp [open $fname r]
fconfigure $fp -translation binary
set replaydata [read $fp]
close $fp
set schemestart [string first "SCHM" $replaydata]
if {$schemestart < 0} {
tk_messageBox -type ok -title "Scheme loading error" -icon error -message "No scheme found in the replay file!"
return
}
# read the version
set offs [expr {$schemestart + 4}]
set scanval [string index $replaydata $offs]
binary scan $scanval cu scmver
# set the target size based on the version
set datasize 221
if {$scmver eq 2} {set datasize 297}
if {$scmver eq 3} {set datasize 407}
set rawschemedata [string range $replaydata $schemestart [expr {$schemestart + $datasize - 1}]]
} else { # read raw scheme data directly
set fp [open $fname r]
fconfigure $fp -translation binary
set rawschemedata [read $fp]
close $fp
}
set hdr ""
set ver ""
binary scan $rawschemedata a4c hdr ver
binary scan $rawschemedata a4cu hdr ver
if {$hdr eq {SCHM} && ($ver eq 1 || $ver eq 2 || $ver eq 3)} {
# proceed with full scanning
fullscanscheme $ver
@@ -406,7 +427,7 @@ proc savescheme {fname} {
if {$diffbytes > 0} { # expand with nulls
set rawschemedata [string cat $rawschemedata [binary format x$diffbytes]]
} else { # truncate
set rawschemedata [string range 0 [expr {$datasize - 1}]
set rawschemedata [string range $rawschemedata 0 [expr {$datasize - 1}]
}
# write the version itself
set binval [binary format cu $wsc_data(version)]
@@ -471,14 +492,16 @@ proc savescheme {fname} {
}
# open a .wsc scheme file (get the target name)
# or a .WAgame replay file
proc openwsc {} {
global wscfile appdir
set startdir $appdir
if {$wscfile ne ""} {set startdir [file dirname $wscfile]}
set types {
{{Worms: Armageddon game scheme} {.wsc} BINA}
{{Worms: Armageddon replay} {.WAgame} BINA}
}
return [tk_getOpenFile -filetypes $types -initialdir $startdir -title {Open a Worms scheme file}]
return [tk_getOpenFile -filetypes $types -initialdir $startdir -title {Open a Worms scheme or replay file}]
}
# save a .wsc scheme file (get the target name)