-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refact(skymp5-server): eliminate duplicated code and remove macro def…
…initions for Papyrus Classes (#2324)
- Loading branch information
Showing
48 changed files
with
389 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
skymp5-server/cpp/server_guest_lib/script_classes/IPapyrusClass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "IPapyrusClass.h" | ||
#include "SpSnippetFunctionGen.h" | ||
|
||
VarValue IPapyrusClassBase::MakeSPSnippetPromise( | ||
const char* script, const char* name, | ||
std::shared_ptr<IPapyrusCompatibilityPolicy> policy, VarValue self, | ||
const std::vector<VarValue>& arguments, bool method, bool returns, | ||
VarValue defaultResult) | ||
{ | ||
if (auto actor = | ||
policy->GetDefaultActor(script, name, self.GetMetaStackId())) { | ||
auto s = SpSnippetFunctionGen::SerializeArguments(arguments, actor); | ||
auto promise = | ||
SpSnippet(script, name, s.data(), | ||
method ? SpSnippetFunctionGen::GetFormId(self) : 0) | ||
.Execute(actor, | ||
(returns ? SpSnippetMode::kReturnResult | ||
: SpSnippetMode::kNoReturnResult)); | ||
if (returns) | ||
return VarValue(promise); | ||
} | ||
return defaultResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
skymp5-server/cpp/server_guest_lib/script_classes/PapyrusCell.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
skymp5-server/cpp/server_guest_lib/script_classes/PapyrusDebug.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
skymp5-server/cpp/server_guest_lib/script_classes/PapyrusEffectBase.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "PapyrusEffectBase.h" | ||
|
||
#include "SpSnippetFunctionGen.h" | ||
#include "WorldState.h" | ||
#include "script_objects/EspmGameObject.h" | ||
#include "script_objects/MpFormGameObject.h" | ||
|
||
PapyrusEffectBase::PapyrusEffectBase(const std::string& name) | ||
: strName(name) | ||
{ | ||
} | ||
|
||
VarValue PapyrusEffectBase::Play(VarValue self, | ||
const std::vector<VarValue>& arguments) | ||
{ | ||
Helper(self, "Play", arguments); | ||
return VarValue::None(); | ||
} | ||
|
||
VarValue PapyrusEffectBase::Stop(VarValue self, | ||
const std::vector<VarValue>& arguments) | ||
{ | ||
Helper(self, "Stop", arguments); | ||
return VarValue::None(); | ||
} | ||
|
||
void PapyrusEffectBase::Register( | ||
VirtualMachine& vm, std::shared_ptr<IPapyrusCompatibilityPolicy> policy) | ||
{ | ||
AddMethod(vm, "Play", &PapyrusEffectBase::Play); | ||
AddMethod(vm, "Stop", &PapyrusEffectBase::Stop); | ||
} | ||
|
||
void PapyrusEffectBase::Helper(VarValue& self, const char* funcName, | ||
const std::vector<VarValue>& arguments) | ||
{ | ||
const auto& selfRec = GetRecordPtr(self); | ||
if (selfRec.rec) { | ||
if (arguments.size() < 1) { | ||
throw std::runtime_error(std::string(funcName) + | ||
" requires at least one argument"); | ||
} | ||
if (auto actorForm = GetFormPtr<MpObjectReference>(arguments[0])) { | ||
for (auto listener : actorForm->GetActorListeners()) { | ||
SpSnippet( | ||
GetName(), funcName, | ||
SpSnippetFunctionGen::SerializeArguments(arguments, listener).data(), | ||
selfRec.ToGlobalId(selfRec.rec->GetId())) | ||
.Execute(listener, SpSnippetMode::kNoReturnResult); | ||
// Workaround to use this function on player clone | ||
if (actorForm->GetFormId() == listener->GetFormId()) { | ||
SpSnippet(GetName(), funcName, | ||
SpSnippetFunctionGen::SerializeArguments(arguments).data(), | ||
selfRec.ToGlobalId(selfRec.rec->GetId())) | ||
.Execute(listener, SpSnippetMode::kNoReturnResult); | ||
} | ||
} | ||
} | ||
} else { | ||
throw std::runtime_error(std::string(funcName) + ": can't get object!"); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
skymp5-server/cpp/server_guest_lib/script_classes/PapyrusEffectBase.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
#include "IPapyrusClass.h" | ||
|
||
class PapyrusEffectBase : public IPapyrusClass<PapyrusEffectBase> | ||
{ | ||
public: | ||
PapyrusEffectBase(const std::string& name); | ||
const char* GetName() override { return strName.c_str(); } | ||
VarValue Play(VarValue self, const std::vector<VarValue>& arguments); | ||
VarValue Stop(VarValue self, const std::vector<VarValue>& arguments); | ||
|
||
void Register(VirtualMachine& vm, | ||
std::shared_ptr<IPapyrusCompatibilityPolicy> policy) override; | ||
|
||
private: | ||
void Helper(VarValue& self, const char* funcName, | ||
const std::vector<VarValue>& arguments); | ||
|
||
private: | ||
std::string strName; | ||
}; |
48 changes: 2 additions & 46 deletions
48
skymp5-server/cpp/server_guest_lib/script_classes/PapyrusEffectShader.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,6 @@ | ||
#include "PapyrusEffectShader.h" | ||
|
||
#include "WorldState.h" | ||
#include "script_objects/EspmGameObject.h" | ||
#include "script_objects/MpFormGameObject.h" | ||
|
||
VarValue PapyrusEffectShader::Play(VarValue self, | ||
const std::vector<VarValue>& arguments) | ||
{ | ||
Helper(self, "Play", arguments); | ||
return VarValue::None(); | ||
} | ||
|
||
VarValue PapyrusEffectShader::Stop(VarValue self, | ||
const std::vector<VarValue>& arguments) | ||
{ | ||
Helper(self, "Stop", arguments); | ||
return VarValue::None(); | ||
} | ||
|
||
// This is exact copy of PapyrusVisualEffect::Helper | ||
void PapyrusEffectShader::Helper(VarValue& self, const char* funcName, | ||
const std::vector<VarValue>& arguments) | ||
PapyrusEffectShader::PapyrusEffectShader() | ||
: PapyrusEffectBase("EffectShader") | ||
{ | ||
const auto& selfRec = GetRecordPtr(self); | ||
if (selfRec.rec) { | ||
if (arguments.size() < 1) { | ||
throw std::runtime_error(std::string(funcName) + | ||
" requires at least one argument"); | ||
} | ||
if (auto actorForm = GetFormPtr<MpObjectReference>(arguments[0])) { | ||
for (auto listener : actorForm->GetActorListeners()) { | ||
SpSnippet( | ||
GetName(), funcName, | ||
SpSnippetFunctionGen::SerializeArguments(arguments, listener).data(), | ||
selfRec.ToGlobalId(selfRec.rec->GetId())) | ||
.Execute(listener, SpSnippetMode::kNoReturnResult); | ||
// Workaround to use this function on player clone | ||
if (actorForm->GetFormId() == listener->GetFormId()) { | ||
SpSnippet(GetName(), funcName, | ||
SpSnippetFunctionGen::SerializeArguments(arguments).data(), | ||
selfRec.ToGlobalId(selfRec.rec->GetId())) | ||
.Execute(listener, SpSnippetMode::kNoReturnResult); | ||
} | ||
} | ||
} | ||
} else { | ||
throw std::runtime_error(std::string(funcName) + ": can't get object!"); | ||
} | ||
} |
Oops, something went wrong.