Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- do
- doscript('/lua/ui/texture_tools.lua')
- GlobalTables = {}
- local InitDone = false
- local previmport = import
- local LuaPopulated = false
- gsub = string.gsub
- lower = string.lower
- function CreateGlobalTable(tableName)
- if tableName then
- if not GlobalTables[tableName] then
- GlobalTables[tableName] = {}
- end
- end
- end
- function RemoveGlobalTable(tableName)
- if tableName then
- if GlobalTables[tableName] then
- GlobalTables[tableName] = nil
- end
- end
- end
- function GetGlobalTable(tableName)
- if GlobalTables[tableName] then
- return table.deepcopy(GlobalTables[tableName])
- else
- return nil
- end
- end
- function ResetGlobalTable(tableName)
- if tableName then
- if GlobalTables[tableName] then
- GlobalTables[tableName] = {}
- end
- end
- end
- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- function ResetModTables(ResetHookedFiles)
- GlobalTables['ActiveModUids'] = nil
- GlobalTables['ActiveModIdentifiers'] = nil
- GlobalTables['ActiveModNames'] = nil
- GlobalTables['ActiveMods'] = nil
- GlobalTables['ActiveModLocations'] = nil
- GlobalTables['PreviousMods'] = nil
- if ResetHookedFiles then
- CreateGlobalTable('PrevHookedFiles')
- GlobalTables['PrevHookedFiles'] = GlobalTables['HookedFiles']
- GlobalTables['HookedFiles'] = nil
- end
- end
- function ResetLuaTables()
- GlobalTables['LuaFileNames'] = nil
- GlobalTables['LuaFilePaths'] = nil
- end
- -------------------------------------------------------------------------------------------------------------
- function IsSim()
- local result
- if not rawget(_G, 'GetCurrentUIState') then
- result = true
- else
- result = false
- end
- return result
- end
- function CreateDataTables()
- CreateGlobalTable('ActiveMods')
- CreateGlobalTable('ActiveModIdentifiers')
- CreateGlobalTable('ActiveModNames')
- CreateGlobalTable('ActiveModLocations')
- CreateGlobalTable('PreviousMods')
- CreateGlobalTable('ModBasePaths')
- CreateGlobalTable('HookedFiles')
- end
- function InitModData(Reset)
- if Reset then
- ResetModTables(true)
- end
- if IsSim() then
- if not GlobalTables['ActiveModUids'] then
- local activeModUIds = {}
- CreateDataTables()
- for id, Params in __active_mods do
- activeModUIds[Params.uid] = true
- GlobalTables['ActiveModLocations'][Params.uid] = Params.location
- end
- GlobalTables['ActiveMods'] = __active_mods
- GlobalTables['ActiveModUids'] = activeModUIds
- GlobalTables['PreviousMods'] = activeModUIds
- end
- else
- if not GlobalTables['ActiveModUids'] then
- local ActiveMods = GetPreference('active_mods') or {}
- CreateDataTables()
- GlobalTables['ActiveModUids'] = ActiveMods
- GlobalTables['PreviousMods'] = ActiveMods
- for i,file in DiskFindFiles('/mods', '*mod_info.lua') do
- local Location = Dirname(file)
- local env = {
- location = Location,
- name = file,
- description = "<LOC uimod_0006>(No description)",
- author = '',
- copyright = '',
- exclusive = false,
- icon = '/textures/ui/common/dialogs/mod-manager/generic-icon_bmp.dds',
- selectable = true,
- hookdir = '/hook',
- shadowdir = '/shadow',
- uid = file,
- }
- local ok, result = pcall(doscript, file, env)
- if ok then
- if ActiveMods[env.uid] and (env.enabled != false) then
- env.location = Location
- GlobalTables['ActiveMods'][env.uid] = env
- GlobalTables['ActiveModLocations'][env.uid] = Location
- if env.identifier then
- GlobalTables['ActiveModIdentifiers'][env.identifier] = Location
- end
- end
- else
- WARN("Problem loading " .. file .. ":\n" .. result)
- end
- end
- end
- end
- InitDone = true
- if Reset then
- Populate_Lua_Files(true)
- Populate_Textures(true)
- end
- end
- function GetLuaFolderParams(ReturnWhat)
- local ignoreBaseFolders = { '/mods', '/schook', '/textures', '/frontend' }
- local removeBasePathParts = { '/frontend', }
- local ignoreModFolders = { '/hook', '/shadow', '/textures', }
- local removeModPathParts = { '/frontend', '/modhook', }
- if not ReturnWhat then
- return ignoreBaseFolders, removeBasePathParts, ignoreModFolders, removeModPathParts
- elseif ReturnWhat == 'Base' then
- return ignoreBaseFolders, removeBasePathParts
- elseif ReturnWhat == 'Mods' then
- return ignoreModFolders, removeModPathParts
- elseif ReturnWhat == 'IgnoreBase' then
- return ignoreBaseFolders
- elseif ReturnWhat == 'RemoveBase' then
- return removeBasePathParts
- elseif ReturnWhat == 'IgnoreMods' then
- return ignoreModFolders
- elseif ReturnWhat == 'RemoveMod' then
- return removeModPathParts
- end
- end
- function Populate_Lua_Files(Reset)
- if Reset then
- ResetLuaTables()
- end
- CreateGlobalTable('LuaFileNames')
- CreateGlobalTable('LuaFilePaths')
- local ignoreBaseFolders, removeBasePathParts, ignoreModFolders, removeModPathParts = GetLuaFolderParams()
- local removeBaseTexturePathParts = { '/textures', '/ui', }
- local FrontEndFiles = {}
- --dump all base lua in the table, baring the above folders.
- for k,file in DiskFindFiles('/', '*.lua') do
- local Path = lower(file)
- local FileName = Basename(Path, false)
- local FolderName = Dirname(Path)
- local ProcessLua = true
- local inFolder = false
- for id, partPath in ignoreBaseFolders do
- if string.find(FolderName, partPath, 1) then
- ProcessLua = false
- inFolder = partPath
- break
- end
- end
- if inFolder and not ProcessLua then
- if inFolder == '/textures' or inFolder == '/ui' then
- local LeadingPath = FolderName
- for id,pathPart in removeBaseTexturePathParts do
- LeadingPath = string.gsub(LeadingPath,pathPart,"")
- end
- FolderName = LeadingPath
- ProcessLua = true
- elseif inFolder == '/frontend' then
- table.insert(FrontEndFiles, Path)
- end
- end
- FolderName = ParseBaseRedirectPath(FolderName)
- local JoinPath = FolderName .. '/' .. FileName
- if ProcessLua then
- GlobalTables.LuaFilePaths[JoinPath] = { OriginalFile = Path }
- end
- end
- for id, file in FrontEndFiles do
- local Path = lower(file)
- local FileName = Basename(Path, false)
- local FolderName = Dirname(Path)
- local ProcessLua = true
- FolderName = string.gsub(FolderName,'/frontend',"")
- local JoinPath = FolderName .. '/' .. FileName
- local ExistingFilePath = GlobalTables.LuaFilePaths[JoinPath].OriginalFile
- if not ExistingFilePath then
- GlobalTables.LuaFilePaths[JoinPath] = { OriginalFile = Path }
- else
- if JoinPath == ExistingFilePath then
- if not GlobalTables.LuaFilePaths[JoinPath].Hooks then
- GlobalTables.LuaFilePaths[JoinPath].Hooks = {}
- end
- table.insert(GlobalTables.LuaFilePaths[JoinPath].Hooks, Path)
- end
- end
- end
- --dump all mod lua into the table, baring the above folders.
- for uid, location in GlobalTables.ActiveModLocations do
- for k,file in DiskFindFiles(location, '*.lua') do
- local Path = string.lower(file)
- local FileName = Basename(Path, false)
- local FolderName = Dirname(Path)
- local ProcessLua = true
- for id, partPath in ignoreModFolders do
- if string.find(FolderName, partPath, 1) then
- ProcessLua = false
- break
- end
- end
- local JoinPath = FolderName .. '/' .. FileName
- if ProcessLua then
- local LeadingPath = string.gsub(JoinPath,location,"")
- for id,pathPart in removeModPathParts do
- LeadingPath = string.gsub(LeadingPath,pathPart,"")
- end
- LeadingPath = ParseModRedirectPath(LeadingPath)
- local ExistingFilePath = GlobalTables.LuaFilePaths[LeadingPath].OriginalFile
- if not ExistingFilePath then
- GlobalTables.LuaFilePaths[LeadingPath] = { OriginalFile = Path }
- else
- if LeadingPath == ExistingFilePath then
- if not GlobalTables.LuaFilePaths[LeadingPath].Hooks then
- GlobalTables.LuaFilePaths[LeadingPath].Hooks = {}
- end
- table.insert(GlobalTables.LuaFilePaths[LeadingPath].Hooks, Path)
- end
- end
- end
- end
- end
- LuaPopulated = true
- end
- function ParseBaseRedirectPath(FilePath)
- return FilePath
- end
- function ParseModRedirectPath(FilePath)
- if FilePath then
- local FileRedirects = { ['/lua/scripttasks'] = '/lua/sim/tasks/' }
- local FolderName = Dirname(FilePath)
- local RedirectPath = FileRedirects[FolderName]
- if RedirectPath then
- return RedirectPath .. Basename(FilePath, false)
- else
- return FilePath
- end
- else
- return nil
- end
- end
- function GetLuaFile(FilePath)
- if FilePath then
- local Path = lower(FilePath)
- local OriginalPath = GlobalTables.LuaFilePaths[Path].OriginalFile
- if OriginalPath then
- return OriginalPath
- else
- if string.sub(Path, 1,5) == '/mods' then
- local ignoreBaseFolders, removeBasePathParts, ignoreModFolders, removeModPathParts = GetLuaFolderParams()
- local ModLocations = GetModLocations()
- local LeadingPath = Path
- for uid, location in ModLocations do
- LeadingPath = string.gsub(LeadingPath,location,"")
- for id,pathPart in removeModPathParts do
- LeadingPath = string.gsub(Path,pathPart,"")
- end
- end
- return GlobalTables.LuaFilePaths[LeadingPath].OriginalFile
- else
- return nil
- end
- end
- else
- return nil
- end
- end
- function GetLuaFileHooks(FilePath)
- if FilePath then
- local Path = lower(FilePath)
- local FileHooks = GlobalTables.LuaFilePaths[Path].Hooks
- if FileHooks then
- return FileHooks
- else
- if string.sub(Path, 1,5) == '/mods' then
- local ignoreBaseFolders, removeBasePathParts, ignoreModFolders, removeModPathParts = GetLuaFolderParams()
- local ModLocations = GetModLocations()
- local LeadingPath = Path
- for uid, location in ModLocations do
- LeadingPath = string.gsub(LeadingPath,location,"")
- for id,pathPart in removeModPathParts do
- LeadingPath = string.gsub(LeadingPath,pathPart,"")
- end
- end
- return GlobalTables.LuaFilePaths[LeadingPath].Hooks
- else
- return nil
- end
- end
- else
- return nil
- end
- end
- function GetActiveMods()
- return GlobalTables['ActiveMods']
- end
- function GetActiveModUids()
- return GlobalTables['ActiveModUids'] or false
- end
- function GetModLocations()
- return GlobalTables['ActiveModLocations'] or false
- end
- function GetHookedFiles()
- return GlobalTables['HookedFiles'] or false
- end
- function GetPreviousHookedFiles()
- return GlobalTables['PrevHookedFiles'] or false
- end
- function CleanHookedFiles()
- --WARN('CLEANING FILES')
- InitModData(true)
- local Modules = GetPreviousHookedFiles()
- if Modules and table.getsize(Modules) > 0 then
- for hookedFile, Param in Modules do
- dirty_module(hookedFile, nil)
- end
- for hookedFile, Param in Modules do
- local env = import(hookedFile, false, true)
- end
- GlobalTables['PrevHookedFiles'] = nil
- end
- end
- function ModsChanged()
- if not IsSim() then
- local activeMods = GetPreference('active_mods') or {}
- local PreviousMods = GlobalTables['PreviousMods']
- if activeMods then
- if not table.equal(activeMods, PreviousMods) then
- return true
- else
- return false
- end
- else
- return false
- end
- else
- return false
- end
- end
- function HookFile(FilePath, newFile, env, silent)
- local ok, msg = pcall(doscript, newFile, env)
- if not ok then
- WARN("Problem hooking file '" .. newFile .."'!\n" .. msg)
- __modules[FilePath] = nil
- else
- if not silent then
- LOG('Hooked --> ' .. FilePath .. ' with '.. newFile)
- end
- GlobalTables['HookedFiles'][FilePath] = true
- env.__moduleinfo.FilesHooked = true
- end
- return env
- end
- function import(name, Bypass, silent)
- if Bypass then
- return previmport(name)
- end
- if not name then
- WARN('Import called with no file path')
- return
- end
- if not LuaPopulated then
- Populate_Lua_Files(false)
- Populate_Textures(false)
- end
- --if ModsChanged() then
- -- WARN('MODS CHANGED !!!')
- --CleanHookedFiles()
- --end
- local FilePath = string.lower(name)
- local existing = __modules[FilePath]
- if existing then
- return existing
- else
- local OriginalFile = GetLuaFile(FilePath)
- local FileHooks = GetLuaFileHooks(FilePath)
- local env = false
- if OriginalFile then
- env = previmport(OriginalFile)
- end
- if FileHooks then
- if env and not env.__moduleinfo.FilesHooked then
- for FileName, newFile in FileHooks do
- HookFile(OriginalFile, newFile, env, silent)
- end
- end
- end
- if env then
- return env
- else
- --> We should'nt really ever get here.
- -- WARN('running original import --> ' .. FilePath)
- --> this will mostly run for direct paths, ie /mods/blah/blah.lua
- return previmport(FilePath)
- end
- end
- end
- if not InitDone then
- InitModData()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment