Skip to content

Commit

Permalink
add config disablejitwhenserver
Browse files Browse the repository at this point in the history
  • Loading branch information
fesily committed Aug 8, 2024
1 parent e874a97 commit 82f4fca
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 35 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.5.3)
project(DontStarveLuaJIT VERSION 0.5.8)

set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/Mod)
if (UNIX AND APPLE)
Expand Down
2 changes: 1 addition & 1 deletion Mod/bin64/windows/signatures_client.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Mod/bin64/windows/signatures_server.json

Large diffs are not rendered by default.

12 changes: 11 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.5.3"
version = "0.5.8"

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

Expand Down Expand Up @@ -76,6 +76,16 @@ configuration_options = {
{ description = "Off", data = false },
},
default = true
},
{
name = "DisableJITWhenServer",
label = translate({ en = "DisableJITWhenServer", zh = "服务器禁用luajit" }),
hover = translate({ en = "server process disable luajit mod", zh = "服务器进程禁用luajit" }),
options = {
{ description = "On", data = true },
{ description = "Off", data = false },
},
default = false
}
}
--restart_required = true
19 changes: 12 additions & 7 deletions Mod/modmain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@ do
local fp = _G.io.open("luajit_config.json", "w");
if fp then
local config = {
modmain_path = _G.debug.getinfo(1).source
modmain_path = _G.debug.getinfo(1).source,
server_disable_luajit = GetModConfigData("DisableJITWhenServer");
}
fp:write(_G.json.encode(config))
end
end

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


if GetModConfigData("EnabledJIT") then
local jit = require 'jit'
local hasluajit, jit = _G.pcall(require, 'jit')
if not hasluajit then
return
end
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",
Expand Down
6 changes: 6 additions & 0 deletions src/DontStarveInjector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ set(SOURCES
${DONTSTARVEINJECTOR_UTIL_DIR}/gum_platform.cpp
)

if (NOT MSVC)
list(APPEND SOURCES
luajit_config.cpp
)
endif()

find_package(libzip CONFIG REQUIRED)
find_package(ZLIB REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
Expand Down
10 changes: 9 additions & 1 deletion src/DontStarveInjector/DontStarveInjector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ extern "C" DONTSTARVEINJECTOR_API void Inject(bool isClient) {

#ifndef _WIN32
#include <dlfcn.h>
#include "luajit_config.hpp"

int (*origin)(const char* path);
int chdir_hook(const char* path){
Expand All @@ -288,7 +289,14 @@ int chdir_hook(const char* path){
std::this_thread::sleep_for(200ms);
}
#endif
Inject(!getExePath().string().contains("dontstarve_dedicated_server_nullrenderer"));
auto isClientMode = !getExePath().string().contains("dontstarve_dedicated_server_nullrenderer");
if (!isClientMode) {
auto config = luajit_config::read_from_file();
if (config && config->server_disable_luajit) {
return origin(path);
}
}
Inject(isClientMode);
spdlog::default_logger_raw()->flush();
injector = true;
}
Expand Down
1 change: 1 addition & 0 deletions src/DontStarveInjector/loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(Winmm_SOURCES
${DONTSTARVEINJECTOR_UTIL_DIR}/platform.cpp
PersistentString.cpp
updater.cpp
../luajit_config.cpp
)
if (ENABLE_STEAM_SUPPORT)
list(APPEND Winmm_SOURCES steam.cpp)
Expand Down
22 changes: 3 additions & 19 deletions src/DontStarveInjector/loader/updater.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,24 @@
#include <filesystem>
#include <fstream>
#include <string>
#include <string_view>
#include <format>
#include <charconv>
#include <Windows.h>
#include <spdlog/spdlog.h>
#include <nlohmann/json.hpp>
#include <fstream>
#include <string>
#include "platform.hpp"
#include "steam.hpp"
#include "PersistentString.hpp"
#include "../luajit_config.hpp"

using namespace std::literals;

std::filesystem::path getGameDir();

struct luajit_config {
std::string modmain_path;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(luajit_config, modmain_path);

static std::optional<luajit_config> read_from_file() {
auto path = getGameDir() / "data" / "luajit_config.json";
std::ifstream sf(path.string().c_str());
if (!sf.is_open())
return std::nullopt;
nlohmann::json j;
sf >> j;
return j.get<luajit_config>();
}
const std::optional<luajit_config>& getLuajitConfig();

static std::optional<std::filesystem::path> getModDir() {
static std::optional<luajit_config> config = read_from_file();
auto& config = getLuajitConfig();
if (config)
return std::filesystem::path(config->modmain_path).parent_path();
return std::nullopt;
Expand Down
17 changes: 13 additions & 4 deletions src/DontStarveInjector/loader/winmm_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "platform.hpp"
#include "PersistentString.hpp"
#include "steam.hpp"
#include "../luajit_config.hpp"

extern "C" {
#include <lua.hpp>
Expand Down Expand Up @@ -130,10 +131,10 @@ std::optional<std::filesystem::path> getModindexPath()
}
#endif


bool isClientMod = []() {
return !getExePath().filename().string().contains("server");
}();
const std::optional<luajit_config>& getLuajitConfig() {
static auto config = luajit_config::read_from_file();
return config;
}


void updater();
Expand All @@ -146,6 +147,14 @@ void DontStarveInjectorStart() {
spdlog::set_default_logger(std::make_shared<spdlog::logger>("", sinks.begin(), sinks.end()));
auto dir = getGameDir();

bool isClientMod = !getExePath().filename().string().contains("server");
if (!isClientMod) {
auto& config = getLuajitConfig();
if (config && config->server_disable_luajit) {
spdlog::error("config found disablejit when server: ON");
return;
}
}
// auto updater
if (isClientMod && !std::string_view(GetCommandLineA()).contains("-disable_check_luajit_mod")) {
updater();
Expand Down
17 changes: 17 additions & 0 deletions src/DontStarveInjector/luajit_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "luajit_config.hpp"
#include <nlohmann/json.hpp>
#include <fstream>

std::filesystem::path getGameDir();

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(luajit_config, modmain_path, server_disable_luajit);
std::optional<luajit_config> luajit_config::read_from_file(std::filesystem::path path) {
if (path.empty())
path = getGameDir() / "data" / "luajit_config.json";
std::ifstream sf(path.string().c_str());
if (!sf.is_open())
return std::nullopt;
nlohmann::json j;
sf >> j;
return j.get<luajit_config>();
}
10 changes: 10 additions & 0 deletions src/DontStarveInjector/luajit_config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include <string>
#include <optional>
#include <filesystem>

struct luajit_config {
std::string modmain_path;
bool server_disable_luajit;
static std::optional<luajit_config> read_from_file(std::filesystem::path path = {});
};

0 comments on commit 82f4fca

Please sign in to comment.