Skip to content

Instantly share code, notes, and snippets.

@AbigailBuccaneer
Created January 25, 2015 02:41
Show Gist options
  • Save AbigailBuccaneer/e0f786861a7471dbc408 to your computer and use it in GitHub Desktop.
Save AbigailBuccaneer/e0f786861a7471dbc408 to your computer and use it in GitHub Desktop.
Run Garry's Mod Lua code outside of Garry's Mod
--
-- prelude.lua - simple implementation of Garry's Mod Lua functionality
--
function AddCSLuaFile() end
function Vector() end
function Angle() end
function loadfile(filename)
local file, error = io.open(filename, "r")
if not file then return nil, error end
local source = file:read("*all")
file:close()
if not source then return nil, "couldn't read source!" end
-- TODO this isn't perfect and could do with some proper tokenization
source = source:gsub("!=", " ~= ")
source = source:gsub("!", " not ")
source = source:gsub("&&", " and ")
source = source:gsub("||", " or ")
source = source:gsub(";", "\n")
-- TODO this isn't true but would be if all loop bodies
-- were wrapped with "repeat until false"
source = source:gsub("continue", "break")
return loadstring(source, "@" .. filename)
end
function dofile(filename)
local thunk = assert(loadfile(filename))
return thunk()
end
function include(name)
local path = debug.getinfo(2, "S").source
assert(path:sub(1, 1) == "@") -- we can't include if we can't get the current path
path = path:sub(2):gsub("/[^/]*$", "")
dofile(path .. "/" .. name)
end
-- Replace the standard Lua package loader with one that (mostly) deals
-- with Garry's additions to Lua.
package.loaders[2] = function(name)
local errors = {}
name = string.gsub(name, '%.', '/')
for path in string.gmatch(package.path, '[^;]+') do
path = string.gsub(path, '%?', name)
local thunk, error = loadfile(path)
if thunk then return thunk end
table.insert(errors, error)
end
return table.concat(errors, '\n') .. '\n'
end
require "includes.extensions.table"
require "includes.util"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment