Error() public method

public Error ( string message ) : void
message string
return void
Beispiel #1
0
		public void Validate()
		{
			var log = new Logger("Validator");
			using (new LogTimed(log, "Starting", "Finished"))
			{
				// dummy variables used to run scripts without effecting user data.
				var p = new Personality(this, "tmpValidator", "tmpValidator");
				var c = new Controller(this, "DUMMY");
				c.AddPersonality(p);
				var output = new StringBuilder();
				var vars = new Dictionary<string, Variable>();


				personControlLock.EnterReadLock();
				try
				{
					if (personalities.Count == 0)
						log.Error(StringsScripting.No_personalities);
					if (inputReplace == null || inputReplace.Count == 0)
						log.Warning(StringsScripting.No_input_replace);
				}
				finally
				{ personControlLock.ExitReadLock(); }



				if (allscripts.IsEmpty)
					log.Error(StringsScripting.No_scripts);


				// validate startup scripts.
				scriptsLock.EnterReadLock();
				try
				{
					foreach (var s in setupPersonality)
						validateScript(c, s, vars, output);
					foreach (var s in setupController)
						validateScript(c, s, vars, output);
				}
				finally
				{ scriptsLock.ExitReadLock(); }

				// validate all other scripts.
				foreach (var s in allscripts.GetAll())
					if (s.IsSet)
						validateScript(c, s, vars, output);
			}
			// ToDo 2: Scripts can change inbetwen here and validation, so we could be dirty but dirty being false.
			Dirty = false;
		}
Beispiel #2
0
		/// <summary>
		/// Parses raw lines in to the system.
		/// </summary>
		private void parseScriptGroup(List<string> rawLines, string filePath, string fileKey, Logger fileLog)
		{
			// split-up the lines in to indatation based blocks.
			var group = new GroupInfo(filePath, fileKey, fileLog);
			scriptsLock.EnterWriteLock();
			scriptGroups.Add(group);
			scriptsLock.ExitWriteLock();
			var blocks = group.Blocks;
			int currentLine = 0;
			string blockKey = null;
			string[] blockTags = null;
			string[] blockResponses = null;
			int blockLine = 0;
			while (currentLine < rawLines.Count)
			{
				string str = rawLines[currentLine];
				int indent = 0;
				fileLog.SetId(currentLine);
				if (parseCutLine(ref str, ref indent)) // line empty?
				{
					if (indent == 0) // indent 0 defines what the key of the upcoming object is.
					{
						List<string[]> args;
						parseBlockStart(str, out blockKey, out args, fileLog);
						blockTags = null;
						blockResponses = null;
						if (args.Count > 0)
							blockTags = KeyClean(args[0], fileLog);
						if (args.Count > 1)
							blockResponses = SanitizeInputReplace(args[1]);

						blockLine = currentLine;
						// make sure key is a valid type.
						var rootKey = KeySplit(blockKey)[0];
						if (rootKey != "script" && rootKey != "list" && rootKey != "setup" && rootKey != "personality")
						{
							fileLog.Error(string.Format(StringsScripting.Formatted_Error_Invalid_root_type, rootKey), currentLine);
							break;
						}
						++currentLine;
					}
					else if (indent > 0)
					{
						if (blockKey == null)
						{
							fileLog.Error(StringsScripting.Invalid_Indentation, currentLine);
							break;
						}
						var log = new Logger(fileKey + "." + blockKey);
						var lines = parseBlock(rawLines, ref currentLine, indent, log);
						if (lines == null)
							blocks.Add(new BlockBase(blockKey, null, blockTags, group, log));
						else
						{
							// Figureout type of script, then add it.
							var keySplit = KeySplit(blockKey);
							if (keySplit.Length == 2)
							{
								if (keySplit[1] == null || keySplit[1].Length == 0)
								{
									fileLog.Warning(StringsScripting.Warning_Empty_sub_key);
									continue;
								}
								var key = fileKey + '.' + keySplit[1];
								switch (keySplit[0])
								{
									case "setup":

										scriptsLock.EnterWriteLock();
										try
										{
											if (keySplit[1] == "controller")
												setupController.Add(new Script(this, false, blockKey, lines, blockTags, group, log));
											else if (keySplit[1] == "personality")
												setupPersonality.Add(new Script(this, false, blockKey, lines, blockTags, group, log));
											//else
											//ToDo : Error unknown setup type.
										}
										finally
										{ scriptsLock.ExitWriteLock(); }
										// ToDo : Remove error from strings.
										//if (blockTags != null && blockTags.Length > 0)
										//	fileLog.Warning(StringsScripting.Setup_has_tags, blockLine);
										// warn if has responses
										if (blockResponses != null && blockResponses.Length >= 0)
											fileLog.Warning(StringsScripting.Setup_has_responses, blockLine);
										break;
									case "script":
									case "list":
										{
											var script = new Script(this, keySplit[0] == "list", blockKey, lines, blockTags, group, log);
											blocks.Add(script);
											addScript(keySplit[0], fileKey, keySplit[1], script, blockTags, blockResponses);
										}
										break;
									case "personality":
										{
											key = keySplit[1];
											// does personality already exist?
											Personality p = null;
											Variable<Personality> vP = null;
											if (personalities.TryGetValue(key, out vP))
												p = vP.Value;
											// if not create new.
											else
												p = CreatePersonality(key);
											// run through the script to fill the personalities variables.
											var script = new Script(this, false, blockKey, lines, null, group, log);
											var c = new Controller(this, "DUMMY");
											c.AddPersonality(p);
											var sb = new StringBuilder();
											validateScript(c, script, null, sb);
											runThroughScript(c, script, sb);
										}
										break;
									default:
										fileLog.Error(string.Format(StringsScripting.Formatted_Error_Invalid_root_type, keySplit[0]), blockLine);
										return;
								}
							}
							else
							{
								fileLog.Error(string.Format(StringsScripting.Formatted_Error_Invalid_root_type, keySplit[0]), blockLine);
								break;
							}
						}
					}
				}
				else
					++currentLine;
			}
			return;
		}
Beispiel #3
0
		/// <summary> Gets all lines in file, returns null on IOException. </summary>
		private List<string> getFileLines(string file, out Logger log)
		{
			log = new Logger(file);
			var result = new List<string>();
			try
			{
				using (var stream = new StreamReader(file))
					while (!stream.EndOfStream)
						result.Add(stream.ReadLine());
			}
			catch (IOException ex)
			{
				log.Error(ex.Message);
				return null;
			}
			return result;
		}
Beispiel #4
0
		/// <summary>
		/// Load files in path and all sub directories.
		/// </summary>
		public void LoadFromDirectory(string path)
		{
			var log = new Logger("Loader");
			if (!Directory.Exists(path))
			{
				log.Error(string.Format(StringsScripting.Formatted_Directory_not_found, path));
				return;
			}

			using (new LogTimed(log, string.Format(StringsScripting.Formatted_Log_Load_Start, path), string.Format(StringsScripting.Formatted_Log_Load_Finish, path)))
			{
				// load all input replace files.
				var files = Directory.GetFiles(path, "input replace.csv", SearchOption.AllDirectories);
				personControlLock.EnterWriteLock();
				try
				{
					if (inputReplace == null)
						inputReplace = new List<Dictionary<string, string>>();
					foreach (var file in files)
					{
						log.Info(string.Format(StringsScripting.Formatted_Log_Loading_file, file));
						Logger fileLog;
						var rawLines = getFileLines(file, out fileLog);
						if (rawLines == null)
							continue;
						parseInputReplace(rawLines, fileLog);
					}
				}
				finally
				{ personControlLock.ExitWriteLock(); }

				// load all .vtscript files.
				files = Directory.GetFiles(path, "*.vtscript", SearchOption.AllDirectories);
				foreach (string file in files)
				{
					log.Info(string.Format(StringsScripting.Formatted_Log_Loading_file, file));
					Logger fileLog;
					// get all lines from the file.
					var rawLines = getFileLines(file, out fileLog);
					if (rawLines == null)
						continue;
					// get the base script key from the file name.
					string fileKey = KeyClean(Path.GetFileNameWithoutExtension(file));
					// parse as a group
					parseScriptGroup(rawLines, file, fileKey, fileLog);
				}
			}
		}
Beispiel #5
0
		public Script QueryScript(VType.Query query, Logger log)
		{
			if (query == null)
			{
				log.Error(StringsScripting.Query_empty);
				return null;
			}

			var list = allscripts.GetAll().ToList();
			VType.Query.QueryReduceByTag(list, query, log);
			if (list == null || list.Count == 0)
			{
				log.Error(StringsScripting.Query_empty);
				return null;
			}
			// ToDo 6: Make not random.
			int r = random.Next(0, list.Count);
			return list[r].Value;
		}
Beispiel #6
0
		public Script QueryScript(Variable query, Logger log)
		{
			if (query == null || !query.IsSet)
				log.Error(StringsScripting.Query_empty);
			else if (query.Value is VType.Query)
				return QueryScript((VType.Query)query.Value, log);
			else if (query.Value is string)
				return QueryScript((string)query.Value, log);
			else
				log.ErrorF(StringsScripting.Formatted_Invalid_Type, "Query", query.Value.GetType().Name);
			return null;
		}