Skip to content

Commit cd39f38

Browse files
authored
Make PluginMonitor not try to run non-lua files (#429)
By creating this pull request, I understand that code that is AI generated or otherwise automatically generated may be rejected without further discussion. I declare that I fully understand all code I pushed into this PR, and wrote all this code myself and own the rights to this code.
2 parents 6c3174a + a5ca508 commit cd39f38

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

include/Common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class Application final {
131131
};
132132

133133
void SplitString(std::string const& str, const char delim, std::vector<std::string>& out);
134+
std::string LowerString(std::string str);
134135

135136
std::string ThreadName(bool DebugModeOverride = false);
136137
void RegisterThread(const std::string& str);

src/Common.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,13 @@ void SplitString(const std::string& str, const char delim, std::vector<std::stri
384384
out.push_back(str.substr(start, end - start));
385385
}
386386
}
387+
388+
std::string LowerString(std::string str) {
389+
std::ranges::transform(str, str.begin(), ::tolower);
390+
return str;
391+
}
392+
393+
387394
static constexpr size_t STARTING_MAX_DECOMPRESSION_BUFFER_SIZE = 15 * 1024 * 1024;
388395
static constexpr size_t MAX_DECOMPRESSION_BUFFER_SIZE = 30 * 1024 * 1024;
389396

src/TPluginMonitor.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,26 @@ void TPluginMonitor::operator()() {
5757
mFileTimes[Pair.first] = CurrentTime;
5858
// grandparent of the path should be Resources/Server
5959
if (fs::equivalent(fs::path(Pair.first).parent_path().parent_path(), mPath)) {
60-
beammp_infof("File \"{}\" changed, reloading", Pair.first);
61-
// is in root folder, so reload
62-
std::ifstream FileStream(Pair.first, std::ios::in | std::ios::binary);
63-
auto Size = std::filesystem::file_size(Pair.first);
64-
auto Contents = std::make_shared<std::string>();
65-
Contents->resize(Size);
66-
FileStream.read(Contents->data(), Contents->size());
67-
TLuaChunk Chunk(Contents, Pair.first, fs::path(Pair.first).parent_path().string());
68-
auto StateID = mEngine->GetStateIDForPlugin(fs::path(Pair.first).parent_path());
69-
auto Res = mEngine->EnqueueScript(StateID, Chunk);
70-
Res->WaitUntilReady();
71-
if (Res->Error) {
72-
beammp_lua_errorf("Error while hot-reloading \"{}\": {}", Pair.first, Res->ErrorMessage);
60+
if (LowerString(fs::path(Pair.first).extension().string()) == ".lua") {
61+
beammp_infof("File \"{}\" changed, reloading", Pair.first);
62+
// is in root folder, so reload
63+
std::ifstream FileStream(Pair.first, std::ios::in | std::ios::binary);
64+
auto Size = std::filesystem::file_size(Pair.first);
65+
auto Contents = std::make_shared<std::string>();
66+
Contents->resize(Size);
67+
FileStream.read(Contents->data(), Contents->size());
68+
TLuaChunk Chunk(Contents, Pair.first, fs::path(Pair.first).parent_path().string());
69+
auto StateID = mEngine->GetStateIDForPlugin(fs::path(Pair.first).parent_path());
70+
auto Res = mEngine->EnqueueScript(StateID, Chunk);
71+
Res->WaitUntilReady();
72+
if (Res->Error) {
73+
beammp_lua_errorf("Error while hot-reloading \"{}\": {}", Pair.first, Res->ErrorMessage);
74+
} else {
75+
mEngine->ReportErrors(mEngine->TriggerLocalEvent(StateID, "onInit"));
76+
mEngine->ReportErrors(mEngine->TriggerEvent("onFileChanged", "", Pair.first));
77+
}
7378
} else {
74-
mEngine->ReportErrors(mEngine->TriggerLocalEvent(StateID, "onInit"));
79+
beammp_debugf("File \"{}\" changed, not reloading because it's not a lua file. Triggering 'onFileChanged' event instead", Pair.first);
7580
mEngine->ReportErrors(mEngine->TriggerEvent("onFileChanged", "", Pair.first));
7681
}
7782
} else {

0 commit comments

Comments
 (0)