Jump to content
Sign in to follow this  
V3ct0r

Кодировщик для LUA скриптов

Recommended Posts

Кодировщик для LUA скриптов

 

Некоторые GameServer.exe не поддерживают русскую кодировку в скриптах, поэтому приходится записывать русские символы в кодовом представлении:

\207\240\232\226\229\242 \226\241\229\236!

Данная программа предназначена для автоматического перевода русских символов в код.

 

Скачать (755 КБ):

Coder 1.2.exe

 

Онлайн версия кодировщика.

  • Like 2

Share this post


Link to post
Share on other sites

Иногда спасает кодировка Windows - 1251 в Notepad++ оч быстро все это проветрить 

Сначала меняем кодировку потом пишем русский текст.

wqwqwqwq.png

Edited by Shapamoe

Share this post


Link to post
Share on other sites

@MaxWatson  держи.

 

Так же есть онлайн версия кодировщика.


Share this post


Link to post
Share on other sites

В данной теме по подробней разобрал тему кодирования  русских символов.

И предоставил исходный код нароботок. при желании можно разобраться.

Скоро внесу его в проект ServerManager

 

  • Thanks 1

Share this post


Link to post
Share on other sites

Исходный код программы для кодирования/декодирования строк на языке программирования C++:

#include <iostream>
#include <string>

// Lua strings coder class
class Coder
{
    public:

        // Encode a string
        static std::string encode(const std::string& src)
        {
            // Check that given string is empty
            if (src.empty() == true)
            {
                return src;
            }

            // The result string
            std::string str{ "" };

            // Encode the string
            for (char const& c : src)
            {
                // Get character ascii code
                unsigned int ascii = static_cast<unsigned int>(
                    static_cast<unsigned char>(c)
                );

                // Check the ascii code
                if ( (ascii >= 192 && ascii <= 255) || (ascii == 168) || (ascii == 184) ) // From 'А' to 'я'
                {
                    // Encode the character
                    str.push_back('\\');
                    str.append(std::to_string(ascii));
                }
                else
                {
                    // Do not encode the character
                    str.push_back(c);
                }
            }

            return str;
        }

        // Decode a string
        static std::string decode(const std::string& src, const char d = '\\')
        {
            // Get the length of the given string 
            const std::size_t len = src.length();

            // Check the length
            if (len == 0)
            {
                // The given string is empty
                return src;
            }

            // Search for slashes ('\')
            std::size_t pos = src.find_first_of(d);

            // Check that slashes are found
            if (pos == std::string::npos)
            {
                // The given string doesn't contain encoded characters
                return src;
            }

            // The number of characters after the slash in the code
            const std::size_t step = 3;

            // Some useful variables
            std::size_t start = 0;
            std::size_t end = 0;

            // The result string
            std::string str{ "" };

            // Decode the string
            do
            {
                // Update 'end' position
                end = pos + 1;

                // Calculate delta between current and start positions
                std::size_t delta = (end - start);

                // Handle the string before the slash
                if (delta > 1) 
                {
                    // We have characters before the slash
                    str.append( src.substr( start, (delta - 1) ) ); 
                    start += delta;
                }
                else 
                {
                    // We haven't characters before the slash
                    start++;
                }

                // Calculate delta between the end of the string and current positions
                delta = (len - end);

                // Check that we have at least 3 characters after the slash
                if (delta >= step)
                {
                    // Get the code
                    std::string code = src.substr(end, step);

                    // The valid code flag
                    bool valid = true;

                    // Checking the code . . . 
                    for (char const& c : code)
                    {
                        if (std::isdigit(c) == 0)
                        {
                            // Non valid code
                            valid = false;

                            // Remove the slash
                            if (c == d)
                            {
                                start--;
                                code.pop_back();
                            }

                            break;
                        }
                    }

                    // Check the flag
                    if (valid == true)
                    {
                        // Convert std::string to unsigned long
                        const unsigned long ascii = std::stoul(code, nullptr, 10);

                        // Check the code number
                        if ( (ascii >= 192 && ascii <= 255) || (ascii == 168) || (ascii == 184) ) // From 'А' to 'я'
                        {
                            // Valid code
                            str.push_back(static_cast<const char>(ascii));
                        }
                        else
                        {
                            // Non-valid code
                            code.insert(code.begin(), d); str.append(code);
                        }
                    }
                    else
                    {
                        // Non-valid code
                        code.insert(code.begin(), d); str.append(code);
                    }

                    // Update 'start' position
                    start += step;
                }
                else
                {
                    // Append the rest of the string
                    std::string sub = src.substr(end, delta);
                    sub.insert(sub.begin(), d); str.append(sub);

                    // Update 'start' position
                    start += delta;
                }
            } 
            while ( (pos = src.find(d, start) ) != std::string::npos );

            // Handle the rest of string
            if (start != len)
            {
                str.append(src.substr(start, (len - start)));
            }

            return str;
        }
};

// The entry point
int main(int argc, char* argv[])
{
    // For Russian language support
    setlocale(LC_ALL, "ru");

    // Tests for Coder::encode()
    std::cout << "Coder::encode() test:" << std::endl;
    {
        // Empty string
        std::cout << Coder::encode("") << std::endl;

        // Russian alphabet
        std::cout << Coder::encode("абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ") << std::endl;

        // Hello world!
        std::cout << Coder::encode("Привет, мир!") << std::endl;

        // 'Ё' and 'ё'
        std::cout << Coder::encode("Ё and ё!") << std::endl;
    }

    // Tests for Coder::decode()
    std::cout << std::endl << std::endl << "Coder::decode() test:" << std::endl;
    {
        // Empty string
        std::cout << Coder::decode("") << std::endl;

        // Non-encoded string
        std::cout << Coder::decode("Hello world!") << std::endl;

        // Encoded string
        std::cout << Coder::decode("\\207\\240\\232\\226\\229\\242 \\236\\232\\240") << std::endl;

        // Encoded string with non-encoded characters (1)
        std::cout << Coder::decode("\\207\\240\\232\\226\\229\\242 \\236\\232\\240!!") << std::endl;

        // Encoded string with non-encoded characters (2)
        std::cout << Coder::decode("Hello world is: \\207\\240\\232\\226\\229\\242 \\236\\232\\240!") << std::endl;
        
        // Encoded string with non-encoded characters (3)
        std::cout << Coder::decode("AAAA\\207\\240BBB\\232\\226CCC\\229\\242 \\236DDD\\232\\240FFF") << std::endl;

        // Encoded string with non-encoded characters and non-valid code \600, \100 and \333
        std::cout << Coder::decode("Hello world is: \\207\\240\\600\\226\\229\\242 \\100\\232\\240!\\333") << std::endl;

        // Encoded string with non-encoded characters and non-valid code \AAA, \bbb and \CcC
        std::cout << Coder::decode("Hello world is: \\207\\240\\AAA\\226\\229\\242 \\bbb\\232\\240!\\CcC") << std::endl;

        // Encoded string with non-encoded characters and non-valid code \24, \19 and \32
        std::cout << Coder::decode("Hello world is: \\207\\240\\24\\226\\229\\242 \\19\\232\\240!\\32") << std::endl;

        // Russian alphabet
        std::cout << Coder::decode("\\224\\225\\226\\227\\228\\229\\184\\230\\231\\232\\233\\234\\235\\236\\237\\238\\239\\240\\241\\242\\243\\244\\245\\246\\247\\248\\249\\250\\251\\252\\253\\254\\255\\192\\193\\194\\195\\196\\197\\168\\198\\199\\200\\201\\202\\203\\204\\205\\206\\207\\208\\209\\210\\211\\212\\213\\214\\215\\216\\217\\218\\219\\220\\221\\222\\223");
    }

    return 0;
}

 

Результаты тестов:

Coder::encode() test:

\224\225\226\227\228\229\184\230\231\232\233\234\235\236\237\238\239\240\241\242\243\244\245\246\247\248\249\250\251\252\253\254\255\192\193\194\195\196\197\168\198\199\200\201\202\203\204\205\206\207\208\209\210\211\212\213\214\215\216\217\218\219\220\221\222\223
\207\240\232\226\229\242, \236\232\240!
\168 and \184!


Coder::decode() test:

Hello world!
Привет мир
Привет мир!!
Hello world is: Привет мир!
AAAAПрBBBивCCCет мDDDирFFF
Hello world is: Пр\600вет \100ир!\333
Hello world is: Пр\AAAвет \bbbир!\CcC
Hello world is: Пр\24вет \19ир!\32
абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ

 


Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...