Skip to content

Commit

Permalink
Merge pull request #105 from nowsprinting/feature/exitcode_expire_lif…
Browse files Browse the repository at this point in the history
…espan

Can specify exit code when Autopilot lifespan expires.
  • Loading branch information
bo40 authored Nov 19, 2024
2 parents 9c26535 + 683e0e8 commit 44398ea
Show file tree
Hide file tree
Showing 15 changed files with 322 additions and 60 deletions.
52 changes: 40 additions & 12 deletions Editor/Localization/ja.po
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ msgstr "名前"
msgid "Custom name of this setting used by Reporter. If omitted, the asset file name is used."
msgstr "このAutopilotSettingsインスタンスの名前。Reporterで使用されます。省略時はアセットファイル名がデフォルトとして使用されます"

# Header: Agent Assignment
msgid "Agent Assignment"
# Header: Agent Assignment Settings
msgid "Agent Assignment Settings"
msgstr "Agent割り当て設定"

# sceneAgentMapping
Expand All @@ -59,18 +59,46 @@ msgstr "Scene横断Agents"
msgid "Agents running by scene crossing. The specified agents will have the same lifespan as Autopilot (i.e., use DontDestroyOnLoad) for specifying, e.g., ErrorHandlerAgent and UGUIEmergencyExitAgent."
msgstr "Sceneを横断して実行されるAgent。指定されたAgentの寿命は Autopilot と同じになります(つまり、DontDestroyOnLoad を使用します)。たとえば、ErrorHandlerAgent や UGUIEmergencyExitAgent などを指定します"

# Header: Autopilot Run Settings
msgid "Autopilot Run Settings"
msgstr "オートパイロット実行設定"
# Header: Autopilot Lifespan Settings
msgid "Autopilot Lifespan Settings"
msgstr "オートパイロット実行時間設定"

# lifespanSec
msgid "Lifespan Sec"
msgid "Lifespan [sec]"
msgstr "実行時間[秒]"

# lifespanSec tooltip
msgid "Autopilot running lifespan [sec]. When specified zero, so unlimited running"
msgid "Autopilot running lifespan [sec]. When specified zero, so unlimited running."
msgstr "実行時間上限を秒で指定します。0を指定すると無制限に動作します"

# exitCode
msgid "Exit Code"
msgstr "終了コード"

# exitCode tooltip
msgid "Select the exit code used when Autopilot lifespan expires."
msgstr "オートパイロットの実行時間が満了したときに使用される終了コードを選択します"

# customExitCode
msgid "Custom Exit Code"
msgstr "カスタム終了コード"

# customExitCode tooltip
msgid "Input exit code by integer value."
msgstr "終了コードを整数値で入力します"

# exitMessage
msgid "Message"
msgstr "メッセージ"

# exitMessage tooltip
msgid "Message sent by the Reporter when Autopilot lifespan expires."
msgstr "オートパイロットの実行時間が満了したとき、Reporterから送信されるメッセージ"

# Header: Autopilot Run Settings
msgid "Autopilot Run Settings"
msgstr "オートパイロット実行設定"

# randomSeed
msgid "Random Seed"
msgstr "擬似乱数シード"
Expand Down Expand Up @@ -133,11 +161,11 @@ msgstr "このAgentインスタンスの説明"
#: Editor/UI/Agents/DoNothingAgentEditor.cs

# lifespanSec (same as AutopilotSettingsEditor.cs)
msgid "Lifespan Sec"
msgid "Lifespan [sec]"
msgstr "実行時間[秒]"

# lifespanSec tooltip
msgid "Agent running lifespan [sec]. When specified zero, so unlimited running"
msgid "Agent running lifespan [sec]. When specified zero, so unlimited running."
msgstr "実行時間上限を秒で指定します。0を指定すると無制限に動作します"


Expand Down Expand Up @@ -207,15 +235,15 @@ msgstr "連続実行するAgentを指定します"
#: Editor/UI/Agents/UGUIMonkeyAgentEditor.cs

# lifespanSec (same as DoNothingAgentEditor.cs)
msgid "Lifespan Sec"
msgid "Lifespan [sec]"
msgstr "実行時間[秒]"

# lifespanSec tooltip (same as DoNothingAgentEditor.cs)
msgid "Agent running lifespan [sec]. When specified zero, so unlimited running"
msgid "Agent running lifespan [sec]. When specified zero, so unlimited running."
msgstr "実行時間上限を秒で指定します。0を指定すると無制限に動作します"

# delayMillis
msgid "Delay Millis"
msgid "Delay [millis]"
msgstr "操作間隔[ミリ秒]"

# delayMillis tooltip
Expand Down
4 changes: 2 additions & 2 deletions Editor/UI/Agents/DoNothingAgentEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public class DoNothingAgentEditor : UnityEditor.Editor
{
private static readonly string s_description = L10n.Tr("Description");
private static readonly string s_descriptionTooltip = L10n.Tr("Description about this agent instance");
private static readonly string s_lifespanSec = L10n.Tr("Lifespan Sec");
private static readonly string s_lifespanSec = L10n.Tr("Lifespan [sec]");

private static readonly string s_lifespanSecTooltip =
L10n.Tr("Agent running lifespan [sec]. When specified zero, so unlimited running");
L10n.Tr("Agent running lifespan [sec]. When specified zero, so unlimited running.");

/// <inheritdoc/>
public override void OnInspectorGUI()
Expand Down
6 changes: 3 additions & 3 deletions Editor/UI/Agents/UGUIMonkeyAgentEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public class UGUIMonkeyAgentEditor : UnityEditor.Editor
private SerializedProperty _descriptionProp;
private GUIContent _descriptionGUIContent;

private static readonly string s_lifespanSec = L10n.Tr("Lifespan Sec");
private static readonly string s_lifespanSec = L10n.Tr("Lifespan [sec]");

private static readonly string s_lifespanSecTooltip =
L10n.Tr("Agent running lifespan [sec]. When specified zero, so unlimited running");
L10n.Tr("Agent running lifespan [sec]. When specified zero, so unlimited running.");

private SerializedProperty _lifespanProp;
private GUIContent _lifespanGUIContent;

private static readonly string s_delayMillis = L10n.Tr("Delay Millis");
private static readonly string s_delayMillis = L10n.Tr("Delay [millis]");
private static readonly string s_delayMillisTooltip = L10n.Tr("Delay time between random operations [ms]");
private SerializedProperty _delayMillisProp;
private GUIContent _delayMillisGUIContent;
Expand Down
27 changes: 22 additions & 5 deletions Editor/UI/Settings/AutopilotSettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AutopilotSettingsEditor : UnityEditor.Editor
private static readonly string s_name = L10n.Tr("Name");
private static readonly string s_nameTooltip = L10n.Tr("Custom name of this setting used by Reporter. If omitted, the asset file name is used.");

private static readonly string s_agentAssignmentHeader = L10n.Tr("Agent Assignment");
private static readonly string s_agentAssignmentHeader = L10n.Tr("Agent Assignment Settings");
private static readonly string s_sceneAgentMaps = L10n.Tr("Scene Agent Mapping");
private static readonly string s_sceneAgentMapsTooltip = L10n.Tr("Scene to Agent assign mapping");
private static readonly string s_fallbackAgent = L10n.Tr("Fallback Agent");
Expand All @@ -30,10 +30,17 @@ public class AutopilotSettingsEditor : UnityEditor.Editor
private static readonly string s_sceneCrossingAgents = L10n.Tr("Scene Crossing Agents");
private static readonly string s_sceneCrossingAgentsTooltip = L10n.Tr("Agents running by scene crossing. The specified agents will have the same lifespan as Autopilot (i.e., use DontDestroyOnLoad) for specifying, e.g., ErrorHandlerAgent and UGUIEmergencyExitAgent.");

private static readonly string s_autopilotRunSettingsHeader = L10n.Tr("Autopilot Run Settings");
private static readonly string s_lifespanSec = L10n.Tr("Lifespan Sec");
private static readonly string s_lifespanSecTooltip = L10n.Tr("Autopilot running lifespan [sec]. When specified zero, so unlimited running");
private static readonly string s_autopilotLifespanSettingsHeader = L10n.Tr("Autopilot Lifespan Settings");
private static readonly string s_lifespanSec = L10n.Tr("Lifespan [sec]");
private static readonly string s_lifespanSecTooltip = L10n.Tr("Autopilot running lifespan [sec]. When specified zero, so unlimited running.");
private static readonly string s_exitCode = L10n.Tr("Exit Code");
private static readonly string s_exitCodeTooltip = L10n.Tr("Select the exit code used when Autopilot lifespan expires.");
private static readonly string s_customExitCode = L10n.Tr("Custom Exit Code");
private static readonly string s_customExitCodeTooltip = L10n.Tr("Input exit code by integer value.");
private static readonly string s_exitMessage = L10n.Tr("Message");
private static readonly string s_exitMessageTooltip = L10n.Tr("Message sent by the Reporter when Autopilot lifespan expires.");

private static readonly string s_autopilotRunSettingsHeader = L10n.Tr("Autopilot Run Settings");
private static readonly string s_randomSeed = L10n.Tr("Random Seed");
private static readonly string s_randomSeedTooltip = L10n.Tr("Random using the specified seed value");
private static readonly string s_timeScale = L10n.Tr("Time Scale");
Expand Down Expand Up @@ -69,9 +76,19 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(AutopilotSettings.sceneCrossingAgents)),
new GUIContent(s_sceneCrossingAgents, s_sceneCrossingAgentsTooltip));

DrawHeader(s_autopilotRunSettingsHeader);
DrawHeader(s_autopilotLifespanSettingsHeader);
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(AutopilotSettings.lifespanSec)),
new GUIContent(s_lifespanSec, s_lifespanSecTooltip));
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(AutopilotSettings.exitCode)),
new GUIContent(s_exitCode, s_exitCodeTooltip));
EditorGUI.BeginDisabledGroup(((AutopilotSettings)target).exitCode != ExitCodeWhenLifespanExpired.Custom);
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(AutopilotSettings.customExitCode)),
new GUIContent(s_customExitCode, s_customExitCodeTooltip));
EditorGUI.EndDisabledGroup();
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(AutopilotSettings.exitMessage)),
new GUIContent(s_exitMessage, s_exitMessageTooltip));

DrawHeader(s_autopilotRunSettingsHeader);
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(AutopilotSettings.randomSeed)),
new GUIContent(s_randomSeed, s_randomSeedTooltip));
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(AutopilotSettings.timeScale)),
Expand Down
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,23 @@ for specifying, e.g., `ErrorHandlerAgent` and `UGUIEmergencyExitAgent`.
> [!WARNING]
> Recommend set an [ErrorHandlerAgent](#ErrorHandlerAgent) to interrupt Autopilot if an exception occurs during execution.
#### Autopilot Run Settings
#### Autopilot Lifespan Settings

This item can also be overridden from the commandline (see below).
<dl>
<dt>Lifespan</dt><dd>Specifies the execution time limit in seconds. Defaults to 300 seconds, 0 specifies unlimited operation.
This item can also be overridden from the commandline (see below).</dd>
<dt>Exit Code</dt><dd>Select the exit code used when Autopilot lifespan expires.</dd>
<dt>Custom Exit Code</dt><dd>Specify the exit code as an integer value.</dd>
<dt>Message</dt><dd>Message sent by the Reporter when Autopilot lifespan expires.</dd>
</dl>

#### Autopilot Run Settings

<dl>
<dt>Lifespan</dt><dd>Specifies the execution time limit in seconds. Defaults to 300 seconds, 0 specifies unlimited operation</dd>
<dt>Random Seed</dt><dd>Specify when you want to fix the seed given to the pseudo-random number generator (optional). This is a setting related to the pseudo-random number generator used by the autopilot. To fix the seed of the pseudo-random number generator in the game itself, it is necessary to implement this setting on the game title side. </dd>
<dt>Time Scale</dt><dd>Time.timeScale. Default is 1.0</dd>
<dt>Random Seed</dt><dd>Specify when you want to fix the seed given to the pseudo-random number generator (optional). This is a setting related to the pseudo-random number generator used by the autopilot. To fix the seed of the pseudo-random number generator in the game itself, it is necessary to implement this setting on the game title side.
This item can also be overridden from the commandline (see below).</dd>
<dt>Time Scale</dt><dd>Time.timeScale. Default is 1.0.
This item can also be overridden from the commandline (see below).</dd>
<dt>Loggers</dt><dd>Logger used for this autopilot settings. If omitted, <code>Debug.unityLogger</code> will be used as default.</dd>
<dt>Reporters</dt><dd>Reporter to be called on Autopilot terminate.</dd>
</dl>
Expand Down Expand Up @@ -237,8 +246,8 @@ This Agent implementation uses open source [test-helper.monkey](https://github.c
An instance of this Agent (.asset file) can contain the following.

<dl>
<dt>Lifespan Sec</dt><dd>Duration of random operation execution time in secounds. If 0 is specified, the operation is almost unlimited (TimeSpan.MaxValue). With this setting, neither Autopilot nor the app itself will exit when the Agent exits. It will not do anything until the next Scene switch</dd>
<dt>Delay Millis</dt><dd>Wait interval [milliseconds] between random operations</dd>
<dt>Lifespan</dt><dd>Duration of random operation execution time in secounds. If 0 is specified, the operation is almost unlimited (TimeSpan.MaxValue). With this setting, neither Autopilot nor the app itself will exit when the Agent exits. It will not do anything until the next Scene switch</dd>
<dt>Delay</dt><dd>Wait interval [milliseconds] between random operations</dd>
<dt>Secs Searching Components</dt><dd>Seconds to determine that an error has occurred when an object that can be interacted with does not exist</dd>
<dt>Touch and Hold Millis</dt><dd>Delay time for touch-and-hold [ms]</dd>
<dt>Enable Gizmos</dt><dd>Show Gizmos on GameView during running monkey test if true</dd>
Expand Down Expand Up @@ -296,7 +305,7 @@ An Agent that does nothing.
The following settings can be configured for this Agent instance (.asset file).

<dl>
<dt>Lifespan Sec</dt><dd>Specifies the do nothing time in seconds. 0 means unlimited time to do nothing. If 0 is specified, an unlimited amount of time for no action is taken. It will not do anything until the next Scene is switched.</dd>
<dt>Lifespan</dt><dd>Specifies the do nothing time in seconds. 0 means unlimited time to do nothing. If 0 is specified, an unlimited amount of time for no action is taken. It will not do anything until the next Scene is switched.</dd>
</dl>


Expand Down
21 changes: 15 additions & 6 deletions README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,23 @@ Sceneごとに自動実行を行なうAgent設定ファイル(.asset)の対
> [!WARNING]
> 実行中に例外が発生した時点でオートパイロットを中断するために、[ErrorHandlerAgent](#ErrorHandlerAgent) の設定をお勧めします。
#### オートパイロット実行設定
#### オートパイロット実行時間設定

この項目は、コマンドラインから上書きもできます(後述)。
<dl>
<dt>実行時間</dt><dd>実行時間上限を秒で指定します。デフォルトは300秒で、0を指定すると無制限に動作します。
この項目は、コマンドラインから上書きもできます(後述)。</dd>
<dt>終了コード</dt><dd>オートパイロットの実行時間が満了したときに使用される終了コードを選択します</dd>
<dt>カスタム終了コード</dt><dd>終了コードを整数値で指定します</dd>
<dt>メッセージ</dt><dd>オートパイロットの実行時間が満了したとき、Reporterから送信されるメッセージ</dd>
</dl>

#### オートパイロット実行設定

<dl>
<dt>Lifespan</dt><dd>実行時間上限を秒で指定します。デフォルトは300秒、0を指定すると無制限に動作します</dd>
<dt>Random Seed</dt><dd>疑似乱数発生器に与えるシードを固定したいときに指定します(省略可)。なお、これはオートパイロットの使用する疑似乱数発生器に関する設定であり、ゲーム本体の疑似乱数発生器シードを固定するにはゲームタイトル側での実装が必要です。</dd>
<dt>Time Scale</dt><dd>Time.timeScaleを指定します。デフォルトは1.0</dd>
<dt>Random Seed</dt><dd>疑似乱数発生器に与えるシードを固定したいときに指定します(省略可)。なお、これはオートパイロットの使用する疑似乱数発生器に関する設定であり、ゲーム本体の疑似乱数発生器シードを固定するにはゲームタイトル側での実装が必要です。
この項目は、コマンドラインから上書きもできます(後述)。</dd>
<dt>Time Scale</dt><dd>Time.timeScaleを指定します。デフォルトは1.0。
この項目は、コマンドラインから上書きもできます(後述)。</dd>
<dt>Loggers</dt><dd>オートパイロットが使用するLogger指定します。省略時は <code>Debug.unityLogger</code> がデフォルトとして使用されます</dd>
<dt>Reporters</dt><dd>オートパイロット終了時に通知を行なうReporterを指定します</dd>
</dl>
Expand Down Expand Up @@ -301,7 +310,7 @@ Automated QAによる操作のレコーディングは、Unityエディターの
このAgentのインスタンス(.assetファイル)には以下を設定できます。

<dl>
<dt>Lifespan Sec</dt><dd>なにもしない時間を秒で指定します。0を指定すると無制限になにもしません。この設定でAgentが終了してもオートパイロットおよびアプリ自体は終了しません。次にSceneが切り替わるまでなにもしない状態になります</dd>
<dt>実行時間</dt><dd>なにもしない時間を秒で指定します。0を指定すると無制限になにもしません。この設定でAgentが終了してもオートパイロットおよびアプリ自体は終了しません。次にSceneが切り替わるまでなにもしない状態になります</dd>
</dl>


Expand Down
3 changes: 1 addition & 2 deletions Runtime/AgentDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public AgentDispatcher(AutopilotSettings settings, ILogger logger, RandomFactory

public void Dispose()
{
var inspectors = Object.FindObjectsOfType<AgentInspector>();
foreach (var inspector in inspectors)
foreach (var inspector in AgentInspector.Instances)
{
_logger.Log($"Destroy running agent: {inspector.gameObject.name}");
Object.Destroy(inspector.gameObject);
Expand Down
5 changes: 5 additions & 0 deletions Runtime/AgentInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ namespace DeNA.Anjin
/// </remarks>
public class AgentInspector : MonoBehaviour
{
/// <summary>
/// Returns the running Agent instance array.
/// No caching.
/// </summary>
public static AgentInspector[] Instances => FindObjectsOfType<AgentInspector>();
}
}
12 changes: 6 additions & 6 deletions Runtime/Autopilot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ private void Start()

if (_settings.lifespanSec > 0)
{
StartCoroutine(Lifespan(_settings.lifespanSec));
StartCoroutine(Lifespan(
_settings.lifespanSec,
_settings.ExitCode,
_settings.exitMessage));
}

if (Math.Abs(_settings.timeScale - 1.0f) > 0.001 && _settings.timeScale > 0) // 0 is ignored
Expand All @@ -118,13 +121,10 @@ private void DispatchByLoadedScenes()
/// <summary>
/// Terminate when ran specified time.
/// </summary>
/// <param name="timeoutSec"></param>
/// <returns></returns>
private IEnumerator Lifespan(int timeoutSec)
private IEnumerator Lifespan(int timeoutSec, ExitCode exitCode, string message)
{
yield return new WaitForSecondsRealtime(timeoutSec);
yield return UniTask.ToCoroutine(() =>
TerminateAsync(ExitCode.Normally, "Autopilot has reached the end of its lifespan."));
yield return UniTask.ToCoroutine(() => TerminateAsync(exitCode, message));
}

private void OnDestroy()
Expand Down
Loading

0 comments on commit 44398ea

Please sign in to comment.