Make your Garry's Mod I/O asynchronous!
Garry's Mod still doesn't have file.AsyncWrite function,.
Also file.AsyncRead also doesn't work properly in menu state,
and Rubat doesn't want to elaborate in any way.
Also gm_async_write exists,
but it doesn't provide binaries for client-side and doesn't have binaries for MacOS.
- Go to Releases page
- Download the binary for your OS and realm
- Builds target the x86-64 Garry's Mod engine (
win64/linux64) gmsv_asyncio_*is loaded by servers and by the menu stategmcl_asyncio_*is loaded by the client state- Both files are the same binary; install whichever realms you need
- Builds target the x86-64 Garry's Mod engine (
- Place the binary in
<Garry's Mod>/garrysmod/lua/bin/ require("asyncio")
Works in server, client and menu state. In menu state, where the engine does not
define them, the module also defines the FSASYNC_* enums.
-- Asynchronously writes given content to fileName in DATA folder
-- Have same limitations as https://wiki.facepunch.com/gmod/file.Write
-- Arguments:
-- fileName: path to a file
-- content: data to be written to a file
-- callback: function that will be called when async operation finishes
-- Arguments are:
-- * fileName: string path to a file
-- * gamePath: always "DATA"
-- * status: see https://wiki.facepunch.com/gmod/Enums/FSASYNC
-- Returns:
-- status: FSASYNC_OK if successful, otherwise FSASYNC_ERR_
-- see https://wiki.facepunch.com/gmod/Enums/FSASYNC
number asyncio.AsyncWrite( string fileName, string content, function callback )
-- Same as asyncio.AsyncWrite, but instead of rewriting content of the file, just appends content to file
number asyncio.AsyncAppend( string fileName, string content, function callback )
-- Asynchronously reads content from a specified file
-- Have same limitations as https://wiki.facepunch.com/gmod/file.Read
-- Arguments:
-- fileName: path to a file
-- gamePath: see https://wiki.facepunch.com/gmod/File_Search_Paths
-- callback: function that will be called when async operation finishes
-- Arguments are:
-- * fileName: string path to a file
-- * gamePath: gamePath specified earlier
-- * status: see https://wiki.facepunch.com/gmod/Enums/FSASYNC
-- * data: contents of a file
-- Returns:
-- status: FSASYNC_OK if successful, otherwise FSASYNC_ERR_
-- see https://wiki.facepunch.com/gmod/Enums/FSASYNC
number asyncio.AsyncRead( string fileName, string gamePath, function callback )
-- Good luck with coding!local data = "Hello World!"
local ok = asyncio.AsyncWrite("example.txt", data, function(fileName, gamePath, status)
local ok = asyncio.AsyncRead(fileName, gamePath, function(fileName, gamePath, status, fileContent)
print(status, status == FSASYNC_OK) -- 0 true
print(fileContent) -- Hello World!
print(data == fileContent) -- true
end)
end)
print("Write successful:", ok == FSASYNC_OK) -- Write successful: trueThe module only uses headers from garrysmod_common and the x86-64 branch of
sourcesdk-minimal; it links no Source SDK libraries and finds the engine
filesystem at runtime. The SDK's IAppSystem must be cut to the engine's five
methods (see flake.nix or .github/workflows/build.yml).
nix build .#linux64 # gm{sv,cl}_asyncio_linux64.dll, runs the unit tests
nix build .#win64 # gm{sv,cl}_asyncio_win64.dll, clang-cl + MSVC SDK (xwin)Without Nix:
cmake -B build -S . -DGARRYSMOD_COMMON_PATH=<garrysmod_common> -DSOURCESDK_MINIMAL_PATH=<patched sourcesdk-minimal>
cmake --build build && ctest --test-dir buildtests/lua/asyncio_roundtrip.lua is a live test: include it in any realm and
read ASYNCIO_TEST once done is true.
Feel free to create issues and pull request ❤️