core/logic/Translator.h
author Fyren <fyrenmoo@gmail.com>
Wed May 25 21:10:18 2011 -0400 (2011-05-25)
changeset 3291 1be149cf4e89
parent 3004 e3ac0dc6d9a4
child 3532 88a0d2c30bc3
permissions -rw-r--r--
Added support for language aliases (bug 4858, r=dvander).
     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 #ifndef _INCLUDE_SOURCEMOD_TRANSLATOR_H_
    33 #define _INCLUDE_SOURCEMOD_TRANSLATOR_H_
    34 
    35 #include "common_logic.h"
    36 #include <sm_trie_tpl.h>
    37 #include <sh_string.h>
    38 #include <sh_vector.h>
    39 #include "sm_memtable.h"
    40 #include "ITextParsers.h"
    41 #include <ITranslator.h>
    42 
    43 /* :TODO: write a templatized version of tries? */
    44 
    45 using namespace SourceHook;
    46 class Translator;
    47 
    48 enum PhraseParseState
    49 {
    50 	PPS_None = 0,
    51 	PPS_Phrases,
    52 	PPS_InPhrase,
    53 };
    54 
    55 struct Language
    56 {
    57 	char m_code2[4];
    58 	int m_CanonicalName;
    59 };
    60 
    61 class CPhraseFile : 
    62 	public ITextListener_SMC,
    63 	public IPhraseFile
    64 {
    65 public:
    66 	CPhraseFile(Translator *pTranslator, const char *file);
    67 	~CPhraseFile();
    68 public:
    69 	void ReparseFile();
    70 	const char *GetFilename();
    71 	TransError GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans);
    72 public: //ITextListener_SMC
    73 	void ReadSMC_ParseStart();
    74 	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
    75 	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
    76 	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
    77 	void ReadSMC_ParseEnd(bool halted, bool failed);
    78 private:
    79 	void ParseError(const char *message, ...);
    80 	void ParseWarning(const char *message, ...);
    81 private:
    82 	KTrie<int> m_PhraseLookup;
    83 	String m_File;
    84 	Translator *m_pTranslator;
    85 	PhraseParseState m_ParseState;
    86 	int m_CurPhrase;
    87 	BaseMemTable *m_pMemory;
    88 	BaseStringTable *m_pStringTab;
    89 	unsigned int m_LangCount;
    90 	String m_ParseError;
    91 	String m_LastPhraseString;
    92 	bool m_FileLogged;
    93 };
    94 
    95 class Translator : 
    96 	public ITextListener_SMC,
    97 	public SMGlobalClass,
    98 	public ITranslator
    99 {
   100 public:
   101 	Translator();
   102 	~Translator();
   103 public: // SMGlobalClass
   104 	ConfigResult OnSourceModConfigChanged(const char *key, 
   105 		const char *value, 
   106 		ConfigSource source, 
   107 		char *error, 
   108 		size_t maxlength);
   109 	void OnSourceModAllInitialized();
   110 	void OnSourceModLevelChange(const char *mapName);
   111 	void OnSourceModShutdown();
   112 public: // ITextListener_SMC
   113 	void ReadSMC_ParseStart();
   114 	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
   115 	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
   116 	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
   117 public:
   118 	void RebuildLanguageDatabase(const char *lang_header_file);
   119 	unsigned int FindOrAddPhraseFile(const char *phrase_file);
   120 	BaseStringTable *GetStringTable();
   121 	unsigned int GetLanguageCount();
   122 	bool GetLanguageInfo(unsigned int number, const char **code, const char **name);
   123 	bool GetLanguageByCode(const char *code, unsigned int *index);
   124 	bool GetLanguageByName(const char *name, unsigned int *index);
   125 	CPhraseFile *GetFileByIndex(unsigned int index);
   126 public: //ITranslator
   127 	unsigned int GetServerLanguage();
   128 	unsigned int GetClientLanguage(int client);
   129 	const char *GetInterfaceName();
   130 	unsigned int GetInterfaceVersion();
   131 	IPhraseCollection *CreatePhraseCollection();
   132 	int SetGlobalTarget(int index);
   133 	int GetGlobalTarget() const;
   134 	size_t FormatString(
   135 		char *buffer, 
   136 		size_t maxlength, 
   137 		SourcePawn::IPluginContext *pContext,
   138 		const cell_t *params,
   139 		unsigned int param);
   140 	bool FormatString(
   141 		char *buffer,
   142 		size_t maxlength,
   143 		const char *format,
   144 		IPhraseCollection *pPhrases,
   145 		void **params,
   146 		unsigned int numparams,
   147 		size_t *pOutLength,
   148 		const char **pFailPhrase);
   149 private:
   150 	bool AddLanguage(const char *langcode, const char *description);
   151 private:
   152 	CVector<Language *> m_Languages;
   153 	CVector<CPhraseFile *> m_Files;
   154 	BaseStringTable *m_pStringTab;
   155 	KTrie<unsigned int> m_LCodeLookup;
   156 	KTrie<unsigned int> m_LAliases;
   157 	bool m_InLanguageSection;
   158 	String m_CustomError;
   159 	unsigned int m_ServerLang;
   160 	char m_InitialLang[4];
   161 };
   162 
   163 /* Nice little wrapper to handle error logging and whatnot */
   164 bool CoreTranslate(char *buffer, 
   165 				   size_t maxlength,
   166 				   const char *format,
   167 				   unsigned int numparams,
   168 				   size_t *pOutLength,
   169 				   ...);
   170 
   171 extern IPhraseCollection *g_pCorePhrases;
   172 extern unsigned int g_pCorePhraseID;
   173 extern Translator g_Translator;
   174 
   175 #endif //_INCLUDE_SOURCEMOD_TRANSLATOR_H_
   176