H-uru / Plasma

Cyan Worlds's Plasma game engine
http://h-uru.github.io/Plasma/
GNU General Public License v3.0
205 stars 81 forks source link

Close `hsStream`s automatically in destructors #1448

Closed dgelessus closed 1 year ago

dgelessus commented 1 year ago

A step towards RAII for hsStream. This makes streams much easier to manage correctly, especially when using std::unique_ptr, collections and such.

In fact, I removed hsStream::Close entirely, because almost nothing closed a stream without destroying it soon afterwards. Sometimes this was done to reuse a single stream object for multiple files, but it's not hard to recreate the stream for each file instead.

This isn't "proper full RAII", because opening a stream is still its own method call and not part of the constructor. I wasn't sure how to handle error reporting and virtual Open calls if the constructor does the opening. This isn't a big problem though I think - the automatic closing is the important part.

The diff is long, but it's mostly boring deleting of all the manual Close calls. The actually interesting changes are in hsStream and its implementations. The updated logic in plRegistryNode, plStreamSource, and pfPatcher could use some extra eyes perhaps.