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