Skip to content

Commit

Permalink
support jit.*
Browse files Browse the repository at this point in the history
add profilerjit
support server auto load mod
try fix luajit_config.json crash
  • Loading branch information
fesily committed Feb 10, 2025
1 parent 4b2bc8f commit 6185f50
Show file tree
Hide file tree
Showing 24 changed files with 6,898 additions and 58 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ else ()
message(FATAL_ERROR "Not supported platform")
endif ()

project(DontStarveLuaJIT VERSION 0.6.1)
project(DontStarveLuaJIT VERSION 0.7.0)

set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/Mod)
if (UNIX AND APPLE)
Expand Down
2 changes: 1 addition & 1 deletion Mod/install.bat
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if not exist "%destination%" (
)

echo [INFO] moving files...
robocopy "%source%" "%destination%" /E /MOVE /NP /NFL /NDL >NUL
robocopy "%source%" "%destination%" /E /NFL /NDL /IS /IT /IM >NUL

if errorlevel 8 (
echo [ERROR] moving files failed
Expand Down
16 changes: 15 additions & 1 deletion Mod/modinfo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ description = translate(

author = "fesil"

version = "0.6.1"
version = "0.7.0"

forumthread = "https://github.com/fesily/DontStarveLuaJit2"

Expand All @@ -33,8 +33,11 @@ reign_of_giants_compatible = true
dst_compatible = true

--TODO: need test compatible without the mod
--all_clients_require_mod = true
client_only_mod = true

priority = 2e53

-- Preview image
icon_atlas = "images/modicon.xml"
icon = "modicon.tex"
Expand Down Expand Up @@ -85,6 +88,17 @@ configuration_options = {
{ description = "Off", data = false },
},
default = false
},
{
name = "EnableProfiler",
label = translate({ en = "EnableProfilerCosoleCommand", zh = "启用性能分析控制台命令" }),
hover = translate({ en = "ProfilerJit.start | ProfilerJit.stop", zh = "ProfilerJit.start | ProfilerJit.stop" }),
options = {
{ description = translate({en = "off", zh = "关闭"}), data = "off" },
{ description = translate({en = "Detailed Sampling Mode", zh = "详细采样模式"}), data = "fzvp" },
{ description = translate({en = "Origin Sampling Mode", zh = "原始采样模式"}), data = "Gz" },
},
default = 'off'
}
}
--restart_required = true
181 changes: 129 additions & 52 deletions Mod/modmain.lua
Original file line number Diff line number Diff line change
@@ -1,63 +1,140 @@
_G = GLOBAL

local hasluajit, jit = _G.pcall(require, 'jit')
local hasluajit, jit = _G.pcall(require, "jit")
if not hasluajit then
return
return
end

if not _G.TheNet:IsDedicated() then
local fp = _G.io.open("unsafedata/luajit_config.json", "w");
if fp then
local config = {
modmain_path = _G.debug.getinfo(1).source,
server_disable_luajit = GetModConfigData("DisableJITWhenServer");
}
fp:write(_G.json.encode(config))
end
local fp = _G.io.open("unsafedata/luajit_config.json", "w")
if fp then
local config = {
modmain_path = _G.debug.getinfo(1).source,
server_disable_luajit = GetModConfigData("DisableJITWhenServer"),
}
fp:write(_G.json.encode(config))
fp:close()
end
end

local old_GetServerModNames = _G.KnownModIndex.GetServerModNames
local old_GetServerModNamesTable = _G.KnownModIndex.GetServerModNamesTable
local old_GetEnabledServerModNames = _G.ModManager.GetEnabledServerModNames

_G.KnownModIndex.GetServerModNames = function (...)
local names = old_GetServerModNames(...)
table.insert(names, modname)
return names
end

_G.KnownModIndex.GetServerModNamesTable = function (...)
local names = old_GetServerModNamesTable(...)
table.insert(names, {modname = modname})
return names
end
_G.ModManager.GetEnabledServerModNames = function (...)
local names = old_GetEnabledServerModNames(...)
table.insert(names, modname)
return names
end

if GetModConfigData("EnabledJIT") then
local TEMPLATES = require("widgets/redux/templates")
local old_getbuildstring = TEMPLATES.GetBuildString
TEMPLATES.GetBuildString = function()
return (old_getbuildstring() or "") .. "(LuaJIT)"
end

if GetModConfigData("JitOpt") then
require("jit.opt").start(
"minstitch=2",
"maxtrace=4000",
"maxrecord=8000",
"sizemcode=64",
"maxmcode=4000",
"maxirconst=1000"
)
end

local enbaleBlackList = GetModConfigData("ModBlackList")

AddSimPostInit(function()
jit.on()

local prefix = "../mods/workshop-"
local blacklists = {}
if enbaleBlackList and #blacklists > 0 then
for i in ipairs(blacklists) do
blacklists[i] = prefix .. blacklists[i]
end
local function startWith(str, prefix)
return str:find(prefix, 1, true) == 1
end
local _kleiloadlua = _G.kleiloadlua
_G.kleiloadlua = function(script, ...)
local m = _kleiloadlua(script, ...)
if type(script) == "string" then
for _, blacklist in ipairs(blacklists) do
if startWith(script, blacklist) then
jit.off(m, true)
break
end
end
end
return m
end
end
end)

if GetModConfigData("EnableProfiler") ~= "off" then
local env = _G.getfenv()
env.modimport = function(modulename)
_G.print("modimport: " .. MODROOT .. modulename, _G.package.path)
if string.sub(modulename, #modulename - 3, #modulename) ~= ".lua" then
modulename = modulename .. ".lua"
end
local result = _G.kleiloadlua(MODROOT .. modulename)
if result == nil then
_G.error("Error in modimport: " .. modulename .. " not found!")
elseif type(result) == "string" then
_G.error(
"Error in modimport: " .. ModInfoname(modname) .. " importing " .. modulename .. "!\n" .. result
)
else
_G.setfenv(result, setmetatable(env, { __index = _G }))
result()
end
end
local old_require = env.require
env.require = function(modulename)
local ok, ret = _G.pcall(old_require, modulename)
if ok then
return ret
end
return env.modimport(modulename)
end

local profiler = require("jit.p")
local zone = require("jit.zone")
local mode = GetModConfigData("EnableProfiler")

local TEMPLATES = require "widgets/redux/templates"
local old_getbuildstring = TEMPLATES.GetBuildString
TEMPLATES.GetBuildString = function()
return (old_getbuildstring() or "") .. "(LuaJIT)"
end

if GetModConfigData("JitOpt") then
require("jit.opt").start("minstitch=2", "maxtrace=4000",
"maxrecord=8000", "sizemcode=64",
"maxmcode=4000", "maxirconst=1000")
end

local enbaleBlackList = GetModConfigData("ModBlackList")

AddSimPostInit(function()
jit.on()

local prefix = "../mods/workshop-"
local blacklists = {
}
if enbaleBlackList and #blacklists > 0 then
for i in ipairs(blacklists) do
blacklists[i] = prefix .. blacklists[i]
end
local function startWith(str, prefix)
return str:find(prefix, 1, true) == 1
end
local _kleiloadlua = _G.kleiloadlua
_G.kleiloadlua = function(script, ...)
local m = _kleiloadlua(script, ...)
if (type(script) == "string") then
for _, blacklist in ipairs(blacklists) do
if startWith(script, blacklist) then
jit.off(m, true)
break
end
end
end
return m
end
end
end)
_G.rawset(_G, "ProfilerJit", {
start = function(m)
m = m or mode
local sim = _G.getmetatable(TheSim).__index
local old_profiler_push = sim.ProfilerPush
sim.ProfilerPush = function(name, ...)
zone(name)
old_profiler_push(name, ...)
end
local old_profiler_pop = sim.ProfilerPop
sim.ProfilerPop = function(...)
zone()
old_profiler_pop(...)
end
profiler.start(m, "unsafedata/profiler")
end,
stop = profiler.stop,
})
end
end
Loading

0 comments on commit 6185f50

Please sign in to comment.