/// <summary> /// Creates a loader that loads one of the <paramref name="storedPlayers"/> that corresponds to the data in /// <paramref name="newInfo"/>. /// </summary> /// <param name="level">The level to load the player into.</param> /// <param name="storedPlayers">The stored data of all players.</param> /// <param name="insigniaGetter">The source of graphical representations of the players.</param> /// <param name="newInfo">Info of the player to load.</param> public Loader(LevelManager level, IList <StPlayer> storedPlayers, InsigniaGetter insigniaGetter, PlayerInfo newInfo) { this.level = level; this.storedPlayer = (from stPlayer in storedPlayers where stPlayer.InsigniaID == newInfo.Insignia.Index select stPlayer).FirstOrDefault(); if (storedPlayer == null) { throw new ArgumentException("StoredPlayers did not contain player entry for player with provided playerInfo", nameof(storedPlayers)); } this.type = newInfo.PlayerType; this.teamID = newInfo.TeamID; this.insignia = insigniaGetter.MarkUsed(newInfo.Insignia); //Clear the stored player plugin data storedPlayer.UserPlugin = new PluginData(); }
/// <summary> /// Creates a loader that loads the given <paramref name="storedPlayer"/> into the <paramref name="level"/>. /// </summary> /// <param name="level">The level to load the player into.</param> /// <param name="storedPlayer">Stored data of the player.</param> /// <param name="insigniaGetter">The source of graphical representations of the players.</param> /// <param name="loadPlaceholder">If the player should be loaded as a placeholder player, or if it should be loaded with it's /// actual type and plugin.</param> public Loader(LevelManager level, StPlayer storedPlayer, InsigniaGetter insigniaGetter, bool loadPlaceholder) { this.level = level; this.storedPlayer = storedPlayer; this.insignia = insigniaGetter.GetUnusedInsignia(storedPlayer.InsigniaID); if (loadPlaceholder) { this.type = PlayerType.Placeholder; teamID = 0; } else { if (storedPlayer.TypeID == 0) { throw new ArgumentException("StoredPlayer had no type", nameof(storedPlayer)); } type = level.Package.GetPlayerType(storedPlayer.TypeID); teamID = storedPlayer.TeamID; } }
/// <summary> /// Stores the players state in an instance of <see cref="StPlayer"/> for serialization. /// </summary> /// <param name="player">The player to store.</param> /// <returns>Player data stored in an instance of <see cref="StPlayer"/>.</returns> public static StPlayer Save(Player player) { var storedPlayer = new StPlayer { Id = player.ID, TeamID = player.TeamID, TypeID = player.PlayerType?.ID ?? 0, InsigniaID = player.Insignia.Index }; storedPlayer.UnitIDs.AddRange(from unitType in player.units from unit in unitType.Value select unit.ID); storedPlayer.BuildingIDs.AddRange(from buildingType in player.buildings from building in buildingType.Value select building.ID); storedPlayer.Resources.AddRange(from resourceType in player.resources select new StResource { Id = resourceType.Key.ID, Amount = resourceType.Value }); storedPlayer.UserPlugin = new PluginData(); try { player.Plugin?.SaveState(new PluginDataWrapper(storedPlayer.UserPlugin, player.Level)); } catch (Exception e) { string message = $"Saving player plugin failed with Exception: {e.Message}"; Urho.IO.Log.Write(LogLevel.Error, message); throw new SavingException(message, e); } return(storedPlayer); }
/// <summary> /// Loads placeholder player with ownership of units and buildings. /// </summary> /// <param name="level">The level the placeholder player is loading into.</param> /// <param name="storedPlayer">The stored data for this placeholder player.</param> /// <param name="insigniaGetter">Provider of graphical identifications for players.</param> /// <returns>Loader that loads the placeholder player from the data in the <paramref name="storedPlayer"/>.</returns> public static IPlayerLoader GetLoaderToPlaceholder(LevelManager level, StPlayer storedPlayer, InsigniaGetter insigniaGetter) { return(new Loader(level, storedPlayer, insigniaGetter, true)); }