1313#include " Event.h"
1414#include " AdapterChrome.hpp"
1515#include " AdapterTracy.hpp"
16+ #include < memory>
17+ #include < string>
1618
1719using namespace intercept ;
1820using namespace std ::chrono_literals;
@@ -579,6 +581,46 @@ game_value compileRedirect2(const game_state& state, game_value_parameter messag
579581 return comp;
580582}
581583
584+ game_value compileRedirectFinal (const game_state& state, game_value_parameter message) {
585+ if (!profiler.compileScope ) {
586+ static r_string compileEventText (" compile" );
587+ static r_string profName (" scriptProfiler.cpp" );
588+ profiler.compileScope = GProfilerAdapter->createScope (compileEventText, profName, __LINE__);
589+ }
590+
591+ auto tempData = GProfilerAdapter->enterScope (profiler.compileScope );
592+
593+ r_string str = message;
594+
595+ auto comp = sqf::compile_final (str);
596+ auto bodyCode = static_cast <game_data_code*>(comp.data .get ());
597+ if (!bodyCode->instructions ) {
598+ GProfilerAdapter->leaveScope (tempData);
599+ return comp;
600+ }
601+
602+ #ifdef WITH_BROFILER
603+ if (auto brofilerData = std::dynamic_pointer_cast<ScopeTempStorageBrofiler>(tempData)) {
604+ r_string src = getScriptFromFirstLine (bodyCode->instructions ->front ()->sdp , false );
605+ brofilerData->evtDt ->sourceCode = src;
606+ }
607+ #endif
608+
609+ GProfilerAdapter->leaveScope (tempData);
610+
611+ auto & funcPath = bodyCode->instructions ->front ()->sdp .sourcefile ;
612+ std::string scriptName = getScriptName (str, funcPath, 32 );
613+ // if (scriptName.empty()) scriptName = "<unknown>";
614+
615+ if (bodyCode->instructions && !scriptName.empty () && scriptName != " <unknown>" )
616+ addScopeInstruction (bodyCode, scriptName);
617+
618+ return comp;
619+ }
620+
621+
622+
623+
582624game_value callExtensionRedirect (uintptr_t st, game_value_parameter ext, game_value_parameter msg) {
583625 if (!profiler.callExtScope ) {
584626 static r_string compileEventText (" callExtension" );
@@ -640,36 +682,6 @@ std::optional<std::string> getCommandLineParam(std::string_view needle) {
640682}
641683
642684scriptProfiler::scriptProfiler () {
643- if (getCommandLineParam (" -profilerEnableInstruction" sv)) {
644- instructionLevelProfiling = true ;
645- }
646-
647- auto startAdapter = getCommandLineParam (" -profilerAdapter" sv);
648-
649- if (startAdapter) {
650- if (false ) {
651- #ifdef WITH_CHROME
652- } else if (*startAdapter == " Chrome" sv) {
653- auto chromeAdapter = std::make_shared<AdapterChrome>();
654- GProfilerAdapter = chromeAdapter;
655-
656- auto chromeOutput = getCommandLineParam (" -profilerOutput" sv);
657- if (chromeOutput)
658- chromeAdapter->setTargetFile (*chromeOutput);
659- #endif
660- #ifdef WITH_BROFILER
661- } else if (*startAdapter == " Brofiler" sv) {
662- GProfilerAdapter = std::make_shared<AdapterBrofiler>();
663- #endif
664- } else if (*startAdapter == " Arma" sv) {
665- GProfilerAdapter = std::make_shared<AdapterArmaDiag>();
666- } else if (*startAdapter == " Tracy" sv) {
667- GProfilerAdapter = std::make_shared<AdapterTracy>();
668- }
669- } else {
670- GProfilerAdapter = std::make_shared<AdapterTracy>();
671- }
672-
673685
674686}
675687#ifndef __linux__
@@ -980,6 +992,45 @@ class GameInstructionNewExpression : public game_instruction {
980992#pragma endregion Instructions
981993#endif
982994void scriptProfiler::preStart () {
995+
996+ if (getCommandLineParam (" -profilerEnableInstruction" sv)) {
997+ instructionLevelProfiling = true ;
998+ }
999+
1000+ auto startAdapter = getCommandLineParam (" -profilerAdapter" sv);
1001+
1002+ if (startAdapter) {
1003+ if (false ) {
1004+ #ifdef WITH_CHROME
1005+ }
1006+ else if (*startAdapter == " Chrome" sv) {
1007+ auto chromeAdapter = std::make_shared<AdapterChrome>();
1008+ GProfilerAdapter = chromeAdapter;
1009+
1010+ auto chromeOutput = getCommandLineParam (" -profilerOutput" sv);
1011+ if (chromeOutput)
1012+ chromeAdapter->setTargetFile (*chromeOutput);
1013+ #endif
1014+ #ifdef WITH_BROFILER
1015+ }
1016+ else if (*startAdapter == " Brofiler" sv) {
1017+ GProfilerAdapter = std::make_shared<AdapterBrofiler>();
1018+ #endif
1019+ }
1020+ else if (*startAdapter == " Arma" sv) {
1021+ GProfilerAdapter = std::make_shared<AdapterArmaDiag>();
1022+ }
1023+ else if (*startAdapter == " Tracy" sv) {
1024+ GProfilerAdapter = std::make_shared<AdapterTracy>();
1025+ }
1026+ }
1027+ else {
1028+ GProfilerAdapter = std::make_shared<AdapterTracy>();
1029+ }
1030+
1031+
1032+
1033+
9831034 #ifndef __linux__
9841035 if (getCommandLineParam (" -profilerEnableEngine" sv)) {
9851036 engineProf = std::make_shared<EngineProfiling>();
@@ -1003,7 +1054,7 @@ void scriptProfiler::preStart() {
10031054 static auto _profilerCompile = client::host::register_sqf_command (" compile" , " Profiler redirect" , compileRedirect2, game_data_type::CODE, game_data_type::STRING);
10041055 // static auto _profilerCompile2 = client::host::register_sqf_command("compile2", "Profiler redirect", compileRedirect, game_data_type::CODE, game_data_type::STRING);
10051056 // static auto _profilerCompile3 = client::host::register_sqf_command("compile3", "Profiler redirect", compileRedirect2, game_data_type::CODE, game_data_type::STRING);
1006- static auto _profilerCompileF = client::host::register_sqf_command (" compileFinal" , " Profiler redirect" , compileRedirect2 , game_data_type::CODE, game_data_type::STRING);
1057+ static auto _profilerCompileF = client::host::register_sqf_command (" compileFinal" , " Profiler redirect" , compileRedirectFinal , game_data_type::CODE, game_data_type::STRING);
10071058 static auto _profilerCallExt = client::host::register_sqf_command (" callExtension" , " Profiler redirect" , callExtensionRedirect, game_data_type::STRING, game_data_type::STRING, game_data_type::STRING);
10081059 static auto _profilerDiagLog = client::host::register_sqf_command (" diag_log" , " Profiler redirect" , diag_logRedirect, game_data_type::NOTHING, game_data_type::ANY);
10091060 static auto _profilerProfScript = client::host::register_sqf_command (" profileScript" , " Profiler redirect" , profileScript, game_data_type::ARRAY, game_data_type::ARRAY);
0 commit comments