core/Translator.h
author David Anderson <dvander@alliedmods.net>
Mon Sep 29 20:02:19 2008 -0700 (2008-09-29)
changeset 2335 cf312a67fb8f
parent 2289 44045ead61cd
child 2478 1899f97aded6
permissions -rw-r--r--
Fixed OnConfigsExecuted() not working on listen servers (bug 3287, r=me).
     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 "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 #include <ITranslator.h>
    42 
    43 #define MAX_TRANSLATE_PARAMS		32
    44 #define CORELANG_ENGLISH			0
    45 
    46 /* :TODO: write a templatized version of tries? */
    47 
    48 using namespace SourceHook;
    49 class Translator;
    50 
    51 enum PhraseParseState
    52 {
    53 	PPS_None = 0,
    54 	PPS_Phrases,
    55 	PPS_InPhrase,
    56 };
    57 
    58 struct Language
    59 {
    60 	char m_code2[3];
    61 	int m_FullName;
    62 };
    63 
    64 class CPhraseFile : 
    65 	public ITextListener_SMC,
    66 	public IPhraseFile
    67 {
    68 public:
    69 	CPhraseFile(Translator *pTranslator, const char *file);
    70 	~CPhraseFile();
    71 public:
    72 	void ReparseFile();
    73 	const char *GetFilename();
    74 	TransError GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans);
    75 public: //ITextListener_SMC
    76 	void ReadSMC_ParseStart();
    77 	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
    78 	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
    79 	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
    80 	void ReadSMC_ParseEnd(bool halted, bool failed);
    81 private:
    82 	void ParseError(const char *message, ...);
    83 	void ParseWarning(const char *message, ...);
    84 private:
    85 	Trie *m_pPhraseLookup;
    86 	String m_File;
    87 	Translator *m_pTranslator;
    88 	PhraseParseState m_ParseState;
    89 	int m_CurPhrase;
    90 	BaseMemTable *m_pMemory;
    91 	BaseStringTable *m_pStringTab;
    92 	unsigned int m_LangCount;
    93 	String m_ParseError;
    94 	String m_LastPhraseString;
    95 	bool m_FileLogged;
    96 };
    97 
    98 class Translator : 
    99 	public ITextListener_SMC,
   100 	public SMGlobalClass,
   101 	public ITranslator
   102 {
   103 public:
   104 	Translator();
   105 	~Translator();
   106 public: // SMGlobalClass
   107 	ConfigResult OnSourceModConfigChanged(const char *key, 
   108 		const char *value, 
   109 		ConfigSource source, 
   110 		char *error, 
   111 		size_t maxlength);
   112 	void OnSourceModAllInitialized();
   113 	void OnSourceModLevelChange(const char *mapName);
   114 	void OnSourceModShutdown();
   115 public: // ITextListener_SMC
   116 	void ReadSMC_ParseStart();
   117 	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
   118 	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
   119 	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
   120 public:
   121 	void RebuildLanguageDatabase(const char *lang_header_file);
   122 	unsigned int FindOrAddPhraseFile(const char *phrase_file);
   123 	BaseStringTable *GetStringTable();
   124 	unsigned int GetLanguageCount();
   125 	bool GetLanguageInfo(unsigned int number, const char **code, const char **name);
   126 	bool GetLanguageByCode(const char *code, unsigned int *index);
   127 	bool GetLanguageByName(const char *name, unsigned int *index);
   128 	CPhraseFile *GetFileByIndex(unsigned int index);
   129 public: //ITranslator
   130 	unsigned int GetServerLanguage();
   131 	unsigned int GetClientLanguage(int client);
   132 	const char *GetInterfaceName();
   133 	unsigned int GetInterfaceVersion();
   134 	IPhraseCollection *CreatePhraseCollection();
   135 	int SetGlobalTarget(int index);
   136 	int GetGlobalTarget() const;
   137 	size_t FormatString(
   138 		char *buffer, 
   139 		size_t maxlength, 
   140 		SourcePawn::IPluginContext *pContext,
   141 		const cell_t *params,
   142 		unsigned int param);
   143 	bool FormatString(
   144 		char *buffer,
   145 		size_t maxlength,
   146 		const char *format,
   147 		IPhraseCollection *pPhrases,
   148 		void **params,
   149 		unsigned int numparams,
   150 		size_t *pOutLength,
   151 		const char **pFailPhrase);
   152 private:
   153 	bool AddLanguage(const char *langcode, const char *description);
   154 private:
   155 	CVector<Language *> m_Languages;
   156 	CVector<CPhraseFile *> m_Files;
   157 	BaseStringTable *m_pStringTab;
   158 	Trie *m_pLCodeLookup;
   159 	bool m_InLanguageSection;
   160 	String m_CustomError;
   161 	unsigned int m_ServerLang;
   162 	char m_InitialLang[3];
   163 };
   164 
   165 /* Nice little wrapper to handle error logging and whatnot */
   166 bool CoreTranslate(char *buffer, 
   167 				   size_t maxlength,
   168 				   const char *format,
   169 				   unsigned int numparams,
   170 				   size_t *pOutLength,
   171 				   ...);
   172 
   173 extern IPhraseCollection *g_pCorePhrases;
   174 extern unsigned int g_pCorePhraseID;
   175 extern Translator g_Translator;
   176 
   177 #endif //_INCLUDE_SOURCEMOD_TRANSLATOR_H_