REQSW.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. namespace OpenVIII.Fields.Scripts.Instructions
  3. {
  4. /// <summary>
  5. /// Request remote execution (asynchronous execution, guaranteed)
  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. If the specified priority is already busy executing, the request will block until it becomes available and only then return. The remote execution is still carried out asynchronously, with no notification of completion.
  8. /// </summary>
  9. internal sealed class REQSW : JsmInstruction
  10. {
  11. private Int32 _objectIndex;
  12. private Int32 _priority;
  13. private Int32 _scriptId;
  14. public REQSW(Int32 objectIndex, Int32 priority, Int32 scriptId)
  15. {
  16. _objectIndex = objectIndex;
  17. _priority = priority;
  18. _scriptId = scriptId;
  19. }
  20. public REQSW(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(REQSW)}({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(REQSW)}(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.Execute(_scriptId, _priority);
  42. return DummyAwaitable.Instance;
  43. }
  44. }
  45. }