Guest User

Domino

a guest
Aug 26th, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.85 KB | None | 0 0
  1.  
  2.  
  3. do
  4. doscript('/lua/ui/texture_tools.lua')
  5.  
  6. GlobalTables = {}
  7.  
  8. local InitDone = false
  9. local previmport = import
  10. local LuaPopulated = false
  11.  
  12. gsub = string.gsub
  13. lower = string.lower
  14.  
  15.  
  16. function CreateGlobalTable(tableName)
  17.     if tableName then
  18.         if not GlobalTables[tableName] then
  19.             GlobalTables[tableName] = {}
  20.         end
  21.     end
  22. end
  23.  
  24. function RemoveGlobalTable(tableName)
  25.     if tableName then
  26.         if GlobalTables[tableName] then
  27.             GlobalTables[tableName] = nil
  28.         end
  29.     end
  30. end
  31.  
  32. function GetGlobalTable(tableName)
  33.     if GlobalTables[tableName] then
  34.         return table.deepcopy(GlobalTables[tableName])
  35.     else
  36.         return nil
  37.     end
  38. end
  39.  
  40. function ResetGlobalTable(tableName)
  41.     if tableName then
  42.         if GlobalTables[tableName] then
  43.             GlobalTables[tableName] = {}
  44.         end
  45.     end
  46. end
  47.  
  48. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  49.  
  50. function ResetModTables(ResetHookedFiles)
  51.     GlobalTables['ActiveModUids'] = nil
  52.    
  53.     GlobalTables['ActiveModIdentifiers'] = nil
  54.     GlobalTables['ActiveModNames'] = nil
  55.    
  56.     GlobalTables['ActiveMods'] = nil
  57.     GlobalTables['ActiveModLocations'] = nil
  58.     GlobalTables['PreviousMods'] = nil
  59.    
  60.     if ResetHookedFiles then
  61.         CreateGlobalTable('PrevHookedFiles')
  62.         GlobalTables['PrevHookedFiles'] = GlobalTables['HookedFiles']
  63.         GlobalTables['HookedFiles'] = nil
  64.     end
  65. end
  66.  
  67. function ResetLuaTables()
  68.     GlobalTables['LuaFileNames'] = nil
  69.     GlobalTables['LuaFilePaths'] = nil
  70. end
  71.  
  72. -------------------------------------------------------------------------------------------------------------
  73.  
  74. function IsSim()
  75.     local result
  76.     if not rawget(_G, 'GetCurrentUIState') then
  77.         result = true
  78.     else
  79.         result = false
  80.     end
  81.    
  82.     return result
  83. end
  84.  
  85. function CreateDataTables()
  86.     CreateGlobalTable('ActiveMods')
  87.     CreateGlobalTable('ActiveModIdentifiers')
  88.     CreateGlobalTable('ActiveModNames')
  89.     CreateGlobalTable('ActiveModLocations')
  90.     CreateGlobalTable('PreviousMods')
  91.     CreateGlobalTable('ModBasePaths')
  92.     CreateGlobalTable('HookedFiles')
  93. end
  94.  
  95. function InitModData(Reset)
  96.  
  97.     if Reset then
  98.         ResetModTables(true)
  99.     end
  100.  
  101.     if IsSim() then
  102.         if not GlobalTables['ActiveModUids'] then
  103.             local activeModUIds = {}
  104.            
  105.             CreateDataTables()
  106.            
  107.             for id, Params in __active_mods do
  108.                 activeModUIds[Params.uid] = true
  109.                 GlobalTables['ActiveModLocations'][Params.uid] = Params.location
  110.             end
  111.            
  112.             GlobalTables['ActiveMods'] = __active_mods
  113.             GlobalTables['ActiveModUids'] = activeModUIds
  114.             GlobalTables['PreviousMods'] = activeModUIds       
  115.         end
  116.     else
  117.         if not GlobalTables['ActiveModUids'] then
  118.             local ActiveMods = GetPreference('active_mods') or {}
  119.            
  120.             CreateDataTables()
  121.  
  122.             GlobalTables['ActiveModUids'] = ActiveMods
  123.             GlobalTables['PreviousMods'] = ActiveMods
  124.            
  125.             for i,file in DiskFindFiles('/mods', '*mod_info.lua') do
  126.                 local Location = Dirname(file)
  127.                 local env = {
  128.                     location = Location,
  129.                     name = file,
  130.                     description = "<LOC uimod_0006>(No description)",
  131.                     author = '',
  132.                     copyright = '',
  133.                     exclusive = false,
  134.                     icon = '/textures/ui/common/dialogs/mod-manager/generic-icon_bmp.dds',
  135.                     selectable = true,
  136.                     hookdir = '/hook',
  137.                     shadowdir = '/shadow',
  138.                     uid = file,
  139.                 }
  140.                 local ok, result = pcall(doscript, file, env)
  141.                 if ok then
  142.                     if ActiveMods[env.uid] and (env.enabled != false) then
  143.                         env.location = Location
  144.                         GlobalTables['ActiveMods'][env.uid] = env
  145.                         GlobalTables['ActiveModLocations'][env.uid] = Location
  146.                        
  147.                         if env.identifier then
  148.                             GlobalTables['ActiveModIdentifiers'][env.identifier] = Location
  149.                         end                    
  150.                     end                
  151.                 else
  152.                     WARN("Problem loading " .. file .. ":\n" .. result)
  153.                 end
  154.             end        
  155.         end
  156.     end
  157.     InitDone = true
  158.     if Reset then
  159.         Populate_Lua_Files(true)
  160.         Populate_Textures(true)
  161.     end
  162. end
  163.  
  164. function GetLuaFolderParams(ReturnWhat)
  165.     local ignoreBaseFolders = { '/mods', '/schook', '/textures', '/frontend' }
  166.     local removeBasePathParts = { '/frontend', }
  167.    
  168.     local ignoreModFolders = { '/hook', '/shadow', '/textures', }
  169.     local removeModPathParts = { '/frontend', '/modhook', }
  170.    
  171.     if not ReturnWhat then
  172.         return ignoreBaseFolders, removeBasePathParts, ignoreModFolders, removeModPathParts
  173.     elseif ReturnWhat == 'Base' then
  174.         return ignoreBaseFolders, removeBasePathParts
  175.     elseif ReturnWhat == 'Mods' then
  176.         return ignoreModFolders, removeModPathParts
  177.     elseif ReturnWhat == 'IgnoreBase' then
  178.         return ignoreBaseFolders
  179.     elseif ReturnWhat == 'RemoveBase' then
  180.         return removeBasePathParts
  181.     elseif ReturnWhat == 'IgnoreMods' then
  182.         return ignoreModFolders
  183.     elseif ReturnWhat == 'RemoveMod' then
  184.         return removeModPathParts
  185.     end
  186. end
  187.  
  188. function Populate_Lua_Files(Reset)
  189.     if Reset then
  190.         ResetLuaTables()
  191.     end
  192.    
  193.     CreateGlobalTable('LuaFileNames')
  194.     CreateGlobalTable('LuaFilePaths')
  195.    
  196.     local ignoreBaseFolders, removeBasePathParts, ignoreModFolders, removeModPathParts = GetLuaFolderParams()
  197.     local removeBaseTexturePathParts = { '/textures', '/ui', }
  198.     local FrontEndFiles = {}
  199.  
  200.     --dump all base lua in the table, baring the above folders.
  201.     for k,file in DiskFindFiles('/', '*.lua') do
  202.         local Path = lower(file)
  203.         local FileName = Basename(Path, false)
  204.         local FolderName = Dirname(Path)
  205.         local ProcessLua = true
  206.         local inFolder = false
  207.                        
  208.         for id, partPath in ignoreBaseFolders do
  209.             if string.find(FolderName, partPath, 1) then
  210.                 ProcessLua = false
  211.                 inFolder = partPath
  212.                 break
  213.             end
  214.         end
  215.        
  216.         if inFolder and not ProcessLua then
  217.             if inFolder == '/textures' or inFolder == '/ui' then
  218.                 local LeadingPath = FolderName
  219.                 for id,pathPart in removeBaseTexturePathParts do
  220.                     LeadingPath = string.gsub(LeadingPath,pathPart,"")
  221.                 end
  222.                                    
  223.                 FolderName = LeadingPath
  224.                 ProcessLua = true
  225.             elseif inFolder == '/frontend' then
  226.                 table.insert(FrontEndFiles, Path)
  227.             end
  228.         end
  229.        
  230.         FolderName = ParseBaseRedirectPath(FolderName)
  231.        
  232.         local JoinPath = FolderName .. '/' .. FileName
  233.         if ProcessLua then
  234.             GlobalTables.LuaFilePaths[JoinPath] = { OriginalFile = Path }          
  235.         end
  236.     end
  237.    
  238.     for id, file in FrontEndFiles do
  239.         local Path = lower(file)
  240.         local FileName = Basename(Path, false)
  241.         local FolderName = Dirname(Path)
  242.         local ProcessLua = true
  243.                
  244.         FolderName = string.gsub(FolderName,'/frontend',"")
  245.        
  246.         local JoinPath = FolderName .. '/' .. FileName
  247.        
  248.         local ExistingFilePath = GlobalTables.LuaFilePaths[JoinPath].OriginalFile
  249.                    
  250.         if not ExistingFilePath then
  251.             GlobalTables.LuaFilePaths[JoinPath] = { OriginalFile = Path }
  252.         else
  253.             if JoinPath == ExistingFilePath then
  254.                 if not GlobalTables.LuaFilePaths[JoinPath].Hooks then
  255.                     GlobalTables.LuaFilePaths[JoinPath].Hooks = {}
  256.                 end                    
  257.                 table.insert(GlobalTables.LuaFilePaths[JoinPath].Hooks, Path)
  258.             end
  259.         end
  260.     end
  261.  
  262.     --dump all mod lua into the table, baring the above folders.
  263.     for uid, location in GlobalTables.ActiveModLocations do
  264.         for k,file in DiskFindFiles(location, '*.lua') do
  265.             local Path = string.lower(file)
  266.             local FileName = Basename(Path, false)
  267.             local FolderName = Dirname(Path)
  268.             local ProcessLua = true
  269.            
  270.             for id, partPath in ignoreModFolders do
  271.                 if string.find(FolderName, partPath, 1) then
  272.                     ProcessLua = false
  273.                     break
  274.                 end
  275.             end
  276.                        
  277.             local JoinPath = FolderName .. '/' .. FileName         
  278.             if ProcessLua then
  279.                 local LeadingPath = string.gsub(JoinPath,location,"")
  280.    
  281.                 for id,pathPart in removeModPathParts do
  282.                     LeadingPath = string.gsub(LeadingPath,pathPart,"")
  283.                 end
  284.                                
  285.                 LeadingPath = ParseModRedirectPath(LeadingPath)
  286.                                                
  287.                 local ExistingFilePath = GlobalTables.LuaFilePaths[LeadingPath].OriginalFile
  288.                    
  289.                 if not ExistingFilePath then
  290.                     GlobalTables.LuaFilePaths[LeadingPath] = { OriginalFile = Path }
  291.                 else
  292.                     if LeadingPath == ExistingFilePath then
  293.                         if not GlobalTables.LuaFilePaths[LeadingPath].Hooks then
  294.                             GlobalTables.LuaFilePaths[LeadingPath].Hooks = {}
  295.                         end                    
  296.                         table.insert(GlobalTables.LuaFilePaths[LeadingPath].Hooks, Path)
  297.                     end
  298.                 end
  299.             end
  300.         end
  301.     end
  302.        
  303.     LuaPopulated = true
  304. end
  305.  
  306. function ParseBaseRedirectPath(FilePath)
  307.     return FilePath
  308. end
  309.  
  310. function ParseModRedirectPath(FilePath)
  311.     if FilePath then
  312.         local FileRedirects = { ['/lua/scripttasks'] = '/lua/sim/tasks/' }
  313.         local FolderName = Dirname(FilePath)
  314.         local RedirectPath = FileRedirects[FolderName]
  315.        
  316.         if RedirectPath then
  317.             return RedirectPath .. Basename(FilePath, false)
  318.         else
  319.             return FilePath
  320.         end    
  321.     else
  322.         return nil
  323.     end
  324. end
  325.  
  326. function GetLuaFile(FilePath)
  327.     if FilePath then
  328.         local Path = lower(FilePath)
  329.         local OriginalPath = GlobalTables.LuaFilePaths[Path].OriginalFile
  330.        
  331.         if OriginalPath then
  332.             return OriginalPath
  333.         else   
  334.             if string.sub(Path, 1,5) == '/mods' then
  335.                 local ignoreBaseFolders, removeBasePathParts, ignoreModFolders, removeModPathParts = GetLuaFolderParams()
  336.                 local ModLocations = GetModLocations()
  337.                 local LeadingPath = Path
  338.                
  339.                 for uid, location in ModLocations do
  340.                     LeadingPath = string.gsub(LeadingPath,location,"")
  341.        
  342.                     for id,pathPart in removeModPathParts do
  343.                         LeadingPath = string.gsub(Path,pathPart,"")
  344.                     end
  345.                 end
  346.                
  347.                 return GlobalTables.LuaFilePaths[LeadingPath].OriginalFile
  348.             else       
  349.                 return nil
  350.             end
  351.         end
  352.     else
  353.         return nil
  354.     end
  355. end
  356.  
  357. function GetLuaFileHooks(FilePath)
  358.     if FilePath then
  359.         local Path = lower(FilePath)
  360.         local FileHooks = GlobalTables.LuaFilePaths[Path].Hooks
  361.        
  362.         if FileHooks then
  363.             return FileHooks
  364.         else   
  365.             if string.sub(Path, 1,5) == '/mods' then
  366.                 local ignoreBaseFolders, removeBasePathParts, ignoreModFolders, removeModPathParts = GetLuaFolderParams()
  367.                 local ModLocations = GetModLocations()
  368.                 local LeadingPath = Path
  369.                
  370.                 for uid, location in ModLocations do
  371.                     LeadingPath = string.gsub(LeadingPath,location,"")
  372.        
  373.                     for id,pathPart in removeModPathParts do
  374.                         LeadingPath = string.gsub(LeadingPath,pathPart,"")
  375.                     end
  376.                 end
  377.                
  378.                 return GlobalTables.LuaFilePaths[LeadingPath].Hooks
  379.             else       
  380.                 return nil
  381.             end
  382.         end
  383.     else
  384.         return nil
  385.     end
  386. end
  387.  
  388. function GetActiveMods()
  389.     return GlobalTables['ActiveMods']
  390. end
  391.  
  392. function GetActiveModUids()
  393.     return GlobalTables['ActiveModUids'] or false
  394. end
  395.  
  396. function GetModLocations()
  397.     return GlobalTables['ActiveModLocations'] or false
  398. end
  399.  
  400. function GetHookedFiles()
  401.     return GlobalTables['HookedFiles'] or false
  402. end
  403.  
  404. function GetPreviousHookedFiles()
  405.     return GlobalTables['PrevHookedFiles'] or false
  406. end
  407.  
  408. function CleanHookedFiles()
  409.     --WARN('CLEANING FILES')
  410.     InitModData(true)
  411.    
  412.     local Modules = GetPreviousHookedFiles()
  413.        
  414.     if Modules and table.getsize(Modules) > 0 then
  415.         for hookedFile, Param in Modules do
  416.             dirty_module(hookedFile, nil)
  417.         end
  418.                    
  419.         for hookedFile, Param in Modules do
  420.             local env = import(hookedFile, false, true)
  421.         end
  422.        
  423.         GlobalTables['PrevHookedFiles'] = nil
  424.     end
  425. end
  426.  
  427. function ModsChanged()
  428.     if not IsSim() then
  429.         local activeMods = GetPreference('active_mods') or {}
  430.         local PreviousMods = GlobalTables['PreviousMods']
  431.  
  432.         if activeMods then
  433.             if not table.equal(activeMods, PreviousMods) then
  434.                 return true
  435.             else   
  436.                 return false
  437.             end
  438.         else
  439.             return false
  440.         end
  441.     else
  442.         return false
  443.     end
  444. end
  445.  
  446. function HookFile(FilePath, newFile, env, silent)
  447.     local ok, msg = pcall(doscript, newFile, env)
  448.     if not ok then
  449.         WARN("Problem hooking file '" .. newFile .."'!\n" .. msg)
  450.         __modules[FilePath] = nil
  451.     else
  452.         if not silent then
  453.             LOG('Hooked --> ' .. FilePath .. ' with '.. newFile)
  454.         end
  455.         GlobalTables['HookedFiles'][FilePath] = true
  456.         env.__moduleinfo.FilesHooked = true
  457.     end
  458.    
  459.     return env
  460. end
  461.  
  462. function import(name, Bypass, silent)
  463.    
  464.     if Bypass then
  465.         return previmport(name)
  466.     end
  467.    
  468.     if not name then
  469.         WARN('Import called with no file path')
  470.         return
  471.     end
  472.    
  473.     if not LuaPopulated then
  474.         Populate_Lua_Files(false)
  475.         Populate_Textures(false)
  476.     end
  477.    
  478.     --if ModsChanged() then
  479.     --  WARN('MODS CHANGED !!!')
  480.         --CleanHookedFiles()
  481.     --end
  482.        
  483.     local FilePath = string.lower(name)
  484.     local existing = __modules[FilePath]
  485.     if existing then
  486.         return existing
  487.     else
  488.         local OriginalFile = GetLuaFile(FilePath)
  489.         local FileHooks = GetLuaFileHooks(FilePath)
  490.         local env = false
  491.                
  492.         if OriginalFile then
  493.             env = previmport(OriginalFile)
  494.         end
  495.        
  496.         if FileHooks then
  497.             if env and not env.__moduleinfo.FilesHooked then
  498.                 for FileName, newFile in FileHooks do
  499.                     HookFile(OriginalFile, newFile, env, silent)
  500.                 end
  501.             end
  502.         end
  503.        
  504.         if env then
  505.             return env
  506.         else
  507.             --> We should'nt really ever get here.
  508.         --  WARN('running original import --> ' .. FilePath)
  509.         --> this will mostly run for direct paths, ie /mods/blah/blah.lua
  510.             return previmport(FilePath)
  511.         end
  512.     end
  513. end
  514.  
  515. if not InitDone then
  516.     InitModData()
  517. end
  518.  
  519. end
Advertisement
Add Comment
Please, Sign In to add comment