All pastes #412900 Raw Edit

YSI_text.own

public cpp v1 · immutable
#412900 ·published 2007-03-28 00:44 UTC
rendered paste body
/*----------------------------------------------------------------------------*-YAdmin system - Language Text CoreProvides interfaces for displaying text from anywhere by way of native likefunctions using text indexes rather than text.Changelog:23/03/07:First version.-*----------------------------------------------------------------------------*/#define MAX_TEXT 128#define MAX_TEXT_NAME 32#define MAX_TEXT_ENTRY 80#define TEXT_NO_TEXT -1#define TEXT_NO_POINTERS BINTREE_NOT_FOUND#define Text_RegisterTag(%1) \	forward Text_Tag_%1(identifier[], text[]); \	public Text_Tag_%1(identifier[], text[]) Text_AddToBuffer(identifier, text)forward Text_ResetAll();forward Text_NewLanguage(Language:languageID);forward Text_Parse();enum E_TEXT_POINTERS{	E_TEXT_POINTERS_NAME[MAX_TEXT_NAME],	E_TEXT_POINTERS_POINTER[MAX_LANGUAGES]}static	g_sTextTable[MAX_LANGUAGES][MAX_TEXT][MAX_TEXT_ENTRY],	g_sNameTable[MAX_TEXT][E_TEXT_POINTERS],	Bintree:g_sSearchTree[MAX_TEXT][E_BINTREE_TREE],	g_sTextInited,	g_sBufferIndex,	g_sTextCount,	Language:g_sBufferLang;/*----------------------------------------------------------------------------*-Function:	Text_TextParams:	-Return:	-Notes:	Initalises all the arrays and datas.-*----------------------------------------------------------------------------*/Text_Text(){	for (new i = 0; i < MAX_TEXT; i++)	{		for (new Language:j; j < MAX_LANGUAGES; j++)		{			g_sTextTable[j][i] = "\x01";			g_sNameTable[i][E_TEXT_POINTERS_POINTER][j] = TEXT_NO_TEXT;		}		g_sNameTable[i][E_TEXT_POINTERS_NAME][0] = 1; // = "\x01";		g_sNameTable[i][E_TEXT_POINTERS_NAME][1] = 0;	}	Text_ResetAll();}/*----------------------------------------------------------------------------*-Function:	Text_SetLangPointerParams:	index - The index of the text pointer you want to setup.	Language:languageid - The index of the language you want to setup for.	pointer - The pointer to the text for this identifier in this language.Return:	-Notes:	--*----------------------------------------------------------------------------*/#define Text_SetLangPointer(%1,%2,%3) \	g_sNameTable[%1][E_TEXT_POINTERS_POINTER][%2] = %3/*----------------------------------------------------------------------------*-Function:	FindTextPointersParams:	data[] - The textual identifier of the string we want to find.Return:	A pointer to the array of pointers to get the language specific text.	TEXT_NO_POINTERS on fail.Notes:	Finds a matching hash, then checks the text too for collisions.-*----------------------------------------------------------------------------*/Text_FindTextPointers(data[]){	new		leaf = -1,		pointer,		value = bernstein(data);	while ((pointer = Bintree_FindValue(g_sSearchTree, value, leaf)) != BINTREE_NOT_FOUND)	{		if (!strcmp(g_sNameTable[pointer][E_TEXT_POINTERS_NAME], data)) return pointer;	}	return TEXT_NO_POINTERS;}	/*----------------------------------------------------------------------------*-Function:	Text_AddToBufferParams:	Language:languageID - Language index.	identifier[] - The textual identifier of the string we're saving.	text[] - The text we're saving.Return:	-Notes:	This function saves the passed data into the arrays.  The text is just	dumped in anywhere for speed, the identifier MAY be dumped if the tree	doesn't exist yet, otherwise the pointers are just updated for the new	text.-*----------------------------------------------------------------------------*/Text_AddToBuffer(identifier[], text[]){	new		pos;	if (g_sTextInited && (pos = Text_FindTextPointers(identifier)) != TEXT_NO_POINTERS)	{		Text_SetLangPointer(pos, g_sBufferLang, g_sBufferIndex);	}	else	{		for (new Language:i; i < MAX_LANGUAGES; i++)		{			Text_SetLangPointer(g_sBufferIndex, i, TEXT_NO_TEXT);		}		Text_SetLangPointer(g_sBufferIndex, g_sBufferLang, g_sBufferIndex);		strcpy(g_sNameTable[g_sBufferIndex][E_TEXT_POINTERS_NAME], identifier);		pos = g_sBufferIndex;		g_sTextCount++;	}	strcpy(g_sTextTable[g_sBufferLang][g_sBufferIndex], text);	g_sBufferIndex++;}/*----------------------------------------------------------------------------*-Function:	Text_ResetAllParams:	-Return:	-Notes:	Resets all the data, including trees.  Called from the language loader.-*----------------------------------------------------------------------------*/public Text_ResetAll(){	g_sTextInited = 0;	g_sTextCount = 0;	Bintree_Reset(g_sSearchTree, MAX_TEXT);}/*----------------------------------------------------------------------------*-Function:	Text_NewLanguageParams:	-Return:	-Notes:	Sets everything up to input a new language into the system (not hard tbh).-*----------------------------------------------------------------------------*/public Text_NewLanguage(Language:languageID){	g_sBufferIndex = 0;	g_sBufferLang = languageID;}/*----------------------------------------------------------------------------*-Function:	Text_ParseParams:	-Return:	-Notes:	This function sorts the data in the buffer (entered since Text_NewLanguage	was last called), into the binary tree system.  The tree system is:		Bintree:g_sSearchTree[MAX_TEXT][E_BINTREE_TREE]	This contains the hashes of the text identifier for fast searching		g_sNameTable[MAX_TEXT][E_TEXT_POINTERS]	This is indexed by g_sSearchTree, it contains the textual identifier for	collision checking and the pointers for all the languages		g_sTextTable[MAX_LANGUAGES][MAX_TEXT][MAX_TEXT_ENTRY]	This contains all the entries for each language in no particular order,	they're saved as they're sent.  There are multiple indexes in g_sNameTable	into this, depending on the language.		All this is determined here.  If a language has been loaded already the	existing binary tree is used and data inserted at read, otherwise it is	generated here.		This setup allows very fast and safe loading of text strings for display,	flexibility in that adding new text and using it is very simple, text	entries only exist once in the system, even identifiers, through careful	data layout planning and means it is very simple to load languages after	the first has been loaded and the tree generated.-*----------------------------------------------------------------------------*/public Text_Parse(){	if (!g_sTextInited)	{		new			data[MAX_TEXT][E_BINTREE_INPUT];		for (new i = 0; i < g_sBufferIndex; i++)		{			data[i][E_BINTREE_INPUT_VALUE] = bernstein(g_sNameTable[i][E_TEXT_POINTERS_NAME]);			data[i][E_BINTREE_INPUT_POINTER] = i;		}		Bintree_Generate(g_sSearchTree, data, g_sBufferIndex);		g_sTextInited = 1;		g_sTextCount = g_sBufferIndex;	}}#define Text_GetPlayerLanguage(%1) \	Language:CallRemoteFunction("Langs_GetPlayerLanguage", "i", (%1))/*Text_GetText(identifier[], Language:languageID){	return Text_GetTextFromIndex(Text_FindTextPointers(identifier), languageID);}*/#define Text_GetPlayerText(%1,%2) \	Text_GetTextFromIndex(Text_FindTextPointers(%1), Text_GetPlayerLanguage(%2))/*Text_GetPlayerText(identifier[], playerid){	return Text_GetTextFromIndex(Text_FindTextPointers(identifier), Text_GetPlayerLanguage(playerid));}*/#define Text_GetText(%1,%2) \	Text_GetTextFromIndex(Text_FindTextPointers((%1)), %2)Text_GetTextFromIndex(index, Language:languageID){	if (index == TEXT_NO_POINTERS) return Text_Error();	index = g_sNameTable[index][E_TEXT_POINTERS_POINTER][languageID];	if (index == TEXT_NO_TEXT) return Text_Error();	return g_sTextTable[languageID][index];}Text_Error(){	new		ret[MAX_TEXT_ENTRY] = "-- Text not found -- ";	return ret;}Text_GetTextLinear(identifier[], Language:languageID){	new		ret[MAX_TEXT_ENTRY];	for (new i = 0; i < MAX_TEXT; i++)	{		if (!strcmp(g_sNameTable[i][E_TEXT_POINTERS_NAME], identifier))		{			new				index = g_sNameTable[i][E_TEXT_POINTERS_POINTER][languageID];			if (index != TEXT_NO_TEXT) return g_sTextTable[languageID][index];		}	}	return ret;}#define SendClientMessage2(%1,%2,%3) \	SendClientMessage((%1), (%2), Text_GetPlayerText((%3), (%1)))SendClientMessageToAll2(color, message[]){	new		messageid;	if ((messageid = Text_FindTextPointers(message)) == TEXT_NO_POINTERS) return;	for (new i = 0; i < MAX_PLAYERS; i++) if (IsPlayerConnected(i)) SendClientMessage(i, color, Text_GetTextFromIndex(messageid, Text_GetPlayerLanguage(i)));}#define GameTextForPlayer2(%1,%2,%3,%4) \	GameTextForPlayer((%1), Text_GetPlayerText((%2), (%1)), %3, %4)GameTextForAll2(message[], time, style){	new		messageid = Text_FindTextPointers(message);	for (new i = 0; i < MAX_PLAYERS; i++) if (IsPlayerConnected(i)) GameTextForPlayer(i, Text_GetTextFromIndex(messageid, Text_GetPlayerLanguage(i)), time, style);}// format2(output[], len, language, identifier[], {Float,_}:...);#define format2(%1,%2,%3,%4,%5) \	format((%1), (%2), Text_GetText((%4), (%3)), %5)