1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/smn_lang.cpp Sun Jul 06 04:49:55 2008 +0000
1.3 @@ -0,0 +1,116 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#include "PluginSys.h"
1.36 +#include "Translator.h"
1.37 +#include "LibrarySys.h"
1.38 +#include "sm_stringutil.h"
1.39 +#include "PlayerManager.h"
1.40 +
1.41 +static cell_t sm_LoadTranslations(IPluginContext *pCtx, const cell_t *params)
1.42 +{
1.43 + char *filename, *ext;
1.44 + char buffer[PLATFORM_MAX_PATH];
1.45 + CPlugin *pl = (CPlugin *)g_PluginSys.FindPluginByContext(pCtx->GetContext());
1.46 +
1.47 + pCtx->LocalToString(params[1], &filename);
1.48 + UTIL_Format(buffer, sizeof(buffer), "%s", filename);
1.49 +
1.50 + /* Make sure there is no extension */
1.51 + if ((ext = strstr(buffer, ".txt")) != NULL
1.52 + || (ext = strstr(buffer, ".cfg")) != NULL)
1.53 + {
1.54 + /* Simple heuristic -- just see if it's at the end and terminate if so */
1.55 + if ((unsigned)(ext - buffer) == strlen(buffer) - 4)
1.56 + {
1.57 + *ext = '\0';
1.58 + }
1.59 + }
1.60 +
1.61 + pl->GetPhrases()->AddPhraseFile(buffer);
1.62 +
1.63 + return 1;
1.64 +}
1.65 +
1.66 +static cell_t sm_SetGlobalTransTarget(IPluginContext *pContext, const cell_t *params)
1.67 +{
1.68 + g_SourceMod.SetGlobalTarget(params[1]);
1.69 +
1.70 + return 1;
1.71 +}
1.72 +
1.73 +static cell_t sm_GetClientLanguage(IPluginContext *pContext, const cell_t *params)
1.74 +{
1.75 + CPlayer *player = g_Players.GetPlayerByIndex(params[1]);
1.76 + if (!player || !player->IsConnected())
1.77 + {
1.78 + return pContext->ThrowNativeError("Invalid client index %d", params[1]);
1.79 + }
1.80 +
1.81 + return g_Translator.GetClientLanguage(params[1]);
1.82 +}
1.83 +
1.84 +static cell_t sm_GetServerLanguage(IPluginContext *pContext, const cell_t *params)
1.85 +{
1.86 + return g_Translator.GetServerLanguage();
1.87 +}
1.88 +
1.89 +static cell_t sm_GetLanguageCount(IPluginContext *pContext, const cell_t *params)
1.90 +{
1.91 + return g_Translator.GetLanguageCount();
1.92 +}
1.93 +
1.94 +static cell_t sm_GetLanguageInfo(IPluginContext *pContext, const cell_t *params)
1.95 +{
1.96 + const char *code;
1.97 + const char *name;
1.98 +
1.99 + if (!g_Translator.GetLanguageInfo(params[1], &code, &name))
1.100 + {
1.101 + return pContext->ThrowNativeError("Invalid language number %d", params[1]);
1.102 + }
1.103 +
1.104 + pContext->StringToLocalUTF8(params[2], params[3], code, NULL);
1.105 + pContext->StringToLocalUTF8(params[4], params[5], name, NULL);
1.106 +
1.107 + return 1;
1.108 +}
1.109 +
1.110 +REGISTER_NATIVES(langNatives)
1.111 +{
1.112 + {"LoadTranslations", sm_LoadTranslations},
1.113 + {"SetGlobalTransTarget", sm_SetGlobalTransTarget},
1.114 + {"GetClientLanguage", sm_GetClientLanguage},
1.115 + {"GetServerLanguage", sm_GetServerLanguage},
1.116 + {"GetLanguageCount", sm_GetLanguageCount},
1.117 + {"GetLanguageInfo", sm_GetLanguageInfo},
1.118 + {NULL, NULL},
1.119 +};