Skip to content
Snippets Groups Projects
StartServer.cs 6.16 KiB
using System.Collections;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using static CommunicationEvents;
using static UIconfig;

public class StartServer : MonoBehaviour
{
    [SerializeField]
    TMPro.TextMeshProUGUI WaitingText;

    public static Process process;
    public static ProcessStartInfo processInfo;
    public int autostart = 0; //when 1 in Start() will be ServerRoutine() launched. 
    public int autoend = 0; //Update() also affected
    public int autoprepareGame = 0;
    public int autocheckIfServerIsRunning = 0;


    // Start is called before the first frame update
    void Start()
    {
        CommunicationEvents.ServerRunning = false;

        if (ServerAutoStart == true && autostart == 1)
        {
            StartCoroutine(ServerRoutine());
        }
        if (ServerAutoStart == true && autostart == 2)
        {
            StartCoroutine(ServerRoutine1());
        }
    }

    void Update()
    {
        if (autoend == 1)
        {
            if (CommunicationEvents.ServerRunning && Input.anyKey)
            {
                SceneManager.LoadScene(1);
            }

            //if(!ServerRunning) UnityEngine.Debug.Log("waiting " + ServerRunning);
        }
    }

    void PrepareGame()
    {
        if (autoprepareGame != 0)
        {
            WaitingText.text = "Press any key to start the game";
            CommunicationEvents.ServerRunning = true;
            UnityEngine.Debug.Log("server fin");
        }
    }

    IEnumerator ServerRoutine1()
    {

        string command = "\"" + Application.streamingAssetsPath + "\"/start.BAT " + "\"" + Application.streamingAssetsPath + "\"";
        command = command.Replace("/", @"\");
        command = "\"" + command + "\"";
        UnityEngine.Debug.Log(command);
        ProcessStartInfo processInfo;

        //processInfo = new ProcessStartInfo("cmd.exe", "/C " + command);
        bool cmd = true;