Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to run a real bash shell in-game ?

Discussion in 'Scripting' started by Lahms, Feb 11, 2019.

  1. Lahms

    Lahms

    Joined:
    Feb 11, 2019
    Posts:
    5
    Hi, sorry for the disturbing, I'm currently trying to create an in-game bash (which is linked to my unix system under the game). And I have to admit, that I'm a bit lost. Because I have to create a process using /bin/bash but also to get the output of commands..
    If you know a clean way to do that using the unity API, I would be REALLY grateful :) Thank you in advance !
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    You can make a custom C++ plugin and communicate your functionality through it.
    Although, I haven't written one myself, so it's better to google on how to do it.

    But it's 100% possible for sure.

    Also, that's a weird game.
     
    Joe-Censored likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    Using System.Diagnostics.Process you can start up /bin/bash on your unix machine.

    With it you can then set the 'StandardInput' and 'StandardOutput' to whatever StreamWriter and StreamReader you'd like of your choice.

    With this you can then intercept keyboard inputs and send them to the StandardInput for input purposes. And you can read the output from StandardOutput and display it on screen how ever you like.

    Then all you have to do is create a UI for it in game.
     
    Kurt-Dekker, ihgyug and Joe-Censored like this.
  4. Lahms

    Lahms

    Joined:
    Feb 11, 2019
    Posts:
    5
    Thank you so much for your quick answer, I will try to do it asap :)
    PS : it is a school game project, as my last project of my cybersecurity master degree I'm doing a security oriented RPG :p
     
  5. Lahms

    Lahms

    Joined:
    Feb 11, 2019
    Posts:
    5
    Hi again everybody, another day, another problem..
    After several try to do my own GUI, I just saw that you can directly use your terminal though unity.. Sadly impossible to get it work, seems that everybody have a terminal spawning, but all I get is an error.

    There is my code :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Diagnostics;
    5. using System.IO;
    6.  
    7. public class eniemetest : MonoBehaviour {
    8.  
    9.  
    10.     void test()
    11.     {
    12.         UnityEngine.Debug.Log("toto");
    13.         // This section of code is defining a shell and the directory where it should open in    
    14.             ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash");
    15.             startInfo.FileName = "/lib/terminfo/x/xterm-256color";
    16.         startInfo.WorkingDirectory = "/";
    17.             startInfo.UseShellExecute = false;
    18.             startInfo.RedirectStandardInput = true;
    19.             startInfo.RedirectStandardOutput = true;
    20.          startInfo.RedirectStandardError = true;
    21.  
    22.         //This starts the shell
    23.             Process process = new Process();
    24.             process.StartInfo = startInfo;
    25.             process.Start();
    26.      
    27.         process.StandardInput.WriteLine("mkdir /home/solal/test \r");
    28.         UnityEngine.Debug.Log("Text2");
    29.     }
    30.  
    31.     void Start()
    32.     {
    33.     UnityEngine.Debug.Log("Tttttttttttt");
    34.     test();
    35.     }
    36. }
    And there is the error, that I dont understand at all :



    I'm really sorry for asking that much help, but I feel a bit lost with the Unity API, it is usually easy to have a bash terminal in C x)

    Thank you in advance :)
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Doesn't a Win32Exception means this is a Windows computer instead of *nix?
     
    xVergilx likes this.
  7. Lahms

    Lahms

    Joined:
    Feb 11, 2019
    Posts:
    5
    I'm on
    Linux version 4.13.0-46-generic (buildd@lcy01-amd64-002) (gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3.2)) #51-Ubuntu SMP Tue Jun 12 12:36:29 UTC 2018

    and my unity is 2018.2.7f1
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    Win32Exception is just an exception type in the .net framework:
    https://docs.microsoft.com/en-us/do...tmodel.win32exception?view=netframework-4.7.2

    And if you check Process.Start can potentially throw it:
    https://docs.microsoft.com/en-us/do...mework-4.7.2#System_Diagnostics_Process_Start

    Even on *nix, the runtime/framework should behave the way it's documented. So if the framework is documented to throw an exception of type X under condition Y... then no matter the platform, it should throw exception X, even if its name is weird.

    So really, it's just that the class holds a name that reflects its historical roots rather than the actual platform it's on.
     
    Joe-Censored and xVergilx like this.
  9. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    974
    What is the value of NativeErrorCode on your Win32Exception?
     
  10. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Thanks, I wasn't aware of that.
     
  11. Lahms

    Lahms

    Joined:
    Feb 11, 2019
    Posts:
    5
    Hi all, Well I am sorry I didn't resolve my error, but manage to get an interactive shell easily like this :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Diagnostics;
    5. using System.IO;
    6.  
    7. public class interaction : MonoBehaviour {
    8.     public int toto =0;
    9.     // Use this for initialization
    10.     void Start () {
    11.        
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.        
    17.     }
    18.  
    19.     void OnTriggerEnter2D(Collider2D col)
    20.         {
    21.         UnityEngine.Debug.Log("GameObject1 collided with " + col.name);
    22.     UnityEngine.Debug.Log("entered");
    23.     toto=5;
    24.         UnityEngine.Debug.Log("toto");
    25.         // This section of code is defining a shell and the directory where it should open in    
    26.             ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash");
    27.             startInfo.FileName = "open";
    28.         startInfo.WorkingDirectory = "/";
    29.         startInfo.Arguments = "xterm";
    30.             startInfo.UseShellExecute = false;
    31.             startInfo.RedirectStandardInput = true;
    32.             startInfo.RedirectStandardOutput = true;
    33.          startInfo.RedirectStandardError = true;
    34.  
    35.         //This starts the shell
    36.             Process process = new Process();
    37.             process.StartInfo = startInfo;
    38.             process.Start();
    39.        
    40.        
    41.         UnityEngine.Debug.Log("Text2");
    42.     }
    43. }
    44.  
    If it might help someone later :) I guess it closes the topic ! Thanks to every person who tried to help <3
     
    students_t likes this.