1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-02-07 13:27:13 +01:00

Assign/Remove/Modify methods.

This commit is contained in:
Sandu Liviu Catalin 2021-02-05 14:06:13 +02:00
parent 90597e4287
commit 4da00718f7
4 changed files with 132 additions and 31 deletions

View File

@ -146,7 +146,7 @@ void PvClass::DoChanged(SQInteger id, bool status, SQInteger value) const
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void PvClass::AssignStatus(SQInteger id, SQInteger value) void PvClass::AssignPrivilege(SQInteger id, SQInteger value)
{ {
// Find the current status of this entry // Find the current status of this entry
SQInteger current = GetEntryValue(id); SQInteger current = GetEntryValue(id);
@ -167,7 +167,7 @@ void PvClass::AssignStatus(SQInteger id, SQInteger value)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void PvClass::RemoveStatus(SQInteger id) void PvClass::RemovePrivilege(SQInteger id)
{ {
// Look for the status of this value // Look for the status of this value
auto itr = mPrivileges.find(id); auto itr = mPrivileges.find(id);
@ -197,7 +197,7 @@ void PvClass::RemoveStatus(SQInteger id)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void PvClass::ModifyStatus(SQInteger id, SQInteger value) void PvClass::ModifyPrivilege(SQInteger id, SQInteger value)
{ {
// Find the current status of this entry // Find the current status of this entry
SQInteger current = GetEntryValue(id); SQInteger current = GetEntryValue(id);
@ -229,6 +229,24 @@ void PvClass::ModifyStatus(SQInteger id, SQInteger value)
} }
} }
// ------------------------------------------------------------------------------------------------
void PvClass::AssignPrivilege(StackStrF & tag, SQInteger value)
{
AssignPrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID, value);
}
// ------------------------------------------------------------------------------------------------
void PvClass::RemovePrivilege(StackStrF & tag)
{
RemovePrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID);
}
// ------------------------------------------------------------------------------------------------
void PvClass::ModifyPrivilege(StackStrF & tag, SQInteger value)
{
ModifyPrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID, value);
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void PvClass::AssignParent(const Ref & parent) void PvClass::AssignParent(const Ref & parent)
{ {
@ -253,13 +271,13 @@ void PvClass::AssignParent(const Ref & parent)
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool PvClass::Can(SQInteger id) const bool PvClass::Can(SQInteger id) const
{ {
// Get the current status of the specified entry
SQInteger current = GetEntryValue(id);
// Retrieve the function responsible for the query event // Retrieve the function responsible for the query event
const Function & query = GetOnQuery(id); const Function & query = GetOnQuery(id);
// Is there someone that can arbitrate this request? // Is there someone that can arbitrate this request?
if (!query.IsNull()) if (!query.IsNull())
{ {
// Get the current status of the specified entry
SQInteger current = GetEntryValue(id);
// Attempt arbitration // Attempt arbitration
LightObj r = query.Eval(current); LightObj r = query.Eval(current);
// If NULL or false the request was denied // If NULL or false the request was denied
@ -268,6 +286,11 @@ bool PvClass::Can(SQInteger id) const
return true; // Request allowed return true; // Request allowed
} }
} }
// We use the >= comparison to settle arbitration
else if (current >= ValidManager().ValidEntry(id).mDefault)
{
return true;
}
// Request failed, no arbitration // Request failed, no arbitration
return false; return false;
} }
@ -392,12 +415,17 @@ void Register_Privilege_Class(HSQUIRRELVM vm, Table & ns)
.CbFunc(_SC("OnLost"), &SqPvClass::SetOnLost) .CbFunc(_SC("OnLost"), &SqPvClass::SetOnLost)
.CbFunc(_SC("OnGained"), &SqPvClass::SetOnGained) .CbFunc(_SC("OnGained"), &SqPvClass::SetOnGained)
// Member Methods // Member Methods
.Func(_SC("Can"), &SqPvUnit::Can) .Func(_SC("Can"), &SqPvClass::Can)
.Func(_SC("Assign"), &SqPvClass::AssignPrivilegeWithID)
.Func(_SC("AssignWithTag"), &SqPvClass::AssignPrivilegeWithTag)
.Func(_SC("Remove"), &SqPvClass::RemovePrivilegeWithID)
.Func(_SC("RemoveWithTag"), &SqPvClass::RemovePrivilegeWithTag)
.Func(_SC("Modify"), &SqPvClass::ModifyPrivilegeWithID)
.Func(_SC("ModifyWithTag"), &SqPvClass::ModifyPrivilegeWithTag)
.Func(_SC("GetUnit"), &SqPvClass::GetUnitWithID) .Func(_SC("GetUnit"), &SqPvClass::GetUnitWithID)
.FmtFunc(_SC("GetUnitWithTag"), &SqPvClass::GetUnitWithTag) .FmtFunc(_SC("GetUnitWithTag"), &SqPvClass::GetUnitWithTag)
.Func(_SC("HaveUnit"), &SqPvClass::HaveUnitWithID) .Func(_SC("HaveUnit"), &SqPvClass::HaveUnitWithID)
.FmtFunc(_SC("HaveUnitWithTag"), &SqPvClass::HaveUnitWithTag) .FmtFunc(_SC("HaveUnitWithTag"), &SqPvClass::HaveUnitWithTag)
// Member Overloads
); );
} }

View File

@ -130,6 +130,19 @@ struct PvClass
*/ */
PvClass & operator = (PvClass && o) = delete; PvClass & operator = (PvClass && o) = delete;
/* --------------------------------------------------------------------------------------------
* Update the hash of the specified unit.
*/
void UpdateUnitHash(SQInteger id, size_t hash)
{
auto itr = mUnits.find(PvIdentity(id));
// Only update if it exists
if (itr != mUnits.end())
{
itr->first.mHash = hash;
}
}
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Modify the associated user tag. * Modify the associated user tag.
*/ */
@ -221,18 +234,33 @@ struct PvClass
* Assign a status value. Does not care if a parent (class or global) has the same status. * Assign a status value. Does not care if a parent (class or global) has the same status.
* Later if the parent changes this status, we will keep having this status value. * Later if the parent changes this status, we will keep having this status value.
*/ */
void AssignStatus(SQInteger id, SQInteger value); void AssignPrivilege(SQInteger id, SQInteger value);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Remove a status value. If the specified status value is not assigned, nothing happens. * Remove a status value. If the specified status value is not assigned, nothing happens.
*/ */
void RemoveStatus(SQInteger id); void RemovePrivilege(SQInteger id);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Assign a status value. If a parent (class or global) has the same value, nothing changes. * Assign a status value. If a parent (class or global) has the same value, nothing changes.
* Same as AssignStatus but the status will not be enforced if we have it (inherited or not). * Same as AssignStatus but the status will not be enforced if we have it (inherited or not).
*/ */
void ModifyStatus(SQInteger id, SQInteger value); void ModifyPrivilege(SQInteger id, SQInteger value);
/* --------------------------------------------------------------------------------------------
* See AssignPrivilege().
*/
void AssignPrivilege(StackStrF & tag, SQInteger value);
/* --------------------------------------------------------------------------------------------
* See RemovePrivilege().
*/
void RemovePrivilege(StackStrF & tag);
/* --------------------------------------------------------------------------------------------
* See ModifyPrivilege().
*/
void ModifyPrivilege(StackStrF & tag, SQInteger value);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Change the parent class. * Change the parent class.
@ -263,19 +291,6 @@ struct PvClass
* See if a unit with a certain tag inherits this class. * See if a unit with a certain tag inherits this class.
*/ */
bool HaveUnitWithTag(StackStrF & tag); bool HaveUnitWithTag(StackStrF & tag);
/* --------------------------------------------------------------------------------------------
* Update the hash of the specified unit.
*/
void UpdateUnitHash(SQInteger id, size_t hash)
{
auto itr = mUnits.find(PvIdentity(id));
// Only update if it exists
if (itr != mUnits.end())
{
itr->first.mHash = hash;
}
}
}; };
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
@ -326,6 +341,13 @@ struct SqPvClass
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
bool Can(LightObj & obj) const; bool Can(LightObj & obj) const;
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
void AssignPrivilegeWithID(SQInteger id, SQInteger value) { Valid().AssignPrivilege(id, value); }
void AssignPrivilegeWithTag(StackStrF & tag, SQInteger value) { Valid().AssignPrivilege(tag, value); }
void RemovePrivilegeWithID(SQInteger id) { Valid().RemovePrivilege(id); }
void RemovePrivilegeWithTag(StackStrF & tag) { Valid().RemovePrivilege(tag); }
void ModifyPrivilegeWithID(SQInteger id, SQInteger value) { Valid().ModifyPrivilege(id, value); }
void ModifyPrivilegeWithTag(StackStrF & tag, SQInteger value) { Valid().ModifyPrivilege(tag, value); }
// --------------------------------------------------------------------------------------------
SQMOD_NODISCARD LightObj GetUnitWithID(SQInteger id) const { return Valid().GetUnitWithID(id); } SQMOD_NODISCARD LightObj GetUnitWithID(SQInteger id) const { return Valid().GetUnitWithID(id); }
SQMOD_NODISCARD LightObj GetUnitWithTag(StackStrF & tag) const { return Valid().GetUnitWithTag(tag); } SQMOD_NODISCARD LightObj GetUnitWithTag(StackStrF & tag) const { return Valid().GetUnitWithTag(tag); }
SQMOD_NODISCARD bool HaveUnitWithID(SQInteger id) const { return Valid().HaveUnitWithID(id); } SQMOD_NODISCARD bool HaveUnitWithID(SQInteger id) const { return Valid().HaveUnitWithID(id); }

View File

@ -161,7 +161,7 @@ void PvUnit::DoChanged(SQInteger id, bool status, SQInteger value) const
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void PvUnit::AssignStatus(SQInteger id, SQInteger value) void PvUnit::AssignPrivilege(SQInteger id, SQInteger value)
{ {
// Find the current status of this entry // Find the current status of this entry
SQInteger current = GetEntryValue(id); SQInteger current = GetEntryValue(id);
@ -182,7 +182,7 @@ void PvUnit::AssignStatus(SQInteger id, SQInteger value)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void PvUnit::RemoveStatus(SQInteger id) void PvUnit::RemovePrivilege(SQInteger id)
{ {
// Look for the status of this value // Look for the status of this value
auto itr = mPrivileges.find(id); auto itr = mPrivileges.find(id);
@ -212,7 +212,7 @@ void PvUnit::RemoveStatus(SQInteger id)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void PvUnit::ModifyStatus(SQInteger id, SQInteger value) void PvUnit::ModifyPrivilege(SQInteger id, SQInteger value)
{ {
// Find the current status of this entry // Find the current status of this entry
SQInteger current = GetEntryValue(id); SQInteger current = GetEntryValue(id);
@ -244,6 +244,24 @@ void PvUnit::ModifyStatus(SQInteger id, SQInteger value)
} }
} }
// ------------------------------------------------------------------------------------------------
void PvUnit::AssignPrivilege(StackStrF & tag, SQInteger value)
{
AssignPrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID, value);
}
// ------------------------------------------------------------------------------------------------
void PvUnit::RemovePrivilege(StackStrF & tag)
{
RemovePrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID);
}
// ------------------------------------------------------------------------------------------------
void PvUnit::ModifyPrivilege(StackStrF & tag, SQInteger value)
{
ModifyPrivilege(ValidManager().GetValidEntryWithTag(tag.CacheHash())->mID, value);
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void PvUnit::AssignClass(const std::shared_ptr< PvClass > & cls) void PvUnit::AssignClass(const std::shared_ptr< PvClass > & cls)
{ {
@ -265,13 +283,13 @@ void PvUnit::AssignClass(const std::shared_ptr< PvClass > & cls)
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool PvUnit::Can(SQInteger id) const bool PvUnit::Can(SQInteger id) const
{ {
// Get the current status of the specified entry
SQInteger current = GetEntryValue(id);
// Retrieve the function responsible for the query event // Retrieve the function responsible for the query event
const Function & query = GetOnQuery(id); const Function & query = GetOnQuery(id);
// Is there someone that can arbitrate this request? // Is there someone that can arbitrate this request?
if (!query.IsNull()) if (!query.IsNull())
{ {
// Get the current status of the specified entry
SQInteger current = GetEntryValue(id);
// Attempt arbitration // Attempt arbitration
LightObj r = query.Eval(current); LightObj r = query.Eval(current);
// If NULL or false the request was denied // If NULL or false the request was denied
@ -280,6 +298,11 @@ bool PvUnit::Can(SQInteger id) const
return true; // Request allowed return true; // Request allowed
} }
} }
// We use the >= comparison to settle arbitration
else if (current >= ValidManager().ValidEntry(id).mDefault)
{
return true;
}
// Request failed, no arbitration // Request failed, no arbitration
return false; return false;
} }
@ -364,6 +387,12 @@ void Register_Privilege_Unit(HSQUIRRELVM vm, Table & ns)
.CbFunc(_SC("OnGained"), &SqPvUnit::SetOnGained) .CbFunc(_SC("OnGained"), &SqPvUnit::SetOnGained)
// Member Methods // Member Methods
.Func(_SC("Can"), &SqPvUnit::Can) .Func(_SC("Can"), &SqPvUnit::Can)
.Func(_SC("Assign"), &SqPvUnit::AssignPrivilegeWithID)
.Func(_SC("AssignWithTag"), &SqPvUnit::AssignPrivilegeWithTag)
.Func(_SC("Remove"), &SqPvUnit::RemovePrivilegeWithID)
.Func(_SC("RemoveWithTag"), &SqPvUnit::RemovePrivilegeWithTag)
.Func(_SC("Modify"), &SqPvUnit::ModifyPrivilegeWithID)
.Func(_SC("ModifyWithTag"), &SqPvUnit::ModifyPrivilegeWithTag)
); );
} }

View File

@ -207,18 +207,33 @@ struct PvUnit
* Assign a status value. Does not care if a parent (class or global) has the same status. * Assign a status value. Does not care if a parent (class or global) has the same status.
* Later if the parent changes this status, we will keep having this status value. * Later if the parent changes this status, we will keep having this status value.
*/ */
void AssignStatus(SQInteger id, SQInteger value); void AssignPrivilege(SQInteger id, SQInteger value);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Remove a status value. If the specified status value is not assigned, nothing happens. * Remove a status value. If the specified status value is not assigned, nothing happens.
*/ */
void RemoveStatus(SQInteger id); void RemovePrivilege(SQInteger id);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Assign a status value. If a parent (class or global) has the same value, nothing changes. * Assign a status value. If a parent (class or global) has the same value, nothing changes.
* Same as AssignStatus but the status will not be enforced if we have it (inherited or not). * Same as AssignStatus but the status will not be enforced if we have it (inherited or not).
*/ */
void ModifyStatus(SQInteger id, SQInteger value); void ModifyPrivilege(SQInteger id, SQInteger value);
/* --------------------------------------------------------------------------------------------
* See AssignPrivilege().
*/
void AssignPrivilege(StackStrF & tag, SQInteger value);
/* --------------------------------------------------------------------------------------------
* See RemovePrivilege().
*/
void RemovePrivilege(StackStrF & tag);
/* --------------------------------------------------------------------------------------------
* See ModifyPrivilege().
*/
void ModifyPrivilege(StackStrF & tag, SQInteger value);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Assign a new class. * Assign a new class.
@ -276,6 +291,13 @@ struct SqPvUnit
SQMOD_NODISCARD LightObj GetManager() const; SQMOD_NODISCARD LightObj GetManager() const;
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
bool Can(LightObj & obj) const; bool Can(LightObj & obj) const;
// --------------------------------------------------------------------------------------------
void AssignPrivilegeWithID(SQInteger id, SQInteger value) { Valid().AssignPrivilege(id, value); }
void AssignPrivilegeWithTag(StackStrF & tag, SQInteger value) { Valid().AssignPrivilege(tag, value); }
void RemovePrivilegeWithID(SQInteger id) { Valid().RemovePrivilege(id); }
void RemovePrivilegeWithTag(StackStrF & tag) { Valid().RemovePrivilege(tag); }
void ModifyPrivilegeWithID(SQInteger id, SQInteger value) { Valid().ModifyPrivilege(id, value); }
void ModifyPrivilegeWithTag(StackStrF & tag, SQInteger value) { Valid().ModifyPrivilege(tag, value); }
}; };
} // Namespace:: SqMod } // Namespace:: SqMod