private void Initialise() { if (m_script != null) { return; } try { if (m_scriptConstructor == null) { m_script = m_scriptFactory.CreateScript(m_scriptString, m_scriptContext, false, false); } else { m_script = m_scriptConstructor.Create(m_scriptString, m_scriptContext); m_script.Line = m_scriptString; } } catch { if (!m_worldModel.EditMode) { throw; } m_script = new FailedScript(m_scriptString); if (m_scriptConstructor == null) { m_script = new MultiScript(m_scriptFactory.WorldModel, m_script); } } m_script.Parent = m_parent; m_scriptString = null; }
public IScript CreateScript(string line, Element proc) { string remainingScript; IScript newScript; MultiScript result = new MultiScript(); bool finished = false; IScript lastIf = null; IScript lastFirstTime = null; bool dontAdd; bool addedError; IfScriptConstructor ifConstructor = null; line = Utility.RemoveSurroundingBraces(line); while (!finished) { line = Utility.GetScript(line, out remainingScript); if (line != null) { line = line.Trim(); line = Utility.RemoveComments(line); } if (!string.IsNullOrEmpty(line)) { newScript = null; dontAdd = false; addedError = false; if (line.StartsWith("else")) { if (lastIf == null) { AddError("Unexpected 'else' (error with parent 'if'?):" + line); } else { if (line.StartsWith("else if")) { ifConstructor.AddElseIf(lastIf, line, proc); } else { ifConstructor.AddElse(lastIf, line, proc); } } dontAdd = true; } else if (line.StartsWith("otherwise")) { if (lastFirstTime == null) { AddError("Unexpected 'otherwise' (error with parent 'firsttime'?):" + line); } else { FirstTimeScriptConstructor.AddOtherwiseScript(lastFirstTime, line, this); } dontAdd = true; } else { lastIf = null; IScriptConstructor constructor = GetScriptConstructor(line); if (constructor != null) { try { newScript = constructor.Create(line, proc); if (constructor.Keyword == "if") { ifConstructor = (IfScriptConstructor)constructor; lastIf = newScript; } if (constructor.Keyword == "firsttime") { lastFirstTime = newScript; } } catch (Exception ex) { AddError(string.Format("Error adding script '{0}': {1}", line, ex.Message)); addedError = true; } } if (newScript == null) { // See if the script is like "myvar = 2". newScript will be null otherwise. newScript = m_setConstructor.Create(line, proc); } if (newScript == null) { // See if the script calls a procedure defined by the game newScript = m_procConstructor.Create(line, proc); } } if (!dontAdd) { if (newScript == null) { if (!addedError) { AddError(string.Format("Unrecognised script command '{0}'", line)); } } else { newScript.Line = line; result.Add(newScript); } } } line = remainingScript; if (string.IsNullOrEmpty(line)) { finished = true; } } return(result); }
public IScript CreateScript(string line, ScriptContext scriptContext, bool lazy, bool addExceptionsToLog = true) { MultiScript result = new MultiScript(m_worldModel); bool finished = false; IScript lastIf = null; IScript lastComment = null; IScript lastFirstTime = null; IfScriptConstructor ifConstructor = null; if (lazy) { return(new LazyLoadScript(this, line, scriptContext)); } line = Utility.RemoveSurroundingBraces(line); line = Utility.RemoveComments(line, m_worldModel.EditMode); while (!finished) { string remainingScript; try { line = Utility.GetScript(line, out remainingScript); } catch (Exception ex) { if (addExceptionsToLog) { AddError(string.Format("Error adding script '{0}': {1}", line, ex.Message)); break; } throw; } if (line != null) { line = line.Trim(); } if (!string.IsNullOrEmpty(line)) { IScript newScript = null; bool dontAdd = false; bool addedError = false; if (line.StartsWith("else")) { if (lastIf == null) { AddError("Unexpected 'else' (error with parent 'if'?):" + line); } else { if (line.StartsWith("else if")) { ifConstructor.AddElseIf(lastIf, line, scriptContext); } else { ifConstructor.AddElse(lastIf, line, scriptContext); } } dontAdd = true; } else if (line.StartsWith("otherwise")) { if (lastFirstTime == null) { AddError("Unexpected 'otherwise' (error with parent 'firsttime'?):" + line); } else { FirstTimeScriptConstructor.AddOtherwiseScript(lastFirstTime, line, this); } dontAdd = true; } else { IScriptConstructor constructor = GetScriptConstructor(line); if (constructor is CommentScriptConstructor) { if (lastComment != null) { ((CommentScript)lastComment).AddLine(line); dontAdd = true; } else { newScript = constructor.Create(line, scriptContext); lastComment = newScript; } } else { lastIf = null; lastComment = null; lastFirstTime = null; if (constructor != null) { try { if (m_lazyLoadingEnabled) { newScript = new LazyLoadScript(this, constructor, line, scriptContext); } else { newScript = constructor.Create(line, scriptContext); } if (constructor.Keyword == "if") { ifConstructor = (IfScriptConstructor)constructor; lastIf = newScript; } if (constructor.Keyword == "firsttime") { lastFirstTime = newScript; } } catch (Ciloci.Flee.ExpressionCompileException ex) { AddError(string.Format("Error compiling expression in '{0}': {1}", line, ex.Message)); addedError = true; } catch (Exception ex) { AddError(string.Format("Error adding script '{0}': {1}", line, ex.Message)); addedError = true; } } if (!addedError) { if (newScript == null) { // See if the script is like "myvar = 2". newScript will be null otherwise. newScript = m_setConstructor.Create(line, scriptContext); } if (newScript == null) { // See if the script calls a procedure defined by the game newScript = m_procConstructor.Create(line, scriptContext); } } } } if (!dontAdd && !addedError) { if (newScript == null) { AddError(string.Format("Unrecognised script command '{0}'", line)); } else { if (!m_lazyLoadingEnabled) { newScript.Line = line; } result.Add(newScript); } } } line = remainingScript; if (string.IsNullOrEmpty(line)) { finished = true; } } return(result); }