ValvePython / vpk

📦 Open, Search, Extract and Create VPKs in python
MIT License
161 stars 13 forks source link

Add support for reading and writing VPK archives without a dependency on the file system? #26

Open 0xFurtiv opened 3 years ago

0xFurtiv commented 3 years ago

In order to create a VPK archive using this project, the files must first exist in the OS's file system. If I read a file and perform some simple string transformations on it in-memory, I would currently need to write the file back to the OS's file system in order to create a VPK archive using this package.

It would be nice if we could create a VPK archive without depending on the OS's file system. This would allow for more flexibility with the project, especially if we could dependency inject a mock file system for unit tests.

One possible proposal is to change the NewVPK constructor API of this package to accept a file system object rather than a path to a directory. An example using PyFilesystem could look like

from fs import open_fs

with open_fs('./some/directory') as some_dir:
    vpk.new(some_dir).save("output.vpk")

PyFilesystem supports in-memory file systems, which could be used in place of open_fs('./some/directory') to enable unit tests that do not need to access the OS's file system. Support for creating VPK archives from ZIP and TAR archives for free seems like a neat bonus.

rossengeorgiev commented 3 years ago

That would be cool, but it will need a rewrite of the package. Alternatively, you can try replace the open with your own, which returns in-memory files. Keep in mind that most VPK for games, have 100s thousands of files, and even storing the file tree is very memory wasteful.

rossengeorgiev commented 2 years ago

Providing different fopen function is used in steamctl to files from inside a VPK directly from the Steam CDN. https://github.com/ValvePython/steamctl/blob/4d6b19a921a4db521ed6e0d05a1dae2458441a9a/steamctl/commands/depot/gcmds.py#L79-L86