Skip to content

Commit 0baac6b

Browse files
committed
Roblox launching + multiple log folder support for activity watcher
1 parent ebeca62 commit 0baac6b

12 files changed

Lines changed: 268 additions & 210 deletions

File tree

include/BootstrapScripts/ScriptManager.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace ScriptManager {
1515
std::vector<std::string> platform;
1616
std::string run;
1717
std::vector<AppDataDirectory> appdirectories;
18+
std::vector<std::string> logfolders;
1819
std::vector<std::string> capabilities;
1920
};
2021

@@ -35,4 +36,7 @@ namespace ScriptManager {
3536
inline bool HasCapability(const BootstrapScript &script, const std::string &cap) {
3637
return std::find(script.capabilities.begin(), script.capabilities.end(), cap) != script.capabilities.end();
3738
}
39+
40+
// returns a bootstrap script struct by it's name
41+
BootstrapScript* FindFirstScriptByName(std::string);
3842
}

include/Bootstrapper.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include "NativeStrapper.hpp"
4+
#include <string>
5+
6+
namespace Bootstrapper {
7+
enum BootstrapResult {
8+
BOOTSTRAP_SUCCESS,
9+
BOOTSTRAP_SCRIPT_NOT_FOUND,
10+
BOOTSTRAP_SCRIPT_ERR
11+
};
12+
extern std::string Exception;
13+
14+
BootstrapResult MainBootstrap(NativeStrapper::ArgConfig*);
15+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "BootstrapScripts/ScriptManager.hpp"
4+
35
namespace ActivityWatcher {
4-
void StartWatcherThread();
6+
void StartWatcherThread(ScriptManager::BootstrapScript*);
57
}

include/DiscordRPC/TailFileWatcher.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string>
44
#include <filesystem>
55
#include <unordered_map>
6+
#include <vector>
67

78
namespace TailFileWatcher {
89
struct FileInfo {
@@ -11,10 +12,10 @@ namespace TailFileWatcher {
1112
};
1213

1314
struct FileWatcherObj {
14-
std::unordered_map<std::string, FileInfo> files;
15+
std::unordered_map<std::string, std::unordered_map<std::string, FileInfo>> filesByFolder;
1516
std::string undealt_with;
1617
};
1718

18-
FileWatcherObj* InitTailFileWatcher(const std::string& folder_path);
19-
bool TailFileWatcherDealWith(FileWatcherObj* obj, const std::string& folder_path);
19+
FileWatcherObj* InitTailFileWatcher(const std::vector<std::string>& folders);
20+
bool TailFileWatcherDealWith(TailFileWatcher::FileWatcherObj* obj, const std::vector<std::string>& folders);
2021
}

include/NativeStrapper.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#pragma once
2+
#include <cstddef>
23

34
namespace NativeStrapper {
5+
struct ArgConfig {
6+
char* URI = NULL;
7+
char* BootstrapScript = NULL;
8+
bool ActivityWatch = false;
9+
};
10+
411
inline constexpr const char* NativeStrapperVersion = "0.10";
512
inline constexpr const char* NativeStrapperRepo = "https://github.com/3443e/NativeStrapper";
613
inline constexpr const char* NativeStrapperDocs = "https://nativestrapper.gitlab.io/";

providedscripts/Linux/bypass.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ metadata = {
66
appdirectories = {
77
{path = "/home/" ..NativeStrapper.Constants.USER.. "/.var/app/org.vinegarhq.Sober/data/sober/appData", label = "Main"}
88
},
9-
log_folders = {"/home/" ..NativeStrapper.Constants.USER.. "/.var/app/org.vinegarhq.Sober/data/sober/appData/logs"}
9+
logfolders = {
10+
"/home/" ..NativeStrapper.Constants.USER.. "/ov2",
11+
"/home/" ..NativeStrapper.Constants.USER.. "/.var/app/org.vinegarhq.Sober/data/sober/appData/logs"
12+
}
1013
}
1114

1215
function bootstrap(uri)

src/BootstrapScripts/ScriptManager.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <QFile>
88
#include <QString>
99
#include <QRegularExpression>
10-
1110
// woo
1211
extern "C" {
1312
#include <lua.h>
@@ -130,6 +129,11 @@ bool ScriptManager::ReadMetadata(const std::string &luaFilePath, BootstrapScript
130129
}
131130
lua_pop(L, 1);
132131

132+
lua_getfield(L, metaIndex, "logfolders");
133+
if (lua_istable(L, -1)) {
134+
out.logfolders = ReadStringArray(L, lua_gettop(L));
135+
}
136+
lua_pop(L, 1);
133137

134138
lua_getfield(L, metaIndex, "capabilities");
135139
if (lua_istable(L, -1)) {
@@ -227,4 +231,13 @@ void ScriptManager::LoadScripts() {
227231
for (const auto &script : ScriptManager::LoadedScripts) {
228232
URIHandler::InstallURIs(script.title, script.uris);
229233
}
234+
}
235+
236+
ScriptManager::BootstrapScript* ScriptManager::FindFirstScriptByName(std::string scriptName) {
237+
QString safeScriptName = QString::fromStdString(scriptName).toLower().replace(" ", "-");
238+
for (auto &script : ScriptManager::LoadedScripts) {
239+
QString safeTitle = QString::fromStdString(script.title).toLower().replace(" ", "-");
240+
if (safeTitle == safeScriptName) return &script;
241+
}
242+
return NULL; // nuh
230243
}

src/Bootstrapper.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <thread>
2+
#include "Bootstrapper.hpp"
3+
#include "BootstrapScripts/LuaScript.hpp"
4+
#include "BootstrapScripts/ScriptManager.hpp"
5+
#include "Logger.hpp"
6+
#include "UserInterface/BootstrapWindow.hpp"
7+
#include "UserInterface/OnboardingWindow.hpp"
8+
#include <QCoreApplication>
9+
#include <QProcess>
10+
#include <QMessageBox>
11+
12+
std::string Bootstrapper::Exception = "";
13+
14+
Bootstrapper::BootstrapResult Bootstrapper::MainBootstrap(NativeStrapper::ArgConfig* argConfig) {
15+
if (argConfig->URI && argConfig->BootstrapScript) {
16+
Logger::Log("Launched with bootstrap script and URI.", Logger::LogSeverity::SLOG, "MainBootstrap");
17+
QString safeTitle = QString::fromStdString(std::string(argConfig->BootstrapScript)).toLower().replace(" ", "-");
18+
19+
ScriptManager::BootstrapScript* script = ScriptManager::FindFirstScriptByName(argConfig->BootstrapScript);
20+
if (script == NULL) {
21+
Logger::Log("Couldn't find bootstrap script.", Logger::LogSeverity::SFATAL, "MainBootstrap");
22+
Bootstrapper::Exception = QString("Bootstrap script \"%1\" not found. Please open NativeStrapper and re-import it.").arg(argConfig->BootstrapScript).toStdString();
23+
return Bootstrapper::BootstrapResult::BOOTSTRAP_SCRIPT_NOT_FOUND;
24+
}
25+
26+
Logger::Log("Found bootstrap script, creating bootstrapper window", Logger::LogSeverity::SLOG, "MainBootstrap");
27+
BootstrapWindow *w = new BootstrapWindow();
28+
w->show();
29+
30+
Logger::OnLog = [w](std::string message, Logger::LogSeverity severity, std::string from) {
31+
QMetaObject::invokeMethod(w, [w, message]() {
32+
w->setLog(QString::fromStdString(message));
33+
}, Qt::QueuedConnection);
34+
};
35+
36+
Logger::Log("Executing bootstrap script...", Logger::LogSeverity::SINFO, "MainBootstrap");
37+
38+
std::string scriptPath = ScriptManager::GetScriptPath(script->title);
39+
std::string uri = std::string(argConfig->URI);
40+
std::string runCmd = script->run;
41+
42+
size_t pos = runCmd.find("%u");
43+
if (pos != std::string::npos) {
44+
runCmd.replace(pos, 2, uri);
45+
}
46+
47+
std::thread scriptThread([scriptPath, uri, w, runCmd, safeTitle]() {
48+
bool RunResult = LuaScript::RunScript(scriptPath, uri, w);
49+
if (!RunResult) {
50+
Logger::Log("Script experienced an error while running.", Logger::LogSeverity::SFATAL, "MainBootstrap");
51+
Bootstrapper::Exception = "Script experienced an error while running.";
52+
return Bootstrapper::BootstrapResult::BOOTSTRAP_SCRIPT_ERR;
53+
}
54+
55+
// calling these functions directly causes a segfault, smh... SMH. SHAKING MY HEAD, SHAKING MY FUCKING HEAD
56+
QMetaObject::invokeMethod(w, [w]() {
57+
w->setStatus("Starting Roblox...");
58+
w->setIndeterminate(true);
59+
}, Qt::QueuedConnection);
60+
61+
QStringList parts = QProcess::splitCommand(
62+
QString::fromStdString(runCmd)
63+
);
64+
QString robloxexepath = parts.takeFirst();
65+
QString exePath = QCoreApplication::applicationFilePath();
66+
67+
bool robloxStarted = QProcess::startDetached(robloxexepath, parts);
68+
69+
if (robloxStarted) {
70+
QProcess::startDetached(exePath, {"--bootstrap-script", safeTitle, "--activity-watch"});
71+
QMetaObject::invokeMethod(qApp,[]() {
72+
QCoreApplication::quit();
73+
}, Qt::QueuedConnection);
74+
}
75+
});
76+
77+
scriptThread.detach();
78+
79+
return Bootstrapper::BootstrapResult::BOOTSTRAP_SUCCESS;
80+
} else {
81+
auto* w = new OnboardingWindow(); /* putting onboarding window on the stack causes it to not show */
82+
w->show();
83+
}
84+
85+
return Bootstrapper::BootstrapResult::BOOTSTRAP_SUCCESS;
86+
}

src/DiscordRPC/ActivityWatcher.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ enum RPCState {
3434
RPCNIL
3535
};
3636

37-
void MainActivityWatcher() {
38-
auto* watcher = TailFileWatcher::InitTailFileWatcher(GetLogDir());
37+
void MainActivityWatcher(ScriptManager::BootstrapScript* bootstrapScript) {
38+
auto* watcher = TailFileWatcher::InitTailFileWatcher(bootstrapScript->logfolders);
3939
RoLogObject* currentlogobj = nullptr;
4040
bool wasingame = false;
4141

@@ -49,12 +49,12 @@ void MainActivityWatcher() {
4949

5050
while (true) {
5151
std::this_thread::sleep_for(std::chrono::seconds(1));
52-
TailFileWatcher::TailFileWatcherDealWith(watcher, GetLogDir());
52+
TailFileWatcher::TailFileWatcherDealWith(watcher, bootstrapScript->logfolders);
5353

5454
std::stringstream ss(watcher->undealt_with);
5555
std::string line;
5656
while (std::getline(ss, line)) {
57-
if (!currentlogobj && !watcher->files.empty()) {
57+
if (!currentlogobj && !watcher->filesByFolder.empty()) {
5858
currentlogobj = RoLogCreateObject(nullptr);
5959
}
6060
if (!currentlogobj) {
@@ -94,8 +94,8 @@ void MainActivityWatcher() {
9494
}
9595
}
9696

97-
void ActivityWatcher::StartWatcherThread() {
98-
std::thread watcherthread(MainActivityWatcher);
97+
void ActivityWatcher::StartWatcherThread(ScriptManager::BootstrapScript* bootstrapScript) {
98+
std::thread watcherthread(MainActivityWatcher, bootstrapScript);
9999

100100
{
101101
std::stringstream ss;

0 commit comments

Comments
 (0)