diff --git a/lua/gpm/fixes.lua b/lua/gpm/fixes.lua index 943bac99..930a7610 100644 --- a/lua/gpm/fixes.lua +++ b/lua/gpm/fixes.lua @@ -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 diff --git a/lua/gpm/fs.lua b/lua/gpm/fs.lua index d0268fdd..a6780841 100644 --- a/lua/gpm/fs.lua +++ b/lua/gpm/fs.lua @@ -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 @@ -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 ) diff --git a/lua/gpm/init.lua b/lua/gpm/init.lua index 80b3bb37..c0ee0b87 100644 --- a/lua/gpm/init.lua +++ b/lua/gpm/init.lua @@ -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 ) diff --git a/lua/gpm/sources/http.lua b/lua/gpm/sources/http.lua index ef592c5c..10a7da7d 100644 --- a/lua/gpm/sources/http.lua +++ b/lua/gpm/sources/http.lua @@ -11,6 +11,7 @@ local util = util -- Variables local CompileString = CompileString +local logger = gpm.Logger local SERVER = SERVER local ipairs = ipairs local pairs = pairs @@ -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() @@ -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 ) diff --git a/lua/gpm/sources/lua.lua b/lua/gpm/sources/lua.lua index e91fb7ae..7408b29d 100644 --- a/lua/gpm/sources/lua.lua +++ b/lua/gpm/sources/lua.lua @@ -7,11 +7,11 @@ 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 @@ -19,38 +19,17 @@ 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 @@ -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