using System;
using System.Data;
using System.Text;
using System.Collections;
using System.Threading;
using Microsoft.VisualBasic;
using Ion.Storage;
namespace Holo.Managers
{
///
/// Provides functions for management and manipulation of string objects.
///
public static class stringManager
{
///
/// Contains the strings loaded from system_strings.
///
private static Hashtable langStrings;
///
/// Contains the array of swearwords to be filtered from chat etc, loaded from system_wordfilter.
///
private static string[] swearWords;
///
/// Swearwords in chat etc should be replaced by this censor.
///
private static string filterCensor;
///
/// The language extension to use for the emulator.
///
internal static string langExt;
///
/// Initializes the string manager with a certain language.
///
/// The language to use for the emulator, eg, 'en' for English.
public static void Init(string langExtension, bool Update)
{
langExt = langExtension;
langStrings = new Hashtable();
Out.WriteLine("Initializing strings from system_strings table for language '" + langExtension + "' ...");
string[] langKeys;
string[] langVars;
DataTable dTable;
using (DatabaseClient dbClient = Eucalypt.dbManager.GetClient())
{
dTable = dbClient.getTable("SELECT stringid, var_" + langExt + " FROM system_strings ORDER BY id ASC");
}
langKeys = new string[dTable.Rows.Count];
langVars = new string[dTable.Rows.Count];
foreach (DataRow dRow in dTable.Rows)
{
langStrings.Add(Convert.ToString(dRow["stringid"]), Convert.ToString(dRow["var_" + langExt]));
}
Out.WriteLine("Loaded " + langStrings.Count + " strings from system_strings table.");
if (Config.getTableEntry("welcomemessage_enable") == "1")
{
if (getString("welcomemessage_text") != "")
{
Config.enableWelcomeMessage = true;
Out.WriteLine("Welcome message enabled.");
}
else
Out.WriteLine("Welcome message was preferred as enabled, but has been left blank. Ignored.");
}
else
{
Out.WriteLine("Welcome message disabled.");
}
if (Update)
Thread.CurrentThread.Abort();
}
///
/// Intializes/reloads the word filter, which filters swearwords etc from texts.
///
public static void initFilter(bool Update)
{
if (Config.getTableEntry("wordfilter_enable") == "1")
{
Out.WriteLine("Initializing word filter...");
DataColumn dCol;
using (DatabaseClient dbClient = Eucalypt.dbManager.GetClient())
{
dCol = dbClient.getColumn("SELECT word FROM wordfilter");
}
swearWords = dataHandling.dColToArray(dCol);
filterCensor = Config.getTableEntry("wordfilter_censor");
if (swearWords.Length == 0 || filterCensor == "")
Out.WriteLine("Word filter was preferred as enabled but no words and/or replacement found, wordfilter disabled.");
else
{
Config.enableWordFilter = true;
Out.WriteLine("Word filter enabled, " + swearWords.Length + " word(s) found, replacement: " + filterCensor);
}
}
else
{
Out.WriteLine("Word filter disabled.");
}
if (Update)
Thread.CurrentThread.Abort();
}
///
/// Retrieves a system_strings entry for a certain key. The strings are loaded at the initialization of the string manager.
///
/// The key of the string to retrieve.
public static string getString(string stringID)
{
try { return langStrings[stringID].ToString(); }
catch { return stringID; }
}
///
/// Filters the swearwords in an input string and replaces them by the set censor.
///
/// The string to filter.
public static string filterSwearwords(string Text)
{
if (Config.enableWordFilter)
{
for (int i = 0; i < swearWords.Length; i++)
Text = Strings.Replace(Text, swearWords[i], filterCensor, 1, -1, Constants.vbTextCompare);
}
return Text;
}
///
/// Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length. If any error occurs, then "" is returned.
///
/// The input string.
/// The zero-based starting character position of a substring in this instance.
/// The number of characters in the substring.
public static string getStringPart(string Input, int startIndex, int Length)
{
try { return Input.Substring(startIndex, Length); }
catch { return ""; }
}
///
/// Wraps a string array of parameters to one string, separated by spaces.
///
/// The string arrays with parameters.
/// The parameter ID in the array to start off with. Parameters with lower IDs won't be included.
public static string wrapParameters(string[] s, int startIndex)
{
StringBuilder sb = new StringBuilder();
//try
{
for (int i = startIndex; i < s.Length; i++)
sb.Append(" " + s[i]);
return sb.ToString().Substring(1);
}
//catch { return ""; }
}
}
}