Skip to content

Instantly share code, notes, and snippets.

@danielga
Created December 14, 2015 03:33
Show Gist options
  • Select an option

  • Save danielga/512893c61fdeb71843fd to your computer and use it in GitHub Desktop.

Select an option

Save danielga/512893c61fdeb71843fd to your computer and use it in GitHub Desktop.
A pure Lua module for Garry's Mod that provides the same functionality as the gm_vfs module by wrapping gm_filesystem.
--[[
gm_filesystem wrapper for gm_vfs
A pure Lua module for Garry's Mod that provides the same functionality
as the gm_vfs module by wrapping gm_filesystem.
-----------------------------------------------------------------------
Copyright (c) 2015, Daniel Almeida
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]
require("filesystem")
VFS_SEEK_SET = filesystem.SEEK_SET
VFS_SEEK_CURRENT = filesystem.SEEK_CUR
VFS_SEEK_END = filesystem.SEEK_END
VFS_WRITE_DATA = 0
VFS_WRITE_STRING = 1
VFS_WRITE_UINT32 = 2
VFS_WRITE_UINT16 = 3
VFS_WRITE_UINT8 = 4
VFS_WRITE_FLOAT = 5
VFS_WRITE_DOUBLE = 6
VFS_WRITE_BYTE = 7
VFS_READ_STRING = 1
VFS_READ_UINT32 = 2
VFS_READ_UINT16 = 3
VFS_READ_UINT8 = 4
VFS_READ_FLOAT = 5
VFS_READ_DOUBLE = 6
VFS_READ_BYTE = 7
vfs = {}
function vfs.Close(handle)
handle:Close()
end
function vfs.Write(handle, write_type, data)
if write_type == VFS_WRITE_DATA then
return handle:Write(data)
elseif write_type == VFS_WRITE_STRING then
return handle:WriteString(data)
elseif write_type == VFS_WRITE_UINT32 then
return handle:WriteUInt(data, 32)
elseif write_type == VFS_WRITE_UINT16 then
return handle:WriteUInt(data, 16)
elseif write_type == VFS_WRITE_UINT8 or write_type == VFS_WRITE_BYTE then
return handle:WriteUInt(data, 8)
elseif write_type == VFS_WRITE_FLOAT then
return handle:WriteFloat(data)
elseif write_type == VFS_WRITE_DOUBLE then
return handle:WriteDouble(data)
end
error("unrecognized value type")
end
function vfs.Read(handle, read_type)
if read_type == VFS_WRITE_STRING then
return handle:ReadString()
elseif read_type == VFS_WRITE_UINT32 then
return handle:ReadUInt(32)
elseif read_type == VFS_WRITE_UINT16 then
return handle:ReadUInt(16)
elseif read_type == VFS_WRITE_UINT8 or read_type == VFS_WRITE_BYTE then
return handle:ReadUInt(8)
elseif read_type == VFS_WRITE_FLOAT then
return handle:ReadFloat()
elseif read_type == VFS_WRITE_DOUBLE then
return handle:ReadDouble()
end
error("unrecognized value type")
end
function vfs.Seek(handle, pos, method)
return handle:Seek(pos, method)
end
function vfs.Tell(handle)
return handle:Tell()
end
function vfs.EOF(handle)
return handle:EOF()
end
function vfs.RemoveFile(filepath)
return filesystem.Remove(filepath, "VFS")
end
function vfs.CRC32(handle)
print(debug.traceback("gm_vfs wrapper tried to call vfs.CRC32"))
end
local meta = {
Close = function(self) return vfs.Close(self.handle) end,
Write = function(self, write_type, data) return vfs.Write(self.handle, write_type, data) end,
Read = function(self, read_type) return vfs.Read(self.handle, read_type) end,
Seek = function(self, pos, method) return vfs.Seek(self.handle, pos, method) end,
Tell = function(self) return vfs.Tell(self.handle) end,
EOF = function(self) return vfs.EOF(self.handle) end,
CRC32 = function(self) return vfs.CRC32(self.handle) end
}
function vfs.Open(filepath, mode)
local handle = filesystem.Open(filepath, mode, "VFS")
if IsValid(handle) then
return setmetatable({handle = handle}, meta)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment