예제 #1
0
        /// <summary>
        /// Tests if the <see cref="GameMessageCollection"/> for a certain language exists and can compile
        /// successfully without error.
        /// </summary>
        /// <param name="language">The language to try to compile.</param>
        /// <param name="errors">When this method returns false, contains the compilation errors.</param>
        /// <returns>
        /// True if the <paramref name="language"/>'s <see cref="GameMessageCollection"/> compiled
        /// successfully; otherwise false.
        /// </returns>
        public static bool TestCompilation(string language, out IEnumerable <CompilerError> errors)
        {
            var coll = new GameMessageCollection(language, true);

            errors = coll.CompilationErrors;
            return(coll.CompilationErrors.IsEmpty());
        }
예제 #2
0
        /// <summary>
        /// Loads the raw <see cref="GameMessage"/>s for a language.
        /// </summary>
        /// <param name="language">The language to load the messages for.</param>
        /// <returns>The raw <see cref="GameMessage"/>s for the <paramref name="language"/>.</returns>
        public static IEnumerable <KeyValuePair <GameMessage, string> > LoadRawMessages(string language)
        {
            var coll     = new GameMessageCollection(language, true);
            var messages = coll.ToImmutable();

            return(messages);
        }
예제 #3
0
        /// <summary>
        /// Reads a GameMessage from the BitStream.
        /// </summary>
        /// <param name="bitStream">BitStream to read from.</param>
        /// <param name="gameMessages">Collection of GameMessages to use to grab the message.</param>
        /// <returns>String of the parsed GameMessage read from the BitStream.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="gameMessages" /> is <c>null</c>.</exception>
        public static string ReadGameMessage(this BitStream bitStream, GameMessageCollection gameMessages)
        {
            if (gameMessages == null)
            {
                throw new ArgumentNullException("gameMessages");
            }

            var messageID  = bitStream.ReadUInt(_gameMessageIDBits);
            var paramCount = bitStream.ReadByte();

            // Parse the parameters
            string[] parameters = null;
            if (paramCount > 0)
            {
                parameters = new string[paramCount];
                for (var i = 0; i < paramCount; i++)
                {
                    parameters[i] = bitStream.ReadString(GameData.MaxServerMessageParameterLength);
                }
            }

            // Parse the message and return it
            var gameMessage = (GameMessage)messageID;
            var message     = gameMessages.GetMessage(gameMessage, parameters);

            return(message);
        }
예제 #4
0
        /// <summary>
        /// Attempts to change the <see cref="CurrentLanguage"/> to a new language.
        /// </summary>
        /// <param name="newLanguage">The name of the language to change to.</param>
        /// <returns>True if the language was successfully changed; false if the language was already set to the <paramref name="newLanguage"/>
        /// or the <paramref name="newLanguage"/> does not exist or is invalid.</returns>
        public static bool TryChangeCurrentLanguage(string newLanguage)
        {
            if (string.IsNullOrEmpty(newLanguage))
            {
                Debug.Fail("Invalid newLanguage value.");
                return(false);
            }

            GameMessageCollection newLanguageCollection;

            // Try to create the GameMessageCollection for the new language
            try
            {
                newLanguageCollection = Create(newLanguage);
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to change language to `{0}`. Exception: {1}";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, newLanguage, ex);
                }
                Debug.Fail(string.Format(errmsg, newLanguage, ex));
                return(false);
            }

            // Change to the new language
            GameMessageCollection oldLanguage;

            lock (_currentLanguageSync)
            {
                // Check if we are just changing to the same language
                if (_currentLanguage == newLanguageCollection)
                {
                    return(false);
                }

                oldLanguage      = _currentLanguage;
                _currentLanguage = newLanguageCollection;
            }

            // Raise the event
            if (CurrentLanguageChanged != null)
            {
                CurrentLanguageChanged.Raise(null, ValueChangedEventArgs.Create(oldLanguage, _currentLanguage));
            }

            return(true);
        }
예제 #5
0
        /// <summary>
        /// Gets the <see cref="GameMessageCollection"/> for the specified language.
        /// </summary>
        /// <param name="language">The game message language.</param>
        /// <returns>The <see cref="GameMessageCollection"/> for the specified language.</returns>
        public static GameMessageCollection Create(string language = _defaultLanguageName)
        {
            GameMessageCollection instance;

            lock (_instancesSync)
            {
                if (!_instances.TryGetValue(language, out instance))
                {
                    instance = new GameMessageCollection(language, false);
                    _instances.Add(language, instance);
                }
            }

            return(instance);
        }
 /// <summary>
 /// Gets a message from a <see cref="GameMessage"/> packed into a string using <see cref="GameMessageHelper.AsString"/>.
 /// </summary>
 /// <param name="coll">The <see cref="GameMessageCollection"/>.</param>
 /// <param name="s">The <see cref="GameMessage"/> and arguments packed into a string.</param>
 /// <returns>The parsed <see cref="GameMessage"/>, or null if the parsing failed.</returns>
 public static string TryGetMessageFromString(this GameMessageCollection coll, string s)
 {
     try
     {
         var kvp = GameMessageHelper.FromString(s);
         return(coll.GetMessage(kvp.Key, kvp.Value));
     }
     catch (ArgumentException)
     {
         return(null);
     }
     catch (FormatException)
     {
         return(null);
     }
 }
        /// <summary>
        /// Gets a message from a <see cref="GameMessage"/> packed into a string using <see cref="GameMessageHelper.AsString"/>.
        /// </summary>
        /// <param name="coll">The <see cref="GameMessageCollection"/>.</param>
        /// <param name="s">The <see cref="GameMessage"/> and arguments packed into a string.</param>
        /// <returns>The parsed <see cref="GameMessage"/>.</returns>
        public static string GetMessageFromString(this GameMessageCollection coll, string s)
        {
            var kvp = GameMessageHelper.FromString(s);

            return(coll.GetMessage(kvp.Key, kvp.Value));
        }
예제 #8
0
 /// <summary>
 /// Initializes the <see cref="GameMessageCollection"/> class.
 /// </summary>
 static GameMessageCollection()
 {
     _instances       = new Dictionary <string, GameMessageCollection>(StringComparer.OrdinalIgnoreCase);
     _defaultMessages = Create();
     _currentLanguage = _defaultMessages;
 }