Example #1
0
        //TODO: Refactor this behind its own object to provide download URL for character.
        /// <inheritdoc />
        public async Task OnGameInitialized()
        {
            if (Logger.IsInfoEnabled)
            {
                Logger.Info("About to start downloading map data.");
            }

            //When we start the loading screen for the game
            //To know what world we should load we should
            CharacterSessionDataResponse characterSessionData = await CharacterService.GetCharacterSessionData(LocalCharacterData.CharacterId, AuthTokenRepo.RetrieveWithType())
                                                                .ConfigureAwait(false);

            if (!characterSessionData.isSuccessful)
            {
                Logger.Error($"Failed to query Character Session Data: {characterSessionData.ResultCode}:{(int)characterSessionData.ResultCode}");
                return;
            }

            //TODO: Handle failure
            ProjectVersionStage.AssertAlpha();
            //TODO: Handle throwing/error
            //We need to know the world the zone is it, so we can request a download URL for it.
            long worldId = await ZoneDataService.GetZoneWorld(characterSessionData.ZoneId)
                           .ConfigureAwait(false);

            //With the worldid we can get the download URL.
            ContentDownloadURLResponse urlDownloadResponse = await ContentService.RequestWorldDownloadUrl(worldId, AuthTokenRepo.RetrieveWithType())
                                                             .ConfigureAwait(false);

            //TODO: Handle failure
            if (urlDownloadResponse.isSuccessful)
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info($"Download URL: {urlDownloadResponse.DownloadURL}");
                }

                //Can't do web request not on the main thread, sadly.
                await new UnityYieldAwaitable();

                //TODO: Do we need to be on the main unity3d thread
                UnityWebRequestAsyncOperation asyncOperation = UnityWebRequestAssetBundle.GetAssetBundle(urlDownloadResponse.DownloadURL, 0).SendWebRequest();

                //TODO: We should render these operations to the loading screen UI.
                asyncOperation.completed += operation =>
                {
                    AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(asyncOperation.webRequest);

                    string[] paths = bundle.GetAllScenePaths();

                    foreach (string p in paths)
                    {
                        Debug.Log($"Found Scene in Bundle: {p}");
                    }

                    AsyncOperation sceneAsync = SceneManager.LoadSceneAsync(System.IO.Path.GetFileNameWithoutExtension(paths.First()));

                    sceneAsync.completed += operation1 =>
                    {
                        //When the scene is finished loading we should cleanup the asset bundle
                        //Don't clean up the WHOLE BUNDLE, just the compressed downloaded data
                        bundle.Unload(false);

                        //TODO: We need a way/system to reference the bundle later so it can be cleaned up inbetween scene loads.
                    };

                    sceneAsync.allowSceneActivation = true;
                };
            }
        }
Example #2
0
        //TODO: We need to handle failure cases, maybe with a window popup and bringing back to the titlescreen.
        /// <inheritdoc />
        public async Task OnGameInitialized()
        {
            //When we reach this scene, the pre lobby burst scene
            //we need to actually connect to the zone/lobby.
            //it verry well could be a zone. Maybe we were in a party and are reconnecting to it
            //no matter what though, we need to get information about our
            //character session and then the zone it should be connecting to
            //then we can connect.

            //First we need to know what zone this session should be going to
            CharacterSessionDataResponse sessionData = await CharacterDataService.GetCharacterSessionData(CharacterDataRepo.CharacterId, AuthTokenRepo.RetrieveWithType())
                                                       .ConfigureAwait(true);

            //TODO: Handle this better
            if (!sessionData.isSuccessful)
            {
                Logger.Error($"Failed to query session data for Character: {CharacterDataRepo.CharacterId}. Cannot connect to instance server.");
                return;
            }

            ResolveServiceEndpointResponse zoneEndpointResponse = await ZoneService.GetServerEndpoint(sessionData.ZoneId);

            if (!zoneEndpointResponse.isSuccessful)
            {
                Logger.Error($"Failed to query endpoint for Zone: {sessionData.ZoneId} which Character: {CharacterDataRepo.CharacterId} is in. Cannot connect to instance server.");
                return;
            }

            //TODO: Don't hardcode gameserver connection details
            //As soon as we start we should attempt to connect to the login server.
            bool result = await Client.ConnectAsync(IPAddress.Parse(zoneEndpointResponse.Endpoint.EndpointAddress), zoneEndpointResponse.Endpoint.EndpointPort)
                          .ConfigureAwait(true);

            if (!result)
            {
                throw new InvalidOperationException($"Failed to connect to Server: {zoneEndpointResponse.Endpoint.EndpointAddress} Port: {zoneEndpointResponse.Endpoint.EndpointPort}");
            }

            if (Logger.IsDebugEnabled)
            {
                Logger.Debug($"Connected client. isConnected: {Client.isConnected}");
            }

            //Basically we just take the network client and tell the client manager to start dealing with it
            //since it's connecting the manager should start pumping the messages out of it.
            await NetworkClientManager.StartHandlingNetworkClient(Client)
            .ConfigureAwait(true);

            //We should broadcast that the connection has been established to any interested subscribers
            OnNetworkConnectionEstablished?.Invoke(this, EventArgs.Empty);
        }
Example #3
0
        /// <inheritdoc />
        protected override void OnEventFired(object source, CharacterButtonClickedEventArgs args)
        {
            UnityExtended.UnityMainThreadContext.PostAsync(async() =>
            {
                //We must actually create a session
                //before the character can login.
                CharacterSessionEnterResponse enterResponse = await CharacterService.TryEnterSession(args.CharacterGuid.EntityId, AuthTokenRepo.RetrieveWithType())
                                                              .ConfigureAwait(true);

                //TODO: handle character session failure
                CharacterData.UpdateCharacterId(args.CharacterGuid.EntityId);

                if (Logger.IsInfoEnabled)
                {
                    Logger.Info($"Recieved character session entry response: {enterResponse.isSuccessful} Result: {enterResponse.ResultCode} for ZoneId: {enterResponse.ZoneId}");
                }

                //TODO: Use the scene manager service.
                //TODO: Don't hardcode scene ids. Don't load scenes directly.
                SceneManager.LoadSceneAsync(2).allowSceneActivation = true;
            });
        }
Example #4
0
        private async Task OnFinishedDisconnecting(int zoneId)
        {
            //Now we need to try to create a new session
            while (true)
            {
                CharacterSessionEnterResponse enterResponse = await CharacterService.SetCharacterSessionData(DataRepo.CharacterId, zoneId, AuthTokenRepo.RetrieveWithType())
                                                              .ConfigureAwait(false);

                //While the response isn't successful we shouild log way, and continue if it's
                if (enterResponse.isSuccessful)
                {
                    break;
                }

                await Task.Delay(500)
                .ConfigureAwait(false);

                Debug.LogError($"Failed to set character session data to ZoneId: {zoneId} for Reason: {enterResponse.ResultCode}");
            }

            //Join main unity thread
            await new UnityYieldAwaitable();

            //We should dispose any resources in the scene that are disposable before loading a new scene
            foreach (var d in Disposables)
            {
                d.Dispose();
            }

            //We just load to the loading screen and we'll reload the into the current zone
            SceneManager.LoadSceneAsync((int)GameSceneType.WorldDownloadingScreen, LoadSceneMode.Single).allowSceneActivation = true;
        }
        /// <inheritdoc />
        public async Task <IPrefabContentResourceHandle> LoadAvatarPrefabAsync(long avatarId)
        {
            //If it's already available, we can just return immediately
            if (IsAvatarResourceAvailable(avatarId))
            {
                return(TryLoadAvatarPrefab(avatarId));
            }

            ContentDownloadURLResponse downloadUrlResponse = await ContentClient.RequestAvatarDownloadUrl(avatarId, AuthTokenRepo.RetrieveWithType())
                                                             .ConfigureAwait(false);

            //TODO: Handle failure
            TaskCompletionSource <IPrefabContentResourceHandle> completionSource = new TaskCompletionSource <IPrefabContentResourceHandle>();

            //Asset bundle requests can sadly only happen on the main thread, so we must join the main thread.
            await new UnityYieldAwaitable();

            //TODO: We should handle caching, versioning and etc here.
            UnityWebRequestAsyncOperation asyncOperation = UnityWebRequestAssetBundle.GetAssetBundle(downloadUrlResponse.DownloadURL, 0).SendWebRequest();

            //TODO: We should render these operations to the loading screen UI.
            asyncOperation.completed += operation =>
            {
                //When we first get back on the main thread, the main concern
                //is that this resource manager may be from the last scene
                //and that the client may have moved on
                //to avoid this issues we check disposal state
                //and do nothing, otherwise if we check AFTER then we just have to release the assetbundle immediately anyway.
                if (isDisposed)
                {
                    //Just tell anyone awaiting this that it is canceled. They should handle that case, not us.
                    completionSource.SetCanceled();
                    return;
                }


                //GetContent will throw if the assetbundle has already been loaded.
                //So to prevent this from occuring due to multiple requests for the
                //content async we will check, on this main thread, via a write lock.
                lock (SyncObj)
                {
                    //We're on the main thread again. So, we should check if another
                    //request already got the bundle
                    if (IsAvatarResourceAvailable(avatarId))
                    {
                        completionSource.SetResult(TryLoadAvatarPrefab(avatarId));
                        return;
                    }

                    //otherwise, we still don't have it so we should initialize it.
                    this.ResourceHandleCache[avatarId] = new ReferenceCountedPrefabContentResourceHandle(DownloadHandlerAssetBundle.GetContent(asyncOperation.webRequest));
                    completionSource.SetResult(TryLoadAvatarPrefab(avatarId));                     //we assume this will work now.
                }
            };

            return(await completionSource.Task
                   .ConfigureAwait(false));
        }