Example #1
0
        /// <summary>
        /// Copies the content of a script to here.
        /// </summary>
        /// <param name="from">The script to copy from.</param>
        /// <param name="forModification">If false, the script incl. compiled assembly is copied. If true,
        /// the compiled assembly is not copied, so that the script text can be modified.</param>
        public void CopyFrom(AbstractScript from, bool forModification)
        {
            if (object.ReferenceEquals(this, from))
            {
                return;
            }

            var oldScriptText = _scriptText;

            _scriptText = from._scriptText;
            _isDirty    = from._isDirty;

            _wasTriedToCompile = forModification ? false : from._wasTriedToCompile;

            _errors = from._errors;                                          // immutable

            _compilerResult = forModification ? null : from._compilerResult; // (not cloning is intented here)

            _scriptObject = CreateNewScriptObject();                         // we create a new script object, because we are unable to clone it

            if (oldScriptText != _scriptText)
            {
                EhSelfChanged(EventArgs.Empty);
            }
        }
Example #2
0
        /// <summary>
        /// Does the compilation of the script into an assembly.
        /// If it was not compiled before or is dirty, it is compiled first.
        /// From the compiled assembly, a new instance of the newly created script class is created
        /// and stored in m_ScriptObject.
        /// </summary>
        /// <returns>True if successfully compiles, otherwise false.</returns>
        public virtual bool Compile()
        {
            this.m_WasTriedToCompile = true;

            if (_compilerResult != null)
            {
                return(true);
            }

            _compilerResult = ScriptCompilerService.Compile(new string[] { ScriptText }, out m_Errors);
            bool bSucceeded = (null != _compilerResult);

            if (_compilerResult != null)
            {
                this.m_ScriptObject = null;

                try
                {
                    this.m_ScriptObject = _compilerResult.ScriptAssembly.CreateInstance(this.ScriptObjectType);
                    if (null == m_ScriptObject)
                    {
                        bSucceeded  = false;
                        m_Errors    = new string[1];
                        m_Errors[0] = string.Format("Unable to create scripting object  (expected type: {0}), please verify namespace and class name!\n", this.ScriptObjectType);
                    }
                }
                catch (Exception ex)
                {
                    bSucceeded  = false;
                    m_Errors    = new string[1];
                    m_Errors[0] = string.Format("Exception during creation of scripting object: {0}\n", ex.Message);
                }
            }
            return(bSucceeded);
        }
Example #3
0
        /// <summary>
        /// Copies the content of a script to here.
        /// </summary>
        /// <param name="from">The script to copy from.</param>
        /// <param name="forModification">If false, the script incl. compiled assembly is copied. If true,
        /// the compiled assembly is not copied, so that the script text can be modified.</param>
        public void CopyFrom(AbstractScript from, bool forModification)
        {
            this.m_ScriptText   = from.m_ScriptText;
            this.m_ScriptObject = from.m_ScriptObject;
            this.m_IsDirty      = from.m_IsDirty;

            this.m_WasTriedToCompile = forModification ? false : from.m_WasTriedToCompile;

            this.m_Errors = null == from.m_Errors ? null: (string[])from.m_Errors.Clone();

            this._compilerResult = forModification ? null :  from._compilerResult;// (not cloning is intented here)
        }
        /// <summary>
        /// Tries to get a compilation result, by providing the hash of the script texts.
        /// </summary>
        /// <param name="scriptTextHash">The script text hash.</param>
        /// <param name="result">Returns the compilation result. This can be either a successful result or an unsuccessful result.</param>
        /// <returns>True if the compilation result corresponding to the script text hash could be found; otherwise, false.</returns>
        public bool TryGetValue(string scriptTextHash, out IScriptCompilerResult result)
        {
            if (string.IsNullOrEmpty(scriptTextHash))
            {
                throw new ArgumentNullException(nameof(scriptTextHash));
            }

            _lock.EnterReadLock();
            try
            {
                return(_compilerResultsByTextHash.TryGetValue(scriptTextHash, out result));
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }
Example #5
0
        public virtual bool SetCompilerResult(IScriptCompilerResult result)
        {
            _scriptText     = result.ScriptText(0);
            _compilerResult = result;
            bool bSucceeded = (_compilerResult is IScriptCompilerSuccessfulResult);

            if (_compilerResult is IScriptCompilerSuccessfulResult)
            {
                _scriptObject = CreateNewScriptObject();
                return(true);
            }
            else if (_compilerResult is IScriptCompilerFailedResult failedCompilerResult) // compiler result was not successful
            {
                _errors = failedCompilerResult.CompileErrors;
                return(false);
            }
            else
            {
                throw new InvalidProgramException("Unkown compiler result");
            }
        }
        /// <summary>
        /// Tries to add a compilation result.
        /// </summary>
        /// <param name="result">The compilation result.</param>
        /// <returns>True if successful; otherwise false (if it is already present).</returns>
        public bool TryAdd(IScriptCompilerResult result)
        {
            if (null == result)
            {
                throw new ArgumentNullException(nameof(result));
            }

            _lock.EnterUpgradeableReadLock();
            try
            {
                if (_compilerResultsByTextHash.ContainsKey(result.ScriptTextHash))
                {
                    return(false);
                }
                else
                {
                    _lock.EnterWriteLock();
                    try
                    {
                        _compilerResultsByTextHash.Add(result.ScriptTextHash, result);
                        if (result is ScriptCompilerSuccessfulResult successfulResult)
                        {
                            _compilerResultsByAssembly.Add(successfulResult.ScriptAssembly, successfulResult);
                        }
                        return(true);
                    }
                    finally
                    {
                        _lock.ExitWriteLock();
                    }
                }
            }
            finally
            {
                _lock.ExitUpgradeableReadLock();
            }
        }
Example #7
0
		/// <summary>
		/// Does the compilation of the script into an assembly.
		/// If it was not compiled before or is dirty, it is compiled first.
		/// From the compiled assembly, a new instance of the newly created script class is created
		/// and stored in m_ScriptObject.
		/// </summary>
		/// <returns>True if successfully compiles, otherwise false.</returns>
		public virtual bool Compile()
		{
			this._wasTriedToCompile = true;

			if (_compilerResult != null)
				return true;

			_compilerResult = ScriptCompilerService.Compile(new string[] { ScriptText }, out _errors);
			bool bSucceeded = (null != _compilerResult);

			if (_compilerResult != null)
			{
				this._scriptObject = null;

				try
				{
					this._scriptObject = _compilerResult.ScriptAssembly.CreateInstance(this.ScriptObjectType);
					if (null == _scriptObject)
					{
						bSucceeded = false;
						_errors = new string[1];
						_errors[0] = string.Format("Unable to create scripting object  (expected type: {0}), please verify namespace and class name!\n", this.ScriptObjectType);
					}
				}
				catch (Exception ex)
				{
					bSucceeded = false;
					_errors = new string[1];
					_errors[0] = string.Format("Exception during creation of scripting object: {0}\n", ex.Message);
				}
			}
			return bSucceeded;
		}
Example #8
0
		/// <summary>
		/// Copies the content of a script to here.
		/// </summary>
		/// <param name="from">The script to copy from.</param>
		/// <param name="forModification">If false, the script incl. compiled assembly is copied. If true,
		/// the compiled assembly is not copied, so that the script text can be modified.</param>
		public void CopyFrom(AbstractScript from, bool forModification)
		{
			if (object.ReferenceEquals(this, from))
				return;

			var oldScriptText = _scriptText;
			this._scriptText = from._scriptText;
			this._scriptObject = from._scriptObject;
			this._isDirty = from._isDirty;

			this._wasTriedToCompile = forModification ? false : from._wasTriedToCompile;

			this._errors = null == from._errors ? null : (string[])from._errors.Clone();

			this._compilerResult = forModification ? null : from._compilerResult; // (not cloning is intented here)

			if (oldScriptText != _scriptText)
				EhSelfChanged(EventArgs.Empty);
		}
 /// <summary>
 /// Copies the content of a script to here.
 /// </summary>
 /// <param name="from">The script to copy from.</param>
 /// <param name="forModification">If false, the script incl. compiled assembly is copied. If true,
 /// the compiled assembly is not copied, so that the script text can be modified.</param>
 public void CopyFrom(AbstractScript from, bool forModification)
 {
   this.m_ScriptText  = from.m_ScriptText;
   this.m_ScriptObject   = from.m_ScriptObject;
   this.m_IsDirty = from.m_IsDirty;
   
   this.m_WasTriedToCompile = forModification ? false : from.m_WasTriedToCompile;
   
   this.m_Errors   = null==from.m_Errors ? null: (string[])from.m_Errors.Clone();
   
   this._compilerResult = forModification ? null :  from._compilerResult; // (not cloning is intented here)
 }