Example #1
0
        private void OnSceneChanged(SceneType sceneType, Scene scene)
        {
            Interface.Oxide.LogDebug($"Scene changed to {scene.name} ({scene.buildIndex}).");

            if (sceneType == SceneType.Game)
            {
                RemotePlayer.CreatePlayerPrefab();

                localPlayer        = GameObject.Find("Player") ?? throw new NotImplementedException("Could not find local player");
                localPlayerControl = localPlayer.GetComponent <PlayerControl>() ?? throw new NotImplementedException("Could not find PlayerControl on local player");
                localPoseControl   = localPlayer.transform.Find("dude/mixamorig:Hips").GetComponent <PoseControl>() ?? throw new NotImplementedException("Could not find PoseControl on local player");
                localPlayerBase    = localPlayer.AddComponent <LocalPlayer>();

                InitSpectator();
                InitUI();
                InitClient();
            }
            else if (sceneType == SceneType.Menu)
            {
                InitMenuUI();

                if (firstLaunch)
                {
                    firstLaunch = false;

                    // Don't connect to server automatically if an update is available or if this is the first ever launch.
                    if (firstEverLaunch)
                    {
                        menuUi.ShowFirstLaunch();
                        return;
                    }

                    if (updateAvailable)
                    {
                        menuUi.ShowUpdateAvailable();
                        return;
                    }
                }
                else
                {
                    return;
                }

                var launchArguments = Environment.GetCommandLineArgs().Skip(1).ToArray();

                for (int i = 0; i < launchArguments.Length; i += 2)
                {
                    string argument = launchArguments[i].ToLower();
                    string value    = i < launchArguments.Length - 1 ? launchArguments[i + 1].ToLower() : null;

                    if (argument == "--goimp-connect")
                    {
                        string[] ipPort     = value.Split(':');
                        string   ipString   = ipPort[0];
                        string   portString = ipPort[1];

                        IPAddress ipAddress;
                        short     port;

                        if (!IPAddress.TryParse(ipString, out ipAddress))
                        {
                            Interface.Oxide.LogError($"Launch arguments contained invalid ip: {ipString}");
                            return;
                        }

                        if (!short.TryParse(portString, out port))
                        {
                            Interface.Oxide.LogError($"Launch arguments contained invalid port: {portString}");
                            return;
                        }

                        launchEndPoint = new IPEndPoint(ipAddress, port);

                        // Wait for game to finish loading then continue game.
                        var loader = GameObject.FindObjectOfType <Loader>();
                        var loadingFinishedField = typeof(Loader).GetField("loadFinished", BindingFlags.Instance | BindingFlags.NonPublic);

                        Timer.TimerInstance timerInstance = null;
                        Action timerCallback = () =>
                        {
                            bool loadingFinished = (bool)loadingFinishedField.GetValue(loader);

                            if (loadingFinished)
                            {
                                loader.ContinueGame();
                                timerInstance.Destroy();
                            }
                        };

                        timerInstance = Timer.Repeat(0, -1, timerCallback, this);
                    }
                }
            }

            if (sceneType != SceneType.Game)
            {
                DestroyClient();
                DestroyUI();
                DestroySpectator();

                if (ListenServer.Running)
                {
                    ListenServer.Stop();
                }
            }
            else if (sceneType != SceneType.Menu)
            {
                DestroyMenuUI();
            }
        }