core/Translator.h
author Borja Ferrer <faluco@alliedmods.net>
Sat Feb 23 20:18:49 2008 +0000 (2008-02-23)
changeset 1894 e6efc8ffa47a
parent 1656 b0b86d57530c
child 1925 61d7a244b641
permissions -rw-r--r--
Executable memory is now provided by MM:S's allocator, this should reduce virtual memory usage.
     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 	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
    93 	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
    94 	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
    95 	void ReadSMC_ParseEnd(bool halted, bool failed);
    96 private:
    97 	void ParseError(const char *message, ...);
    98 	void ParseWarning(const char *message, ...);
    99 private:
   100 	Trie *m_pPhraseLookup;
   101 	String m_File;
   102 	Translator *m_pTranslator;
   103 	PhraseParseState m_ParseState;
   104 	int m_CurPhrase;
   105 	BaseMemTable *m_pMemory;
   106 	BaseStringTable *m_pStringTab;
   107 	unsigned int m_LangCount;
   108 	String m_ParseError;
   109 	String m_LastPhraseString;
   110 	bool m_FileLogged;
   111 };
   112 
   113 class Translator : 
   114 	public ITextListener_SMC,
   115 	public SMGlobalClass
   116 {
   117 public:
   118 	Translator();
   119 	~Translator();
   120 public: // SMGlobalClass
   121 	ConfigResult OnSourceModConfigChanged(const char *key, 
   122 		const char *value, 
   123 		ConfigSource source, 
   124 		char *error, 
   125 		size_t maxlength);
   126 	void OnSourceModAllInitialized();
   127 	void OnSourceModLevelChange(const char *mapName);
   128 public: // ITextListener_SMC
   129 	void ReadSMC_ParseStart();
   130 	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
   131 	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
   132 	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
   133 public:
   134 	void RebuildLanguageDatabase(const char *lang_header_file);
   135 	unsigned int FindOrAddPhraseFile(const char *phrase_file);
   136 	BaseStringTable *GetStringTable();
   137 	unsigned int GetLanguageCount();
   138 	bool GetLanguageInfo(unsigned int number, const char **code, const char **name);
   139 	bool GetLanguageByCode(const char *code, unsigned int *index);
   140 	bool GetLanguageByName(const char *name, unsigned int *index);
   141 	size_t Translate(char *buffer, size_t maxlength, void **params, const Translation *pTrans);
   142 	CPhraseFile *GetFileByIndex(unsigned int index);
   143 	TransError CoreTrans(int client, 
   144 						 char *buffer, 
   145 						 size_t maxlength, 
   146 						 const char *phrase, 
   147 						 void **params, 
   148 						 size_t *outlen=NULL);
   149 	unsigned int GetServerLanguage();
   150 	unsigned int GetClientLanguage(int client);
   151 private:
   152 	bool AddLanguage(const char *langcode, const char *description);
   153 private:
   154 	CVector<Language *> m_Languages;
   155 	CVector<CPhraseFile *> m_Files;
   156 	BaseStringTable *m_pStringTab;
   157 	Trie *m_pLCodeLookup;
   158 	bool m_InLanguageSection;
   159 	String m_CustomError;
   160 	unsigned int m_ServerLang;
   161 	char m_InitialLang[3];
   162 };
   163 
   164 extern CPhraseFile *g_pCorePhrases;
   165 extern unsigned int g_pCorePhraseID;
   166 extern Translator g_Translator;
   167 
   168 #endif //_INCLUDE_SOURCEMOD_TRANSLATOR_H_