/// <summary> /// Adds a <see cref="ConFileObject"/> to this Scope /// </summary> /// <remarks> /// Used by the script engine to add objects, and set them as active /// </remarks> /// <param name="obj"></param> /// <param name="token"></param> internal void AddObject(ConFileObject obj, Token token) { // Ensure that we aren't passing peoples problems CheckObjectToken(obj); // Generate key var type = obj.Token.TokenArgs.ReferenceType; var key = new Tuple <string, ReferenceType>(obj.Name, type); // If object exists, then we fetch the existing reference if (ContainsObject(key)) { // Log warning string err = $"Object \"{obj.Name}\" is already defined, Setting existing one as active"; Logger.Warning(err, token.File, token.Position); // Get existing reference obj = GetObject(key, token); } else { // Add object Objects.Add(key, obj); // Register object? if (ParentScope != null && RegisterObjects) { if (ScopeType == ScopeType.Attached) { ParentScope.AddObject(obj, token); } else { ParentScope.AddObject(obj.Clone(), token); } } } // Set object as active SetActiveObject(obj); }
/// <summary> /// Adds a <see cref="ConFileObject"/> to this Scope /// </summary> /// <param name="obj">The object reference to add</param> /// <param name="setActive"> /// Set this as the active object of its <see cref="ReferenceType"/>? /// </param> public void AddObject(ConFileObject obj, bool setActive = true) { // Ensure that we aren't passing peoples problems CheckObjectToken(obj); // Generate key var type = obj.Token.TokenArgs.ReferenceType; var key = new Tuple <string, ReferenceType>(obj.Name, type); // If object exists, throw exception if (ContainsObject(key)) { throw new Exception("Object is already defined in this scope!"); } // Do we set as the active object? if (setActive) { SetActiveObject(obj); } // Register object? if (ParentScope != null && RegisterObjects) { if (ScopeType == ScopeType.Attached) { ParentScope.AddObject(obj, setActive); } else { ParentScope.AddObject(obj.Clone(), setActive); } } // Add object Objects.Add(key, obj); }