core/logic/smn_lang.cpp
author Peace-Maker <januk@freenet.de>
Sat May 10 05:42:33 2014 -0700 (2014-05-10)
changeset 4354 7011587e0021
parent 3413 f1539fcb176c
permissions -rw-r--r--
Remove LogMessageEx definition from logging.inc (bug 5897, r=kyles).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "common_logic.h"
    33 #include "Translator.h"
    34 #include <IPlayerHelpers.h>
    35 #include <IPluginSys.h>
    36 #include <ISourceMod.h>
    37 
    38 static cell_t sm_LoadTranslations(IPluginContext *pCtx, const cell_t *params)
    39 {
    40 	char *filename, *ext;
    41 	char buffer[PLATFORM_MAX_PATH];
    42 	IPlugin *pl = pluginsys->FindPluginByContext(pCtx->GetContext());
    43 
    44 	pCtx->LocalToString(params[1], &filename);
    45 	smcore.Format(buffer, sizeof(buffer), "%s", filename);
    46 
    47 	/* Make sure there is no extension */
    48 	if ((ext = strstr(buffer, ".txt")) != NULL
    49 		|| (ext = strstr(buffer, ".cfg")) != NULL)
    50 	{
    51 		/* Simple heuristic -- just see if it's at the end and terminate if so */
    52 		if ((unsigned)(ext - buffer) == strlen(buffer) - 4)
    53 		{
    54 			*ext = '\0';
    55 		}
    56 	}
    57 
    58 	pl->GetPhrases()->AddPhraseFile(buffer);
    59 
    60 	return 1;
    61 }
    62 
    63 static cell_t sm_SetGlobalTransTarget(IPluginContext *pContext, const cell_t *params)
    64 {
    65 	g_pSM->SetGlobalTarget(params[1]);
    66 
    67 	return 1;
    68 }
    69 
    70 static cell_t sm_GetClientLanguage(IPluginContext *pContext, const cell_t *params)
    71 {
    72 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
    73 	if (!player || !player->IsConnected())
    74 	{
    75 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
    76 	}
    77 
    78 	return g_Translator.GetClientLanguage(params[1]);
    79 }
    80 
    81 static cell_t sm_GetServerLanguage(IPluginContext *pContext, const cell_t *params)
    82 {
    83 	return g_Translator.GetServerLanguage();
    84 }
    85 
    86 static cell_t sm_GetLanguageCount(IPluginContext *pContext, const cell_t *params)
    87 {
    88 	return g_Translator.GetLanguageCount();
    89 }
    90 
    91 static cell_t sm_GetLanguageInfo(IPluginContext *pContext, const cell_t *params)
    92 {
    93 	const char *code;
    94 	const char *name;
    95 
    96 	if (!g_Translator.GetLanguageInfo(params[1], &code, &name))
    97 	{
    98 		return pContext->ThrowNativeError("Invalid language number %d", params[1]);
    99 	}
   100 
   101 	pContext->StringToLocalUTF8(params[2], params[3], code, NULL);
   102 	pContext->StringToLocalUTF8(params[4], params[5], name, NULL);
   103 
   104 	return 1;
   105 }
   106 
   107 static cell_t sm_SetClientLanguage(IPluginContext *pContext, const cell_t *params)
   108 {
   109 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
   110 	
   111 	if (!player || !player->IsConnected())
   112 	{
   113 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   114 	}
   115 	
   116 	player->SetLanguageId(params[2]);
   117 
   118 	return 1;
   119 }
   120 
   121 static cell_t sm_GetLanguageByCode(IPluginContext *pContext, const cell_t *params)
   122 {
   123 	char *code;
   124 	unsigned int langid;
   125 
   126 	pContext->LocalToString(params[1], &code);
   127 
   128 	if (g_Translator.GetLanguageByCode(code, &langid))
   129 		return langid;
   130 
   131 	return -1;
   132 }
   133 
   134 static cell_t sm_GetLanguageByName(IPluginContext *pContext, const cell_t *params)
   135 {
   136 	char *name;
   137 	unsigned int langid;
   138 
   139 	pContext->LocalToString(params[1], &name);
   140 
   141 	if (g_Translator.GetLanguageByName(name, &langid))
   142 		return langid;
   143 
   144 	return -1;
   145 }
   146 
   147 REGISTER_NATIVES(langNatives)
   148 {
   149 	{"LoadTranslations",			sm_LoadTranslations},
   150 	{"SetGlobalTransTarget",		sm_SetGlobalTransTarget},
   151 	{"GetClientLanguage",			sm_GetClientLanguage},
   152 	{"GetServerLanguage",			sm_GetServerLanguage},
   153 	{"GetLanguageCount",			sm_GetLanguageCount},
   154 	{"GetLanguageInfo",				sm_GetLanguageInfo},
   155 	{"SetClientLanguage",			sm_SetClientLanguage},
   156 	{"GetLanguageByCode",			sm_GetLanguageByCode},
   157 	{"GetLanguageByName",			sm_GetLanguageByName},
   158 	{NULL,							NULL},
   159 };