Skip to content

Commit

Permalink
use flag ugc_directory
Browse files Browse the repository at this point in the history
  • Loading branch information
fesily committed Aug 21, 2024
1 parent 1a58166 commit 96774e8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 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.8)
project(DontStarveLuaJIT VERSION 0.5.9)

set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/Mod)
if (UNIX AND APPLE)
Expand Down
2 changes: 1 addition & 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.8"
version = "0.5.9"

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

Expand Down
22 changes: 17 additions & 5 deletions src/DontStarveInjector/gameio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include <optional>
#include "util/zipfile.hpp"
#include "util/gum_platform.hpp"
#include "util/platform.hpp"

#include <spdlog/spdlog.h>

#include <thread>
#include <chrono>
Expand Down Expand Up @@ -46,8 +49,18 @@ static std::filesystem::path to_path(const char *p) {
}

static std::optional<std::filesystem::path> get_workshop_dir() {
static auto dir = std::filesystem::relative(std::filesystem::path("..") / ".." / ".." / "workshop" / "content" / "322330");
return dir;
auto dir = std::filesystem::relative(std::filesystem::path("..") / ".." / ".." / "workshop");
const auto cmd = get_cwd();
auto flag = "-ugc_directory";
if (cmd.contains(flag)) {
const auto cmds = get_cwds();
auto iter = std::find(cmds.begin(), cmds.end(), flag);
iter++;
const auto& value = *iter;
dir = value;
spdlog::info("workshop_dir ugc_directory: {}", value);
}
return dir / "content" / "322330";
}

static FILE *lj_fopen(char const *f, const char *mode) noexcept {
Expand All @@ -58,7 +71,7 @@ static FILE *lj_fopen(char const *f, const char *mode) noexcept {
if (fp)
return fp;

const auto &workshop_dir = get_workshop_dir();
static const auto workshop_dir = get_workshop_dir();
constexpr auto mods_root = "../mods/workshop-"sv;
if (path_s.starts_with(mods_root) && workshop_dir) {
auto mod_path = std::filesystem::path(path_s.substr(mods_root.size()));
Expand Down Expand Up @@ -195,8 +208,6 @@ static void lj_clearerr(FILE *fp) noexcept {
return clearerr(fp);
}

#include "util/platform.hpp"

static int lj_need_transform_path() noexcept {
static bool has_lua_debug_flag = [] {
std::string_view cmd = get_cwd();

Check warning on line 213 in src/DontStarveInjector/gameio.cpp

View workflow job for this annotation

GitHub Actions / compile (macos-latest, macos, macos-x64)

object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl]

Check warning on line 213 in src/DontStarveInjector/gameio.cpp

View workflow job for this annotation

GitHub Actions / compile (macos-latest, macos, macos-x64)

object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl]
Expand All @@ -205,6 +216,7 @@ static int lj_need_transform_path() noexcept {
}
return cmd.contains("-enable_lua_debugger");
}();
spdlog::info("lj_need_transform_path: {}", has_lua_debug_flag);
return has_lua_debug_flag;
}

Expand Down
21 changes: 20 additions & 1 deletion src/DontStarveInjector/util/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static std::string GetCommandLineByPid(DWORD processId) {
HMODULE hMod;
DWORD cbNeeded;

// »ñÈ¡ÃüÁîÐÐ
// ��ȡ������
PROCESS_BASIC_INFORMATION pbi;
ULONG returnLength;
if (NtQueryInformationProcess(hProcess, ProcessBasicInformation, &pbi, sizeof(pbi), &returnLength) == 0) {
Expand Down Expand Up @@ -178,6 +178,25 @@ std::string get_cwd(uintptr_t pid) {
#endif
}

std::vector<std::string> get_cwds(uintptr_t pid) {
std::vector<std::string> cmds;
#ifdef _WIN32
cmds.resize(__argc);
for (int i = 0; i < __argc; i++) {
cmds[i] = __argv[i];
}
#else
auto param = pid == 0 ? std::string("/proc/self/cmdline") : "/proc/" + std::to_string(pid) + "/cmdline";
std::ifstream file(param.c_str());
std::string cmd;
std::string cmdline;
while (std::getline(file, cmdline, '\0')) {
cmds.push_back(cmdline);
}
#endif
return cmds;
}

void set_worker_directory(const char *path) {
#ifdef _WIN32
SetCurrentDirectoryA(path);
Expand Down
2 changes: 2 additions & 0 deletions src/DontStarveInjector/util/platform.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <filesystem>
#include <vector>

std::filesystem::path getExePath();

Expand All @@ -15,5 +16,6 @@ void unloadlib(module_handler_t h);
uintptr_t getParentId();

std::string get_cwd(uintptr_t pid = 0);
std::vector<std::string> get_cwds(uintptr_t pid = 0);

void set_worker_directory(const char *path);

0 comments on commit 96774e8

Please sign in to comment.