added %T format support
authorBorja Ferrer <faluco@alliedmods.net>
Sat Jan 27 03:25:34 2007 +0000 (2007-01-27)
changeset 3898ba73c27410b
parent 388 4c346c316a49
child 390 ccc8b9173b91
added %T format support
added a new lang native
core/msvc8/sourcemod_mm.vcproj
core/sm_stringutil.cpp
core/sm_stringutil.h
core/smn_lang.cpp
core/smn_textparse.cpp
core/systems/PluginSys.cpp
core/systems/PluginSys.h
plugins/include/lang.inc
     1.1 --- a/core/msvc8/sourcemod_mm.vcproj	Sat Jan 27 03:10:31 2007 +0000
     1.2 +++ b/core/msvc8/sourcemod_mm.vcproj	Sat Jan 27 03:25:34 2007 +0000
     1.3 @@ -1,7 +1,7 @@
     1.4  <?xml version="1.0" encoding="Windows-1252"?>
     1.5  <VisualStudioProject
     1.6  	ProjectType="Visual C++"
     1.7 -	Version="8.00"
     1.8 +	Version="8,00"
     1.9  	Name="sourcemod_mm"
    1.10  	ProjectGUID="{E39527CD-7CAB-4420-97CC-DA1B93B260BC}"
    1.11  	RootNamespace="sourcemod_mm"
    1.12 @@ -244,6 +244,10 @@
    1.13  				>
    1.14  			</File>
    1.15  			<File
    1.16 +				RelativePath="..\smn_lang.cpp"
    1.17 +				>
    1.18 +			</File>
    1.19 +			<File
    1.20  				RelativePath="..\smn_player.cpp"
    1.21  				>
    1.22  			</File>
     2.1 --- a/core/sm_stringutil.cpp	Sat Jan 27 03:10:31 2007 +0000
     2.2 +++ b/core/sm_stringutil.cpp	Sat Jan 27 03:25:34 2007 +0000
     2.3 @@ -16,6 +16,8 @@
     2.4  #include <stdarg.h>
     2.5  #include "sm_stringutil.h"
     2.6  #include "CLogger.h"
     2.7 +#include "PluginSys.h"
     2.8 +#include "CTranslator.h"
     2.9  
    2.10  #define LADJUST			0x00000004		/* left adjustment */
    2.11  #define ZEROPAD			0x00000080		/* zero (as opposed to blank) pad */
    2.12 @@ -28,6 +30,47 @@
    2.13  		return 0; \
    2.14  	}
    2.15  
    2.16 +size_t Translate(char *buffer, size_t maxlen, IPluginContext *pCtx, const char *key, cell_t target, const cell_t *params, int *arg)
    2.17 +{
    2.18 +	unsigned int langid;
    2.19 +	CPlugin *pl = (CPlugin *)g_PluginSys.FindPluginByContext(pCtx->GetContext());
    2.20 +	size_t langcount = pl->GetLangFileCount();
    2.21 +
    2.22 +	if (!g_Translator.GetLanguageByCode("en", &langid)) 	//:TODO: hardcoded this just for testing
    2.23 +	{
    2.24 +		//:TODO: error out something
    2.25 +	}
    2.26 +
    2.27 +	CPhraseFile *phrfl;
    2.28 +	TransError err = Trans_Okay;
    2.29 +	Translation pTrans;
    2.30 +
    2.31 +	for (size_t i=0; i<langcount && err!=Trans_Okay; i++)
    2.32 +	{
    2.33 +		phrfl = g_Translator.GetFileByIndex(pl->GetFileByIndex(i));
    2.34 +		err = phrfl->GetTranslation(key, langid, &pTrans);
    2.35 +	}
    2.36 +
    2.37 +	if (err != Trans_Okay)
    2.38 +	{
    2.39 +		//:TODO: we didnt find our translation in any file :o
    2.40 +	}
    2.41 +
    2.42 +	void *new_params[MAX_TRANSLATE_PARAMS];
    2.43 +	unsigned int max_params = pTrans.fmt_count;
    2.44 +	for (size_t i=0; i<max_params; i++)
    2.45 +	{
    2.46 +		pCtx->LocalToPhysAddr(params[*arg], reinterpret_cast<cell_t **>(&new_params[i]));
    2.47 +		(*arg)++;
    2.48 +		if ((*arg) + i > (size_t)params[0])
    2.49 +		{
    2.50 +			//:TODO: we are missing arguments zOMG
    2.51 +		}
    2.52 +	}
    2.53 +
    2.54 +	return g_Translator.Translate(buffer, maxlen, new_params, &pTrans);
    2.55 +}
    2.56 +
    2.57  //:TODO: review this code before we choose a license
    2.58  
    2.59  void AddString(char **buf_p, size_t &maxlen, const char *string, int width, int prec)
    2.60 @@ -564,6 +607,16 @@
    2.61  				arg++;
    2.62  				break;
    2.63  			}
    2.64 +		case 'T':
    2.65 +			{
    2.66 +				CHECK_ARGS(0);
    2.67 +				char *key;
    2.68 +				cell_t target = params[arg++];
    2.69 +				pCtx->LocalToString(params[arg++], &key);
    2.70 +				llen -= Translate(buf_p, llen, pCtx, key, target, params, &arg);
    2.71 +				buf_p += llen;
    2.72 +				break;
    2.73 +			}
    2.74  		case '%':
    2.75  			{
    2.76  				if (!llen)
     3.1 --- a/core/sm_stringutil.h	Sat Jan 27 03:10:31 2007 +0000
     3.2 +++ b/core/sm_stringutil.h	Sat Jan 27 03:25:34 2007 +0000
     3.3 @@ -20,6 +20,8 @@
     3.4  
     3.5  using namespace SourcePawn;
     3.6  
     3.7 +#define LANG_SERVER		0
     3.8 +
     3.9  size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext *pCtx, const cell_t *params, int *param);
    3.10  const char *stristr(const char *str, const char *substr);
    3.11  unsigned int strncopy(char *dest, const char *src, size_t count);
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/core/smn_lang.cpp	Sat Jan 27 03:25:34 2007 +0000
     4.3 @@ -0,0 +1,29 @@
     4.4 +/**
     4.5 + * ===============================================================
     4.6 + * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4.7 + * ===============================================================
     4.8 + *
     4.9 + * This file is not open source and may not be copied without explicit
    4.10 + * written permission of AlliedModders LLC.  This file may not be redistributed 
    4.11 + * in whole or significant part.
    4.12 + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    4.13 + *
    4.14 + * Version: $Id$
    4.15 + */
    4.16 +
    4.17 +#include "PluginSys.h"
    4.18 +#include "CTranslator.h"
    4.19 +
    4.20 +static cell_t sm_LoadTranslations(IPluginContext *pCtx, const cell_t *params)
    4.21 +{
    4.22 +	char *filename;
    4.23 +	unsigned int index;
    4.24 +	CPlugin *pl = (CPlugin *)g_PluginSys.FindPluginByContext(pCtx->GetContext());
    4.25 +
    4.26 +	pCtx->LocalToString(params[1], &filename);
    4.27 +
    4.28 +	index = g_Translator.FindOrAddPhraseFile(filename);
    4.29 +	pl->AddLangFile(index);
    4.30 +
    4.31 +	return 1;
    4.32 +}
     5.1 --- a/core/smn_textparse.cpp	Sat Jan 27 03:10:31 2007 +0000
     5.2 +++ b/core/smn_textparse.cpp	Sat Jan 27 03:25:34 2007 +0000
     5.3 @@ -268,6 +268,9 @@
     5.4  	pContext->LocalToPhysAddr(params[3], &c_line);
     5.5  	pContext->LocalToPhysAddr(params[4], &c_col);
     5.6  
     5.7 +	*c_line = line;
     5.8 +	*c_col = col;
     5.9 +
    5.10  	return (cell_t)p_err;
    5.11  }
    5.12  
     6.1 --- a/core/systems/PluginSys.cpp	Sat Jan 27 03:10:31 2007 +0000
     6.2 +++ b/core/systems/PluginSys.cpp	Sat Jan 27 03:25:34 2007 +0000
     6.3 @@ -491,6 +491,21 @@
     6.4  	m_LastAccess = t;
     6.5  }
     6.6  
     6.7 +void CPlugin::AddLangFile(unsigned int index)
     6.8 +{
     6.9 +	m_PhraseFiles.push_back(index);
    6.10 +}
    6.11 +
    6.12 +size_t CPlugin::GetLangFileCount() const
    6.13 +{
    6.14 +	return m_PhraseFiles.size();
    6.15 +}
    6.16 +
    6.17 +unsigned int CPlugin::GetFileByIndex(unsigned int index) const
    6.18 +{
    6.19 +	return m_PhraseFiles.at(index);
    6.20 +}
    6.21 +
    6.22  /*******************
    6.23   * PLUGIN ITERATOR *
    6.24   *******************/
     7.1 --- a/core/systems/PluginSys.h	Sat Jan 27 03:10:31 2007 +0000
     7.2 +++ b/core/systems/PluginSys.h	Sat Jan 27 03:25:34 2007 +0000
     7.3 @@ -21,6 +21,7 @@
     7.4  #include <IHandleSys.h>
     7.5  #include <sh_list.h>
     7.6  #include <sh_stack.h>
     7.7 +#include <sh_vector.h>
     7.8  #include "sm_globals.h"
     7.9  #include "vm/sp_vm_basecontext.h"
    7.10  #include "PluginInfoDatabase.h"
    7.11 @@ -176,6 +177,21 @@
    7.12  	 * Returns true if a plugin is usable.
    7.13  	 */
    7.14  	bool IsRunnable() const;
    7.15 +
    7.16 +	/**
    7.17 +	* Adds a language file index to the plugin's list.
    7.18 +	*/
    7.19 +	void AddLangFile(unsigned int index);
    7.20 +
    7.21 +	/**
    7.22 +	* Get language file count for this plugin.
    7.23 +	*/
    7.24 +	size_t GetLangFileCount() const;
    7.25 +
    7.26 +	/**
    7.27 +	* Get language file index based on the vector index.
    7.28 +	*/
    7.29 +	unsigned int GetFileByIndex(unsigned int index) const;
    7.30  public:
    7.31  	/**
    7.32  	* Returns the modification time during last plugin load.
    7.33 @@ -207,6 +223,7 @@
    7.34  	IdentityToken_t *m_ident;
    7.35  	Handle_t m_handle;
    7.36  	bool m_WasRunning;
    7.37 +	CVector<unsigned int> m_PhraseFiles;
    7.38  };
    7.39  
    7.40  class CPluginManager : 
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/plugins/include/lang.inc	Sat Jan 27 03:25:34 2007 +0000
     8.3 @@ -0,0 +1,26 @@
     8.4 +/**
     8.5 + * ===============================================================
     8.6 + * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     8.7 + * ===============================================================
     8.8 + *
     8.9 + *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
    8.10 + * or modified under the Terms and Conditions of its License Agreement, which is found 
    8.11 + * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
    8.12 + * may change at any time.  To view the latest information, see:
    8.13 + *   http://www.sourcemod.net/license.php
    8.14 + *
    8.15 + * Version: $Id$
    8.16 + */
    8.17 +
    8.18 +#if defined _lang_included
    8.19 + #endinput
    8.20 +#endif
    8.21 +#define _lang_included
    8.22 +
    8.23 +/**
    8.24 + * @brief Loads a translation file for the plugin calling this native.
    8.25 + *
    8.26 + * @param path		Translation file.
    8.27 + * @noreturn
    8.28 + */
    8.29 +native LoadTranslations(const String:file[]);
    8.30 \ No newline at end of file