public void Load(byte[] bytes, Action done) { if (m_isBusy) { throw new InvalidOperationException("m_isBusy"); } m_isBusy = true; m_progressIndicator.IsVisible = true; m_progressIndicator.SetText("LOADING..."); for (int i = 0; i < m_mapCameras.Count; ++i) { m_mapCameras[i].IsOn = false; } m_job.Submit( () => { try { ProtobufSerializer serializer = null; try { var pool = Dependencies.Serializer; if (pool != null) { serializer = pool.Acquire(); m_map = serializer.Deserialize <MapRoot>(bytes); } } finally { if (serializer != null) { var pool = Dependencies.Serializer; if (pool != null) { pool.Release(serializer); } } } CalculateBounds(); //how to make sure than no one accessing cameras during background thread job ? for (int i = 0; i < m_mapCameras.Count; ++i) { m_mapCameras[i].Map = m_map; } } catch (Exception e) { return(e); } return(null); }, result => { if (result is Exception) { Debug.LogError(result.ToString()); } m_isBusy = false; m_progressIndicator.IsVisible = false; if (Loaded != null) { Loaded(this, EventArgs.Empty); } if (done != null) { done(); } for (int i = 0; i < m_mapCameras.Count; ++i) { m_mapCameras[i].IsOn = m_isOn; } }); }
public MatchServerClient(ITimeService time, string url, Guid roomId) { m_serializer = new ProtobufSerializer(); m_time = time; m_url = string.Format("{0}?roomId={1}&identity={2}&cmd=", url, roomId, ServerContainer.ServerIdentity); }
private void DownloadMapDataById(Guid mapId, ServerEventHandler <MapData> callback) { MapData mapData = null; Error error = new Error(); string dataPath = m_persistentDataPath + "/Maps/"; string filePath = dataPath + mapId + ".data"; if (!File.Exists(filePath)) { error.Code = StatusCode.NotFound; if (m_lag == 0) { callback(error, mapData); } else { m_job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error, mapData)); } } else { m_job.Submit(() => { error.Code = StatusCode.OK; ProtobufSerializer serializer = null; try { var pool = Dependencies.Serializer; if (pool != null) { serializer = pool.Acquire(); byte[] mapDataBytes = File.ReadAllBytes(filePath); mapData = serializer.Deserialize <MapData>(mapDataBytes); } } catch (Exception e) { error.Code = StatusCode.UnhandledException; error.Message = e.Message; } finally { if (serializer != null) { var pool = Dependencies.Serializer; if (pool != null) { pool.Release(serializer); } } } return(null); }, result => { callback(error, mapData); }); } }
private void InitEngine(Guid clientId, ServerEventHandler callback) { if (m_engine != null) { MatchFactory.DestroyMatchEngine(m_engine); } m_engine = null; DownloadMapData(m_room.MapInfo.Id, (error, mapData) => { if (HasError(error)) { if (callback != null) { callback(error); } } else { m_job.Submit(() => { ProtobufSerializer serializer = null; MapRoot mapRoot = null; try { var pool = Dependencies.Serializer; if (pool != null) { serializer = pool.Acquire(); mapRoot = serializer.Deserialize <MapRoot>(mapData.Bytes); } } finally { if (serializer != null) { var pool = Dependencies.Serializer; if (pool != null) { pool.Release(serializer); } } } return(mapRoot); }, result => { MapRoot mapRoot = (MapRoot)result; IMatchEngine engine = MatchFactory.CreateMatchEngine(mapRoot, m_room.Players.Count); Dictionary <int, VoxelAbilities>[] allAbilities = new Dictionary <int, VoxelAbilities> [m_room.Players.Count]; for (int i = 0; i < m_room.Players.Count; ++i) { allAbilities[i] = m_abilities[m_room.Players[i]].ToDictionary(a => a.Type); } if (m_replay == null) { m_replay = MatchFactory.CreateReplayRecorder(); } //Zero is neutral for (int i = 0; i < m_room.Players.Count; ++i) { Guid playerGuid = m_room.Players[i]; engine.RegisterPlayer(m_room.Players[i], i, allAbilities); } engine.CompletePlayerRegistration(); m_prevTickTime = Time.realtimeSinceStartup; m_engine = engine; if (callback != null) { callback(error); } m_pingTimer.Ping(clientId); if (Ping != null) { Ping(new Error(StatusCode.OK), new RTTInfo()); } }); } }); }
public MatchServerImpl(ITimeService timeService, string persistentDataPath, Guid creatorClientId, Room room, Guid[] clientIds, Player[] players, ReplayData replay) { m_serializer = new ProtobufSerializer(); m_time = timeService; m_persistentDataPath = persistentDataPath; m_room = room; m_room.CreatorClientId = creatorClientId; m_registeredClients = new HashSet <Guid>(); m_readyToPlayClients = new HashSet <Guid>(); m_readyToBeUnregisteredClients = new List <DisconnectedClient>(); m_clientIdToPlayers = new Dictionary <Guid, Dictionary <Guid, Player> >(); m_playerToClientId = new Dictionary <Guid, Guid>(); m_clientIds = clientIds.ToList(); for (int i = 0; i < m_clientIds.Count; ++i) { Guid clientId = m_clientIds[i]; if (clientId != Guid.Empty) { Dictionary <Guid, Player> idToPlayer; if (!m_clientIdToPlayers.TryGetValue(clientId, out idToPlayer)) { idToPlayer = new Dictionary <Guid, Player>(); m_clientIdToPlayers.Add(clientId, idToPlayer); } Player player = players[i]; idToPlayer.Add(player.Id, player); m_playerToClientId.Add(player.Id, clientId); } } m_players = players.ToDictionary(p => p.Id); //Adding neutral player to room m_neutralPlayer = new Player(); m_neutralPlayer.BotType = BotType.Neutral; m_neutralPlayer.Name = "Neutral"; m_neutralPlayer.Id = Guid.NewGuid(); //Dictionary<Guid, Player> idToPlayer = new Dictionary<Guid, Player>(); //idToPlayer.Add(m_neutralPlayer.Id, m_neutralPlayer); m_players.Add(m_neutralPlayer.Id, m_neutralPlayer); if (!m_room.Players.Contains(m_neutralPlayer.Id)) { m_room.Players.Insert(0, m_neutralPlayer.Id); m_clientIds.Insert(0, Guid.Empty); } m_abilities = new Dictionary <Guid, VoxelAbilities[]>(); m_taskTemplates = new Dictionary <Guid, SerializedTask[]>(); m_taskTemplatesInfo = new Dictionary <Guid, SerializedNamedTaskLaunchInfo[]>(); for (int i = 0; i < m_room.Players.Count; ++i) { m_abilities.Add(m_room.Players[i], CreateDefaultAbilities()); m_taskTemplates.Add(m_room.Players[i], CreateDefaultTaskTemplates()); m_taskTemplatesInfo.Add(m_room.Players[i], CreateDefaultTaskTemplateInfo()); } m_pingTimer = new PingTimer(m_clientIdToPlayers.Keys.ToArray(), 3); if (replay != null) { m_replay = MatchFactory.CreateReplayPlayer(); m_replay.Load(replay); } }