REQ.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. namespace OpenVIII.Fields.Scripts.Instructions
  3. {
  4. /// <summary>
  5. /// Request remote execution
  6. /// Go to the method Label in the group Argument with a specified Priority.
  7. /// Requests that a remote entity executes one of its member functions at a specified priority. The request is asynchronous and returns immediately without waiting for the remote execution to start or finish. If the specified priority is already busy executing, the request will fail silently.
  8. /// </summary>
  9. internal sealed class REQ : JsmInstruction
  10. {
  11. private Int32 _objectIndex;
  12. private Int32 _priority;
  13. private Int32 _scriptId;
  14. public REQ(Int32 objectIndex, Int32 priority, Int32 scriptId)
  15. {
  16. _objectIndex = objectIndex;
  17. _priority = priority;
  18. _scriptId = scriptId;
  19. }
  20. public REQ(Int32 objectIndex, IStack<IJsmExpression> stack)
  21. : this(objectIndex,
  22. scriptId: ((IConstExpression)stack.Pop()).Int32(),
  23. priority: ((IConstExpression)stack.Pop()).Int32())
  24. {
  25. }
  26. public override String ToString()
  27. {
  28. return $"{nameof(REQ)}({nameof(_objectIndex)}: {_objectIndex}, {nameof(_priority)}: {_priority}, {nameof(_scriptId)}: {_scriptId})";
  29. }
  30. public override void Format(ScriptWriter sw, IScriptFormatterContext formatterContext, IServices services)
  31. {
  32. formatterContext.GetObjectScriptNamesById(_scriptId, out String typeName, out String methodName);
  33. sw.AppendLine($"{nameof(REQ)}(priority: {_priority}, GetObject<{typeName}>().{methodName}());");
  34. }
  35. public override IAwaitable TestExecute(IServices services)
  36. {
  37. EventEngine engine = ServiceId.Field[services].Engine;
  38. FieldObject targetObject = engine.GetObject(_objectIndex);
  39. if (!targetObject.IsActive)
  40. throw new NotSupportedException($"Unknown expected behavior when trying to call a method of the inactive object (Id: {_objectIndex}).");
  41. targetObject.Scripts.TryExecute(_scriptId, _priority);
  42. return DummyAwaitable.Instance;
  43. }
  44. }
  45. }