From aa8bf86dc15c2b841ebdf31572058f0dadcc6dc7 Mon Sep 17 00:00:00 2001 From: Borja Ferrer Date: Sat, 27 Jan 2007 03:25:34 +0000 Subject: [PATCH] added %T format support added a new lang native --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40395 --- core/msvc8/sourcemod_mm.vcproj | 6 +++- core/sm_stringutil.cpp | 53 ++++++++++++++++++++++++++++++++++ core/sm_stringutil.h | 2 ++ core/smn_lang.cpp | 29 +++++++++++++++++++ core/smn_textparse.cpp | 3 ++ core/systems/PluginSys.cpp | 15 ++++++++++ core/systems/PluginSys.h | 17 +++++++++++ plugins/include/lang.inc | 26 +++++++++++++++++ 8 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 core/smn_lang.cpp create mode 100644 plugins/include/lang.inc diff --git a/core/msvc8/sourcemod_mm.vcproj b/core/msvc8/sourcemod_mm.vcproj index 913534d3..7fdd69da 100644 --- a/core/msvc8/sourcemod_mm.vcproj +++ b/core/msvc8/sourcemod_mm.vcproj @@ -1,7 +1,7 @@ + + diff --git a/core/sm_stringutil.cpp b/core/sm_stringutil.cpp index 133a2a87..1dda1ab9 100644 --- a/core/sm_stringutil.cpp +++ b/core/sm_stringutil.cpp @@ -16,6 +16,8 @@ #include #include "sm_stringutil.h" #include "CLogger.h" +#include "PluginSys.h" +#include "CTranslator.h" #define LADJUST 0x00000004 /* left adjustment */ #define ZEROPAD 0x00000080 /* zero (as opposed to blank) pad */ @@ -28,6 +30,47 @@ return 0; \ } +size_t Translate(char *buffer, size_t maxlen, IPluginContext *pCtx, const char *key, cell_t target, const cell_t *params, int *arg) +{ + unsigned int langid; + CPlugin *pl = (CPlugin *)g_PluginSys.FindPluginByContext(pCtx->GetContext()); + size_t langcount = pl->GetLangFileCount(); + + if (!g_Translator.GetLanguageByCode("en", &langid)) //:TODO: hardcoded this just for testing + { + //:TODO: error out something + } + + CPhraseFile *phrfl; + TransError err = Trans_Okay; + Translation pTrans; + + for (size_t i=0; iGetFileByIndex(i)); + err = phrfl->GetTranslation(key, langid, &pTrans); + } + + if (err != Trans_Okay) + { + //:TODO: we didnt find our translation in any file :o + } + + void *new_params[MAX_TRANSLATE_PARAMS]; + unsigned int max_params = pTrans.fmt_count; + for (size_t i=0; iLocalToPhysAddr(params[*arg], reinterpret_cast(&new_params[i])); + (*arg)++; + if ((*arg) + i > (size_t)params[0]) + { + //:TODO: we are missing arguments zOMG + } + } + + return g_Translator.Translate(buffer, maxlen, new_params, &pTrans); +} + //:TODO: review this code before we choose a license void AddString(char **buf_p, size_t &maxlen, const char *string, int width, int prec) @@ -564,6 +607,16 @@ reswitch: arg++; break; } + case 'T': + { + CHECK_ARGS(0); + char *key; + cell_t target = params[arg++]; + pCtx->LocalToString(params[arg++], &key); + llen -= Translate(buf_p, llen, pCtx, key, target, params, &arg); + buf_p += llen; + break; + } case '%': { if (!llen) diff --git a/core/sm_stringutil.h b/core/sm_stringutil.h index 61fe0fce..d47b4b7b 100644 --- a/core/sm_stringutil.h +++ b/core/sm_stringutil.h @@ -20,6 +20,8 @@ using namespace SourcePawn; +#define LANG_SERVER 0 + size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext *pCtx, const cell_t *params, int *param); const char *stristr(const char *str, const char *substr); unsigned int strncopy(char *dest, const char *src, size_t count); diff --git a/core/smn_lang.cpp b/core/smn_lang.cpp new file mode 100644 index 00000000..f6d3728f --- /dev/null +++ b/core/smn_lang.cpp @@ -0,0 +1,29 @@ +/** + * =============================================================== + * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. + * =============================================================== + * + * This file is not open source and may not be copied without explicit + * written permission of AlliedModders LLC. This file may not be redistributed + * in whole or significant part. + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php + * + * Version: $Id$ + */ + +#include "PluginSys.h" +#include "CTranslator.h" + +static cell_t sm_LoadTranslations(IPluginContext *pCtx, const cell_t *params) +{ + char *filename; + unsigned int index; + CPlugin *pl = (CPlugin *)g_PluginSys.FindPluginByContext(pCtx->GetContext()); + + pCtx->LocalToString(params[1], &filename); + + index = g_Translator.FindOrAddPhraseFile(filename); + pl->AddLangFile(index); + + return 1; +} diff --git a/core/smn_textparse.cpp b/core/smn_textparse.cpp index 35e4c79a..b61f90de 100644 --- a/core/smn_textparse.cpp +++ b/core/smn_textparse.cpp @@ -268,6 +268,9 @@ static cell_t SMC_ParseFile(IPluginContext *pContext, const cell_t *params) pContext->LocalToPhysAddr(params[3], &c_line); pContext->LocalToPhysAddr(params[4], &c_col); + *c_line = line; + *c_col = col; + return (cell_t)p_err; } diff --git a/core/systems/PluginSys.cpp b/core/systems/PluginSys.cpp index c75fd680..15284a06 100644 --- a/core/systems/PluginSys.cpp +++ b/core/systems/PluginSys.cpp @@ -491,6 +491,21 @@ void CPlugin::SetTimeStamp(time_t t) m_LastAccess = t; } +void CPlugin::AddLangFile(unsigned int index) +{ + m_PhraseFiles.push_back(index); +} + +size_t CPlugin::GetLangFileCount() const +{ + return m_PhraseFiles.size(); +} + +unsigned int CPlugin::GetFileByIndex(unsigned int index) const +{ + return m_PhraseFiles.at(index); +} + /******************* * PLUGIN ITERATOR * *******************/ diff --git a/core/systems/PluginSys.h b/core/systems/PluginSys.h index 1cbb05cf..fe5dc201 100644 --- a/core/systems/PluginSys.h +++ b/core/systems/PluginSys.h @@ -21,6 +21,7 @@ #include #include #include +#include #include "sm_globals.h" #include "vm/sp_vm_basecontext.h" #include "PluginInfoDatabase.h" @@ -176,6 +177,21 @@ public: * Returns true if a plugin is usable. */ bool IsRunnable() const; + + /** + * Adds a language file index to the plugin's list. + */ + void AddLangFile(unsigned int index); + + /** + * Get language file count for this plugin. + */ + size_t GetLangFileCount() const; + + /** + * Get language file index based on the vector index. + */ + unsigned int GetFileByIndex(unsigned int index) const; public: /** * Returns the modification time during last plugin load. @@ -207,6 +223,7 @@ private: IdentityToken_t *m_ident; Handle_t m_handle; bool m_WasRunning; + CVector m_PhraseFiles; }; class CPluginManager : diff --git a/plugins/include/lang.inc b/plugins/include/lang.inc new file mode 100644 index 00000000..66fff159 --- /dev/null +++ b/plugins/include/lang.inc @@ -0,0 +1,26 @@ +/** + * =============================================================== + * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. + * =============================================================== + * + * This file is part of the SourceMod/SourcePawn SDK. This file may only be used + * or modified under the Terms and Conditions of its License Agreement, which is found + * in LICENSE.txt. The Terms and Conditions for making SourceMod extensions/plugins + * may change at any time. To view the latest information, see: + * http://www.sourcemod.net/license.php + * + * Version: $Id$ + */ + +#if defined _lang_included + #endinput +#endif +#define _lang_included + +/** + * @brief Loads a translation file for the plugin calling this native. + * + * @param path Translation file. + * @noreturn + */ +native LoadTranslations(const String:file[]); \ No newline at end of file