core/Translator.h
author David Anderson <dvander@alliedmods.net>
Wed May 02 22:35:51 2007 +0000 (2007-05-02)
changeset 727 fade10824d88
parent 673 fc1d50579d06
child 731 9c7c412be6a0
permissions -rw-r--r--
attempted fix for amb243, timers now simulate ticks if gameframe doesn't give changed curtimes
     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 
    27 /* :TODO: write a templatized version of tries? */
    28 
    29 using namespace SourceHook;
    30 class Translator;
    31 
    32 enum PhraseParseState
    33 {
    34 	PPS_None = 0,
    35 	PPS_Phrases,
    36 	PPS_InPhrase,
    37 };
    38 
    39 struct Language
    40 {
    41 	char m_code2[3];
    42 	int m_FullName;
    43 };
    44 
    45 struct Translation
    46 {
    47 	const char *szPhrase;		/**< Translated phrase. */
    48 	unsigned int fmt_count;		/**< Number of format parameters. */
    49 	int *fmt_order;				/**< Format phrase order. */
    50 };
    51 
    52 #define LANGUAGE_ENGLISH			0
    53 
    54 enum TransError
    55 {
    56 	Trans_Okay = 0,
    57 	Trans_BadLanguage = 1,
    58 	Trans_BadPhrase = 2,
    59 	Trans_BadPhraseLanguage = 3,
    60 	Trans_BadPhraseFile = 4,
    61 };
    62 
    63 class CPhraseFile : public ITextListener_SMC
    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 	void ReadSMC_ParseEnd(bool halted, bool failed);
    75 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
    76 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
    77 	SMCParseResult ReadSMC_LeavingSection();
    78 	SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline);
    79 private:
    80 	void ParseError(const char *message, ...);
    81 	void ParseWarning(const char *message, ...);
    82 private:
    83 	Trie *m_pPhraseLookup;
    84 	String m_File;
    85 	Translator *m_pTranslator;
    86 	PhraseParseState m_ParseState;
    87 	int m_CurPhrase;
    88 	BaseMemTable *m_pMemory;
    89 	BaseStringTable *m_pStringTab;
    90 	unsigned int m_LangCount;
    91 	String m_ParseError;
    92 	String m_LastPhraseString;
    93 	unsigned int m_CurLine;
    94 	bool m_FileLogged;
    95 };
    96 
    97 class Translator : 
    98 	public ITextListener_SMC,
    99 	public SMGlobalClass
   100 {
   101 public:
   102 	Translator();
   103 	~Translator();
   104 public: // SMGlobalClass
   105 	ConfigResult OnSourceModConfigChanged(const char *key, 
   106 		const char *value, 
   107 		ConfigSource source, 
   108 		char *error, 
   109 		size_t maxlength);
   110 	void OnSourceModAllInitialized();
   111 	void OnSourceModLevelChange(const char *mapName);
   112 public: // ITextListener_SMC
   113 	void ReadSMC_ParseStart();
   114 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
   115 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
   116 	SMCParseResult ReadSMC_LeavingSection();
   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 	size_t Translate(char *buffer, size_t maxlength, void **params, const Translation *pTrans);
   124 	CPhraseFile *GetFileByIndex(unsigned int index);
   125 	TransError CoreTrans(int client, 
   126 						 char *buffer, 
   127 						 size_t maxlength, 
   128 						 const char *phrase, 
   129 						 void **params, 
   130 						 size_t *outlen=NULL);
   131 	unsigned int GetServerLanguage();
   132 private:
   133 	bool AddLanguage(const char *langcode, const char *description);
   134 private:
   135 	CVector<Language *> m_Languages;
   136 	CVector<CPhraseFile *> m_Files;
   137 	BaseStringTable *m_pStringTab;
   138 	Trie *m_pLCodeLookup;
   139 	bool m_InLanguageSection;
   140 	String m_CustomError;
   141 	unsigned int m_ServerLang;
   142 	char m_InitialLang[3];
   143 };
   144 
   145 extern CPhraseFile *g_pCorePhrases;
   146 extern Translator g_Translator;
   147 
   148 #endif //_INCLUDE_SOURCEMOD_TRANSLATOR_H_