Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown-gd committed Apr 15, 2023
1 parent a2b0a68 commit e95b5c0
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 43 deletions.
41 changes: 41 additions & 0 deletions lua/gpm/fixes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,47 @@ do

end

-- https://wiki.facepunch.com/gmod/File_Search_Paths
local luaRealm = gpm.LuaRealm
if not luaRealm then
luaRealm = "LUA"

if not MENU_DLL then
if SERVER then
luaRealm = "lsv"
elseif CLIENT then
luaRealm = "lcl"
end
end

gpm.LuaRealm = luaRealm
end

-- https://wiki.facepunch.com/gmod/Global.CompileFile
do

local _CompileFile = table.SetValue( gluaFixes, "CompileFile", CompileFile, true )
local CompileString = CompileString

function CompileFile( filePath )
local f = file.Open( filePath, "r", luaRealm )
if not f then
return _CompileFile( filePath )
end

local code = f:Read( f:Size() )
f:Close()

local func = CompileString( code, filePath, true )
if not func then
return _CompileFile( filePath )
end

return func
end

end

-- https://wiki.facepunch.com/gmod/util.IsBinaryModuleInstalled
do

Expand Down
13 changes: 13 additions & 0 deletions lua/gpm/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ local string = string
local file = file

-- Variables
local CompileString = CompileString
local ipairs = ipairs
local pcall = pcall

-- https://github.com/WilliamVenner/gm_async_write
if not file.AsyncWrite and util.IsBinaryModuleInstalled( "async_write" ) then require( "async_write" ) end
Expand Down Expand Up @@ -79,6 +81,17 @@ function AsyncRead( filePath, gamePath )
return p
end

Compile = promise.Async( function( filePath, gamePath, handleError )
local ok, result = AsyncRead( filePath, gamePath ):SafeAwait()
if not ok then return promise.Reject( result ) end

local ok, result = pcall( CompileString, result.content, result.filePath, handleError )
if not ok then return promise.Reject( result ) end
if not result then return promise.Reject( "file compilation failed" ) end

return result
end )

if not file.AsyncWrite or not file.AsyncAppend then

function AsyncWrite( filePath, content )
Expand Down
2 changes: 1 addition & 1 deletion lua/gpm/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CreateConVar( "gpm_cache_lifetime", "24", FCVAR_ARCHIVE, " - the cache lifetime,

module( "gpm" )

_VERSION = 010101
_VERSION = 010200

-- Include function
function includeShared( filePath )
Expand Down
21 changes: 10 additions & 11 deletions lua/gpm/sources/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local util = util

-- Variables
local CompileString = CompileString
local logger = gpm.Logger
local SERVER = SERVER
local ipairs = ipairs
local pairs = pairs
Expand Down Expand Up @@ -42,17 +43,12 @@ Import = promise.Async( function( url, parentPackage )
return sources.gmad.Import( "data/" .. cachePath, parentPackage )
end

local code = fs.Read( cachePath, "DATA" )
if code then
local ok, result = pcall( CompileString, code, cachePath )
if not ok then return promise.Reject( result ) end
if not result then return promise.Reject( "file compilation failed" ) end
return packages.Initialize( packages.GetMetaData( {
["name"] = packageName
} ), result, {}, parentPackage )
end
local ok, result = fs.Compile( cachePath, "DATA" ):SafeAwait()
if not ok then return promise.Reject( result ) end

return promise.Reject( "file reading failed" )
return packages.Initialize( packages.GetMetaData( {
["name"] = packageName
} ), result, {}, parentPackage )
end

local ok, result = http.Fetch( url, nil, 120 ):SafeAwait()
Expand All @@ -63,10 +59,13 @@ Import = promise.Async( function( url, parentPackage )
local code = result.body
local metadata = util.JSONToTable( code )
if not metadata then
fs.AsyncWrite( cachePath, code )
local ok, err = fs.AsyncWrite( cachePath, code ):SafeAwait()
if not ok then logger:Error( err ) end

local ok, result = pcall( CompileString, code, url )
if not ok then return promise.Reject( result ) end
if not result then return promise.Reject( "file compilation failed" ) end

return packages.Initialize( packages.GetMetaData( {
["name"] = packageName
} ), result, {}, parentPackage )
Expand Down
41 changes: 10 additions & 31 deletions lua/gpm/sources/lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,29 @@ local string = string
local fs = gpm.fs

-- Variables
local CLIENT, SERVER, MENU_DLL = CLIENT, SERVER, MENU_DLL
local CompileString = CompileString
local CLIENT, SERVER = CLIENT, SERVER
local AddCSLuaFile = AddCSLuaFile
local setmetatable = setmetatable
local CompileFile = CompileFile
local luaRealm = gpm.LuaRealm
local ipairs = ipairs
local rawset = rawset
local pcall = pcall
local type = type

module( "gpm.sources.lua" )

LuaRealm = "LUA"

if not MENU_DLL then
if SERVER then
LuaRealm = "lsv"
elseif CLIENT then
LuaRealm = "lcl"
end
end

function CanImport( filePath )
return fs.Exists( filePath, LuaRealm ) and string.EndsWith( filePath, ".lua" ) or fs.IsDir( filePath, LuaRealm )
return fs.Exists( filePath, luaRealm ) and string.EndsWith( filePath, ".lua" ) or fs.IsDir( filePath, luaRealm )
end

Files = setmetatable( {}, {
["__index"] = function( self, filePath )
if type( filePath ) == "string" and fs.Exists( filePath, LuaRealm ) and string.EndsWith( filePath, ".lua" ) then
local code, func = fs.Read( filePath, LuaRealm ), nil
if code then
func = CompileString( code, filePath )
end

if not func then
local ok, result = pcall( CompileFile, filePath )
if ok then
func = result
end
end

if func ~= nil then
rawset( self, filePath, func )
return func
if type( filePath ) == "string" and string.EndsWith( filePath, ".lua" ) and fs.Exists( filePath, luaRealm ) then
local ok, result = pcall( CompileFile, filePath )
if ok then
rawset( self, filePath, result )
return result
end
end

Expand Down Expand Up @@ -127,11 +106,11 @@ Import = promise.Async( function( filePath, parentPackage )
local send = metadata.send
if send ~= nil then
for _, filePath in ipairs( send ) do
if not fs.Exists( filePath, LuaRealm ) then
if not fs.Exists( filePath, luaRealm ) then
filePath = paths.Join( packagePath, filePath )
end

if fs.Exists( filePath, LuaRealm ) then
if fs.Exists( filePath, luaRealm ) then
AddCSLuaFile( filePath )
end
end
Expand Down

0 comments on commit e95b5c0

Please sign in to comment.