Exemplo n.º 1
0
        void Start()
        {
            if (Instance != null)
            {
                Destroy(gameObject);
                return;
            }

            if (!Config.RunAsMaster)
            {
                // TODO: change UI and do not run rest of code
                var obj = new GameObject("ClientManager");
                obj.AddComponent <Network.ClientManager>();
            }

            DatabaseManager.Init();

            try
            {
                var host = Config.WebHost == "*" ? "localhost" : Config.WebHost;
                Address = $"http://{host}:{Config.WebPort}";

                var config = new HostConfiguration {
                    RewriteLocalhost = Config.WebHost == "*"
                };

                Server = new NancyHost(new UnityBootstrapper(), config, new Uri(Address));
                Server.Start();
            }
            catch (SocketException ex)
            {
                Debug.LogException(ex);
#if UNITY_EDITOR
                UnityEditor.EditorApplication.isPlaying = false;
#else
                // return non-zero exit code
                Application.Quit(1);
#endif
                return;
            }

            DownloadManager.Init();
            RestartPendingDownloads();

            LoaderScene = SceneManager.GetActiveScene().name;
            var version = "Development";
            var info    = Resources.Load <BuildInfo>("BuildInfo");
            if (info != null)
            {
                version = info.Version;
            }
            SIM.Init(version);
            SIM.LogSimulation(SIM.Simulation.ApplicationStart);

            DontDestroyOnLoad(this);
            Instance = this;
        }
Exemplo n.º 2
0
        private void EditorInit()
        {
#if UNITY_EDITOR
            stopWatch.Start();
            var info = Resources.Load <BuildInfo>("BuildInfo");
            SIM.Init(info == null ? "Development" : info.Version);
            SIM.LogSimulation(SIM.Simulation.ApplicationStart);
            Instance = this;

            var sim = Instantiate(Instance.SimulatorManagerPrefab);
            sim.name = "SimulatorManager";
            bool useSeed    = false;
            int? seed       = null;
            bool enableNPCs = false;
            bool enablePEDs = false;

            var data = UnityEditor.EditorPrefs.GetString("Simulator/DevelopmentSettings");
            if (data != null)
            {
                try
                {
                    var json = JSONNode.Parse(data);

                    useSeed = json["UseSeed"];
                    if (useSeed)
                    {
                        seed = json["Seed"];
                    }

                    enableNPCs = json["EnableNPCs"];
                    enablePEDs = json["EnablePEDs"];
                }
                catch (System.Exception ex)
                {
                    Debug.LogException(ex);
                }
            }

            sim.Init(seed);
            sim.AgentManager.SetupDevAgents();
            sim.NPCManager.NPCActive = enableNPCs;
            sim.PedestrianManager.PedestriansActive = enablePEDs;
#endif
        }
Exemplo n.º 3
0
        private void Awake()
        {
            stopWatch.Start();
            RenderLimiter.RenderLimitEnabled();

            var info = Resources.Load <BuildInfo>("BuildInfo");

            SIM.Init(info == null ? "Development" : info.Version);

            if (PlayerPrefs.HasKey("Salt"))
            {
                Config.salt = StringToByteArray(PlayerPrefs.GetString("Salt"));
            }
            else
            {
                Config.salt = new byte[8];
                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                rng.GetBytes(Config.salt);
                PlayerPrefs.SetString("Salt", ByteArrayToString(Config.salt));
                PlayerPrefs.Save();
            }
        }
Exemplo n.º 4
0
    private static void LogPlayModeState(PlayModeStateChange state)
    {
        if (state == PlayModeStateChange.EnteredPlayMode)
        {
            Scene scene = SceneManager.GetActiveScene();
            if (scene.name == "LoaderScene")
            {
                var data = EditorPrefs.GetString("Simulator/DevelopmentSettings");
                if (data != null)
                {
                    var json = JSONNode.Parse(data);
                    if (json["EnableAPI"].AsBool)
                    {
                        var api = Object.Instantiate(Simulator.Loader.Instance.ApiManagerPrefab);
                        api.name = "ApiManager";
                        Simulator.Loader.Instance.LoaderUI.SetLoaderUIState(LoaderUI.LoaderUIStateType.READY);
                    }
                }
            }
            else
            {
                var simObj = AssetDatabase.LoadAssetAtPath <GameObject>("Assets/Prefabs/Managers/SimulatorManager.prefab");
                if (simObj == null)
                {
                    Debug.LogError("Missing SimulatorManager.prefab in Resources folder!");
                    return;
                }
                stopWatch.Start();
                var version = "Development";
                var info    = Resources.Load <BuildInfo>("BuildInfo");
                if (info != null)
                {
                    version = info.Version;
                }
                SIM.Init(version);
                SIM.LogSimulation(SIM.Simulation.ApplicationStart);
                var sim = Object.Instantiate(simObj).GetComponent <SimulatorManager>();
                sim.name = "SimulatorManager";

                bool useSeed    = false;
                int? seed       = null;
                bool enableNPCs = false;
                bool enablePEDs = false;

                var data = EditorPrefs.GetString("Simulator/DevelopmentSettings");
                if (data != null)
                {
                    try
                    {
                        var json = JSONNode.Parse(data);

                        useSeed = json["UseSeed"];
                        if (useSeed)
                        {
                            seed = json["Seed"];
                        }

                        enableNPCs = json["EnableNPCs"];
                        enablePEDs = json["EnablePEDs"];
                    }
                    catch (System.Exception ex)
                    {
                        Debug.LogException(ex);
                    }
                }

                sim.Init(seed);
                sim.AgentManager.SetupDevAgents();
                sim.NPCManager.NPCActive = enableNPCs;
                sim.PedestrianManager.PedestriansActive = enablePEDs;
            }
        }
        else if (state == PlayModeStateChange.ExitingPlayMode)
        {
            if (stopWatch.IsRunning)
            {
                stopWatch.Stop();
                SIM.LogSimulation(SIM.Simulation.ApplicationExit, value: (long)stopWatch.Elapsed.TotalSeconds);
            }
        }
    }
Exemplo n.º 5
0
        private void Init()
        {
            RenderLimiter.RenderLimitEnabled();
            if (Instance != null)
            {
                Destroy(gameObject);
                return;
            }

            stopWatch.Start();

            var info = Resources.Load <BuildInfo>("BuildInfo");

            SIM.Init(info == null ? "Development" : info.Version);

            if (PlayerPrefs.HasKey("Salt"))
            {
                Config.salt = StringToByteArray(PlayerPrefs.GetString("Salt"));
            }
            else
            {
                Config.salt = new byte[8];
                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                rng.GetBytes(Config.salt);
                PlayerPrefs.SetString("Salt", ByteArrayToString(Config.salt));
                PlayerPrefs.Save();
            }

            if (Config.RunAsMaster)
            {
                var masterGameObject = new GameObject("MasterManager");
                Network.Master = masterGameObject.AddComponent <MasterManager>();
                masterGameObject.AddComponent <MainThreadDispatcher>();
                Network.Master.SetSettings(NetworkSettings);
                Network.Master.StartConnection();
                DontDestroyOnLoad(masterGameObject);
            }
            else
            {
                var clientGameObject = new GameObject("ClientManager");
                Network.Client = clientGameObject.AddComponent <ClientManager>();
                clientGameObject.AddComponent <MainThreadDispatcher>();
                Network.Client.SetSettings(NetworkSettings);
                Network.Client.StartConnection();
                Network.Initialize(SimulationNetwork.ClusterNodeType.Client, NetworkSettings);
                DontDestroyOnLoad(clientGameObject);
            }

            DatabaseManager.Init();

            DownloadManager.Init();
            RestartPendingDownloads();

            if (Config.RunAsMaster)
            {
                try
                {
                    var host = Config.WebHost == "*" ? "localhost" : Config.WebHost;
                    Address = $"http://{host}:{Config.WebPort}";

                    var config = new HostConfiguration {
                        RewriteLocalhost = Config.WebHost == "*"
                    };

                    Server = new NancyHost(new UnityBootstrapper(), config, new Uri(Address));
                    if (!string.IsNullOrEmpty(Config.Username))
                    {
                        LoginAsync();
                    }
                    else
                    {
                        Server.Start();
                    }
                }
                catch (SocketException ex)
                {
                    Debug.LogException(ex);
#if UNITY_EDITOR
                    UnityEditor.EditorApplication.isPlaying = false;
#else
                    // return non-zero exit code
                    Application.Quit(1);
#endif
                    return;
                }
            }

            LoaderScene = SceneManager.GetActiveScene().name;
            SIM.LogSimulation(SIM.Simulation.ApplicationStart);

            DontDestroyOnLoad(this);
            Instance = this;
        }