Jump to content

[HELP] Load skin only if there is a file on server side?


MADD NØG

Recommended Posts

I have a script to load character skins, but I need to have some way to check if there are files on the serveside (I check on my website the credentials of the client that is starting the resource, to see if he is authorized to start, but if he removes the server file, it can start the models without too many problems) thinking about this problem this was the only solution I found, if anyone has any suggestions on how I can force the resource owner to use a server file:
 

Client (As I need to force the resource owner to start at least 1 file on the server I add an onClientResourceStart to a server side event, if everything goes well it returns a true variable)

local keyAcess = false

addEvent("accessGranted", true)
addEventHandler("accessGranted", resourceRoot, function()
	keyAcess = true
end)

local function privateKey()
	setTimer(function()
    if keyAcess == true then
        outputDebugString("true", 4, 2, 137, 218)
        if not firstConnection then
            firstConnection = true
        end
    else
		triggerServerEvent("checkCredentials", resourceRoot)
		engineRestoreModel(1)
		engineRestoreModel(2)
		outputDebugString("false", 4, 255, 0, 0)
        setTimer(privateKey, 1000, 0)
        firstConnection = false
		keyAcess = false
    end
    end, 5000, 1)
end
addEventHandler("onClientResourceStart", resourceRoot, privateKey)

addEventHandler("onClientResourceStart",getRootElement(),function(res)
	if getResourceName(res) == "chars" or getThisResource() == res then 
		triggerServerEvent("checkCredentials", resourceRoot)
		loadMod('mp_male', 1, 'passwordToDecompileTheModel ')
		loadMod('mp_female', 2, 'passwordToDecompileTheModel')
	end	
end);

 

The problem is that sometimes, for a reason I haven't figured out yet, it doesn't return the keyAccess as true and the engineRestoreModel function fires because it's in false

It could be a player with a high ms or even a server hardware problem or some error in my code, I couldn't identify it yet

Server

addEvent("checkCredentials",true)
addEventHandler("checkCredentials", getRootElement(), function()
	if isObjectInACLGroup("resource."..nomeResource, aclGetGroup(acl)) then
		triggerClientEvent("accessGranted", resourceRoot)
	else
		stopResource(getThisResource())
	end
end)

 

Edited by M4DD NØG
Link to comment

Why don't you just wait for the server-event anyway? You should replace the skins once the access-grant event has been triggered on the client. You could use the resource-start event for triggering the permission-request-event to the server. Also remove those timers because they make no sense.

addEventHandler("accessGranted", resourceRoot, function()
    engineRestoreModel(1)
    engineRestoreModel(2)
    loadMod(...)
    loadMod(...)
end)

You should grant the access to only one client. Currently you are triggering the event for every client on the server which is wrong:

...
        triggerClientEvent(client, "accessGranted", resourceRoot)
...

 

Edited by The_GTA
  • Thanks 1
Link to comment

I tested it as you indicated, but I still have some questions!

Client-Side

local yMMX83dl33KHiPKetn = false
local firstConnection = true

local function decodeSource(path, key)
	local file = fileOpen(path)
	local size = fileGetSize(file)
	local FirstPart = fileRead(file, 65536 + 4)

	fileSetPos(file, 65536 + 4)
	local SecondPart = fileRead(file, size - (65536 + 4))
	fileClose(file)
	return decodeString('tea', FirstPart, { key = key })..SecondPart
end

local function loadMod(path, model, passwordHash)
	engineImportTXD(engineLoadTXD(decodeSource('models/' .. path .. '.txd', passwordHash)), model)
	engineReplaceModel(engineLoadDFF(decodeSource('models/' .. path .. '.dff', passwordHash)), model)
end

addEvent("loadingModels",true)
addEventHandler("loadingModels", resourceRoot, function()

	loadMod(...)
	loadMod(...)
	yMMX83dl33KHiPKetn = true
	privateKey()

end)

function privateKey()
    if yMMX83dl33KHiPKetn == true then
        outputDebugString("True", 4, 2, 137, 218)
        if not firstConnection then
            firstConnection = true
        end
    else
		triggerServerEvent("resourceCheck", resourceRoot)
		engineRestoreModel(1)
		engineRestoreModel(2)
		outputDebugString("false", 4, 255, 0, 0)
		setTimer(privateKey, 1000, 0)
		firstConnection = false
		yMMX83dl33KHiPKetn = false
    end
end

addEventHandler("onClientResourceStart",getRootElement(),function(res)
	if getResourceName(res) == "chars" or getThisResource() == res then 
		triggerServerEvent("resourceCheck", resourceRoot)
	end	
end);

 

Server Side

 

addEvent("resourceCheck",true)
addEventHandler("resourceCheck", getRootElement(), function()
	if isObjectInACLGroup("resource."..resourcename, aclGetGroup(acl)) then
		local file = fileOpen(":"..resourcename.."/engine/"..ServerConf..".lua")
		   if not file then
			   return false
		   end
		   local count = fileGetSize(file)
		   local data = fileRead(file, count)
		   local fileSize = count -- tonumber(16678)
		   fileClose(file)
		if count == fileSize then
			file1, file2, file3 = getResourceFiles(getThisResource())
			if #file3 == defaultfolders then
				triggerClientEvent(client, "loadingModels", resourceRoot) 
			else
				stopResource(getThisResource())
			end
		else
			stopResource(getThisResource())
			return
		end
	else
		stopResource(getThisResource())
	end
end)

More as you warned that I was triggering this event for all players, this causes a memory error when there are many players on the server, and it continues to cause this currently with this code, I'm sure it's referring to the part of me using a triggerServerEvent next to the onClientResourceStart, but I don't know how I can start this check other than this way.

 

spacer.png

Edited by Neto Silva
Link to comment

What is this...

		triggerServerEvent("resourceCheck", resourceRoot)
		engineRestoreModel(1)
		engineRestoreModel(2)
		outputDebugString("false", 4, 255, 0, 0)
		setTimer(privateKey, 1000, 0)

... and most importantly, this?

		setTimer(privateKey, 1000, 0)

You could try perfoming a full garbage collection cycle to clean up Lua string memory.

    engineImportTXD(engineLoadTXD(decodeSource('models/' .. path .. '.txd', passwordHash)), model)
    engineReplaceModel(engineLoadDFF(decodeSource('models/' .. path .. '.dff', passwordHash)), model)
    collectgarbage()

But I think that the root cause of your problem is that you are loading in too many MTA server DFFs and TXDs into the game instead of only loading what is necessary.

Edited by The_GTA
  • Thanks 1
Link to comment
  • 2 weeks later...
On 20/01/2022 at 04:45, The_GTA said:

What is this...

		triggerServerEvent("resourceCheck", resourceRoot)
		engineRestoreModel(1)
		engineRestoreModel(2)
		outputDebugString("false", 4, 255, 0, 0)
		setTimer(privateKey, 1000, 0)

... and most importantly, this?

		setTimer(privateKey, 1000, 0)

You could try perfoming a full garbage collection cycle to clean up Lua string memory.

    engineImportTXD(engineLoadTXD(decodeSource('models/' .. path .. '.txd', passwordHash)), model)
    engineReplaceModel(engineLoadDFF(decodeSource('models/' .. path .. '.dff', passwordHash)), model)
    collectgarbage()

But I think that the root cause of your problem is that you are loading in too many MTA server DFFs and TXDs into the game instead of only loading what is necessary.

 

I'm trying to load another way in a resource I put the following code to receive a confirmation code

Clientside

local hashCodeString = false;

addEventHandler('onClientResourceStart',resourceRoot,function()
    triggerServerEvent("onPlayerReady", resourceRoot);
end)

addEvent("onClientReceivePermissions", true);
addEventHandler("onClientReceivePermissions", resourceRoot, 
function(_hashCodeString)

    hashCodeString = _hashCodeString;

end, false);

function getResourceStatus()
	return hashCodeString
end

Serverside

addEvent("onPlayerReady", true);
addEventHandler("onPlayerReady", resourceRoot,
function()
    local hashCodeString = "hashcode"
    triggerClientEvent(client, "onClientReceivePermissions", resourceRoot, hashCodeString);
end, false);

Meta

 <export function="getResourceStatus" type="client" />
 <download_priority_group>2</download_priority_group>

 

Then in another resource that I only have access to clientside I receive this code and confirm if everything is ok I start the models

local hashCodeNumberTwo = false
local hashCodeNumberOne = false

local function loadModel(path, model)
	engineImportTXD(engineLoadTXD(...)
	engineReplaceModel(engineLoadDFF(...)
	collectgarbage()
end

addEventHandler('onClientResourceStart',resourceRoot,function()

    loadModel(...)
 
    setTimer(function()
        hashCheck()
    end, 5000, 1)
end)

function hashCheck()
    local hashCodeNumberOne = "hashcode"
    local hashCodeNumberTwo = "hashcode"
    local getHashCodeOne = exports["resourceOne"]:getResourceStatus()
    local getHashCodeTwo = exports["resourceTwo"]:getResourceStatus()
    if not getHashCodeOne then
        if hashCodeNumber ~= getHashCodeOne then
            engineRestoreModel(...)
            engineRestoreModel(...)
        end
    end
    if not getHashCodeTwo then
        if getHashCodeTwo ~= hashCodeNumberTwo then
            engineRestoreModel(...)
        end
    end
end

 

The problem is that for some people the models don't load or what I think it could be is that getHashCodeTwo is returning false and the models are being restored after being loaded, do you have any idea what I could be doing wrong?

 

 

Link to comment
  • Moderators
12 hours ago, Neto Silva said:

I think it could be is that getHashCodeTwo is returning

That might be indeed the case. That 5000ms timer is a bit hacky in my opinion.

Just listen in the resource (that has the hashCheck function) to the event:

"onClientReceivePermissions"

(you can cross listen to events of another resources, as long as the element you are listen to is a parent or the source)

 

The predefined variables of that event will be enough to figure out which resource is ready and which is not. See list of predefined variables:

https://wiki.multitheftauto.com/wiki/AddEventHandler

 

 

  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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