core/Translator.h
author David Anderson <dvander@alliedmods.net>
Tue Sep 11 18:08:49 2007 +0000 (2007-09-11)
changeset 1408 2fd63cc4bd9c
parent 1352 4241c756c278
child 1621 ac5d4c68a99a
permissions -rw-r--r--
timelimit function now gets initialized
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 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 #ifndef _INCLUDE_SOURCEMOD_TRANSLATOR_H_
    33 #define _INCLUDE_SOURCEMOD_TRANSLATOR_H_
    34 
    35 #include "sm_trie.h"
    36 #include <sh_string.h>
    37 #include <sh_vector.h>
    38 #include "sm_globals.h"
    39 #include "sm_memtable.h"
    40 #include "ITextParsers.h"
    41 
    42 #define MAX_TRANSLATE_PARAMS		32
    43 #define CORELANG_ENGLISH			0
    44 
    45 /* :TODO: write a templatized version of tries? */
    46 
    47 using namespace SourceHook;
    48 class Translator;
    49 
    50 enum PhraseParseState
    51 {
    52 	PPS_None = 0,
    53 	PPS_Phrases,
    54 	PPS_InPhrase,
    55 };
    56 
    57 struct Language
    58 {
    59 	char m_code2[3];
    60 	int m_FullName;
    61 };
    62 
    63 struct Translation
    64 {
    65 	const char *szPhrase;		/**< Translated phrase. */
    66 	unsigned int fmt_count;		/**< Number of format parameters. */
    67 	int *fmt_order;				/**< Format phrase order. */
    68 };
    69 
    70 #define LANGUAGE_ENGLISH			0
    71 
    72 enum TransError
    73 {
    74 	Trans_Okay = 0,
    75 	Trans_BadLanguage = 1,
    76 	Trans_BadPhrase = 2,
    77 	Trans_BadPhraseLanguage = 3,
    78 	Trans_BadPhraseFile = 4,
    79 };
    80 
    81 class CPhraseFile : public ITextListener_SMC
    82 {
    83 public:
    84 	CPhraseFile(Translator *pTranslator, const char *file);
    85 	~CPhraseFile();
    86 public:
    87 	void ReparseFile();
    88 	const char *GetFilename();
    89 	TransError GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans);
    90 public: //ITextListener_SMC
    91 	void ReadSMC_ParseStart();
    92 	void ReadSMC_ParseEnd(bool halted, bool failed);
    93 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
    94 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
    95 	SMCParseResult ReadSMC_LeavingSection();
    96 	SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline);
    97 private:
    98 	void ParseError(const char *message, ...);
    99 	void ParseWarning(const char *message, ...);
   100 private:
   101 	Trie *m_pPhraseLookup;
   102 	String m_File;
   103 	Translator *m_pTranslator;
   104 	PhraseParseState m_ParseState;
   105 	int m_CurPhrase;
   106 	BaseMemTable *m_pMemory;
   107 	BaseStringTable *m_pStringTab;
   108 	unsigned int m_LangCount;
   109 	String m_ParseError;
   110 	String m_LastPhraseString;
   111 	unsigned int m_CurLine;
   112 	bool m_FileLogged;
   113 };
   114 
   115 class Translator : 
   116 	public ITextListener_SMC,
   117 	public SMGlobalClass
   118 {
   119 public:
   120 	Translator();
   121 	~Translator();
   122 public: // SMGlobalClass
   123 	ConfigResult OnSourceModConfigChanged(const char *key, 
   124 		const char *value, 
   125 		ConfigSource source, 
   126 		char *error, 
   127 		size_t maxlength);
   128 	void OnSourceModAllInitialized();
   129 	void OnSourceModLevelChange(const char *mapName);
   130 public: // ITextListener_SMC
   131 	void ReadSMC_ParseStart();
   132 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
   133 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
   134 	SMCParseResult ReadSMC_LeavingSection();
   135 public:
   136 	void RebuildLanguageDatabase(const char *lang_header_file);
   137 	unsigned int FindOrAddPhraseFile(const char *phrase_file);
   138 	BaseStringTable *GetStringTable();
   139 	unsigned int GetLanguageCount();
   140 	bool GetLanguageInfo(unsigned int number, const char **code, const char **name);
   141 	bool GetLanguageByCode(const char *code, unsigned int *index);
   142 	bool GetLanguageByName(const char *name, unsigned int *index);
   143 	size_t Translate(char *buffer, size_t maxlength, void **params, const Translation *pTrans);
   144 	CPhraseFile *GetFileByIndex(unsigned int index);
   145 	TransError CoreTrans(int client, 
   146 						 char *buffer, 
   147 						 size_t maxlength, 
   148 						 const char *phrase, 
   149 						 void **params, 
   150 						 size_t *outlen=NULL);
   151 	unsigned int GetServerLanguage();
   152 	unsigned int GetClientLanguage(int client);
   153 private:
   154 	bool AddLanguage(const char *langcode, const char *description);
   155 private:
   156 	CVector<Language *> m_Languages;
   157 	CVector<CPhraseFile *> m_Files;
   158 	BaseStringTable *m_pStringTab;
   159 	Trie *m_pLCodeLookup;
   160 	bool m_InLanguageSection;
   161 	String m_CustomError;
   162 	unsigned int m_ServerLang;
   163 	char m_InitialLang[3];
   164 };
   165 
   166 extern CPhraseFile *g_pCorePhrases;
   167 extern Translator g_Translator;
   168 
   169 #endif //_INCLUDE_SOURCEMOD_TRANSLATOR_H_