Official Fulqrum Publishing forum

Official Fulqrum Publishing forum (http://forum.fulqrumpublishing.com/index.php)
-   FMB, Mission & Campaign builder Discussions (http://forum.fulqrumpublishing.com/forumdisplay.php?f=203)
-   -   How add user dll (http://forum.fulqrumpublishing.com/showthread.php?t=32284)

FG28_Kodiak 07-06-2012 09:07 AM

Ok, made some changes:

The dll:
Code:

using System;
using System.Collections.Generic;
using System.Xml;
using maddox.game;
using maddox.game.world;

namespace smpmessage
{

    public sealed class Translations
    {
        private static readonly Lazy<Translations> Lazy = new Lazy<Translations>(() => new Translations());

        private static readonly Dictionary<string, Dictionary<string, Dictionary<string, string>>> _stringPool =
            new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();

        private const string XmlLanguagenode = "SMP/LanguageSection";
        private const string DefaultLanguage = "en";


        public static Translations GetInstance
        {
            get { return Lazy.Value; }
        }


        private Translations()
        {
        }


        public static Translations UseConfigFile(string fileName)
        {
            if (_stringPool.Count == 0)
                LoadSettings(fileName);
            return Lazy.Value;
        }


        private static void LoadSettings(string languageFile)
        {
            XmlDocument xDoc = LoadXMLFile(languageFile);

            var xmlNodeList = xDoc.SelectNodes(XmlLanguagenode);

            if (xmlNodeList != null)
                foreach (XmlNode sectionNode in xmlNodeList)
                {

                    var strs = new Dictionary<string, Dictionary<string, string>>();

                    foreach (XmlNode textNode in sectionNode.ChildNodes)
                    {

                        var texts = new Dictionary<string, string>();

                        foreach (XmlNode txt in textNode.ChildNodes)
                        {
                            texts.Add(txt.Name, txt.InnerText);
                        }

                        if (textNode.Attributes != null) strs.Add(textNode.Attributes["key"].Value, texts);
                    }

                    if (sectionNode.Attributes != null) _stringPool.Add(sectionNode.Attributes["name"].Value, strs);
                }

        }


        private static XmlDocument LoadXMLFile(string file)
        {
            // Load the xml-file in a xml-document
            XmlDocument xDoc = new XmlDocument();

            try
            {
                xDoc.Load(file);
            }
            catch (System.IO.FileNotFoundException)
            {
                throw new Exception("Xml-File " + file + " not found");
            }
            catch (Exception ex)
            {
                throw new Exception("Error loading " + file + ": " + ex.Message);
            }

            return xDoc;
        }


        public string GetString(string section, string key, string language)
        {
            if (_stringPool.Count > 0)
            {
                Dictionary<string, Dictionary<string, string>> strs;

                if (_stringPool.TryGetValue(section, out strs))
                {
                    Dictionary<string, string> texts;

                    if (strs.TryGetValue(key, out texts))
                    {
                        string text;
                        if (texts.TryGetValue(language, out text))
                            return text;
                        if (texts.TryGetValue(DefaultLanguage, out text))
                            return text;
                    }
                }
            }
            return key; // if nothing is found return a empty string -> modify if a error message is needed.
        }


        public string GetString(string section, string key)
        {
            return GetString(section, key, DefaultLanguage);
        }

    }


    public static class SendMessage
    {
        private static readonly IGamePlay GamePlay = Strategy.THIS.GamePlay;
        private static Translations Translator = Translations.GetInstance; //Reference to the Singleton Class
        public static string setupServerLanguage = "en";
        public static string hudON = "on";


        public static void ToAll(string message, string target, params object[] args)
        {
            Send(GetPlayerList(1), message, target, args);
            Send(GetPlayerList(2), message, target, args);
        }


        public static void ToArmy(string message, int army, string target, params object[] args)
        {
            Send(GetPlayerList(army), message, target, args);
        }


        public static void ToPlayers(string message, Player[] players, string target, params object[] args)
        {
            Send(players, message, target, args);
        }


        private static void Send(Player[] playerList, string message, string target, params object[] args)
        {
            List<Player> serverList = new List<Player>();
            if (GamePlay.gpPlayer() != null)
            {
                serverList.Add(GamePlay.gpPlayer());
            }

            if (serverList.Count > 0)
            {
                if (target == "Screen" && hudON == "on")
                {
                    GamePlay.gpLogServer(serverList.ToArray(), "[HUD]:" + message, args);
                }
                else
                {
                    GamePlay.gpLogServer(serverList.ToArray(), message, args);
                }
            }
            if (playerList.Length > 0)
            {
                switch (target)
                {
                    case "ChatAndScreen":
                        {
                            GamePlay.gpLogServer(playerList, message, args);
                            GamePlay.gpHUDLogCenter(playerList, message, args);
                        }
                        break;
                    case "Chat":
                        {
                            GamePlay.gpLogServer(playerList, message, args);
                            break;
                        }
                    case "Screen":
                        GamePlay.gpHUDLogCenter(playerList, message, args);
                        break;
                }
            }

        }


        public static Player[] GetPlayerList(int army)
        {
            List<Player> playerList = new List<Player>();
            List<Player> playerArmyList = new List<Player>();

            if (GamePlay.gpPlayer() != null)
                playerList.Add(GamePlay.gpPlayer());
            if (GamePlay.gpRemotePlayers() != null)
                playerList.AddRange(GamePlay.gpRemotePlayers());

            if (playerList != null)
            {
                foreach (Player player in playerList)
                {
                    if (player.Army() == army && player != null)
                        playerArmyList.Add(player);
                }
            }
            return playerArmyList.ToArray();
        }


        public static void ToAll(string section, string key, string target, params object[] args)
        {
            Send(SendMessage.GetPlayerList(1), section, key, target, args);
            Send(SendMessage.GetPlayerList(2), section, key, target, args);
            SendMessage.GetPlayerList(1);
        }


        public static void ToArmy(string section, string key, int army, string target, params object[] args)
        {
            Send(SendMessage.GetPlayerList(army), section, key, target, args);
        }


        public static void ToPlayers(string section, string key, Player[] players, string target, params object[] args)
        {
            Send(players, section, key, target, args);
        }



        private static void Send(Player[] playerList, string section, string key, string target, params object[] args)
        {
            Dictionary<string, List<Player>> playerLanguageDict = new Dictionary<string, List<Player>>();
            List<Player> serverList = new List<Player>();
            string serverLanguageName = "en";


            if (GamePlay.gpPlayer() != null)
            {
                serverList.Add(GamePlay.gpPlayer());
            }

            foreach (Player player in playerList)
            {
                string languageName = player.LanguageName();
                if (!playerLanguageDict.ContainsKey(languageName))
                {
                    playerLanguageDict.Add(languageName, new List<Player>());
                }
                playerLanguageDict[languageName].Add(player);
            }

            if (serverList.Count > 0)
            {
                foreach (Player serverPlayer in serverList)
                {
                    serverLanguageName = serverPlayer.LanguageName();
                    if (setupServerLanguage != "off")
                    {
                        serverLanguageName = setupServerLanguage;
                    }
                }
                if (target == "Screen" && hudON == "on")
                {
                    GamePlay.gpLogServer(serverList.ToArray(),
                                        "[HUD]:" + Translator.GetString(section, key, serverLanguageName), args);
                }
                else
                {
                    GamePlay.gpLogServer(serverList.ToArray(), Translator.GetString(section, key, serverLanguageName),
                                        args);
                }
            }

            if (playerList.Length > 0)
            {
                foreach (KeyValuePair<string, List<Player>> kvp in playerLanguageDict)
                {
                    switch (target)
                    {
                        case "ChatAndScreen":
                            {
                                GamePlay.gpLogServer(kvp.Value.ToArray(),
                                                    Translator.GetString(section, key, kvp.Key), args);
                                GamePlay.gpHUDLogCenter(kvp.Value.ToArray(),
                                                        Translator.GetString(section, key, kvp.Key), args);
                            }
                            break;
                        case "Chat":
                            GamePlay.gpLogServer(kvp.Value.ToArray(),
                                                Translator.GetString(section, key, kvp.Key), args);
                            break;
                        case "Screen":
                            GamePlay.gpHUDLogCenter(kvp.Value.ToArray(),
                                                    Translator.GetString(section, key, kvp.Key), args);
                            break;
                    }
                }
            }
        }
    }

}

Test cs (Mission file):
Code:

//$reference smpmessage.dll
using System;
using maddox.game;
using maddox.game.world;
using smpmessage;

/* Available methods:
 * -- Translate and send message
 * public static void ToAll(string section, string key, string target, params object[] args)
 * public static void ToArmy(string section, string key, int army, string target, params object[] args)
 * public static void ToPlayers(string section, string key, Player[] players, string target, params object[] args)
 *
 * -- No translate, only send message
 * public static void ToAll(string message, string target, params object[] args)
 * public static void ToArmy(string message, int army, string target, params object[] args)
 * public static void ToPlayers(string message, Player[] players, string target, params object[] args)
 *
 * -- Only translate, no send message
 * public string GetString(string section, string key, string language)
 *
 * Available languages in game:  en  ru  de  fr  es  cs  it  pl
*/

public class Mission : AMission
{
    // Path to localization xml-file, if you want to use localization on your script
    private static string localizationFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\1C SoftClub\\il-2 sturmovik cliffs of dover\\missions\\Single\\LocalizationExample\\localization.xml";

    // Load Languagefile for use in Message
    private Translations Translator = Translations.UseConfigFile(localizationFilePath);
   


    public override void OnBattleStarted()
    {
        base.OnBattleStarted();
        MissionNumberListener = -1;
    }

    public override void OnPlaceEnter(Player player, AiActor actor, int placeIndex)
    {
        base.OnPlaceEnter(player, actor, placeIndex);


        string testmsg = "TestMessage: " + Translator.GetString("SendMessageSection", "GetTranslateText", "en");
        GamePlay.gpLogServer(null, testmsg, null);


        SendMessage.ToAll("SendMessageSection", "GetTranslateText", "en");


        SendMessage.ToAll("SendMessageSection", "SendToChatToALLtranslate", "Chat");
        SendMessage.ToPlayers("SendMessageSection", "SendToChatToPlayertranslate", new Player[] { player }, "Chat");
        SendMessage.ToArmy("SendMessageSection", "SendToChatToArmytranslate", 1, "Chat");

        SendMessage.ToAll("SendMessageSection", "SendToScreenToALLtranslate", "Screen");
        SendMessage.ToPlayers("SendMessageSection", "SendToScreenToPlayertranslate", new Player[] { player }, "Screen");
        SendMessage.ToArmy("SendMessageSection", "SendToScreenToArmytranslate", 1, "Screen");

        SendMessage.ToAll("SendMessageSection", "SendToChatAndScreenToALLtranslate", "ChatAndScreen");
        SendMessage.ToPlayers("SendMessageSection", "SendToChatAndScreenToPlayertranslate", new Player[] { player }, "ChatAndScreen");
        SendMessage.ToArmy("SendMessageSection", "SendToChatAndScreenToArmytranslate", 1, "ChatAndScreen");
       
        // No translate, only send message
        SendMessage.ToAll("Message to ALL - ENGLISH or ДРУГОЙ ЯЗЫК", "Chat");
        SendMessage.ToPlayers("Message to Players - ENGLISH or ДРУГОЙ ЯЗЫК", new Player[] { player }, "Chat");
        SendMessage.ToArmy("Message to Army - ENGLISH or ДРУГОЙ ЯЗЫК", 1, "Chat");

        SendMessage.ToAll("Message to ALL - ENGLISH or ДРУГОЙ ЯЗЫК", "Screen");
        SendMessage.ToPlayers("Message to Players - ENGLISH or ДРУГОЙ ЯЗЫК", new Player[] { player }, "Screen");
        SendMessage.ToArmy("Message to Army - ENGLISH or ДРУГОЙ ЯЗЫК", 1, "Screen");

        SendMessage.ToAll("Message to ALL - ENGLISH or ДРУГОЙ ЯЗЫК", "ChatAndScreen");
        SendMessage.ToPlayers("Message to Players - ENGLISH or ДРУГОЙ ЯЗЫК", new Player[] { player }, "ChatAndScreen");
        SendMessage.ToArmy("Message to Army - ENGLISH or ДРУГОЙ ЯЗЫК", 1, "ChatAndScreen");

        // Only translate, no send message
        //initTranslate.GetString("SendMessageSection", "GetTranslateText", "en");
        //initTranslate.GetString("SendMessageSection", "GetTranslateText", "ru");
        //initTranslate.GetString("SendMessageSection", "GetTranslateText", player.LanguageName());
        // Examples for using translated text:
        /*
        SendMessage.ToAll(initTranslate.GetString("SendMessageSection", "GetTranslateText", "en"), "Chat");
        SendMessage.ToAll(initTranslate.GetString("SendMessageSection", "GetTranslateText", player.LanguageName()), "Chat");
        SendMessage.ToAll(initTranslate.GetString("SendMessageSection", "GetTranslateText", "de"), "Chat");
        */
        // Using parameters in messages
        int number1 = 1000;
        int number2 = 500;
        SendMessage.ToAll("SendMessageSection", "============NO TRANSLATE LINE whith xml-file===========", "Chat");

        SendMessage.ToAll("Parameters", "useParametersLabel", "Chat", number1, number2);
        SendMessage.ToAll("Parameters", "useParametersLabel", "Screen", number1, number2);
        SendMessage.ToAll("Parameters", "useParametersLabel", "ChatAndScreen", number1, number2);

        SendMessage.ToAll("Number1 = {0}, Number2 = {1}", "Chat", number1, number2);
        SendMessage.ToAll("Number1 = {0}, Number2 = {1}", "Screen", number1, number2);
        SendMessage.ToAll("Number1 = {0}, Number2 = {1}", "ChatAndScreen", number1, number2);

        SendMessage.ToAll("============NO TRANSLATE LINE whithout xml-file===========", "Chat");
    }
}

What modified:
Changed the old singleton implementation to a better one (without double locking) and renamed it to Translations
Moved the translationsMessages to the sendMessage Class. Added reference to the Singleton Translations "private static Translations Translator = Translations.GetInstance;" so GetString() could used in the sendMessage class (Translator.GetString(...))
Added a UseConfigFile to the singleton to load the language file later (in cs from mission).

To load the Translationsfile into the singleton use:
private Translations Translator = Translations.UseConfigFile(localizationFilePath);
in mission file cs.


Should work now, couldn't test it at work, only short time in coffee break for modifications :rolleyes:

podvoxx 07-06-2012 09:17 AM

Quote:

Originally Posted by FG28_Kodiak (Post 441764)
Should work now, couldn't test it at work, only short time in coffee break for modifications :rolleyes:

Great thanks, Kodiak! I try this version later.

podvoxx 07-07-2012 02:00 PM

Yes, it work :) Big thanks, Kodiak!
I modify your and my code and get this:

Code:

using System;
using System.Collections.Generic;
using maddox.game;
using maddox.game.world;
using System.Xml;

namespace smpmessage
{
    // ====================================================
    // (based on Small_Bee and FG_Kodiak code)
    // ====================================================
    public sealed class Translate
    {
        #region Parameters
        private static readonly Lazy<Translate> Lazy = new Lazy<Translate>(() => new Translate());
        private static readonly Dictionary<string, Dictionary<string, Dictionary<string, string>>> _stringPool = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
        static readonly IGamePlay GamePlay = Strategy.THIS.GamePlay;

        const string XmlLanguagenode = "SMP/LanguageSection";
        private const string DefaultLanguage = "en";
        public static string setupServerLanguage = "off";
        #endregion

        #region Read File
        public static Translate GetInstance
        {
            get { return Lazy.Value; }
        }


        private Translate()
        {
        }

        public static Translate UseConfigFile(string fileName)
        {
            if (_stringPool.Count == 0)
                LoadSettings(fileName);
            return Lazy.Value;
        }

        private static XmlDocument LoadXMLFile(string file)
        {
            // Load the xml-file in a xml-document
            XmlDocument xDoc = new XmlDocument();

            try
            {
                xDoc.Load(file);
            }
            catch (System.IO.FileNotFoundException)
            {
                throw new Exception("Xml-File " + file + " not found");
            }
            catch (Exception ex)
            {
                throw new Exception("Error loading " + file + ": " + ex.Message);
            }

            return xDoc;
        }

        private static void LoadSettings(string languageFile)
        {
            XmlDocument xDoc = LoadXMLFile(languageFile);

            var xmlNodeList = xDoc.SelectNodes(XmlLanguagenode);

            if (xmlNodeList != null)
                foreach (XmlNode sectionNode in xmlNodeList)
                {

                    var strs = new Dictionary<string, Dictionary<string, string>>();

                    foreach (XmlNode textNode in sectionNode.ChildNodes)
                    {

                        var texts = new Dictionary<string, string>();

                        foreach (XmlNode txt in textNode.ChildNodes)
                        {
                            texts.Add(txt.Name, txt.InnerText);
                        }

                        if (textNode.Attributes != null) strs.Add(textNode.Attributes["key"].Value, texts);
                    }

                    if (sectionNode.Attributes != null) _stringPool.Add(sectionNode.Attributes["name"].Value, strs);
                }

        }

        public string GetString(string section, string key, string language)
        {
            if (_stringPool.Count > 0)
            {
                Dictionary<string, Dictionary<string, string>> strs;

                if (_stringPool.TryGetValue(section, out strs))
                {
                    Dictionary<string, string> texts;

                    if (strs.TryGetValue(key, out texts))
                    {
                        string text;
                        if (setupServerLanguage == "off")
                        {
                            if (texts.TryGetValue(language, out text))
                                return text;
                            if (texts.TryGetValue(DefaultLanguage, out text))
                                return text;
                        }
                        else
                        {
                            if (texts.TryGetValue(setupServerLanguage, out text))
                                return text;
                            if (texts.TryGetValue(DefaultLanguage, out text))
                                return text;
                        }
                    }
                }
            }
            return key; // if nothing is found return a empty string -> modify if a error message is needed.
        }
       
        public string GetString(string section, string key)
        {
            return GetString(section, key, DefaultLanguage);
        }
       
        #endregion

        #region Send Translate Message to List
        public void ToAll(string section, string key, string target, params object[] args)
        {
            switch (target)
            {
                case "ChatAndScreen":
                    {
                        Send(SendMessage.GetPlayerListChatAll(), section, key, "Chat", args);
                        Send(SendMessage.GetPlayerListScreenAll(), section, key, "Screen", args);
                    } break;
                case "Chat":
                    {
                        Send(SendMessage.GetPlayerListChatAll(), section, key, target, target, args);
                        break;
                    }
                case "Screen":
                    {
                        Send(SendMessage.GetPlayerListScreenAll(), section, key, target, target, args);
                    }
                    break;
            }
        }

        public void ToArmy(string section, string key, int army, string target, params object[] args)
        {
            Send(SendMessage.GetPlayerListArmy(army), section, key, target, args);
        }

        public void ToPlayers(string section, string key, Player[] players, string target, params object[] args)
        {
            Send(players, section, key, target, args);
        }
        #endregion

        #region Send
        private void Send(Player[] playerList, string section, string key, string target, params object[] args)
        {
            Dictionary<string, List<Player>> playerLanguageDict = new Dictionary<string, List<Player>>();

            foreach (Player player in playerList)
            {
                string languageName = player.LanguageName();
                if (!playerLanguageDict.ContainsKey(languageName))
                {
                    playerLanguageDict.Add(languageName, new List<Player>());
                }
                playerLanguageDict[languageName].Add(player);
            }

            if (playerList.Length > 0)
            {
                foreach (KeyValuePair<string, List<Player>> kvp in playerLanguageDict)
                {
                    SendMessage.Send(kvp.Value.ToArray(), GetString(section, key, kvp.Key.ToString()), target, args);
                }
            }
        }
        #endregion
    }

    public static class SendMessage
    {
        #region Parameters
        static readonly IGamePlay GamePlay = Strategy.THIS.GamePlay;
        public static string hudON = "off"; 
        #endregion     

        #region Send Message to List
        public static void ToAll(string message, string target, params object[] args)
        {
            switch (target)
            {
                case "ChatAndScreen":
                    {
                        Send(GetPlayerListChatAll(), message, "Chat", args);
                        Send(GetPlayerListScreenAll(), message, "Screen", args);
                    } break;
                case "Chat":
                    {
                        Send(GetPlayerListChatAll(), message, target, args);                       
                        break;
                    }
                case "Screen":
                    {
                        Send(GetPlayerListScreenAll(), message, target, args);                       
                    }
                    break;
            }
        }

        public static void ToArmy(string message, int army, string target, params object[] args)
        {
            Send(GetPlayerListArmy(army), message, target, args);
        }

        public static void ToPlayers(string message, Player[] players, string target, params object[] args)
        {
            Send(players, message, target, args);
        }
        #endregion

        #region Send
        public static void Send(Player[] playerList, string message, string target, params object[] args)
        {           
            if (playerList.Length > 0)
            {
                switch (target)
                {
                    case "ChatAndScreen":
                    {
                        GamePlay.gpLogServer(playerList, message, args);
                        GamePlay.gpHUDLogCenter(playerList, message, args);                     
                    } break;
                    case "Chat":
                    {
                        GamePlay.gpLogServer(playerList, message, args);
                        break;
                    }
                    case "Screen":
                    {
                        GamePlay.gpHUDLogCenter(playerList, message, args);
                        if (hudON == "on")
                        {
                            GamePlay.gpLogServer(playerList, "[HUD]: " + message, args);
                        }
                        break;
                    }
                }
            }

        }
        #endregion

        #region GetPlayerList
        public static Player[] GetPlayerListScreenAll()
        {
            List<Player> players = new List<Player>();
           
            if (GamePlay.gpPlayer() != null)
                if (GamePlay.gpPlayer().Name().ToString() != "Server")
                {
                    players.Add(GamePlay.gpPlayer());
                }
            if (GamePlay.gpRemotePlayers() != null)
                players.AddRange(GamePlay.gpRemotePlayers());
            return players.ToArray();
        }

        public static Player[] GetPlayerListChatAll()
        {
            List<Player> players = new List<Player>();
           
            if (GamePlay.gpPlayer() != null)
                players.Add(GamePlay.gpPlayer());
            if (GamePlay.gpRemotePlayers() != null)
                players.AddRange(GamePlay.gpRemotePlayers());
            return players.ToArray();
        }

        public static Player[] GetPlayerListArmy(int army)
        {
            List<Player> players = new List<Player>();
            List<Player> acceptedPlayers = new List<Player>();
            if (GamePlay.gpPlayer() != null)
                players.Add(GamePlay.gpPlayer());
            if (GamePlay.gpRemotePlayers() != null)
                players.AddRange(GamePlay.gpRemotePlayers());

            if (players != null)
            {
                foreach (Player player in players)
                {
                    if (player.Army() == army) acceptedPlayers.Add(player);
                }             
            }
            return acceptedPlayers.ToArray();
        }
        #endregion
    }
}



All times are GMT. The time now is 08:33 AM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright © 2007 Fulqrum Publishing. All rights reserved.