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

Individual locks for settings #283

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
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
49 changes: 27 additions & 22 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 @@ -130,35 +128,47 @@
local ret = base.getDescription();
if (this.isLocked())
{
ret += "\n\n[color=" + ::Const.UI.Color.NegativeValue + "]Locked[/color]\n";
if (this.LockReason != "")
{
ret += this.getLockReason();
}
ret += "\n\n[color=" + ::Const.UI.Color.NegativeValue + "]Locked[/color]\n" + this.getLockReason();
}
return ret;
}

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 All @@ -173,17 +183,14 @@
function __getSerializationTable()
{
return {
Value = this.getValue(),
Locked = this.isLocked(),
LockReason = this.getLockReason()
Value = this.getValue()
};
}

function __setFromSerializationTable( _table )
{
this.unlock();
this.set(_table.Value, true, false, true, true);
if (_table.Locked) this.lock(_table.LockReason);
}

function flagSerialize( _out )
Expand Down Expand Up @@ -217,8 +224,6 @@
this.unlock();
this.set(::World.Flags.get(valueFlag), true, false, true, true);
}
setPropertyIfFlagExists("Locked", modID);
setPropertyIfFlagExists("LockReason", modID);
}
else
{
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);
}
}