forked from citizenfx/fivem
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConsoleBinding.cpp
More file actions
156 lines (121 loc) · 3.22 KB
/
ConsoleBinding.cpp
File metadata and controls
156 lines (121 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <StdInc.h>
#include <CoreConsole.h>
#include <console/ConsoleWriter.h>
#include <VFSManager.h>
#include <nutsnbolts.h>
#include <CL2LaunchMode.h>
#include <fiDevice.h>
#include <ShlObj.h>
#if defined(IS_RDR3)
#define CONFIG_NAME "redm"
#elif defined(GTA_FIVE)
#define CONFIG_NAME "fivem"
#elif defined (GTA_NY)
#define CONFIG_NAME "citizeniv"
#endif
struct ConsoleWriter : public console::IWriter
{
virtual uint64_t Create(const std::string& path) override
{
auto device = vfs::GetDevice(path);
m_lastDevice = device;
return device->Create(path);
}
virtual void Write(uint64_t handle, const void* data, size_t count) override
{
m_lastDevice->Write(handle, data, count);
}
virtual void Close(uint64_t handle) override
{
m_lastDevice->Close(handle);
m_lastDevice = {};
}
fwRefContainer<vfs::Device> m_lastDevice;
};
static ConsoleWriter g_writer;
static InitFunction initFunction([]()
{
SetConsoleWriter(&g_writer);
static bool safeExec = false;
OnGameFrame.Connect([]()
{
if (safeExec)
{
console::GetDefaultContext()->SaveConfigurationIfNeeded(fmt::sprintf("fxd:/%s%s.cfg", CONFIG_NAME, launch::IsSDKGuest() ? "_sdk" : ""));
}
});
auto execRoot = []()
{
// hide any console prints from this early execution
auto& printFilterEvent = *console::CoreGetPrintFilterEvent();
auto filter = printFilterEvent.Connect([](ConsoleChannel&, const char*)
{
return false;
});
// actually execute commands
se::ScopedPrincipal seContext(se::Principal{ "system.console" });
console::GetDefaultContext()->ExecuteSingleCommandDirect(ProgramArguments{ "exec", fmt::sprintf("%s%s%s.cfg", (safeExec) ? "fxd:/" : "", CONFIG_NAME, launch::IsSDKGuest() ? "_sdk" : "") });
// remove the filter
printFilterEvent.Disconnect(filter);
};
rage::fiDevice::OnInitialMount.Connect([execRoot]()
{
safeExec = true;
execRoot();
}, INT32_MAX);
static ConsoleCommand execCommand("exec", [=](const std::string& path)
{
std::vector<uint8_t> data;
if (safeExec)
{
fwRefContainer<vfs::Stream> stream = vfs::OpenRead(path);
if (!stream.GetRef())
{
console::Printf("cmd", "No such config file: %s\n", path.c_str());
return;
}
data = stream->ReadToEnd();
}
else
{
PWSTR appDataPath;
if (FAILED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &appDataPath)))
{
return;
}
// create the directory if not existent
std::wstring cfxPath = std::wstring(appDataPath) + L"\\CitizenFX";
CreateDirectory(cfxPath.c_str(), nullptr);
std::wstring profilePath = cfxPath + L"\\" + ToWide(path);
CoTaskMemFree(appDataPath);
FILE* f = _wfopen(profilePath.c_str(), L"rb");
if (!f)
{
return;
}
fseek(f, 0, SEEK_END);
auto len = _ftelli64(f);
if (len < 0)
{
fclose(f);
return;
}
data.resize(len);
fseek(f, 0, SEEK_SET);
fread(data.data(), 1, len, f);
fclose(f);
}
data.push_back('\n'); // add a newline at the end
auto consoleCtx = console::GetDefaultContext();
consoleCtx->AddToBuffer(std::string(reinterpret_cast<char*>(&data[0]), data.size()));
try
{
consoleCtx->ExecuteBuffer();
}
catch (std::exception& e)
{
trace("initial console execution failed: %s\n");
}
});
execRoot();
}, INT32_MIN);