• R/O
  • SSH

dwaddle: Commit

Main Dwaddle repository


Commit MetaInfo

Revision6761d61f6cb65ead8948893a6ac60eb945d60d99 (tree)
Time2021-05-25 20:46:27
AuthorRemilia Scarlet <remilia@post...>
CommiterRemilia Scarlet

Log Message

Ensure lump data is loaded during a save. Also use a temporary file
when saving to minimize chance of data loss.

Change Summary

Incremental Difference

diff -r 7aeee144cb71 -r 6761d61f6cb6 src/wadfile.cr
--- a/src/wadfile.cr Fri May 21 16:48:35 2021 -0600
+++ b/src/wadfile.cr Tue May 25 05:46:27 2021 -0600
@@ -159,6 +159,11 @@
159159 end
160160 end
161161
162+ def unloadData
163+ @data = Bytes.new(0)
164+ @dataLoaded = false
165+ end
166+
162167 protected def loadData()
163168 @dataMut.synchronize do
164169 if @parent.nil?
@@ -177,14 +182,18 @@
177182
178183 protected def saveData(outStr : IO)
179184 @dataMut.synchronize do
180- @initSize = @data.size.to_u32!
185+ wasLoaded = @dataLoaded
186+
187+ @initSize = data.size.to_u32!
181188 if @initSize > 0
182189 outStr.flush
183190 @offset = outStr.pos.to_u32
184- outStr.write(@data)
191+ outStr.write(self.data)
185192 else
186193 @offset = 0
187194 end
195+
196+ unloadData unless wasLoaded
188197 end
189198 end
190199
@@ -539,17 +548,17 @@
539548
540549 # Saves the wad to the specified filename. If `assignFilename` is true,
541550 # `filename` becomes the filename that is is associated with the wad.
542- def save(newFilename, assignFilename = false)
551+ def save(newFilename, assignFilename = false, *, backup : String? = nil)
543552 if assignFilename
544553 self.filename = newFilename
545554 end
546555
547- saveToFile(newFilename)
556+ saveToFile(newFilename, backup)
548557 end
549558
550559 # Performs the actual saving of the wad.
551- private def saveToFile(filename : String)
552- File.open(filename, "w+") do |file|
560+ private def saveToFile(filename : String, backup : String? = nil)
561+ tempfile = File.tempfile("dwaddle-", ".wad") do |file|
553562 case @wadType
554563 when WadType::IWAD then file << "IWAD"
555564 when WadType::PWAD then file << "PWAD"
@@ -572,6 +581,14 @@
572581 file.write_bytes(infoTableOff.to_u32, IO::ByteFormat::LittleEndian)
573582 file.flush
574583 end
584+
585+ if File.exists?(filename)
586+ File.copy(filename, backup) if backup
587+ File.delete(filename)
588+ end
589+
590+ File.copy(tempfile.path, filename)
591+ File.delete(tempfile.path)
575592 end
576593
577594 # Searches for UDMF-format levels within the wad and returns the indices of
Show on old repository browser