mock exe mod and keyboard input

This commit is contained in:
2025-01-12 00:43:51 +03:00
parent a4372ceb9e
commit a78494a7b1
7 changed files with 97 additions and 14 deletions
+3 -1
View File
@@ -1,6 +1,8 @@
using mROA.Implementation;
namespace mROA; namespace mROA;
public interface IExecuteModule public interface IExecuteModule
{ {
ICommandExecution Execute(int objectId, int commandId, object parameter); ICommandExecution Execute(ICallRequest command);
} }
+3 -1
View File
@@ -1,7 +1,9 @@
using mROA.Implementation;
namespace mROA; namespace mROA;
public interface IInputModule public interface IInputModule
{ {
void HandleIncomingRequest(object command); void HandleIncomingRequest(int clientId, string command);
void PostResponse(ICommandExecution call); void PostResponse(ICommandExecution call);
} }
-1
View File
@@ -2,6 +2,5 @@ namespace mROA;
public interface IInteractionModule public interface IInteractionModule
{ {
void SetInteractionHandler(Action<(int clientId, byte[] message)> handler);
void SendTo(int clientId, byte[] message); void SendTo(int clientId, byte[] message);
} }
+25 -1
View File
@@ -7,9 +7,33 @@ namespace mROA.Implementation;
public class InputModule(IInteractionModule dataSource, IExecuteModule executeModule) public class InputModule(IInteractionModule dataSource, IExecuteModule executeModule)
: IInputModule : IInputModule
{ {
public void HandleIncomingRequest(object command) public void HandleIncomingRequest(int clientId, string command)
{ {
var type = JsonDocument.Parse(command).RootElement.GetProperty("RequestTypeId").GetInt32();
ICallRequest? request;
switch (type)
{
case 0:
request = JsonSerializer.Deserialize<StaticCallRequest>(command);
break;
case 1:
request = JsonSerializer.Deserialize<CallRequest>(command);
break;
case 2:
request = JsonSerializer.Deserialize<ParametrizedCallRequest>(command);
break;
default:
request = JsonSerializer.Deserialize<StaticCallRequest>(command);
break;
}
var response = executeModule.Execute(request);
var texted = JsonSerializer.Serialize(response);
var binary = Encoding.UTF8.GetBytes(texted);
dataSource.SendTo(clientId, binary);
} }
public void PostResponse(ICommandExecution call) public void PostResponse(ICommandExecution call)
+38 -10
View File
@@ -8,31 +8,59 @@ public class PrepairedExecutionModule(
IContextRepository contextRepo) IContextRepository contextRepo)
: IExecuteModule : IExecuteModule
{ {
public ICommandExecution Execute(int objectId, int commandId, object parameter) public ICommandExecution Execute(ICallRequest command)
{ {
var command = methodRepo.GetMethod(commandId); var currentCommand = methodRepo.GetMethod(command.CommandId);
if (command == null) if (currentCommand == null)
throw new Exception($"Command {commandId} not found"); throw new Exception($"Command {command.CommandId} not found");
if (command.ReturnType != typeof(Task)) var context = command is CallRequest request ? contextRepo.GetObject(request.ObjectId) : null;
var parameter = command is ParametrizedCallRequest callRequest ? callRequest.Parameter : null;
if (currentCommand.ReturnType == typeof(Task<>))
{ {
var tokenSource = new CancellationTokenSource(); var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token; var token = tokenSource.Token;
var result = command.Invoke(contextRepo.GetObject(objectId), [parameter, token]) as Task<object>;
var exec = new AsyncCommandExecution(tokenSource) { CommandId = commandId }; var result =
currentCommand.Invoke(context, parameter is null ? [token] : [parameter, token]) as Task<object>;
var exec = new AsyncCommandExecution(tokenSource)
{ CommandId = command.CommandId, ClientId = command.ClientId };
result.ContinueWith(task => result.ContinueWith(task =>
{ {
var result = task.Result; var result = task.Result;
inputModule.PostResponse(new FinalCommandExecution inputModule.PostResponse(new FinalCommandExecution
{ ExecutionId = exec.ExecutionId, Result = result, CommandId = commandId }); {
ExecutionId = exec.ExecutionId, Result = result, CommandId = command.CommandId,
ClientId = command.ClientId
});
}, token);
return exec;
}
else
{
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
var result = currentCommand.Invoke(context, parameter is null ? [token] : [parameter, token]) as Task;
var exec = new AsyncCommandExecution(tokenSource)
{ CommandId = command.CommandId, ClientId = command.ClientId };
result.ContinueWith(_ =>
{
inputModule.PostResponse(new FinalCommandExecution
{
ExecutionId = exec.ExecutionId, Result = null, CommandId = command.CommandId,
ClientId = command.ClientId
});
}, token); }, token);
return exec; return exec;
} }
var finalResult = command.Invoke(contextRepo.GetObject(objectId), [parameter]); var finalResult = currentCommand.Invoke(context, parameter is null ? [] : [parameter]);
return new FinalCommandExecution { CommandId = commandId, Result = finalResult }; return new FinalCommandExecution { CommandId = command.CommandId, Result = finalResult };
} }
} }
@@ -0,0 +1,18 @@
namespace mROA.Implementation;
public class ConsoleInteractionChanel(IInputModule inputModule) : IInteractionModule
{
public void StartListeningKeyboard()
{
while (true)
{
var input = Console.ReadLine();
inputModule.HandleIncomingRequest(123, input);
}
}
public void SendTo(int clientId, byte[] message)
{
Console.WriteLine("{0}: {1}", clientId, System.Text.Encoding.UTF8.GetString(message));
}
}
@@ -0,0 +1,10 @@
namespace mROA.Implementation;
public class MockExecModule : IExecuteModule
{
public ICommandExecution Execute(ICallRequest command)
{
return new FinalCommandExecution
{ ClientId = command.ClientId, CommandId = command.CommandId, Result = new { A = "wqer", B = 5 } };
}
}