Scripting: Add an optional 'required' parameter to getReference (default: true). If required=false, it will not throw an exception if there's no reference. Fixes PcExpell not working without a reference like it's supposed to, and makes the code nicer for some others (use required=false instead of catching the exception)

This commit is contained in:
scrawl 2014-01-09 02:14:08 +01:00
parent 223c1d5a38
commit 768d9f7237
6 changed files with 19 additions and 30 deletions

View file

@ -54,14 +54,7 @@ namespace MWScript
public: public:
virtual void execute (Interpreter::Runtime& runtime) virtual void execute (Interpreter::Runtime& runtime)
{ {
// FIXME: No way to tell if we have a reference before trying to get it, and it will MWWorld::Ptr bed = R()(runtime, false);
// cause an exception is there isn't one :(
MWWorld::Ptr bed;
try {
bed = R()(runtime);
}
catch(std::runtime_error&) {
}
if (bed.isEmpty() || !MWBase::Environment::get().getMechanicsManager()->sleepInBed(MWBase::Environment::get().getWorld()->getPlayerPtr(), if (bed.isEmpty() || !MWBase::Environment::get().getMechanicsManager()->sleepInBed(MWBase::Environment::get().getWorld()->getPlayerPtr(),
bed)) bed))

View file

@ -23,7 +23,7 @@
namespace MWScript namespace MWScript
{ {
MWWorld::Ptr InterpreterContext::getReference ( MWWorld::Ptr InterpreterContext::getReference (
const std::string& id, bool activeOnly) const std::string& id, bool activeOnly, bool doThrow)
{ {
if (!id.empty()) if (!id.empty())
{ {
@ -31,7 +31,7 @@ namespace MWScript
} }
else else
{ {
if (mReference.isEmpty()) if (mReference.isEmpty() && doThrow)
throw std::runtime_error ("no implicit reference"); throw std::runtime_error ("no implicit reference");
return mReference; return mReference;
@ -39,7 +39,7 @@ namespace MWScript
} }
const MWWorld::Ptr InterpreterContext::getReference ( const MWWorld::Ptr InterpreterContext::getReference (
const std::string& id, bool activeOnly) const const std::string& id, bool activeOnly, bool doThrow) const
{ {
if (!id.empty()) if (!id.empty())
{ {
@ -47,7 +47,7 @@ namespace MWScript
} }
else else
{ {
if (mReference.isEmpty()) if (mReference.isEmpty() && doThrow)
throw std::runtime_error ("no implicit reference"); throw std::runtime_error ("no implicit reference");
return mReference; return mReference;
@ -498,8 +498,8 @@ namespace MWScript
ptr.getRefData().getLocals().mFloats[index] = value; ptr.getRefData().getLocals().mFloats[index] = value;
} }
MWWorld::Ptr InterpreterContext::getReference() MWWorld::Ptr InterpreterContext::getReference(bool required)
{ {
return getReference ("", true); return getReference ("", true, required);
} }
} }

View file

@ -33,9 +33,9 @@ namespace MWScript
bool mActivationHandled; bool mActivationHandled;
boost::shared_ptr<MWWorld::Action> mAction; boost::shared_ptr<MWWorld::Action> mAction;
MWWorld::Ptr getReference (const std::string& id, bool activeOnly); MWWorld::Ptr getReference (const std::string& id, bool activeOnly, bool doThrow=true);
const MWWorld::Ptr getReference (const std::string& id, bool activeOnly) const; const MWWorld::Ptr getReference (const std::string& id, bool activeOnly, bool doThrow=true) const;
public: public:
@ -150,7 +150,7 @@ namespace MWScript
virtual void setMemberFloat (const std::string& id, const std::string& name, float value); virtual void setMemberFloat (const std::string& id, const std::string& name, float value);
MWWorld::Ptr getReference(); MWWorld::Ptr getReference(bool required=true);
///< Reference, that the script is running from (can be empty) ///< Reference, that the script is running from (can be empty)
}; };
} }

View file

@ -699,13 +699,11 @@ namespace MWScript
public: public:
virtual void execute(Interpreter::Runtime& runtime) virtual void execute(Interpreter::Runtime& runtime)
{ {
// No way to tell if we have a reference before trying to get it, and it will MWWorld::Ptr ptr = R()(runtime, false);
// cause an exception is there isn't one :( if (!ptr.isEmpty())
try {
MWWorld::Ptr ptr = R()(runtime);
printLocalVars(runtime, ptr); printLocalVars(runtime, ptr);
} else
catch(std::runtime_error&) { {
// No reference, no problem. // No reference, no problem.
printGlobalVars(runtime); printGlobalVars(runtime);
} }

View file

@ -16,7 +16,7 @@ namespace MWScript
{ {
struct ExplicitRef struct ExplicitRef
{ {
MWWorld::Ptr operator() (Interpreter::Runtime& runtime) const MWWorld::Ptr operator() (Interpreter::Runtime& runtime, bool required=true) const
{ {
std::string id = runtime.getStringLiteral (runtime[0].mInteger); std::string id = runtime.getStringLiteral (runtime[0].mInteger);
runtime.pop(); runtime.pop();
@ -27,12 +27,12 @@ namespace MWScript
struct ImplicitRef struct ImplicitRef
{ {
MWWorld::Ptr operator() (Interpreter::Runtime& runtime) const MWWorld::Ptr operator() (Interpreter::Runtime& runtime, bool required=true) const
{ {
MWScript::InterpreterContext& context MWScript::InterpreterContext& context
= static_cast<MWScript::InterpreterContext&> (runtime.getContext()); = static_cast<MWScript::InterpreterContext&> (runtime.getContext());
return context.getReference(); return context.getReference(required);
} }
}; };
} }

View file

@ -921,8 +921,6 @@ namespace MWScript
virtual void execute (Interpreter::Runtime& runtime, unsigned int arg0) virtual void execute (Interpreter::Runtime& runtime, unsigned int arg0)
{ {
MWWorld::Ptr ptr = R()(runtime);
std::string factionID = ""; std::string factionID = "";
if(arg0 >0 ) if(arg0 >0 )
{ {
@ -931,6 +929,7 @@ namespace MWScript
} }
else else
{ {
MWWorld::Ptr ptr = R()(runtime);
if(MWWorld::Class::get(ptr).getNpcStats(ptr).getFactionRanks().empty()) if(MWWorld::Class::get(ptr).getNpcStats(ptr).getFactionRanks().empty())
{ {
factionID = ""; factionID = "";
@ -955,8 +954,6 @@ namespace MWScript
virtual void execute (Interpreter::Runtime& runtime, unsigned int arg0) virtual void execute (Interpreter::Runtime& runtime, unsigned int arg0)
{ {
MWWorld::Ptr ptr = R()(runtime);
std::string factionID = ""; std::string factionID = "";
if(arg0 >0 ) if(arg0 >0 )
{ {
@ -965,6 +962,7 @@ namespace MWScript
} }
else else
{ {
MWWorld::Ptr ptr = R()(runtime);
if(MWWorld::Class::get(ptr).getNpcStats(ptr).getFactionRanks().empty()) if(MWWorld::Class::get(ptr).getNpcStats(ptr).getFactionRanks().empty())
{ {
factionID = ""; factionID = "";