core/Translator.h
author David Anderson <dvander@alliedmods.net>
Fri Jun 08 01:09:17 2007 +0000 (2007-06-08)
changeset 922 980941c525fa
parent 732 13f3a417a340
child 1105 ce907ba50921
permissions -rw-r--r--
sm_kick now uses targeting properly
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #ifndef _INCLUDE_SOURCEMOD_TRANSLATOR_H_
    16 #define _INCLUDE_SOURCEMOD_TRANSLATOR_H_
    17 
    18 #include "sm_trie.h"
    19 #include <sh_string.h>
    20 #include <sh_vector.h>
    21 #include "sm_globals.h"
    22 #include "sm_memtable.h"
    23 #include "ITextParsers.h"
    24 
    25 #define MAX_TRANSLATE_PARAMS		32
    26 #define CORELANG_ENGLISH			0
    27 
    28 /* :TODO: write a templatized version of tries? */
    29 
    30 using namespace SourceHook;
    31 class Translator;
    32 
    33 enum PhraseParseState
    34 {
    35 	PPS_None = 0,
    36 	PPS_Phrases,
    37 	PPS_InPhrase,
    38 };
    39 
    40 struct Language
    41 {
    42 	char m_code2[3];
    43 	int m_FullName;
    44 };
    45 
    46 struct Translation
    47 {
    48 	const char *szPhrase;		/**< Translated phrase. */
    49 	unsigned int fmt_count;		/**< Number of format parameters. */
    50 	int *fmt_order;				/**< Format phrase order. */
    51 };
    52 
    53 #define LANGUAGE_ENGLISH			0
    54 
    55 enum TransError
    56 {
    57 	Trans_Okay = 0,
    58 	Trans_BadLanguage = 1,
    59 	Trans_BadPhrase = 2,
    60 	Trans_BadPhraseLanguage = 3,
    61 	Trans_BadPhraseFile = 4,
    62 };
    63 
    64 class CPhraseFile : public ITextListener_SMC
    65 {
    66 public:
    67 	CPhraseFile(Translator *pTranslator, const char *file);
    68 	~CPhraseFile();
    69 public:
    70 	void ReparseFile();
    71 	const char *GetFilename();
    72 	TransError GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans);
    73 public: //ITextListener_SMC
    74 	void ReadSMC_ParseStart();
    75 	void ReadSMC_ParseEnd(bool halted, bool failed);
    76 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
    77 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
    78 	SMCParseResult ReadSMC_LeavingSection();
    79 	SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline);
    80 private:
    81 	void ParseError(const char *message, ...);
    82 	void ParseWarning(const char *message, ...);
    83 private:
    84 	Trie *m_pPhraseLookup;
    85 	String m_File;
    86 	Translator *m_pTranslator;
    87 	PhraseParseState m_ParseState;
    88 	int m_CurPhrase;
    89 	BaseMemTable *m_pMemory;
    90 	BaseStringTable *m_pStringTab;
    91 	unsigned int m_LangCount;
    92 	String m_ParseError;
    93 	String m_LastPhraseString;
    94 	unsigned int m_CurLine;
    95 	bool m_FileLogged;
    96 };
    97 
    98 class Translator : 
    99 	public ITextListener_SMC,
   100 	public SMGlobalClass
   101 {
   102 public:
   103 	Translator();
   104 	~Translator();
   105 public: // SMGlobalClass
   106 	ConfigResult OnSourceModConfigChanged(const char *key, 
   107 		const char *value, 
   108 		ConfigSource source, 
   109 		char *error, 
   110 		size_t maxlength);
   111 	void OnSourceModAllInitialized();
   112 	void OnSourceModLevelChange(const char *mapName);
   113 public: // ITextListener_SMC
   114 	void ReadSMC_ParseStart();
   115 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
   116 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
   117 	SMCParseResult ReadSMC_LeavingSection();
   118 public:
   119 	void RebuildLanguageDatabase(const char *lang_header_file);
   120 	unsigned int FindOrAddPhraseFile(const char *phrase_file);
   121 	BaseStringTable *GetStringTable();
   122 	unsigned int GetLanguageCount();
   123 	bool GetLanguageByCode(const char *code, unsigned int *index);
   124 	size_t Translate(char *buffer, size_t maxlength, void **params, const Translation *pTrans);
   125 	CPhraseFile *GetFileByIndex(unsigned int index);
   126 	TransError CoreTrans(int client, 
   127 						 char *buffer, 
   128 						 size_t maxlength, 
   129 						 const char *phrase, 
   130 						 void **params, 
   131 						 size_t *outlen=NULL);
   132 	unsigned int GetServerLanguage();
   133 private:
   134 	bool AddLanguage(const char *langcode, const char *description);
   135 private:
   136 	CVector<Language *> m_Languages;
   137 	CVector<CPhraseFile *> m_Files;
   138 	BaseStringTable *m_pStringTab;
   139 	Trie *m_pLCodeLookup;
   140 	bool m_InLanguageSection;
   141 	String m_CustomError;
   142 	unsigned int m_ServerLang;
   143 	char m_InitialLang[3];
   144 };
   145 
   146 extern CPhraseFile *g_pCorePhrases;
   147 extern Translator g_Translator;
   148 
   149 #endif //_INCLUDE_SOURCEMOD_TRANSLATOR_H_