Page 1 of 1

Adding new reference

PostPosted: 13 Jan 2014, 23:58
by Leeizazombie
Is it possible to make the class library (command) read a .dll in the same folder when it is set to use it like:
using HtmlAgilityPack;
If not, Dzienny can you add this to the source code?
I'm working on a Google translate command, and I cannot achieve it with out this reference, thanks for any help!

Command idea:
/translate sp Hello
Output: Gracias

Re: Adding new reference

PostPosted: 14 Jan 2014, 00:00
by Leeizazombie
Test command:

Code: Select all
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Net;
using System.Web;

namespace MCDzienny
{
    public class CmdTranslate : Command
    {
        public override string name { get { return "translate"; } }
        public override string shortcut { get { return "tl"; } }
        public override string type { get { return "other"; } }
        public override bool museumUsable { get { return false; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }

        public override void Use(Player p, string message)
        {
            string content = message;
            string fromLanguage = "English";
            string toLanguage = "Spanish";
            var languageMap = new Dictionary<string, string>();
            InitLanguageMap(languageMap);
            Uri address = new Uri("http://translate.google.com/translate_t");
            WebClient wc = new WebClient();
            wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);

            // Async Upload to the specified source i.e http://translate.google.com/translate_t for handling the translation.
            wc.UploadStringAsync(address, GetPostData(languageMap[fromLanguage], languageMap[toLanguage], content));
        }
        /// <summary>
        /// Initialize Language Mapping, Key value pair of Language Name, Language Code
        /// </summary>
        /// <param name="languageMap"></param>
        static void InitLanguageMap(Dictionary<string, string> languageMap)
        {
            languageMap.Add("Afrikaans", "af");
            languageMap.Add("Albanian", "sq");
            languageMap.Add("Arabic", "ar");
            languageMap.Add("Armenian", "hy");
            languageMap.Add("Azerbaijani", "az");
            languageMap.Add("Basque", "eu");
            languageMap.Add("Belarusian", "be");
            languageMap.Add("Bengali", "bn");
            languageMap.Add("Bulgarian", "bg");
            languageMap.Add("Catalan", "ca");
            languageMap.Add("Chinese", "zh-CN");
            languageMap.Add("Croatian", "hr");
            languageMap.Add("Czech", "cs");
            languageMap.Add("Danish", "da");
            languageMap.Add("Dutch", "nl");
            languageMap.Add("English", "en");
            languageMap.Add("Esperanto", "eo");
            languageMap.Add("Estonian", "et");
            languageMap.Add("Filipino", "tl");
            languageMap.Add("Finnish", "fi");
            languageMap.Add("French", "fr");
            languageMap.Add("Galician", "gl");
            languageMap.Add("German", "de");
            languageMap.Add("Georgian", "ka");
            languageMap.Add("Greek", "el");
            languageMap.Add("Haitian Creole", "ht");
            languageMap.Add("Hebrew", "iw");
            languageMap.Add("Hindi", "hi");
            languageMap.Add("Hungarian", "hu");
            languageMap.Add("Icelandic", "is");
            languageMap.Add("Indonesian", "id");
            languageMap.Add("Irish", "ga");
            languageMap.Add("Italian", "it");
            languageMap.Add("Japanese", "ja");
            languageMap.Add("Korean", "ko");
            languageMap.Add("Lao", "lo");
            languageMap.Add("Latin", "la");
            languageMap.Add("Latvian", "lv");
            languageMap.Add("Lithuanian", "lt");
            languageMap.Add("Macedonian", "mk");
            languageMap.Add("Malay", "ms");
            languageMap.Add("Maltese", "mt");
            languageMap.Add("Norwegian", "no");
            languageMap.Add("Persian", "fa");
            languageMap.Add("Polish", "pl");
            languageMap.Add("Portuguese", "pt");
            languageMap.Add("Romanian", "ro");
            languageMap.Add("Russian", "ru");
            languageMap.Add("Serbian", "sr");
            languageMap.Add("Slovak", "sk");
            languageMap.Add("Slovenian", "sl");
            languageMap.Add("Spanish", "es");
            languageMap.Add("Swahili", "sw");
            languageMap.Add("Swedish", "sv");
            languageMap.Add("Tamil", "ta");
            languageMap.Add("Telugu", "te");
            languageMap.Add("Thai", "th");
            languageMap.Add("Turkish", "tr");
            languageMap.Add("Ukrainian", "uk");
            languageMap.Add("Urdu", "ur");
            languageMap.Add("Vietnamese", "vi");
            languageMap.Add("Welsh", "cy");
            languageMap.Add("Yiddish", "yi");
        }

        /// <summary>
        /// Construct the Post data required for Google Translation
        /// </summary>
        /// <param name="fromLanguage"></param>
        /// <param name="toLanguage"></param>
        /// <returns></returns>
        static string GetPostData(string fromLanguage, string toLanguage, string content)
        {
            // Set the language translation. All we need is the language pair, from and to.
            string strPostData = string.Format("hl=en&ie=UTF8&oe=UTF8submit=Translate&langpair={0}|{1}",
                                                 fromLanguage,
                                                 toLanguage);

            // Encode the content and set the text query string param
            return strPostData += "&text=" + HttpUtility.UrlEncode(content);
        }

        /// <summary>
        /// Handle the Upload Complete event. if event arg e.Result is not null, you have the translated text. Cool nothing went wrong!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                var doc = new HtmlDocument();
                doc.LoadHtml(e.Result);
                var node = doc.DocumentNode.SelectSingleNode("//span[@id='result_box']");
                var output = node != null ? node.InnerText : e.Error.Message;
                Console.WriteLine("Translated Text: " + output);
            }
        }       

        public override void Help(Player p)
        {
            Player.SendMessage(p, "/translate - test version");
        }
    }
}

I'm gonna try it on my own software for now

Re: Adding new reference

PostPosted: 15 Jan 2014, 14:37
by dzienny
It's curently possible only if you compile your custom command in VS. But I will add an option to reference external dlls in a command source code. I'm not sure about the convention yet. I guess you will just have to add it in the first line of the source in the form of a comment, like:
Code: Select all
// References: "HttpAgilityPack"

using HttpAgilityPack;

namespace MCDzienny
{
    class MyCommand : Command
    {
        ...
    }
}

Also, you will have to make sure that the referenced library (dll) is in the main server folder.

Re: Adding new reference

PostPosted: 15 Jan 2014, 20:02
by Leeizazombie
I'm having no luck when adding the dll to the main folder of the server with the comment in the code, I have VS 2012, I'm not sure how to manually compile commands, I tried build solution, but it won't make a CmdTL.dll in the debug folder.
I hope you can get that feature made if you can, thanks <ok>

Re: Adding new reference

PostPosted: 15 Jan 2014, 21:18
by joppiesaus
Okay...
first add your reference by right clicking "add reference" in the solution explorer in the references folder...
Click browse, add MCDzienny_.dll and that library.
Then, goto the "build" menu in the upper menu, click "batch build", select "Release", click (re)build, and let's hope it works.
You'll find the dll in the "Release" folder. Copy your command dll to "mcdzienny\extra\commands\dll", and your other library dll to the main MCDzienny folder.
It should work. :geek:


Sorry for my microsoft-F1 like instructions! xD

Re: Adding new reference

PostPosted: 15 Jan 2014, 22:19
by Leeizazombie
I have no option: "batch build"

Re: Adding new reference

PostPosted: 16 Jan 2014, 09:56
by joppiesaus
Leeizazombie wrote:I have no option: "batch build"

Try build solution?

Re: Adding new reference

PostPosted: 16 Jan 2014, 10:51
by Leeizazombie
The output files are the htmlagility.dll and the MCDzienny_.dll but not the command file

Re: Adding new reference

PostPosted: 02 Apr 2014, 23:54
by dzienny
It was added in the version 11.2.