Example #1
0
        public void Run()
        {
            var message = new DateTimeSyncMessage
            {
                PlayerId = GetPlayerId(GetLocalPlayer()),
                Seconds  = WcDateTime.LocalTime.TotalSeconds
            };

            SyncSystem.Subscribe <DateTimeSyncMessage>(HandleDateTimeSyncMessage);
            SyncSystem.Send(message);
        }
Example #2
0
        /// <summary>
        /// Loads a save for the given player, using the given slot.
        /// <para>If no save exists on the given slot, will load an empty save.</para>
        /// <para>Use <see cref="HandleSaveLoadedMessage"/> to receive the save.</para>
        /// </summary>
        /// <param name="player">The player to create the save for.</param>
        /// <param name="saveSlot">The slot to save to.</param>
        public void Load(player player, int saveSlot = 1)
        {
            // This code must only execute for the local player
            if (GetLocalPlayer() != player)
            {
                return;
            }

            var filename = GetFileName(player, saveSlot);

            Preloader(filename);
            var sb = new StringBuilder();

            for (var i = 0; i < abilityIds.Count; i++)
            {
                var abilityId       = abilityIds[i];
                var originalTooltip = originalTooltips[i];

                var packet = BlzGetAbilityTooltip(abilityId, 0);
                if (packet == originalTooltip)
                {
                    if (i * 2 > abilityIds.Count)
                    {
                        Console.WriteLine("WARNING: More than 50% of the save file storage space is in use. Please contact the mapmaker to increase storage space.");
                    }
                    break;
                }
                else
                {
                    BlzSetAbilityTooltip(abilityId, originalTooltip, 0);
                    sb.Append(packet);
                }
            }

            var save    = TryLoadSave(sb);
            var message = new SaveLoadedMessage <T>
            {
                PlayerId = GetPlayerId(player),
                SaveSlot = saveSlot
            };

            if (save?.SaveData != null && save.HashCode == GetSaveHash(JsonConvert.Serialize(save.SaveData), player))
            {
                message.SaveData = save.SaveData;
            }

            SyncSystem.Send(message);
        }
Example #3
0
        private void HandleDateTimeSyncMessage(DateTimeSyncMessage message)
        {
            this.timestamps[message.PlayerId] = new WcDateTime(message.Seconds);

            if (Util.EnumeratePlayers(PLAYER_SLOT_STATE_PLAYING, MAP_CONTROL_USER).All(x => this.timestamps.ContainsKey(GetPlayerId(x))))
            {
                var sync = this.method switch
                {
                    DateTimeSyncMethod.Earliest => ResolveEarliest(),
                    DateTimeSyncMethod.Latest => ResolveLatest(),
                    DateTimeSyncMethod.Average => ResolveAverage(),
                    _ => ResolveBestFit(),
                };

                SyncSystem.Unsubscribe <DateTimeSyncMessage>(HandleDateTimeSyncMessage);
                WcDateTime.StoreSynchronisedTime(sync.TotalSeconds, this.method);

                this.action?.Invoke(sync);
            }
        }
Example #4
0
        /// <summary>
        /// Creates a new <see cref="SaveSystem{T}"/> instance with the given <paramref name="options"/>.
        /// </summary>
        public SaveSystem(SaveSystemOptions options)
        {
            if (originalTooltips == null)
            {
                originalTooltips = new List <string>();

                for (var i = 0; i < abilityIds.Count; i++)
                {
                    var tooltip = BlzGetAbilityTooltip(abilityIds[i], 0);
                    if (tooltip != "Tool tip missing!")
                    {
                        originalTooltips.Add(tooltip);
                    }
                    else
                    {
                        throw new ArgumentException($"ERROR: Tooltip {abilityIds[i]} cannot be modified for the SaveLoad system. See the WCSharp wiki for more info on Save/Load storage space.");
                    }
                }
            }

            this.saveFolder            = options.SaveFolder;
            this.hash1                 = options.Hash1;
            this.hash2                 = options.Hash2;
            this.salt                  = options.Salt;
            this.bindSavesToPlayerName = options.BindSavesToPlayerName;
            this.suffix                = options.Suffix ?? "";
            this.base64                = options.Base64Provider ?? new Base64();

            if (string.IsNullOrWhiteSpace(this.saveFolder))
            {
                throw new ArgumentException("ERROR: Must define a non-empty save folder for the SaveSystem.");
            }
            if (string.IsNullOrWhiteSpace(this.salt))
            {
                throw new ArgumentException("ERROR: Must define a non-empty save folder for the SaveSystem.");
            }
            if (this.hash1 <= 0)
            {
                throw new ArgumentException("ERROR: Must define a positive non-zero hash1 for the SaveSystem.");
            }
            if (this.hash2 <= 0)
            {
                throw new ArgumentException("ERROR: Must define a positive non-zero hash2 for the SaveSystem.");
            }

            var illegalCharacters = new char[] { '<', '>', ':', '"', '/', '\\', '|', '?', '*' };

            for (var i = 0; i < illegalCharacters.Length; i++)
            {
                var ch = illegalCharacters[i];
                if (this.saveFolder.Contains(ch))
                {
                    throw new ArgumentException($"ERROR: SaveFolder cannot contain {ch} as this is an illegal filename character.");
                }
                if (this.suffix.Contains(ch))
                {
                    throw new ArgumentException($"ERROR: Suffix cannot contain {ch} as this is an illegal filename character.");
                }
            }

            if (this.suffix != "" && !this.suffix.StartsWith("-"))
            {
                this.suffix = "-" + this.suffix;
            }

            SyncSystem.Subscribe <SaveLoadedMessage <T> >(HandleSaveLoadedMessage);
        }
Example #5
0
 /// <inheritdoc/>
 public void Dispose()
 {
     SyncSystem.Unsubscribe <SaveLoadedMessage <T> >(HandleSaveLoadedMessage);
 }