Event Hook for WebRequest unsuitable for my needs

FuhrerDarqueSydeFuhrerDarqueSyde Join Date: 2004-10-04 Member: 32076Members, Constellation
edited April 2013 in Modding
The event is available but not usable for my needs since the only function currently hooked to it (OnWebRequest in ServerWebInterface.lua) returns in every case (else statement at the bottom). I understand my function will execute but I need to be able to handle an additional request value.

Currently, I am trying to add a new actions.request handler by adding my own function to the event as well but the original function returns before mine has a chance to handle and return due to this else statement.

Anyone have any ideas? I can't hook and replace the original OnWebRequest() because it is local and short of modifying the source on my server, which I am desperately trying to avoid for mod creation sake, I am running out of options.

Comments

  • FuhrerDarqueSydeFuhrerDarqueSyde Join Date: 2004-10-04 Member: 32076Members, Constellation
    I just went ahead and edited the ServerWebInterface.lua to not return if it doesnt hit any of the if statements and i added one for getserverstate and adjusted the rcon.js to call that to get the server state.

    Not an ideal solution but it is all I can do until a more appropriate adjustment is made. I need to be able to provide my server logs to my admins without giving them access to the file system.
  • RioRio Join Date: 2005-01-28 Member: 38716Members
    edited April 2013
    Shouldn't your event fire first, if your script gets loaded first? Just create a CustomServer.lua, load your script which hooks into the WebRequest event and load the original Server.lua last.

    Here's an alternative solution and some very bad advice which I haven't tried, but should work:
    Hook the Event.Hook method.
    local onWebRequestOld
    local eventHookOld = Event.Hook
    function Event.Hook(eventName, eventFunction)
        if (eventName == "WebRequest") then
            onWebRequestOld = eventFunction
            return
        end
        
        eventHookOld(eventName, eventFunction)
    end
    
    local function OnWebRequest(actions)
        // your own stuff
        
        // call the old function    
        onWebRequestOld(actions)
    end
    Event.Hook("WebRequest", OnWebRequest)
    
  • FuhrerDarqueSydeFuhrerDarqueSyde Join Date: 2004-10-04 Member: 32076Members, Constellation
    edited April 2013
    the load layout is Script.Load of dak's base loader, its config loads my plugin which loads lua/RingBuffer.lua on its own (even though Shared.lua has a load for it, i need it in my code and it wasnt loaded yet). The event is registered here but for whatever reason, it hits the ServerWebInterface.lua's function first and that function returns server state if it doesn't hit any of the other if statements.

    Server.lua snippet
    // Set the name of the VM for debugging
    Script.Load("lua/base/server.lua")
    decoda_name = "Server"

    Script.Load("lua/PreLoadMod.lua")

    My working (after modifying ServerWebInterface.lua) code
    // Adds enhanced logging data to the WebAdmin interface.
    // This needs to be used in addition to a modification to the WebAdmin files.
    // By: [!?] Squirrels
    // Revision: 8

    // Needed for this plugin but this normally wouldn't be loaded until later which causes this plugin to throw an exception and not load.
    Script.Load("lua/RingBuffer.lua")

    local recentLogEntries = CreateRingBuffer(20)
    local logMessageCount = 0

    -- This formats the incoming message and adds it to the table.
    function addLogEntry(_entry)
    logMessageCount = logMessageCount + 1
    recentLogEntries:Insert({ id = logMessageCount, timestamp = Shared.GetSystemTime(), entry = _entry })
    end

    -- hook EnhancedLogMessage to get the dak message. this function is not local (woo) and is prior to the timestamp being added (double woo).
    local refEnhancedLogMessage = EnhancedLogMessage
    function EnhancedLogMessage(message)
    -- This processes the message for our purposes.
    addLogEntry(message)

    -- This sends the message to the original function so everything still works as planned.
    return refEnhancedLogMessage(message)
    end

    -- Adds functionality to the WebRequest handler.
    local function OnWebRequest(actions)
    if actions.request == "getdaklogs" then
    return "application/json", json.encode(recentLogEntries:ToTable())
    end
    end
    Event.Hook("WebRequest", OnWebRequest)

    -- Default entry point?
    local function OnPluginInitialized()
    end

    I'm wary on hooking Event.Hook, though it should be fine.
Sign In or Register to comment.