core/Translator.h
author Matt Woodrow <pred@alliedmods.net>
Thu May 01 23:48:27 2008 +0000 (2008-05-01)
changeset 2035 941abff6deed
parent 1952 b7aa2b626807
child 2042 4a2faafca477
permissions -rw-r--r--
Added msvc9 Project files to sdktools. Badly.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#ifndef _INCLUDE_SOURCEMOD_TRANSLATOR_H_
ds@1952
    33
#define _INCLUDE_SOURCEMOD_TRANSLATOR_H_
ds@1952
    34
ds@1952
    35
#include "sm_trie.h"
ds@1952
    36
#include <sh_string.h>
ds@1952
    37
#include <sh_vector.h>
ds@1952
    38
#include "sm_globals.h"
ds@1952
    39
#include "sm_memtable.h"
ds@1952
    40
#include "ITextParsers.h"
ds@1952
    41
ds@1952
    42
#define MAX_TRANSLATE_PARAMS		32
ds@1952
    43
#define CORELANG_ENGLISH			0
ds@1952
    44
ds@1952
    45
/* :TODO: write a templatized version of tries? */
ds@1952
    46
ds@1952
    47
using namespace SourceHook;
ds@1952
    48
class Translator;
ds@1952
    49
ds@1952
    50
enum PhraseParseState
ds@1952
    51
{
ds@1952
    52
	PPS_None = 0,
ds@1952
    53
	PPS_Phrases,
ds@1952
    54
	PPS_InPhrase,
ds@1952
    55
};
ds@1952
    56
ds@1952
    57
struct Language
ds@1952
    58
{
ds@1952
    59
	char m_code2[3];
ds@1952
    60
	int m_FullName;
ds@1952
    61
};
ds@1952
    62
ds@1952
    63
struct Translation
ds@1952
    64
{
ds@1952
    65
	const char *szPhrase;		/**< Translated phrase. */
ds@1952
    66
	unsigned int fmt_count;		/**< Number of format parameters. */
ds@1952
    67
	int *fmt_order;				/**< Format phrase order. */
ds@1952
    68
};
ds@1952
    69
ds@1952
    70
#define LANGUAGE_ENGLISH			0
ds@1952
    71
ds@1952
    72
enum TransError
ds@1952
    73
{
ds@1952
    74
	Trans_Okay = 0,
ds@1952
    75
	Trans_BadLanguage = 1,
ds@1952
    76
	Trans_BadPhrase = 2,
ds@1952
    77
	Trans_BadPhraseLanguage = 3,
ds@1952
    78
	Trans_BadPhraseFile = 4,
ds@1952
    79
};
ds@1952
    80
ds@1952
    81
class CPhraseFile : public ITextListener_SMC
ds@1952
    82
{
ds@1952
    83
public:
ds@1952
    84
	CPhraseFile(Translator *pTranslator, const char *file);
ds@1952
    85
	~CPhraseFile();
ds@1952
    86
public:
ds@1952
    87
	void ReparseFile();
ds@1952
    88
	const char *GetFilename();
ds@1952
    89
	TransError GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans);
ds@1952
    90
public: //ITextListener_SMC
ds@1952
    91
	void ReadSMC_ParseStart();
ds@1952
    92
	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
ds@1952
    93
	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
ds@1952
    94
	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
ds@1952
    95
	void ReadSMC_ParseEnd(bool halted, bool failed);
ds@1952
    96
private:
ds@1952
    97
	void ParseError(const char *message, ...);
ds@1952
    98
	void ParseWarning(const char *message, ...);
ds@1952
    99
private:
ds@1952
   100
	Trie *m_pPhraseLookup;
ds@1952
   101
	String m_File;
ds@1952
   102
	Translator *m_pTranslator;
ds@1952
   103
	PhraseParseState m_ParseState;
ds@1952
   104
	int m_CurPhrase;
ds@1952
   105
	BaseMemTable *m_pMemory;
ds@1952
   106
	BaseStringTable *m_pStringTab;
ds@1952
   107
	unsigned int m_LangCount;
ds@1952
   108
	String m_ParseError;
ds@1952
   109
	String m_LastPhraseString;
ds@1952
   110
	bool m_FileLogged;
ds@1952
   111
};
ds@1952
   112
ds@1952
   113
class Translator : 
ds@1952
   114
	public ITextListener_SMC,
ds@1952
   115
	public SMGlobalClass
ds@1952
   116
{
ds@1952
   117
public:
ds@1952
   118
	Translator();
ds@1952
   119
	~Translator();
ds@1952
   120
public: // SMGlobalClass
ds@1952
   121
	ConfigResult OnSourceModConfigChanged(const char *key, 
ds@1952
   122
		const char *value, 
ds@1952
   123
		ConfigSource source, 
ds@1952
   124
		char *error, 
ds@1952
   125
		size_t maxlength);
ds@1952
   126
	void OnSourceModAllInitialized();
ds@1952
   127
	void OnSourceModLevelChange(const char *mapName);
ds@1952
   128
public: // ITextListener_SMC
ds@1952
   129
	void ReadSMC_ParseStart();
ds@1952
   130
	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
ds@1952
   131
	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
ds@1952
   132
	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
ds@1952
   133
public:
ds@1952
   134
	void RebuildLanguageDatabase(const char *lang_header_file);
ds@1952
   135
	unsigned int FindOrAddPhraseFile(const char *phrase_file);
ds@1952
   136
	BaseStringTable *GetStringTable();
ds@1952
   137
	unsigned int GetLanguageCount();
ds@1952
   138
	bool GetLanguageInfo(unsigned int number, const char **code, const char **name);
ds@1952
   139
	bool GetLanguageByCode(const char *code, unsigned int *index);
ds@1952
   140
	bool GetLanguageByName(const char *name, unsigned int *index);
ds@1952
   141
	size_t Translate(char *buffer, size_t maxlength, void **params, const Translation *pTrans);
ds@1952
   142
	CPhraseFile *GetFileByIndex(unsigned int index);
ds@1952
   143
	TransError CoreTransEx(CPhraseFile *pFile,
ds@1952
   144
		int client, 
ds@1952
   145
		char *buffer, 
ds@1952
   146
		size_t maxlength, 
ds@1952
   147
		const char *phrase, 
ds@1952
   148
		void **params, 
ds@1952
   149
		size_t *outlen=NULL);
ds@1952
   150
	TransError CoreTrans(int client, 
ds@1952
   151
						 char *buffer, 
ds@1952
   152
						 size_t maxlength, 
ds@1952
   153
						 const char *phrase, 
ds@1952
   154
						 void **params, 
ds@1952
   155
						 size_t *outlen=NULL);
ds@1952
   156
	unsigned int GetServerLanguage();
ds@1952
   157
	unsigned int GetClientLanguage(int client);
ds@1952
   158
private:
ds@1952
   159
	bool AddLanguage(const char *langcode, const char *description);
ds@1952
   160
private:
ds@1952
   161
	CVector<Language *> m_Languages;
ds@1952
   162
	CVector<CPhraseFile *> m_Files;
ds@1952
   163
	BaseStringTable *m_pStringTab;
ds@1952
   164
	Trie *m_pLCodeLookup;
ds@1952
   165
	bool m_InLanguageSection;
ds@1952
   166
	String m_CustomError;
ds@1952
   167
	unsigned int m_ServerLang;
ds@1952
   168
	char m_InitialLang[3];
ds@1952
   169
};
ds@1952
   170
ds@1952
   171
extern CPhraseFile *g_pCorePhrases;
ds@1952
   172
extern unsigned int g_pCorePhraseID;
ds@1952
   173
extern Translator g_Translator;
ds@1952
   174
ds@1952
   175
#endif //_INCLUDE_SOURCEMOD_TRANSLATOR_H_