Created
December 6, 2022 00:47
-
-
Save TheDelta/690f80b874f7522e9b0f4d842e3a03f8 to your computer and use it in GitHub Desktop.
C# cmd unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Diagnostics; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using TMPro; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using Debug = UnityEngine.Debug; | |
public class InputCommandExecuter : MonoBehaviour | |
{ | |
public int TIMEOUT = 10; | |
[SerializeField] | |
private Button m_btnSubmit; | |
[SerializeField] | |
private TMP_InputField m_inputQuestion; | |
[SerializeField] | |
private TextMeshProUGUI m_textAnswer; | |
private Process m_process; | |
private Task m_task; | |
public void OnSubmit() | |
{ | |
DisableInputs(); | |
ExecuteProgram(); | |
} | |
public void KillProcess() | |
{ | |
if (m_process is {HasExited: true}) | |
{ | |
m_process = null; | |
return; | |
} | |
m_process?.Kill(); | |
} | |
public bool IsProcessRunning() | |
{ | |
return m_process != null; | |
} | |
private void DisableInputs(bool disable = true) | |
{ | |
m_btnSubmit.enabled = !disable; | |
m_inputQuestion.enabled = !disable; | |
} | |
IEnumerator ExecuteTimeout() | |
{ | |
yield return new WaitForSeconds(TIMEOUT); | |
Debug.LogFormat("Execution took too long (timeout is {0}s)! Killing process!", this.TIMEOUT); | |
KillProcess(); | |
} | |
void ExecuteProgram() | |
{ | |
if (m_process != null) return; | |
string question = m_inputQuestion.text; | |
m_process = new Process() | |
{ | |
StartInfo = | |
{ | |
FileName = @"C:\windows\system32\cmd.exe", | |
Arguments = "/C question.bat", | |
WorkingDirectory = Application.dataPath + "/exe/", | |
UseShellExecute = false, | |
CreateNoWindow = true, | |
RedirectStandardInput = true, | |
RedirectStandardOutput = true, | |
}, | |
EnableRaisingEvents = true | |
}; | |
m_process.Start(); | |
m_process.Exited += (sender, args) => | |
{ | |
m_process = null; | |
}; | |
StringBuilder answer = new StringBuilder(); | |
// read stdout and stderr in new thread because it is blocking | |
Thread readerThread = new(() => | |
{ | |
bool questionSent = false; | |
try | |
{ | |
StringBuilder line = new StringBuilder(); | |
while (!m_process.StandardOutput.EndOfStream && !m_process.HasExited) | |
{ | |
var c = m_process.StandardOutput.Read(); | |
if (c == -1) | |
{ | |
answer.Append("---EOF---"); | |
break; | |
} | |
line.Append(char.ConvertFromUtf32(c)); | |
if (line.ToString().StartsWith("Question:")) | |
{ | |
line.Clear(); | |
m_process.StandardInput.WriteLine(question); | |
questionSent = true; | |
} | |
else if (questionSent) | |
{ | |
answer.Append(char.ConvertFromUtf32(c)); | |
m_process.StandardInput.Write('\r'); // simply press any key all the time to exit bat | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
m_process.Close(); | |
} | |
}); | |
readerThread.Start(); | |
if (m_process.WaitForExit(this.TIMEOUT * 1000)) | |
{ | |
readerThread.Join(); | |
} | |
else | |
{ | |
readerThread.Interrupt(); // timeout was hit | |
} | |
m_process.Close(); | |
DisableInputs(false); | |
Debug.Log(answer.ToString()); | |
m_textAnswer.text = answer.ToString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
set /p question= Question: | |
IF "%question%"=="dolphin" goto dolphin | |
IF "%question%"=="test" goto test | |
echo "answer of %question%" | |
pause | |
exit /b | |
:dolphin | |
echo eeeeeeeee | |
pause | |
exit /b | |
:test | |
echo works! | |
pause | |
exit /b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment