using System; using System.Text; using System.Collections; using Microsoft.VisualBasic; using System.Data; 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) { langExt = langExtension; langStrings = new Hashtable(); Out.WriteLine("Initializing strings from system_strings table for language '" + langExtension + "' ..."); DataTable table; using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient())) table = dbClient.ReadDataTable("SELECT stringid,var_" + langExt + " FROM system_strings ORDER BY id ASC"); foreach (DataRow row in table.Rows) { if (row[0].ToString() == "") continue; if (row[1].ToString() == "") row[1] = row[0]; langStrings.Add(row[0], row[1]); } 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."); } } /// /// Intializes/reloads the word filter, which filters swearwords etc from texts. /// public static void initFilter() { if (Config.getTableEntry("wordfilter_enable") == "1") { Out.WriteLine("Initializing word filter..."); using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient())) { DataColumn column = dbClient.ReadDataColumn("SELECT word FROM wordfilter"); swearWords = new string[column.Table.Rows.Count]; for (int i = 0; i < column.Table.Rows.Count; i++) swearWords[i] = column.Table.Rows[i][0].ToString(); } 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."); } } /// /// 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 ""; } } } }