Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement an alternative way to pass parameters #30

Merged
merged 8 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
/src/Debug
/src/ArmaScriptProfiler.vcxproj.user
/src/Release
/out
*.biprivatekey

# CLion
Expand All @@ -50,4 +51,4 @@ cmake-build-*/
.hemtt/local
addons/*.pbo
keys/*
releases/*
releases/*
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "tracy"]
path = tracy
url = https://github.com/dedmen/tracy.git
[submodule "lib/json"]
path = lib/json
url = https://github.com/nlohmann/json.git
2 changes: 1 addition & 1 deletion intercept
Submodule intercept updated 80 files
+4 −2 src/client/CMakeLists.txt
+1 −1 src/client/headers/client/sqf/inventory.hpp
+123 −29 src/client/headers/shared/containers.hpp
+1 −2 src/client/headers/shared/functions.hpp
+8 −13 src/client/headers/shared/types.hpp
+1 −1 src/client/intercept/shared/client_types.cpp
+5 −5 src/client/intercept/shared/types.cpp
+8 −3 src/host/CMakeLists.txt
+1 −1 src/host/common/arguments.hpp
+1 −1 src/host/common/common.cpp
+0 −0 src/host/common/common.hpp
+1 −1 src/host/common/dispatch.hpp
+5 −0 src/host/common/easyloggingc++.hpp
+0 −0 src/host/common/system/spdlog/async_logger.h
+0 −0 src/host/common/system/spdlog/common.h
+0 −0 src/host/common/system/spdlog/details/async_log_helper.h
+0 −0 src/host/common/system/spdlog/details/async_logger_impl.h
+0 −0 src/host/common/system/spdlog/details/file_helper.h
+0 −0 src/host/common/system/spdlog/details/log_msg.h
+0 −0 src/host/common/system/spdlog/details/logger_impl.h
+0 −0 src/host/common/system/spdlog/details/mpmc_bounded_q.h
+0 −0 src/host/common/system/spdlog/details/null_mutex.h
+0 −0 src/host/common/system/spdlog/details/os.h
+0 −0 src/host/common/system/spdlog/details/pattern_formatter_impl.h
+0 −0 src/host/common/system/spdlog/details/registry.h
+0 −0 src/host/common/system/spdlog/details/spdlog_impl.h
+0 −0 src/host/common/system/spdlog/fmt/bundled/LICENSE.rst
+0 −0 src/host/common/system/spdlog/fmt/bundled/format.cc
+0 −0 src/host/common/system/spdlog/fmt/bundled/format.h
+0 −0 src/host/common/system/spdlog/fmt/bundled/ostream.cc
+0 −0 src/host/common/system/spdlog/fmt/bundled/ostream.h
+0 −0 src/host/common/system/spdlog/fmt/bundled/posix.cc
+0 −0 src/host/common/system/spdlog/fmt/bundled/posix.h
+0 −0 src/host/common/system/spdlog/fmt/bundled/printf.cc
+0 −0 src/host/common/system/spdlog/fmt/bundled/printf.h
+0 −0 src/host/common/system/spdlog/fmt/bundled/time.h
+0 −0 src/host/common/system/spdlog/fmt/fmt.h
+0 −0 src/host/common/system/spdlog/fmt/ostr.h
+0 −0 src/host/common/system/spdlog/formatter.h
+0 −0 src/host/common/system/spdlog/logger.h
+0 −0 src/host/common/system/spdlog/sinks/android_sink.h
+0 −0 src/host/common/system/spdlog/sinks/ansicolor_sink.h
+0 −0 src/host/common/system/spdlog/sinks/base_sink.h
+0 −0 src/host/common/system/spdlog/sinks/dist_sink.h
+0 −0 src/host/common/system/spdlog/sinks/file_sinks.h
+0 −0 src/host/common/system/spdlog/sinks/msvc_sink.h
+0 −0 src/host/common/system/spdlog/sinks/null_sink.h
+0 −0 src/host/common/system/spdlog/sinks/ostream_sink.h
+0 −0 src/host/common/system/spdlog/sinks/sink.h
+0 −0 src/host/common/system/spdlog/sinks/stdout_sinks.h
+0 −0 src/host/common/system/spdlog/sinks/syslog_sink.h
+0 −0 src/host/common/system/spdlog/sinks/wincolor_sink.h
+0 −0 src/host/common/system/spdlog/sinks/windebug_sink.h
+0 −0 src/host/common/system/spdlog/spdlog.h
+0 −0 src/host/common/system/spdlog/tweakme.h
+1 −1 src/host/controller/controller.hpp
+0 −4 src/host/extensions/export.cpp
+1 −4 src/host/extensions/export.hpp
+2 −2 src/host/extensions/extensions.cpp
+1 −1 src/host/extensions/extensions.hpp
+5 −1 src/host/extensions/search.cpp
+1 −1 src/host/extensions/search.hpp
+1 −1 src/host/extensions/signing.hpp
+3 −4 src/host/intercept_dll/CMakeLists.txt
+12 −3 src/host/intercept_dll/intercept_dll.cpp
+0 −66 src/host/invoker/invoker.cpp
+0 −4 src/host/invoker/invoker.hpp
+136 −113 src/host/invoker/sqf_functions.cpp
+22 −18 src/host/invoker/sqf_functions.hpp
+16 −0 src/host/loader/CommandScan.cpp
+84 −0 src/host/loader/CommandScan.hpp
+16 −0 src/host/loader/CommandScan214.cpp
+143 −0 src/host/loader/CommandTypes.hpp
+570 −0 src/host/loader/CommandTypesDynamic.hpp
+128 −0 src/host/loader/MemorySection.hpp
+41 −0 src/host/loader/StateTypes.hpp
+175 −362 src/host/loader/loader.cpp
+7 −106 src/host/loader/loader.hpp
+44 −0 src/host/loader/ptraccess.cpp
+23 −0 src/host/loader/ptraccess.h
1 change: 1 addition & 0 deletions lib/json
Submodule json added at 5d2754
13 changes: 13 additions & 0 deletions parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"profilerAdapter": "Tracy",
"profilerOutput": "",
"profilerEnableInstruction": true,
"profilerEnableEngine": false,
"profilerEngineThreads": false,
"profilerEngineDoFile": false,
"profilerEngineDoMem": false,
"profilerNoPaths": false,
"profilerNoInstrumentation": false,
"profilerEnableFAlloc": false,
"profilerEnableNetwork": true
}
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ cmake_minimum_required (VERSION 3.13)

set(INTERCEPT_CLIENT_PATH "${CMAKE_SOURCE_DIR}/intercept/src/client")
set(BROFILER_BASE_PATH "${CMAKE_SOURCE_DIR}/brofiler")
set(LIB_BASE_PATH "${CMAKE_SOURCE_DIR}/lib")

set(INTERCEPT_INCLUDE_PATH "${INTERCEPT_CLIENT_PATH}/headers" "${INTERCEPT_CLIENT_PATH}/headers/shared" "${INTERCEPT_CLIENT_PATH}/headers/client/" "${INTERCEPT_CLIENT_PATH}/headers/client/sqf")
set(BROFILER_INCLUDE_PATH "${BROFILER_BASE_PATH}/BrofilerCore" "${BROFILER_BASE_PATH}/ThirdParty/TaskScheduler/Scheduler/Include")
set(TRACY_INCLUDE_PATH "${CMAKE_SOURCE_DIR}/tracy")

set(JSON_INCLUDE_PATH "${LIB_BASE_PATH}/json/single_include")

if(USE_64BIT_BUILD)
set(INTERCEPT_PLUGIN_NAME "ArmaScriptProfiler_x64")
Expand Down Expand Up @@ -70,6 +71,7 @@ add_library( ${INTERCEPT_PLUGIN_NAME} SHARED ${library_sources} ${INTERCEPT_SOUR
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${INTERCEPT_INCLUDE_PATH} )
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${BROFILER_INCLUDE_PATH} )
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${TRACY_INCLUDE_PATH} )
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${JSON_INCLUDE_PATH} )

target_link_libraries (${INTERCEPT_PLUGIN_NAME} ${CMAKE_THREAD_LIBS_INIT})

Expand Down
94 changes: 92 additions & 2 deletions src/scriptProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
#include <intercept.hpp>
#include <numeric>
#include <random>
#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
#ifndef __linux__
#include <Windows.h>
#else
#include <signal.h>
#include <fstream>
#include <limits.h>
#include <unistd.h>
#include <link.h>
#include <cstring>
#endif
#include "ProfilerAdapter.hpp"
#include "AdapterArmaDiag.hpp"
Expand All @@ -26,6 +32,7 @@ using namespace intercept;
using namespace std::chrono_literals;
std::chrono::high_resolution_clock::time_point startTime;
static sqf_script_type* GameDataProfileScope_type;
static nlohmann::json json;

scriptProfiler profiler{};
bool instructionLevelProfiling = false;
Expand Down Expand Up @@ -896,6 +903,26 @@ std::optional<std::string> getCommandLineParam(std::string_view needle) {
adapterStr = adapterStr.substr(0, adapterStr.length() - 1);
return adapterStr;
}

if (!json.empty() && json.contains(needle.substr(1))) {
switch (json[needle.substr(1)].type()) {
case nlohmann::json::value_t::boolean:
{
if (json[needle.substr(1)].get<bool>()) {
return "true";
}
else {
dedmen marked this conversation as resolved.
Show resolved Hide resolved
return {};
}
}
case nlohmann::json::value_t::string:
{
return json[needle.substr(1)].get<std::string>();
}
default:
return {};
}
}
return {};
}

Expand Down Expand Up @@ -1215,9 +1242,73 @@ class GameInstructionNewExpression : public game_instruction {

#pragma endregion Instructions
#endif


#if __linux__
extern "C" int iterateCallback(struct dl_phdr_info* info, size_t size, void* data) {
std::filesystem::path sharedPath = info->dlpi_name;
if (sharedPath.filename() == "ArmaScriptProfiler_x64.so") {
// native won't convert, and we don't need it to convert
std::memcpy(data, sharedPath.c_str(), sharedPath.native().size());
killerswin2 marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}
return 0;
}
#endif

std::filesystem::path getSharedObjectPath() {
#if __linux__
char pathBuffer[PATH_MAX] = { 0 };
dl_iterate_phdr(iterateCallback, pathBuffer);
return std::filesystem::path{pathBuffer};

#else
wchar_t buffer[MAX_PATH] = { 0 };
HMODULE handle = nullptr;
if(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(&getSharedObjectPath), &handle) == 0) {
sqf::diag_log("getSharedObjectPath: GetModuleHandle failed");
return std::filesystem::path{};
}

DWORD len = GetModuleFileNameW(handle, buffer, MAX_PATH);
if (len == 0) {
return std::filesystem::path{};
}
return std::filesystem::path{buffer};
#endif
}

std::filesystem::path findConfigFilePath() {

std::filesystem::path path = getSharedObjectPath();
path = path.parent_path().parent_path();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be weird with nested mod folders like arma3server/server_mods/my_favourite_ones/@asp. Better to look inside @asp folder i.e. just one parent_path to also avoid bloating the arma server folder. Will also work better with server providers that symlinks the mod folders together into a temp folder.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getSharedObjectPath returns path like

arma3server/server_mods/my_favourite_ones/@asp/intercept/asp.dll
one parent is intercept/ folder, second parent is @asp/ folder

So this should be correct I'd think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it returns the full path to the plugin. To get the ASP folder we need to back out of file and the intercept directory. Yeah, it is a little cleaner to use the parent_path function rather than adding the "go up path" string and having to make the path normalized.


std::filesystem::path dirName{ "config"sv };
std::filesystem::path fileName{ "parameters.json"sv };
path = path / dirName / fileName;

return path;
}

void scriptProfiler::preStart() {
sqf::diag_log("Arma Script Profiler preStart");

std::filesystem::path filePath = findConfigFilePath();

if (!filePath.empty() && std::filesystem::exists(filePath)) {
sqf::diag_log("ASP: Found a Configuration File"sv);
std::ifstream file(filePath);
try {
json = nlohmann::json::parse(file);
}
catch (nlohmann::json::exception& error) {
// log error, set parameterFile false, and continue by using getCommandLineParam
sqf::diag_log(error.what());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefix error with ASP failed to read JSON config: or something to differentiate from other (Intercept) extensions that might be logging JSON errors

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is already prefixed. This line will be printed immediately after "ASP: Found a Configuration File" line. Good enough for me


}
}

auto startAdapter = getCommandLineParam("-profilerAdapter"sv);

if (startAdapter) {
Expand All @@ -1228,7 +1319,6 @@ void scriptProfiler::preStart() {
auto chromeAdapter = std::make_shared<AdapterChrome>();
GProfilerAdapter = chromeAdapter;
sqf::diag_log("ASP: Selected Chrome Adapter"sv);

auto chromeOutput = getCommandLineParam("-profilerOutput"sv);
if (chromeOutput)
chromeAdapter->setTargetFile(*chromeOutput);
Expand Down