Jump to content

MTA need refreshResource function.


MTA Should include this function?  

38 members have voted

  1. 1. MTA Should include this function?

    • Yes
      29
    • No
      9


Recommended Posts

I think, mta should have this function because if you want refresh one resource, refreshResources refresh all and cause lag. I posted it because i need refresh only one resource and MTA don´t have this function. I tryed with 'refreshResources' but my console closed automatically.

Link to comment

Sometimes I forget to update a resource, or leave it for later in the subject that I do not want to use refreshAll to determine if a script has been modified to another (cause too much lag for each minute). This function is perfect to see some automated resources.

is simply the union of refreshResources () with getThisResource () for C ++ developers should not be very difficult to do this

i think C++ need this code to modify: i don't know nothing about C++

/root/MTA10_Server/mods/deathmatch/logic/CResource.cpp:660

  
bool CResource::HasResourceChanged () 
{ 
    string strPath; 
  
    list < CResourceFile* > ::iterator iterf = m_resourceFiles.begin (); 
    for ( ; iterf != m_resourceFiles.end (); iterf++ ) 
    { 
        if ( GetFilePath ( (*iterf)->GetName(), strPath ) ) 
        { 
            CChecksum checksum = CChecksum::GenerateChecksumFromFile ( strPath ); 
            if ( ( *iterf )->GetLastChecksum() != checksum ) 
                return true; 
  
            // Also check if file in http cache has been externally altered 
            CResourceFile* pResourceFile = *iterf; 
            switch ( pResourceFile->GetType () ) 
            { 
                case CResourceFile::RESOURCE_FILE_TYPE_CLIENT_SCRIPT&#058; 
                case CResourceFile::RESOURCE_FILE_TYPE_CLIENT_CONFIG: 
                case CResourceFile::RESOURCE_FILE_TYPE_CLIENT_FILE: 
                { 
                    string strCachedFilePath = pResourceFile->GetCachedPathFilename (); 
                    CChecksum cachedChecksum = CChecksum::GenerateChecksumFromFile ( strCachedFilePath ); 
                    if ( cachedChecksum != checksum ) 
                        return true; 
                } 
                break; 
  
                default: 
                    break; 
            } 
        } 
    } 
  
    if ( GetFilePath ( "meta.xml", strPath ) ) 
    { 
        CChecksum checksum = CChecksum::GenerateChecksumFromFile ( strPath ); 
        if ( checksum != m_metaChecksum ) 
            return true; 
    } 
    return false; 
} 
  

Edited by Guest
Link to comment

In my opinion, I think that MTA should has this function since it started to work, basically becuase of the lag it cause refresh the whole server, imagine a server that need a lot of scripts (10-20 scripts), I seem better to refresh just one of the all resources inside, and as venad said "This function is perfect to see some automated resources."

Link to comment
Why do you want to refresh a single resource? Refreshing is basically checking for new resources and re-parsing the meta.xml.

If your meta.xml file doesn't contain syntax errors, just restarting the resource should do the trick. You are also able to reload only certain parts of a resource (see: https://wiki.multitheftauto.com/wiki/RestartResource)

sometimes need to check if file has changed or not (without refreshall or restartResource)

refreshAll not only check if meta has changed, check if ANY file has changed, we need refreshResource for that check

Link to comment

This is the Lua solution

Verified working

local files = {} 
local Bytes = 0 
addEventHandler("onResourceStart", resourceRoot, 
    function() 
    local meta = xmlLoadFile(":"..getResourceName(getThisResource()).."/meta.xml") 
        if meta then 
            for k,v in ipairs(xmlNodeGetChildren(meta)) do 
                if fileExists(xmlNodeGetAttribute(v, "src")) then 
                local file = fileOpen(xmlNodeGetAttribute(v, "src")) 
                local _ = fileGetSize(file) 
                Bytes = Bytes + _ 
                files[xmlNodeGetAttribute(v, "src")] = _ 
                fileClose(file) 
                end 
            end 
        --print("+++++++ "..tostring(Bytes)) 
        else 
        return error("Could not find META") 
        end 
    end 
) 
--USEFULL FUNCTION 
function refreshResource () 
local newBytes = 0 
local meta = xmlLoadFile(":"..getResourceName(getThisResource()).."/meta.xml") 
    if meta then 
        for k,v in ipairs(xmlNodeGetChildren(meta)) do 
            if fileExists(xmlNodeGetAttribute(v, "src")) then 
            local file = fileOpen(xmlNodeGetAttribute(v, "src")) 
            local _ = fileGetSize(file) 
            newBytes = newBytes + _ 
            fileClose(file) 
            end 
        end 
        --print("+++++++ "..tostring(newBytes).." -- "..tostring(Bytes)) 
        if newBytes ~= Bytes then 
        restartResource(getThisResource()) 
        end 
    else 
    return error("Could not find META"); 
    end 
end 
addCommandHandler("refreshResource", refreshResource) 

But integration with MTA is necessary

Link to comment
Why do you want to refresh a single resource? Refreshing is basically checking for new resources and re-parsing the meta.xml.

If your meta.xml file doesn't contain syntax errors, just restarting the resource should do the trick. You are also able to reload only certain parts of a resource (see: https://wiki.multitheftauto.com/wiki/RestartResource)

I tried with restartResource but doesn't work fine. My script edit meta.xml and need refresh... refreshResources works but cause lag and sometimes close my console. Why not include refreshResource function?

Link to comment
That function should execute the same action as "refresh" command.

Error, it is the same that refreshAll command

refreshAll: If true MTA will check for changes in all resources. If false, MTA will only check for new resources and try to reload resources with errors

refreshResource: MTA will check for changes in single resource

Link to comment
Why do you want to refresh a single resource? Refreshing is basically checking for new resources and re-parsing the meta.xml.

If your meta.xml file doesn't contain syntax errors, just restarting the resource should do the trick. You are also able to reload only certain parts of a resource (see: https://wiki.multitheftauto.com/wiki/RestartResource)

I tried with restartResource and her optionals arguments, but doesn't work... I save meta.xml with xmlSaveFile. I check if meta.xml changes saves sucessfully and yes, save it. But 'downloadFile' return error: File doesn't exists.

Link to comment
This is the Lua solution

Verified working

local files = {} 
local Bytes = 0 
addEventHandler("onResourceStart", resourceRoot, 
    function() 
    local meta = xmlLoadFile(":"..getResourceName(getThisResource()).."/meta.xml") 
        if meta then 
            for k,v in ipairs(xmlNodeGetChildren(meta)) do 
                if fileExists(xmlNodeGetAttribute(v, "src")) then 
                local file = fileOpen(xmlNodeGetAttribute(v, "src")) 
                local _ = fileGetSize(file) 
                Bytes = Bytes + _ 
                files[xmlNodeGetAttribute(v, "src")] = _ 
                fileClose(file) 
                end 
            end 
        --print("+++++++ "..tostring(Bytes)) 
        else 
        return error("Could not find META") 
        end 
    end 
) 
--USEFULL FUNCTION 
function refreshResource () 
local newBytes = 0 
local meta = xmlLoadFile(":"..getResourceName(getThisResource()).."/meta.xml") 
    if meta then 
        for k,v in ipairs(xmlNodeGetChildren(meta)) do 
            if fileExists(xmlNodeGetAttribute(v, "src")) then 
            local file = fileOpen(xmlNodeGetAttribute(v, "src")) 
            local _ = fileGetSize(file) 
            newBytes = newBytes + _ 
            fileClose(file) 
            end 
        end 
        --print("+++++++ "..tostring(newBytes).." -- "..tostring(Bytes)) 
        if newBytes ~= Bytes then 
        restartResource(getThisResource()) 
        end 
    else 
    return error("Could not find META"); 
    end 
end 
addCommandHandler("refreshResource", refreshResource) 

But integration with MTA is necessary

Simplest than yours

  
function refreshResource(res) 
    if ( not res or not getResourceState(res) ) then return false end -- no given resource 
    if ( not getResourceLoadFailureReason(res) == "" ) then return false end -- given resource failed to load 
    local resourceName = getResourceName(res) 
    local meta_file = ":"..resourceName.."/meta.xml" 
    if ( not fileExists(meta_file) ) then return false end -- no meta file was found 
    fileRename(meta_file, meta_file.."_") 
    refreshResources() 
    fileRename(meta_file.."_", meta_file) 
    refreshResources() 
    startResource(getResourceFromName(resourceName)) 
end 
  

By the way, your function would only properly work 1 time since it doesn't clear global variables and it'd only work with the resource where it's being loaded from.

Link to comment
Tomas said:
venadHD said:
This is the Lua solution

Verified working



local files = {} 
local Bytes = 0 
addEventHandler("onResourceStart", resourceRoot, 
    function() 
    local meta = xmlLoadFile(":"..getResourceName(getThisResource()).."/meta.xml") 
        if meta then 
            for k,v in ipairs(xmlNodeGetChildren(meta)) do 
                if fileExists(xmlNodeGetAttribute(v, "src")) then 
                local file = fileOpen(xmlNodeGetAttribute(v, "src")) 
                local _ = fileGetSize(file) 
                Bytes = Bytes + _ 
                files[xmlNodeGetAttribute(v, "src")] = _ 
                fileClose(file) 
                end 
            end 
        --print("+++++++ "..tostring(Bytes)) 
        else 
        return error("Could not find META") 
        end 
    end 
) 
--USEFULL FUNCTION 
function refreshResource () 
local newBytes = 0 
local meta = xmlLoadFile(":"..getResourceName(getThisResource()).."/meta.xml") 
    if meta then 
        for k,v in ipairs(xmlNodeGetChildren(meta)) do 
            if fileExists(xmlNodeGetAttribute(v, "src")) then 
            local file = fileOpen(xmlNodeGetAttribute(v, "src")) 
            local _ = fileGetSize(file) 
            newBytes = newBytes + _ 
            fileClose(file) 
            end 
        end 
        --print("+++++++ "..tostring(newBytes).." -- "..tostring(Bytes)) 
        if newBytes ~= Bytes then 
        restartResource(getThisResource()) 
        end 
    else 
    return error("Could not find META"); 
    end 
end 
addCommandHandler("refreshResource", refreshResource) 
 

 

 

But integration with MTA is necessary

Simplest than yours

  
function refreshResource(res) 
    if ( not res or not getResourceState(res) ) then return false end -- no given resource 
    if ( not getResourceLoadFailureReason(res) == "" ) then return false end -- given resource failed to load 
    local resourceName = getResourceName(res) 
    local meta_file = ":"..resourceName.."/meta.xml" 
    if ( not fileExists(meta_file) ) then return false end -- no meta file was found 
    fileRename(meta_file, meta_file.."_") 
    refreshResources() 
    fileRename(meta_file.."_", meta_file) 
    refreshResources() 
    startResource(getResourceFromName(resourceName)) 
end 
  
 

By the way, your function would only properly work 1 time since it doesn't clear global variables and it'd only work with the resource where it's being loaded from.

You are using refreshResources(),and your script don't work like refreshResource() must be.

Anyway, needed integration with MTA:SA functions.

Link to comment
  • 6 months later...

If refreshResource() is integrated in MTA:SA so refreshResources() could be deleted because if one needs to refresh all resources, he just needs a iterator. However, this breaks the avaiable scripts.

So what now? If that lua version solves the problem of everyone we could just include it somewhere in the Wiki instead of making mess adding more native functions.

  • Like 1
Link to comment
  • 2 weeks later...

I can't understand what you want.

But i'm sure it is possible with lua only. As Jusonex said refreshResources will check all resources meta.xml and load new resources. But if you just use restartResource it will parse the meta.xml also again.

The other case could be if you want to see which resources had been modified and this is also very easy. Just go through the files and generate a hash from the contents and compare them at a later point.

Link to comment
  • 2 weeks later...
This is the Lua solution

Verified working

local files = {} 
local Bytes = 0 
addEventHandler("onResourceStart", resourceRoot, 
    function() 
    local meta = xmlLoadFile(":"..getResourceName(getThisResource()).."/meta.xml") 
        if meta then 
            for k,v in ipairs(xmlNodeGetChildren(meta)) do 
                if fileExists(xmlNodeGetAttribute(v, "src")) then 
                local file = fileOpen(xmlNodeGetAttribute(v, "src")) 
                local _ = fileGetSize(file) 
                Bytes = Bytes + _ 
                files[xmlNodeGetAttribute(v, "src")] = _ 
                fileClose(file) 
                end 
            end 
        --print("+++++++ "..tostring(Bytes)) 
        else 
        return error("Could not find META") 
        end 
    end 
) 
--USEFULL FUNCTION 
function refreshResource () 
local newBytes = 0 
local meta = xmlLoadFile(":"..getResourceName(getThisResource()).."/meta.xml") 
    if meta then 
        for k,v in ipairs(xmlNodeGetChildren(meta)) do 
            if fileExists(xmlNodeGetAttribute(v, "src")) then 
            local file = fileOpen(xmlNodeGetAttribute(v, "src")) 
            local _ = fileGetSize(file) 
            newBytes = newBytes + _ 
            fileClose(file) 
            end 
        end 
        --print("+++++++ "..tostring(newBytes).." -- "..tostring(Bytes)) 
        if newBytes ~= Bytes then 
        restartResource(getThisResource()) 
        end 
    else 
    return error("Could not find META"); 
    end 
end 
addCommandHandler("refreshResource", refreshResource) 

But integration with MTA is necessary

Simplest than yours

  
function refreshResource(res) 
    if ( not res or not getResourceState(res) ) then return false end -- no given resource 
    if ( not getResourceLoadFailureReason(res) == "" ) then return false end -- given resource failed to load 
    local resourceName = getResourceName(res) 
    local meta_file = ":"..resourceName.."/meta.xml" 
    if ( not fileExists(meta_file) ) then return false end -- no meta file was found 
    fileRename(meta_file, meta_file.."_") 
    refreshResources() 
    fileRename(meta_file.."_", meta_file) 
    refreshResources() 
    startResource(getResourceFromName(resourceName)) 
end 
  

By the way, your function would only properly work 1 time since it doesn't clear global variables and it'd only work with the resource where it's being loaded from.

If bytes of file changes your script will never WORK.

Link to comment
  • 1 month later...
  • 2 weeks later...
On 4.1.2016 at 11:04 PM, Jusonex said:

Why do you want to refresh a single resource? Refreshing is basically checking for new resources and re-parsing the meta.xml.

If your meta.xml file doesn't contain syntax errors, just restarting the resource should do the trick. You are also able to reload only certain parts of a resource (see: https://wiki.multitheftauto.com/wiki/RestartResource)

That would be needed if you have some selfmade map loader that never actually starts and stops resources I guess.

If you e.g. want to rename a map, it wont be updated until you restart the whole server, since you cannot just start/stop the resource.

Therefore, having a function to refresh a single, non running resource, can be useful.

Link to comment
  • 1 month later...
On 30/9/2016 at 8:58 PM, Bonsai said:

That would be needed if you have some selfmade map loader that never actually starts and stops resources I guess.

If you e.g. want to rename a map, it wont be updated until you restart the whole server, since you cannot just start/stop the resource.

Therefore, having a function to refresh a single, non running resource, can be useful.

Actually map info is loaded with map, at least on your gamemode and on lot of gamemodes.

Map name must be as the renamed one with no problem without refreshing, since you just open the meta.xml file to get all map data.

Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...