Skip to content

Commit

Permalink
feat: add ability to add and remove individual locks from settings
Browse files Browse the repository at this point in the history
This also deprecates the `lock` function.
  • Loading branch information
LordMidas committed Feb 1, 2023
1 parent 28c2b05 commit cd0f5a7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
36 changes: 25 additions & 11 deletions msu/systems/mod_settings/abstract_setting.nut
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
static Type = "Abstract";
Value = null;
BaseValue = null;
Locked = null;
LockReason = null;
Locks = null;
BeforeChangeCallbacks = null;
AfterChangeCallbacks = null;
Persistence = null; //if it should print change to log for further manipulation
Expand All @@ -14,8 +13,7 @@
base.constructor(_id, _name, _description)
this.Value = _value;
this.BaseValue = _value;
this.Locked = false;
this.LockReason = "";
this.Locks = {};
this.Persistence = true;
this.BeforeChangeCallbacks = [];
this.AfterChangeCallbacks = [];
Expand Down Expand Up @@ -84,7 +82,7 @@

function set( _newValue, _updateJS = true, _updatePersistence = true, _updateBeforeChangeCallback = true, _force = false, _updateAfterChangeCallback = true)
{
if (this.Locked)
if (this.isLocked())
{
::logError("Setting \'" + this.Name + "\'' is locked and its value cannot be changed. Lock reason: " + this.getLockReason());
return false;
Expand Down Expand Up @@ -141,24 +139,40 @@

function isLocked()
{
return this.Locked;
return this.Locks.len() != 0;
}

function getLockReason()
{
return this.LockReason == "" ? "" : this.LockReason.slice(0, -5);
local ret = "";
foreach (lockReason in this.Locks)
{
ret += lockReason + " +++ ";
}
return ret == "" ? "" : ret.slice(0, -5);
}

// Deprecated - use addLock instead
function lock( _lockReason = "" )
{
this.Locked = true;
if (_lockReason != "") this.LockReason += _lockReason + " +++ ";
// "MSU_LegacyLock" is for legacy support for the days when settings used to have
// a this.Locked Boolean which could be set/unset using lock() and unlock()
this.Locks["MSU_LegacyLock" + this.Locks.len()] <- _lockReason;
}

function addLock( _lockID, _lockReason )
{
this.Locks[_lockID] <- _lockReason;
}

function removeLock( _lockID )
{
if (_lockID in this.Locks) delete this.Locks[_lockID];
}

function unlock()
{
this.Locked = false;
this.LockReason = "";
this.Locks.clear();
}

function getUIData(_flags = [])
Expand Down
13 changes: 11 additions & 2 deletions msu/systems/mod_settings/mod_settings_mod_addon.nut
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,19 @@
return true;
}

function lockSetting( _setting, _lockReason )
function addLock( _setting, _lockID, _lockReason )
{
if (typeof _setting == "string") _setting = this.getSetting(_setting);

_setting.lock(_lockReason + format(" (%s (%s))", this.getMod().getID(), this.getMod().getName()));
_lockID = this.getMod().getID() + "." + _lockID;
_setting.addLock(_lockID, _lockReason);
}

function removeLock( _setting, _lockID )
{
if (typeof _setting == "string") _setting = this.getSetting(_setting);

_lockID = this.getMod().getID() + "." + _lockID;
_setting.removeLock(_lockID);
}
}

0 comments on commit cd0f5a7

Please sign in to comment.