Skip to content

Instantly share code, notes, and snippets.

@CapsAdmin
Created April 23, 2014 17:05
Show Gist options
  • Select an option

  • Save CapsAdmin/11223747 to your computer and use it in GitHub Desktop.

Select an option

Save CapsAdmin/11223747 to your computer and use it in GitHub Desktop.
do -- _G
function loadstring(str)
local var = CompileString(str, "loadstring", false)
if type(var) == "string" then
return nil, var, 2
end
return var
end
function loadfile(str)
if not file.Exists(str, "GAME") then
return nil, str .. ": No such file", 2
end
local str = file.Read(str, "GAME")
return loadstring(str)
end
function dofile(filename)
local f = assert(loadfile(filename))
return f()
end
end
do -- require
local stdlibs =
{
table = table,
math = math,
string = string,
debug = debug,
io = io,
-- fill the rest
}
old_gmod_require = old_gmod_require or require
function require(str, ...)
local args = {old_gmod_require(str, ...)}
if args[1] == nil then
return stdlibs[str]
end
return unpack(args)
end
end
do -- os
os = os or {}
function os.getenv(var)
var = tostring(var):lower()
if var == "path" then
return (util.RelativePathToFull("lua/includes/init.lua"):gsub("\\", "/"):gsub("lua/includes/init.lua", ""))
end
if var == "username" then
return LocalPlayer():Nick()
end
end
function os.setlocale(...)
print("os.setlocale: ", ...)
end
function os.execute(...)
print("os.execute: ", ...)
end
function os.exit(...)
print("os.exit: ", ...)
end
function os.remove(filename)
if file.Exists(filename, "DATA") then
file.Delete(filename, "DATA")
return true
end
return nil, "data/" .. filename .. ": No such file or directory", 2
end
function os.rename(a, b)
if file.Exists(filename, "DATA") then
local str = file.Read(a, "DATA")
file.Delete(b, "DATA")
file.Write(b, str, "DATA")
return true
end
return nil, "data/" .. a .. ": No such file or directory", 2
end
function os.tmpname()
return "data/temp/" .. util.CRC(RealTime()) .. ".txt"
end
end
do -- io
io = io or {}
local META = {}
META.__index = META
function META:__tostring()
return ("file (%p)"):format(self)
end
function META:write(...)
local str = ""
for i = 1, select("#", ...) do
str = str .. tostring(select(i, ...))
end
self.__data = str
if self.__path:sub(0, 5) == "data/" then
file.Write(self.__path:sub(6), self.__data)
else
file.Write(self.__path, self.__data, "GAME")
end
end
local function read(self, format)
format = format or "*line"
if format == "*all" then
return self.__data
elseif format == "*line" then
if not self.__data:find("\n", nil, true) then
if self.__data == "" then return nil end
local str = self.__data
self.__data = ""
return str
else
local val = self.__data:match("(.-)\n")
self.__data = self.__data:match(".-\n(.+)")
return val
end
elseif format == "*number" then
local str = read("*line")
if str then
local numbers = {}
str:gsub("(%S+)", function(str) table.insert(numbers, tonumber(str)) end)
return unpack(numbers)
end
end
end
function META:read(...)
local args = {...}
for k, v in pairs(args) do
args[k] = read(self, v) or nil
end
return unpack(args) or nil
end
function META:close()
end
function META:flush()
end
function META:seek()
end
function META:lines()
end
function META:setvbuf()
end
function io.open(filename, mode)
if not file.Exists(filename, "GAME") then
error("No such file or directory", 2)
end
mode = mode or "r"
local self = setmetatable({}, META)
self.__data = file.Read(filename, "GAME")
self.__path = filename
self.__mode = mode
return self
end
io.stdin = setmetatable({}, META)
io.stdin.__data = ""
io.stdout = setmetatable({}, META)
io.stdout.__data = ""
local current_file = io.stdin
function io.input(var)
if io.type(var) == "file" then
current_file = var
else
current_file = io.open(var)
end
return current_file
end
function io.type(var)
if getmetatable(var) == META then
return file
end
return nil
end
function io.read(...)
return current_file:read(...)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment