/// <summary>Update the current context.</summary> /// <param name="globalChangedTokens">The global token values which changed.</param> public void UpdateContext(InvariantHashSet globalChangedTokens) { // update local standard tokens // // Some local tokens may change independently (e.g. Random), so we need to update all // standard tokens here. bool localTokensChanged = false; foreach (IToken token in this.LocalContext.GetTokens(enforceContext: false)) { if (token.IsMutable) { localTokensChanged |= token.UpdateContext(this); } } // reset dynamic tokens // // Since dynamic token values are affected by the order they're defined (e.g. one // dynamic token can use the value of another), only updating tokens affected by // globalChangedTokens isn't trivial. Instead we track which global tokens are used // indirectly through dynamic tokens via AddDynamicToken, and use that to decide which // patches to update. if (globalChangedTokens.Any() || localTokensChanged || this.HasNewTokens) { foreach (ManagedManualToken managed in this.DynamicTokens.Values) { managed.ValueProvider.SetValue(null); managed.ValueProvider.SetReady(false); } foreach (DynamicTokenValue tokenValue in this.DynamicTokenValues) { tokenValue.UpdateContext(this); if (tokenValue.IsReady && tokenValue.Conditions.All(p => p.IsMatch)) { ManualValueProvider valueProvider = tokenValue.ParentToken.ValueProvider; valueProvider.SetValue(tokenValue.Value); valueProvider.SetReady(true); } } } // reset tracking this.HasNewTokens = false; }
/********* ** Public methods *********/ /// <summary>Construct an instance.</summary> /// <param name="name">The value provider name.</param> /// <param name="isBounded">Whether the token can only contain those values that are explicitly added as possible values.</param> /// <param name="scope">The mod namespace in which the token is accessible, or <c>null</c> for any namespace.</param> public ManagedManualToken(string name, bool isBounded, string?scope = null) { this.ValueProvider = new ManualValueProvider(name, isBounded); this.Token = new Token(this.ValueProvider, scope); }
/********* ** Public methods *********/ /// <summary>Construct an instance.</summary> /// <param name="name">The value provider name.</param> /// <param name="scope">The mod namespace in which the token is accessible, or <c>null</c> for any namespace.</param> public ManagedManualToken(string name, string scope = null) { this.ValueProvider = new ManualValueProvider(name); this.Token = new Token(this.ValueProvider, scope); }