mirror of
https://github.com/DaKerboul/perun.git
synced 2026-08-09 17:05:39 +02:00
Feature: UTF8 is now supported!
Feature: Tons of performance monitoring
This commit is contained in:
@@ -19,6 +19,7 @@ Perun.MissionStartNoDeathWindow = PerunConfig.MissionStartNoDeathWindow
|
||||
Perun.DebugMode = PerunConfig.DebugMode
|
||||
Perun.MOTD_L1 = PerunConfig.MOTD_L1
|
||||
Perun.MOTD_L2 = PerunConfig.MOTD_L2
|
||||
Perun.ConnectionError = PerunConfig.ConnectionError_L1
|
||||
|
||||
-- Variable init
|
||||
Perun.Version = "v0.12.0"
|
||||
@@ -43,165 +44,8 @@ Perun.RefreshKeepAlive = 3
|
||||
Perun.FrameTime = 0;
|
||||
Perun.socket = require("socket")
|
||||
Perun.IsServer = DCS.isServer( )
|
||||
|
||||
Perun.ConnectionError = "[Perun] ERROR: Connection broken - contact server admin!"
|
||||
|
||||
-- ################################ Helper function definitions ################################
|
||||
function stripChars(str)
|
||||
-- Hellper functions removes accents characters from string
|
||||
-- via https://stackoverflow.com/questions/50459102/replace-accented-characters-in-string-to-standard-with-lua
|
||||
local _tableAccents = {}
|
||||
_tableAccents["à"] = "a"
|
||||
_tableAccents["á"] = "a"
|
||||
_tableAccents["â"] = "a"
|
||||
_tableAccents["ã"] = "a"
|
||||
_tableAccents["ä"] = "a"
|
||||
_tableAccents["ç"] = "c"
|
||||
_tableAccents["è"] = "e"
|
||||
_tableAccents["é"] = "e"
|
||||
_tableAccents["ê"] = "e"
|
||||
_tableAccents["ë"] = "e"
|
||||
_tableAccents["ì"] = "i"
|
||||
_tableAccents["í"] = "i"
|
||||
_tableAccents["î"] = "i"
|
||||
_tableAccents["ï"] = "i"
|
||||
_tableAccents["ñ"] = "n"
|
||||
_tableAccents["ò"] = "o"
|
||||
_tableAccents["ó"] = "o"
|
||||
_tableAccents["ô"] = "o"
|
||||
_tableAccents["õ"] = "o"
|
||||
_tableAccents["ö"] = "o"
|
||||
_tableAccents["ù"] = "u"
|
||||
_tableAccents["ú"] = "u"
|
||||
_tableAccents["û"] = "u"
|
||||
_tableAccents["ü"] = "u"
|
||||
_tableAccents["ý"] = "y"
|
||||
_tableAccents["ÿ"] = "y"
|
||||
_tableAccents["À"] = "A"
|
||||
_tableAccents["Á"] = "A"
|
||||
_tableAccents["Â"] = "A"
|
||||
_tableAccents["Ã"] = "A"
|
||||
_tableAccents["Ä"] = "A"
|
||||
_tableAccents["Ç"] = "C"
|
||||
_tableAccents["È"] = "E"
|
||||
_tableAccents["É"] = "E"
|
||||
_tableAccents["Ê"] = "E"
|
||||
_tableAccents["Ë"] = "E"
|
||||
_tableAccents["Ì"] = "I"
|
||||
_tableAccents["Í"] = "I"
|
||||
_tableAccents["Î"] = "I"
|
||||
_tableAccents["Ï"] = "I"
|
||||
_tableAccents["Ñ"] = "N"
|
||||
_tableAccents["Ò"] = "O"
|
||||
_tableAccents["Ó"] = "O"
|
||||
_tableAccents["Ô"] = "O"
|
||||
_tableAccents["Õ"] = "O"
|
||||
_tableAccents["Ö"] = "O"
|
||||
_tableAccents["Ù"] = "U"
|
||||
_tableAccents["Ú"] = "U"
|
||||
_tableAccents["Û"] = "U"
|
||||
_tableAccents["Ü"] = "U"
|
||||
_tableAccents["Ý"] = "Y"
|
||||
|
||||
-- Polish accents
|
||||
_tableAccents["ę"] = "e"
|
||||
_tableAccents["Ę"] = "Ę"
|
||||
_tableAccents["ó"] = "o"
|
||||
_tableAccents["Ó"] = "O"
|
||||
_tableAccents["ą"] = "a"
|
||||
_tableAccents["Ą"] = "A"
|
||||
_tableAccents["ś"] = "s"
|
||||
_tableAccents["Ś"] = "S"
|
||||
_tableAccents["ć"] = "c"
|
||||
_tableAccents["Ć"] = "C"
|
||||
_tableAccents["ż"] = "z"
|
||||
_tableAccents["Ż"] = "Z"
|
||||
_tableAccents["ź"] = "z"
|
||||
_tableAccents["Ź"] = "Z"
|
||||
_tableAccents["ł"] = "l"
|
||||
_tableAccents["Ł"] = "L"
|
||||
|
||||
-- Cyrillic script (Russian)
|
||||
_tableAccents["а"] = "a"
|
||||
_tableAccents["А"] = "A"
|
||||
_tableAccents["б"] = "b"
|
||||
_tableAccents["Б"] = "B"
|
||||
_tableAccents["в"] = "v"
|
||||
_tableAccents["В"] = "V"
|
||||
_tableAccents["г"] = "g"
|
||||
_tableAccents["Г"] = "G"
|
||||
_tableAccents["д"] = "d"
|
||||
_tableAccents["Д"] = "D"
|
||||
_tableAccents["е"] = "ye"
|
||||
_tableAccents["Е"] = "YE"
|
||||
_tableAccents["ё"] = "yo"
|
||||
_tableAccents["Ё"] = "YO"
|
||||
_tableAccents["ж"] = "zh"
|
||||
_tableAccents["Ж"] = "Zh"
|
||||
_tableAccents["з"] = "z"
|
||||
_tableAccents["З"] = "Z"
|
||||
_tableAccents["и"] = "ee"
|
||||
_tableAccents["И"] = "EE"
|
||||
_tableAccents["й"] = "i"
|
||||
_tableAccents["Й"] = "I"
|
||||
_tableAccents["к"] = "k"
|
||||
_tableAccents["К"] = "K"
|
||||
_tableAccents["л"] = "l"
|
||||
_tableAccents["Л"] = "L"
|
||||
_tableAccents["м"] = "m"
|
||||
_tableAccents["М"] = "M"
|
||||
-- _tableAccents["Н"] = "M" - disabled due to conflict with english
|
||||
_tableAccents["п"] = "p"
|
||||
_tableAccents["П"] = "P"
|
||||
-- _tableAccents["р"] = "r" - disabled due to conflict with english
|
||||
-- _tableAccents["Р"] = "R" - disabled due to conflict with english
|
||||
-- _tableAccents["с"] = "s" - disabled due to conflict with english
|
||||
-- _tableAccents["С"] = "S" - disabled due to conflict with english
|
||||
_tableAccents["т"] = "t"
|
||||
_tableAccents["Т"] = "T"
|
||||
_tableAccents["у"] = "u"
|
||||
_tableAccents["У"] = "U"
|
||||
_tableAccents["ф"] = "f"
|
||||
_tableAccents["Ф"] = "F"
|
||||
-- _tableAccents["х"] = "h" - disabled due to conflict with english
|
||||
-- _tableAccents["Х"] = "H" - disabled due to conflict with english
|
||||
_tableAccents["ц"] = "ts"
|
||||
_tableAccents["Ц"] = "TS"
|
||||
_tableAccents["ч"] = "ch"
|
||||
_tableAccents["Ч"] = "CH"
|
||||
_tableAccents["ш"] = "sh"
|
||||
_tableAccents["Ш"] = "SH"
|
||||
_tableAccents["щ"] = "sh"
|
||||
_tableAccents["Щ"] = "SH"
|
||||
_tableAccents["ъ"] = ""
|
||||
_tableAccents["Ъ"] = ""
|
||||
_tableAccents["ы"] = "i"
|
||||
_tableAccents["Ы"] = "I"
|
||||
_tableAccents["ь"] = ""
|
||||
_tableAccents["Ь"] = ""
|
||||
_tableAccents["э"] = "e"
|
||||
_tableAccents["Э"] = "E"
|
||||
_tableAccents["ю"] = "yu"
|
||||
_tableAccents["Ю"] = "YU"
|
||||
_tableAccents["я"] = "ya"
|
||||
_tableAccents["Я"] = "YA"
|
||||
|
||||
-- TBD additonal characters for other languages
|
||||
|
||||
-- Check string and replace special chars via replacement table
|
||||
local _normalizedString = ''
|
||||
for _strChar in string.gmatch(str, "([%z\1-\127\194-\244][\128-\191]*)") do
|
||||
if _tableAccents[_strChar] ~= nil then
|
||||
-- Replace char
|
||||
_normalizedString = _normalizedString.._tableAccents[_strChar]
|
||||
else
|
||||
-- No need to replace
|
||||
_normalizedString = _normalizedString.._strChar
|
||||
end
|
||||
end
|
||||
return _normalizedString
|
||||
end
|
||||
|
||||
Perun.GetCategory = function(id)
|
||||
-- Helper function returns object category basing on https://pastebin.com/GUAXrd2U
|
||||
local _killed_target_category = "Other"
|
||||
@@ -235,7 +79,7 @@ Perun.SideID2Name = function(id)
|
||||
[2] = 'BLUE',
|
||||
[3] = 'NEUTRAL', -- TBD check once this is released in DCS
|
||||
}
|
||||
if id > 0 then
|
||||
if id > 0 and id <= 3 then
|
||||
return _sides[id]
|
||||
else
|
||||
return "?"
|
||||
@@ -244,7 +88,6 @@ end
|
||||
|
||||
Perun.AddLog = function(text,LogLevel)
|
||||
-- Adds logs to DCS.log file
|
||||
LogLevel = LogLevel or 1
|
||||
if Perun.DebugMode >= LogLevel then
|
||||
net.log("[Perun] ".. text)
|
||||
end
|
||||
@@ -394,6 +237,7 @@ Perun.ConnectToPerun = function ()
|
||||
Perun.AddLog("Connecting to TCP server",2)
|
||||
Perun.TCP = assert(Perun.socket.tcp())
|
||||
Perun.TCP:settimeout(5000)
|
||||
Perun.TCP:setoption('tcp-nodelay' , true)
|
||||
|
||||
_, _err = Perun.TCP:connect(Perun.TCPPerunHost, Perun.TCPTargetPort)
|
||||
if _err then
|
||||
@@ -403,8 +247,7 @@ Perun.ConnectToPerun = function ()
|
||||
-- Connected
|
||||
Perun.AddLog("Success - connected to TCP server",2)
|
||||
end
|
||||
local _now = DCS.getRealTime()
|
||||
Perun.lastReconnect = _now
|
||||
Perun.lastReconnect = DCS.getRealTime()
|
||||
end
|
||||
|
||||
Perun.SendToPerun = function(data_id, data_package)
|
||||
@@ -418,7 +261,11 @@ Perun.SendToPerun = function(data_id, data_package)
|
||||
_TempData['dcs_current_frame_delay']=(DCS.getRealTime() - Perun.lastFrameStart) * 1000000
|
||||
|
||||
-- Build TCP frame
|
||||
local _TCPFrame="<SOT>" .. stripChars(net.lua2json(_TempData)) .. "<EOT>"
|
||||
local _now = DCS.getRealTime()
|
||||
local _delay_lua2json = 0
|
||||
|
||||
local _TCPFrame="<SOT>" .. net.lua2json(_TempData) .. "<EOT>"
|
||||
_delay_lua2json = (DCS.getRealTime() - _now) * 1000000
|
||||
|
||||
-- TCP Part - sending
|
||||
local _intStatus = nil
|
||||
@@ -426,21 +273,22 @@ Perun.SendToPerun = function(data_id, data_package)
|
||||
local _err=nil
|
||||
local _dropped = true
|
||||
local _delay = 0
|
||||
|
||||
-- Try to send a few times (defind in settings section)
|
||||
local _now = DCS.getRealTime()
|
||||
_now = DCS.getRealTime()
|
||||
while _intStatus == nil and _intTries < Perun.SendRetries do
|
||||
_intStatus, _err = Perun.TCP:send(_TCPFrame)
|
||||
if _err then
|
||||
-- Failure, packet was not send
|
||||
_delay = (DCS.getRealTime() - Perun.lastFrameStart) * 1000000
|
||||
Perun.AddLog("Packed not send : " .. data_id .. " , error: " .. _err .. ", tries: " .. _intTries .. ", delay: " .. _delay,2)
|
||||
_delay = (DCS.getRealTime() - _now) * 1000000
|
||||
Perun.AddLog("Packed not send : " .. data_id .. " , error: " .. _err .. ", tries: " .. _intTries .. ", delay: " .. _delay .. "us" .. ", lua2json: " .. _delay_lua2json .. "us",2)
|
||||
if _now > Perun.lastReconnect + Perun.ReconnectTimeout then
|
||||
Perun.ConnectToPerun()
|
||||
end
|
||||
else
|
||||
-- Succes, packet was send
|
||||
_delay = (DCS.getRealTime() - Perun.lastFrameStart) * 1000000
|
||||
Perun.AddLog("Packet send : " .. data_id .. " , tries:" .. _intTries .. ", delay: " .. _delay,2)
|
||||
_delay = (DCS.getRealTime() - _now) * 1000000
|
||||
Perun.AddLog("Packet send : " .. data_id .. " , tries:" .. _intTries .. ", delay: " .. _delay .. "us" .. ", lua2json: " .. _delay_lua2json .. "us",2)
|
||||
_dropped = false
|
||||
end
|
||||
_intTries=_intTries+1
|
||||
@@ -448,7 +296,8 @@ Perun.SendToPerun = function(data_id, data_package)
|
||||
|
||||
if _dropped == true then
|
||||
-- Add information to log file and send chat message to all that Perun connection is broken
|
||||
Perun.AddLog("ERROR - packed dropped : " .. data_id,1)
|
||||
_delay = (DCS.getRealTime() - _now) * 1000000
|
||||
Perun.AddLog("ERROR - packed dropped : " .. data_id .. ", delay: " .. _delay .. "us" .. ", lua2json: " .. _delay_lua2json .. "us",1)
|
||||
if _now > Perun.lastConnectionError + Perun.ReconnectTimeout then
|
||||
-- Informs all players that there is Peron error; below hack for DCS net.send_chat not working
|
||||
local _all_players = net.get_player_list()
|
||||
@@ -467,7 +316,7 @@ Perun.LogChat = function(playerID,msg,all)
|
||||
-- Logs chat messages
|
||||
local _TempData={}
|
||||
_TempData['player']= net.get_player_info(playerID, "name")
|
||||
_TempData['msg']=stripChars(msg)
|
||||
_TempData['msg']=msg
|
||||
_TempData['all']=all
|
||||
_TempData['ucid']=net.get_player_info(playerID, 'ucid')
|
||||
_TempData['datetime']=os.date('%Y-%m-%d %H:%M:%S')
|
||||
@@ -664,6 +513,7 @@ end
|
||||
|
||||
Perun.LogStatsGet = function(playerID)
|
||||
-- Gets Perun statistics array per player
|
||||
local _now = DCS.getRealTime()
|
||||
local next = next -- Make next function local - this improves performance
|
||||
local _player_hash = nil
|
||||
|
||||
@@ -692,16 +542,17 @@ Perun.LogStatsGet = function(playerID)
|
||||
-- Stats for player are empty
|
||||
Perun.LogStatsCount(playerID,'init') -- Init statistics
|
||||
end
|
||||
|
||||
Perun.AddLog("Getting stats data",2)
|
||||
|
||||
local _delay = (DCS.getRealTime() - _now) * 1000000
|
||||
Perun.AddLog("Getting stats data finished; took: " .. _delay .. "us",2)
|
||||
return Perun.StatData[_player_hash];
|
||||
end
|
||||
|
||||
Perun.UpdateStatus = function()
|
||||
-- Main function for status updates
|
||||
|
||||
-- Diagnostic data
|
||||
-- Update version data
|
||||
local _now = DCS.getRealTime()
|
||||
Perun.ServerData['v_dcs_hook']=Perun.Version
|
||||
|
||||
-- Update clients data - count connected players
|
||||
@@ -711,19 +562,22 @@ Perun.UpdateStatus = function()
|
||||
Perun.ServerData['c_players']=_count
|
||||
|
||||
-- Send
|
||||
Perun.AddLog("Sending server data",2)
|
||||
|
||||
local _delay = (DCS.getRealTime() - _now) * 1000000
|
||||
Perun.AddLog("Updated server data; sending (" .. _delay .. "us)",2)
|
||||
Perun.SendToPerun(1,Perun.ServerData)
|
||||
|
||||
-- Status data - update all subsections
|
||||
-- 1 - Mission data
|
||||
_now = DCS.getRealTime()
|
||||
local _TempData={}
|
||||
|
||||
_TempData['name']=DCS.getMissionName()
|
||||
_TempData['modeltime']=DCS.getModelTime()
|
||||
_TempData['realtime']=DCS.getRealTime()
|
||||
_TempData['pause']=DCS.getPause()
|
||||
_TempData['multiplayer']=DCS.isMultiplayer()
|
||||
_TempData['theatre'] = Perun.MissionData['mission']['theatre']
|
||||
_TempData['weather'] = Perun.MissionData['mission']['weather']
|
||||
Perun.StatusData["mission"] = _TempData
|
||||
|
||||
-- 2 - Players data
|
||||
@@ -734,12 +588,14 @@ Perun.UpdateStatus = function()
|
||||
Perun.StatusData["players"] = _PlayersTable
|
||||
|
||||
-- Send
|
||||
Perun.AddLog("Sending status data",2)
|
||||
_delay = (DCS.getRealTime() - _now) * 1000000
|
||||
Perun.AddLog("Updated status data; sending (" .. _delay .. "us)",2)
|
||||
Perun.SendToPerun(2,Perun.StatusData)
|
||||
end
|
||||
|
||||
Perun.UpdateSlots = function()
|
||||
-- Update and send slots data
|
||||
local _now = DCS.getRealTime()
|
||||
|
||||
Perun.SlotsData['coalitions']=DCS.getAvailableCoalitions()
|
||||
Perun.SlotsData['slots']={}
|
||||
@@ -760,18 +616,20 @@ Perun.UpdateSlots = function()
|
||||
end
|
||||
end
|
||||
|
||||
Perun.AddLog("Sending slots data",2)
|
||||
local _delay = (DCS.getRealTime() - _now) * 1000000
|
||||
Perun.AddLog("Updated slots data; sending (" .. _delay .. "us)",2)
|
||||
Perun.SendToPerun(3,Perun.SlotsData)
|
||||
end
|
||||
|
||||
Perun.UpdateMission = function()
|
||||
-- Main function for mission information updates
|
||||
Perun.AddLog("Updating mission data",2)
|
||||
|
||||
local _now = DCS.getRealTime()
|
||||
|
||||
-- Get mission information
|
||||
Perun.MissionData=DCS.getCurrentMission()
|
||||
|
||||
Perun.AddLog("Mission data updated",2)
|
||||
local _delay = (DCS.getRealTime() - _now) * 1000000
|
||||
Perun.AddLog("Mission data updated; saved (" .. _delay .. "us)",2)
|
||||
end
|
||||
|
||||
--- ################################ Event callbacks ################################
|
||||
@@ -820,7 +678,7 @@ Perun.onSimulationFrame = function()
|
||||
Perun.UpdateSlots()
|
||||
end
|
||||
|
||||
-- Send mission update and update JSON
|
||||
-- Send mission update
|
||||
if _now > Perun.lastSentMission + Perun.RefreshMission then
|
||||
Perun.lastSentMission = _now
|
||||
|
||||
@@ -874,6 +732,7 @@ end
|
||||
|
||||
Perun.onGameEvent = function (eventName,arg1,arg2,arg3,arg4,arg5,arg6,arg7)
|
||||
-- Game event has occured
|
||||
local _now = DCS.getRealTime()
|
||||
Perun.AddLog("Event handler for ".. eventName .. " started",2)
|
||||
|
||||
if eventName == "friendly_fire" then
|
||||
@@ -1023,7 +882,8 @@ Perun.onGameEvent = function (eventName,arg1,arg2,arg3,arg4,arg5,arg6,arg7)
|
||||
Perun.LogEvent(eventName,"Unknown event type",nil,nil);
|
||||
|
||||
end
|
||||
Perun.AddLog("Event handler for " .. eventName .. " finished",2)
|
||||
local _delay = (DCS.getRealTime() - _now) * 1000000
|
||||
Perun.AddLog("Event handler for " .. eventName .. " finished; it took: " .. _delay .. "us",2)
|
||||
end
|
||||
|
||||
-- ########### Finalize and set callbacks ###########
|
||||
|
||||
Reference in New Issue
Block a user