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

Reloadfs pr #569

Merged
merged 3 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
155 changes: 87 additions & 68 deletions Server/Components/Pawn/Manager/Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,55 +431,8 @@ bool PawnManager::Load(DynamicArray<StringView> const& mainScripts)
return Load("gamemodes/" + gamemodes_[0], true);
}

bool PawnManager::Load(std::string const& name, bool isEntryScript)
void PawnManager::openAMX(PawnScript& script, bool isEntryScript)
{
if (mainName_ == name)
{
if (mainScript_)
{
return false;
}
}
else
{
if (findScript(name) != scripts_.end())
{
return false;
}
}

// if the user just supplied a script name, add the extension
// otherwise, don't, as they may have supplied a full abs/rel path.
std::string ext = utils::endsWith(name, ".amx") ? "" : ".amx";

std::string canon;
utils::Canonicalise(basePath_ + scriptPath_ + name + ext, canon);
PawnScript* ptr = new PawnScript(++id_, canon, core);

if (!ptr || !ptr->IsLoaded())
{
// core->logLn(LogLevel::Error, "Unable to load script %s\n\n", name.c_str());
return false;
}
ptr->name_ = name;

PawnScript& script = *ptr;

if (isEntryScript)
{
mainName_ = name;
delete mainScript_;
mainScript_ = ptr;
amxToScript_.emplace(mainScript_->GetAMX(), mainScript_);

unloadNextTick_ = false;
nextScriptName_ = "";
}
else
{
scripts_.push_back(ptr);
amxToScript_.emplace(script.GetAMX(), ptr);
}
script.Register("CallLocalFunction", &utils::pawn_Script_Call);
script.Register("CallRemoteFunction", &utils::pawn_Script_CallAll);
script.Register("Script_CallTargeted", &utils::pawn_Script_CallOne);
Expand Down Expand Up @@ -572,42 +525,63 @@ bool PawnManager::Load(std::string const& name, bool isEntryScript)
{
script.Call("OnPlayerConnect", DefaultReturnValue_True, p->getID());
}
return true;
}

bool PawnManager::Reload(std::string const& name)
bool PawnManager::Load(std::string const& name, bool isEntryScript)
{
// Entry script reload is not supported.
if (mainName_ == name)
{
return false;
}

Unload(name);
return Load(name, false);
}

bool PawnManager::Unload(std::string const& name)
{
auto pos = findScript(name);
bool isEntryScript = mainName_ == name;
if (isEntryScript)
{
if (!mainScript_)
if (mainScript_)
{
return false;
}
}
else
{
if (pos == scripts_.end())
if (findScript(name) != scripts_.end())
{
return false;
}
}
PawnScript& script = isEntryScript ? *mainScript_ : *reinterpret_cast<PawnScript*>(*pos);

// Call `OnPlayerDisconnect`.
// if the user just supplied a script name, add the extension
// otherwise, don't, as they may have supplied a full abs/rel path.
std::string ext = utils::endsWith(name, ".amx") ? "" : ".amx";

std::string canon;
utils::Canonicalise(basePath_ + scriptPath_ + name + ext, canon);
PawnScript* ptr = new PawnScript(++id_, canon, core);

if (!ptr || !ptr->IsLoaded())
{
// core->logLn(LogLevel::Error, "Unable to load script %s\n\n", name.c_str());
return false;
}
ptr->name_ = name;

PawnScript& script = *ptr;

if (isEntryScript)
{
mainName_ = name;
delete mainScript_;
mainScript_ = ptr;
amxToScript_.emplace(mainScript_->GetAMX(), mainScript_);

unloadNextTick_ = false;
nextScriptName_ = "";
}
else
{
scripts_.push_back(ptr);
amxToScript_.emplace(script.GetAMX(), ptr);
}
openAMX(script, isEntryScript);
return true;
}

void PawnManager::closeAMX(PawnScript& script, bool isEntryScript)
{
AMX* amx = script.GetAMX();
int idx;
bool once = true;
Expand Down Expand Up @@ -656,6 +630,51 @@ is `2`.
pluginManager.AmxUnload(script.GetAMX());
eventDispatcher.dispatch(&PawnEventHandler::onAmxUnload, script);
amxToScript_.erase(script.GetAMX());
}

bool PawnManager::Reload(std::string const& name)
{
// Entry script reload is not supported.
if (mainName_ == name)
{
return false;
}
auto pos = findScript(name);
if (pos == scripts_.end())
{
return false;
}
PawnScript& script = *reinterpret_cast<PawnScript*>(*pos);
closeAMX(script, false);
std::string canon;
std::string ext = utils::endsWith(name, ".amx") ? "" : ".amx";
utils::Canonicalise(basePath_ + scriptPath_ + name + ext, canon);
script.tryLoad(canon);
openAMX(script, false);
amxToScript_.emplace(script.GetAMX(), &script);
return true;
}

bool PawnManager::Unload(std::string const& name)
{
auto pos = findScript(name);
bool isEntryScript = mainName_ == name;
if (isEntryScript)
{
if (!mainScript_)
{
return false;
}
}
else
{
if (pos == scripts_.end())
{
return false;
}
}
PawnScript& script = isEntryScript ? *mainScript_ : *reinterpret_cast<PawnScript*>(*pos);
closeAMX(script, isEntryScript);

if (isEntryScript)
{
Expand Down
3 changes: 3 additions & 0 deletions Server/Components/Pawn/Manager/Manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ class PawnManager : public Singleton<PawnManager>, public PawnLookup
});
}

void openAMX(PawnScript& script, bool isEntryScript);
void closeAMX(PawnScript& script, bool isEntryScript);

public:
PawnManager();
~PawnManager();
Expand Down
47 changes: 27 additions & 20 deletions Server/Components/Pawn/Script/Script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,24 @@ extern "C"
/// A map of per-AMX caches
static FlatHashMap<AMX*, AMXCache*> cache;

PawnScript::PawnScript(int id, std::string const& path, ICore* core)
: serverCore(core)
, id_(id)
void PawnScript::tryLoad(std::string const& path)
{
if (loaded_)
{
amx_FloatCleanup(&amx_);
amx_TimeCleanup(&amx_);
amx_StringCleanup(&amx_);
amx_FileCleanup(&amx_);
amx_CoreCleanup(&amx_);
amx_ArgsCleanup(&amx_);
aux_FreeProgram(&amx_);
cache.erase(&amx_);
}
loaded_ = false;
if (path == "")
{
return;
}
int err = aux_LoadProgram(&amx_, const_cast<char*>(path.c_str()), nullptr);
switch (err)
{
Expand All @@ -75,9 +89,8 @@ encountered. This could be caused by many things:
break;
default:
serverCore->printLn("%s", aux_StrError(err));
return;
break;
}

if (loaded_)
{
amx_ArgsInit(&amx_);
Expand All @@ -88,25 +101,19 @@ encountered. This could be caused by many things:
amx_FloatInit(&amx_);
cache.emplace(std::make_pair<AMX*, AMXCache*>(&amx_, &cache_));
}
else
{
return;
}
}

PawnScript::PawnScript(int id, std::string const& path, ICore* core)
: serverCore(core)
, loaded_(false)
, id_(id)
{
tryLoad(path);
}

PawnScript::~PawnScript()
{
if (loaded_)
{
amx_FloatCleanup(&amx_);
amx_TimeCleanup(&amx_);
amx_StringCleanup(&amx_);
amx_FileCleanup(&amx_);
amx_CoreCleanup(&amx_);
amx_ArgsCleanup(&amx_);
aux_FreeProgram(&amx_);
cache.erase(&amx_);
}
tryLoad("");
}

int AMXAPI amx_NumPublics(AMX* amx, int* number)
Expand Down
4 changes: 3 additions & 1 deletion Server/Components/Pawn/Script/Script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,13 @@ class PawnScript : public IPawnScript

using IPawnScript::Register;

void tryLoad(std::string const& path);

private:
ICore* serverCore;
AMX amx_;
AMXCache cache_;
bool loaded_ = false;
bool loaded_;
String name_;

int id_;
Expand Down