core/logic/Translator.h
author Kyle Sanderson <kyle.leet@gmail.com>
Mon Aug 12 00:42:56 2013 +0100 (2013-08-12)
changeset 3994 4bc73bac0f4c
parent 3532 88a0d2c30bc3
child 4057 9f9f98843ca8
permissions -rw-r--r--
Added helper stock to check if a command exists. (bug 5510, r=asherkin)
     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 GetLanguageByCode(const char *code, unsigned int *index);
   123 	bool GetLanguageByName(const char *name, unsigned int *index);
   124 	CPhraseFile *GetFileByIndex(unsigned int index);
   125 public: //ITranslator
   126 	unsigned int GetServerLanguage();
   127 	unsigned int GetClientLanguage(int client);
   128 	const char *GetInterfaceName();
   129 	unsigned int GetInterfaceVersion();
   130 	IPhraseCollection *CreatePhraseCollection();
   131 	int SetGlobalTarget(int index);
   132 	int GetGlobalTarget() const;
   133 	size_t FormatString(
   134 		char *buffer, 
   135 		size_t maxlength, 
   136 		SourcePawn::IPluginContext *pContext,
   137 		const cell_t *params,
   138 		unsigned int param);
   139 	bool FormatString(
   140 		char *buffer,
   141 		size_t maxlength,
   142 		const char *format,
   143 		IPhraseCollection *pPhrases,
   144 		void **params,
   145 		unsigned int numparams,
   146 		size_t *pOutLength,
   147 		const char **pFailPhrase);
   148 	bool GetLanguageInfo(unsigned int number, const char **code, const char **name);
   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