Sfoglia il codice sorgente

Add PRINTDATA support

StuffedAnon 6 anni fa
parent
commit
8f03a4384c
1 ha cambiato i file con 61 aggiunte e 1 eliminazioni
  1. 61 1
      NTERA.Engine/Runtime/Base/Keywords.cs

+ 61 - 1
NTERA.Engine/Runtime/Base/Keywords.cs

@@ -10,6 +10,8 @@ namespace NTERA.Engine.Runtime.Base
 {
 	public static class Keywords
 	{
+		private static Random random = new Random();
+
 		[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
 		private class KeywordAttribute : Attribute
 		{
@@ -226,7 +228,6 @@ namespace NTERA.Engine.Runtime.Base
 
 			runtime.Console.PrintHtml(htmlValue.String, false);
 		}
-
 		[Keyword("PRINT", true, false)]
 		[Keyword("PRINTFORM", true, true)]
 		public static void Print(EraRuntime runtime, StackFrame context, ExecutionNode node)
@@ -244,5 +245,64 @@ namespace NTERA.Engine.Runtime.Base
 
 			runtime.Console.PrintSingleLine(value.ToString());
 		}
+
+		private class PrintDataOptions
+		{
+			public bool PrintLine = false;
+			public bool Wait = false;
+			public bool ForceKana = false;
+			public bool NoColor = false;
+			public int Count = 0;
+			public ExecutionNode ChosenNode;
+		}
+		static private PrintDataOptions CurrentPrintDataOptions;
+
+		[Keyword("PRINTDATA")]
+		[Keyword("PRINTDATAL")]
+		[Keyword("PRINTDATAW")]
+		[Keyword("PRINTDATAK")]
+		[Keyword("PRINTDATAKL")]
+		[Keyword("PRINTDATAKW")]
+		[Keyword("PRINTDATAD")]
+		[Keyword("PRINTDATADL")]
+		[Keyword("PRINTDATADW")]
+		public static void PrintData(EraRuntime runtime, StackFrame context, ExecutionNode node)
+		{
+			/* This function doesn't do anything itself, but lets you specify a bunch of
+			   DATAFORMs followed by a ENDDATA and it picks one at random and prints that.
+			 */
+			CurrentPrintDataOptions = new PrintDataOptions();
+			var opts = node["name"].Substring(9);
+			if (opts.Contains("L"))
+				CurrentPrintDataOptions.PrintLine = true;
+			if (opts.Contains("W"))
+				CurrentPrintDataOptions.Wait = true;
+			if (opts.Contains("K"))
+				CurrentPrintDataOptions.ForceKana = true;
+			if (opts.Contains("D"))
+				CurrentPrintDataOptions.NoColor = true;
+		}
+
+		[Keyword("DATA", true, true)]
+		[Keyword("DATAFORM", true, true)]
+		public static void DataForm(EraRuntime runtime, StackFrame context, ExecutionNode node)
+		{
+			CurrentPrintDataOptions.Count++;
+			if (random.Next(0, CurrentPrintDataOptions.Count) == 0)
+				CurrentPrintDataOptions.ChosenNode = node.Single();
+		}
+
+		[Keyword("ENDDATA")]
+		public static void EndData(EraRuntime runtime, StackFrame context, ExecutionNode node)
+		{
+			if (CurrentPrintDataOptions.ChosenNode == null) {
+				throw new ArgumentException("No DATAFORM found before ENDDATA");
+			}
+			var value = runtime.ComputeExpression(context, CurrentPrintDataOptions.ChosenNode);
+			if (CurrentPrintDataOptions.PrintLine)
+				runtime.Console.PrintSingleLine(value.ToString());
+			else
+				runtime.Console.Write(value.ToString());
+		}
 	}
 }