# Copyright (C) 2026 CoelhoFZ
# SPDX-License-Identifier: GPL-3.0-only
# This file is part of unlocker-CoelhoFZ (GPLv3, see LICENSE).

"""gen_donate.py - generates proxy/donate_strings.h with the donation popup
strings (UTF-16) XOR-encrypted, in 8 languages (pt/en/es/fr/zh/hi/ar/ru).

The plaintext only exists HERE (source) and in the script; the final binary
carries only the encrypted bytes + the key. The Yes/No MessageBox buttons are
localized automatically by Windows (they need no translation from us).

Usage: python3 tools/gen_donate.py > proxy/donate_strings.h
"""

KEY = 0x5A

# Fixed order == Lang enum in proxy/dllmain.cpp (L_PT=0..L_RU=7).
LANGS = ["pt", "en", "es", "fr", "zh", "hi", "ar", "ru"]

MSG = {
    "pt": "❤️ Obrigado por usar o meu script, se quiser, você pode fazer uma pequena doação como forma de agradecimento e também para me incentivar a continuar melhorando o Unlocker :D ❤️\n\nclique em sim para doar ou nao para cancelar.",
    "en": "❤️ Thank you for using my script! If you'd like, you can make a small donation as a thank-you and to encourage me to keep improving the Unlocker! ❤️\n\nClick Yes to donate or No to cancel.",
    "es": "❤️ ¡Gracias por usar mi script! Si quieres, puedes hacer una pequeña donación como agradecimiento y para animarme a seguir mejorando el Unlocker. ❤️\n\nHaz clic en Sí para donar o en No para cancelar.",
    "fr": "❤️ Merci d'utiliser mon script ! Si tu veux, tu peux faire un petit don en guise de remerciement et pour m'encourager à continuer d'améliorer l'Unlocker ! ❤️\n\nClique sur Oui pour faire un don ou sur Non pour annuler.",
    "zh": "❤️ 感谢你使用我的脚本！如果你愿意，可以小额捐赠表示感谢，也能鼓励我继续改进解锁器！ ❤️\n\n点击“是”捐赠，或点击“否”取消。",
    "hi": "❤️ मेरा स्क्रिप्ट इस्तेमाल करने के लिए धन्यवाद! चाहें तो धन्यवाद के रूप में छोटा सा दान कर सकते हैं, जिससे मुझे Unlocker को बेहतर बनाते रहने का प्रोत्साहन भी मिलेगा! ❤️\n\nदान करने के लिए हाँ या रद्द करने के लिए नहीं पर क्लिक करें।",
    "ar": "❤️ شكراً لاستخدامك السكربت الخاص بي! إذا أردت، يمكنك تقديم تبرع صغير كشكر وأيضاً لتشجيعي على مواصلة تحسين Unlocker! ❤️\n\nانقر على نعم للتبرع أو لا للإلغاء.",
    "ru": "❤️ Спасибо, что используете мой скрипт! Если хотите, вы можете сделать небольшое пожертвование в знак благодарности, а также чтобы поддержать дальнейшее улучшение Unlocker! ❤️\n\nНажмите «Да», чтобы пожертвовать, или «Нет», чтобы отменить.",
}

TITLE = {
    "pt": "Agradecimento",
    "en": "Thank you",
    "es": "Gracias",
    "fr": "Merci",
    "zh": "感谢",
    "hi": "धन्यवाद",
    "ar": "شكراً",
    "ru": "Спасибо",
}

# PIX key (only shown for Brazil -> the text stays in Portuguese). It goes to
# the donation popup of the build.
PIX_MSG = "Certo, Aqui vai a minha chave pix: xgobgbank@gmail.com | Muito obrigado!!!!!!!!! ❤️"
PIX_TITLE = "PIX"
URL = "https://buymeacoffee.com/coelhofz"
REG_PATH = "Software\\CoelhoFZ\\Minecraft-Bedrock-Free"
REG_VALUE = "donation_shown"


def xor_utf16(s, k):
    # All content is BMP (emoji + accents + CJK + arabic + devanagari +
    # cyrillic) -> code point == UTF-16 code unit. Valid here.
    return [ord(c) ^ ((k + i) & 0xFFFF) for i, c in enumerate(s)]


def emit(name, s, out):
    units = xor_utf16(s, KEY)
    hexs = ", ".join("0x%04X" % u for u in units)
    out.append(f"constexpr uint16_t {name}[{len(units)}] = {{{hexs}}};")
    out.append(f"constexpr size_t {name}Len = {len(units)};")
    out.append("")


# Copyright notice emitted by the generator, identical to the header the
# published file carries (full text of the license in ../LICENSE).
COPYRIGHT = [
    "// Copyright (C) 2026 CoelhoFZ",
    "// SPDX-License-Identifier: GPL-3.0-only",
    "// This file is part of unlocker-CoelhoFZ (GPLv3, see LICENSE).",
    "",
]

out = []
out += COPYRIGHT
out.append("// donate_strings.h - GENERATED by tools/gen_donate.py. DO NOT edit by hand.")
out.append("// XOR-encrypted (UTF-16) strings for the donation popup in 8 languages.")
out.append("// Plaintext does not exist in the binary.")
out.append("#pragma once")
out.append("#include <cstddef>")
out.append("#include <cstdint>")
out.append("namespace donate {")
out.append("inline void wdec(const uint16_t* in, size_t n, uint16_t k, wchar_t* out) {")
out.append("    volatile uint16_t vk = k;")
out.append("    for (size_t i = 0; i < n; ++i)")
out.append("        out[i] = static_cast<wchar_t>(in[i] ^ static_cast<uint16_t>(vk + i));")
out.append("    out[n] = L'\\0';")
out.append("}")
out.append("")

for lang in LANGS:
    out.append(f"// ---- {lang} ----")
    emit(f"kMsg_{lang}", MSG[lang], out)
    emit(f"kTitle_{lang}", TITLE[lang], out)

out.append("// ---- pix (pt, Brazil only) ----")
emit("kPixMsg", PIX_MSG, out)
emit("kPixTitle", PIX_TITLE, out)

out.append("// ---- url / registro ----")
emit("kUrl", URL, out)
emit("kRegPath", REG_PATH, out)
emit("kRegValue", REG_VALUE, out)

out.append("}  // namespace donate")
out.append("")
print("\n".join(out))
print("// key used: 0x%02X" % KEY, file=__import__("sys").stderr)
