Explorar el Código

Implement inputs

Bepsi hace 6 años
padre
commit
f566080f2e

+ 37 - 32
NTERA.Core/GameInstance.cs

@@ -21,38 +21,43 @@ namespace NTERA.Core
 
 		public void GiveInput(string input)
 		{
-			InputRequest currentRequest = Console.CurrentRequest;
-
-			if (Console.CurrentRequest != null)
-			{
-				switch (currentRequest.InputType)
-				{
-					case InputType.IntValue:
-						long inputValue;
-
-						if (string.IsNullOrEmpty(input) && currentRequest.HasDefValue)// && !IsRunningTimer)
-						{
-							inputValue = currentRequest.DefIntValue;
-							input = inputValue.ToString();
-						}
-						else if (!long.TryParse(input, out inputValue))
-						{
-							break;
-						}
-
-						if (currentRequest.IsSystemInput)
-							ScriptEngine.InputSystemInteger(inputValue);
-						else
-							ScriptEngine.InputInteger(inputValue);
-						break;
-					case InputType.StrValue:
-						if (string.IsNullOrEmpty(input) && currentRequest.HasDefValue)// && !IsRunningTimer)
-							input = currentRequest.DefStrValue;
-
-						ScriptEngine.InputString(input ?? "");
-						break;
-				}
-			}
+			//InputRequest currentRequest = Console.CurrentRequest;
+
+			//if (Console.CurrentRequest != null)
+			//{
+			//	switch (currentRequest.InputType)
+			//	{
+			//		case InputType.IntValue:
+			//			long inputValue;
+
+			//			if (string.IsNullOrEmpty(input) && currentRequest.HasDefValue)// && !IsRunningTimer)
+			//			{
+			//				inputValue = currentRequest.DefIntValue;
+			//				input = inputValue.ToString();
+			//			}
+			//			else if (!long.TryParse(input, out inputValue))
+			//			{
+			//				break;
+			//			}
+
+			//			if (currentRequest.IsSystemInput)
+			//				ScriptEngine.InputSystemInteger(inputValue);
+			//			else
+			//				ScriptEngine.InputInteger(inputValue);
+			//			break;
+			//		case InputType.StrValue:
+			//			if (string.IsNullOrEmpty(input) && currentRequest.HasDefValue)// && !IsRunningTimer)
+			//				input = currentRequest.DefStrValue;
+
+			//			ScriptEngine.InputString(input ?? "");
+			//			break;
+			//	}
+			//}
+
+			if (long.TryParse(input, out long number))
+				ScriptEngine.InputInteger(number);
+			else
+				ScriptEngine.InputString(input);
 
 			Console.GiveInput(input);
 		}

+ 1 - 1
NTERA.Engine/Runtime/Base/Functions.cs

@@ -27,7 +27,7 @@ namespace NTERA.Engine.Runtime.Base
 				var function = method.GetCustomAttribute<FunctionAttribute>();
 
 				if (function != null)
-					output[function.Name] = (runtime, set, parameters) => (Value)method.Invoke(null, new object[] { runtime, set, parameters });
+					output[function.Name] = (Func<EraRuntime, StackFrame, IList<Parameter>, Value>)Delegate.CreateDelegate(typeof(Func<EraRuntime, StackFrame, IList<Parameter>, Value>), method);
 			}
 
 			return output;

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

@@ -41,12 +41,28 @@ namespace NTERA.Engine.Runtime.Base
 				var keywords = method.GetCustomAttributes<KeywordAttribute>();
 
 				foreach (KeywordAttribute keyword in keywords)
-					output[keyword.Name] = (runtime, context, node) => { method.Invoke(null, new object[] { runtime, context, node }); };
+					output[keyword.Name] = (Action<EraRuntime, StackFrame, ExecutionNode>)Delegate.CreateDelegate(typeof(Action<EraRuntime, StackFrame, ExecutionNode>), method);
 			}
 
 			return output;
 		}
 
+		[Keyword("INPUT")]
+		public static void Input(EraRuntime runtime, StackFrame context, ExecutionNode node)
+		{
+			runtime.InputResetEvent.WaitOne();
+
+			context.Variables["RESULT"][0] = runtime.LastInputValue;
+		}
+
+		[Keyword("INPUTS")]
+		public static void InputString(EraRuntime runtime, StackFrame context, ExecutionNode node)
+		{
+			runtime.InputResetEvent.WaitOne();
+
+			context.Variables["RESULTS"][0] = runtime.LastInputValue;
+		}
+
 		[Keyword("TIMES")]
 		public static void Times(EraRuntime runtime, StackFrame context, ExecutionNode node)
 		{

+ 19 - 16
NTERA.Engine/Runtime/EraRuntime.cs

@@ -3,9 +3,11 @@ using System.Collections.Generic;
 using System.Drawing;
 using System.Linq;
 using System.Runtime.CompilerServices;
+using System.Threading;
 using NTERA.Core;
 using NTERA.EmuEra.Game.EraEmu.Content;
 using NTERA.Engine.Compiler;
+using NTERA.Engine.Runtime.Base;
 
 namespace NTERA.Engine.Runtime
 {
@@ -18,7 +20,9 @@ namespace NTERA.Engine.Runtime
 
 		public List<FunctionDefinition> TotalProcedureDefinitions { get; set; } = new List<FunctionDefinition>(BaseDefinitions.DefaultGlobalFunctions);
 		public Dictionary<string, Variable> GlobalVariables { get; set; } = new Dictionary<string, Variable>();
-		public Dictionary<string, Variable> SemiGlobalVariables { get; set; } = new Dictionary<string, Variable>();
+
+		public Value LastInputValue { get; set; }
+		public AutoResetEvent InputResetEvent { get; } = new AutoResetEvent(false);
 
 		public EraRuntime(IExecutionProvider executionProvider)
 		{
@@ -62,7 +66,7 @@ namespace NTERA.Engine.Runtime
 				throw;
 			}
 
-			System.Threading.Thread.Sleep(-1);
+			Thread.Sleep(-1);
 		}
 
 		public ExecutionResult Call(FunctionDefinition function, IList<Parameter> parameters = null)
@@ -71,8 +75,10 @@ namespace NTERA.Engine.Runtime
 
 			foreach (var variable in function.Variables)
 			{
-				var localVariable = new Variable(variable.Name, variable.ValueType);
-				localVariable[0] = variable.CalculatedValue;
+				var localVariable = new Variable(variable.Name, variable.ValueType)
+				{
+					[0] = variable.CalculatedValue
+				};
 
 				localVariables.Add(variable.Name, localVariable);
 			}
@@ -94,7 +100,7 @@ namespace NTERA.Engine.Runtime
 
 			if (function.Filename == "__GLOBAL")
 			{
-				var resultValue = Base.Functions.StaticFunctions[function.Name].Invoke(this, newContext, parameters);
+				var resultValue = Functions.StaticFunctions[function.Name].Invoke(this, newContext, parameters);
 
 				result = new ExecutionResult(ExecutionResultType.FunctionReturn, resultValue);
 			}
@@ -183,7 +189,7 @@ namespace NTERA.Engine.Runtime
 				case "statement":
 					string statement = node["name"];
 
-					if (!Base.Keywords.StaticKeywords.TryGetValue(statement, out var keywordAction))
+					if (!Keywords.StaticKeywords.TryGetValue(statement, out var keywordAction))
 						throw new EraRuntimeException($"Unknown statement: '{statement}'");
 
 					keywordAction(this, context, node);
@@ -326,19 +332,16 @@ namespace NTERA.Engine.Runtime
 			}
 		}
 
-		public void InputString(string input)
-		{
-			throw new NotImplementedException();
-		}
+		public void InputString(string input) => Input(input);
 
-		public void InputInteger(long input)
-		{
-			throw new NotImplementedException();
-		}
+		public void InputInteger(long input) => Input(input);
+
+		public void InputSystemInteger(long input) => Input(input);
 
-		public void InputSystemInteger(long input)
+		public void Input(Value value)
 		{
-			throw new NotImplementedException();
+			LastInputValue = value;
+			InputResetEvent.Set();
 		}
 
 		public CroppedImage GetImage(string name)