A robust Lua library for parsing Valve GUI (VGUI) resource files, specifically designed for LMAOBOX and Lua 5.1. This library provides a complete toolkit for lexing, parsing, and manipulating VGUI resource files with support for preprocessing directives.
- Full VGUI resource file parsing
- Support for #base preprocessing directives
- Recursive file inclusion handling
- Flag and property merging
- Object-oriented design
- Whitespace and comment handling
- Robust error handling
- File system traversal utilities
- Download
vgui.lua
and place it in your LMAOBOX scripts directory - Require the module in your script:
local vgui = require('vgui')
-- Parse a VGUI resource file
local obj = vgui.VguiSerializer.fromFile("tf/custom/my_hud", "basechat.res")
-- Access properties
local property = obj:get("propertyName")
-- Check if object has a value
if obj:isValue() then
print(obj.value)
end
-- Find and parse basechat.res in custom directory
local function findBaseChatRes(directory)
local baseChat = nil
filesystem.EnumerateDirectory(directory .. "/*", function(filename, attributes)
if attributes & E_FileAttribute.FILE_ATTRIBUTE_DIRECTORY ~= 0 then
if filename ~= "." and filename ~= ".." then
local result = findBaseChatRes(directory .. "/" .. filename)
if result then
baseChat = result
end
end
elseif filename:lower() == "basechat.res" then
baseChat = directory .. "/" .. filename
end
end)
return baseChat
end
local function printVguiObject(obj, indent)
indent = indent or 0
local spacing = string.rep(" ", indent)
for name, prop in pairs(obj.properties) do
if prop:isValue() then
print(spacing .. name .. " = " .. tostring(prop.value))
else
print(spacing .. name .. " {")
printVguiObject(prop, indent + 1)
print(spacing .. "}")
end
end
end
Enumeration of token types used in lexical analysis:
- LeftBrace
- RightBrace
- Flag
- String
- Name
Main class for representing VGUI elements:
- Properties management
- Flag comparison
- Property merging
- Value handling
Handles tokenization of input text:
- Whitespace skipping
- Comment handling
- String parsing
- Name and flag token generation
Converts token stream into structured data:
- Value parsing
- Property list handling
- Root object construction
Handles file inclusion and preprocessing:
- #base directive support
- Recursive file processing
- Source provider interface
-- Compare flags between objects
local matches = obj1:compareFlags(obj2)
-- Get name and flags as a key
local key = obj:getNameFlagKey()
-- Merge properties from another object
local success = obj1:tryMerge(obj2)
-- Add or merge a single property
obj:mergeOrAddProperty(property)
The library includes robust error handling for common issues:
- Missing files
- Invalid syntax
- Malformed tokens
- Recursive inclusion loops
- Lua 5.1
- LMAOBOX
- File system access permissions
- Only supports VGUI resource file format
- Requires LMAOBOX environment
- Limited to local filesystem access
https://github.com/dresswithpockets/Vgui - For inspiration