/// <summary> /// Try to execute the replacement action. /// </summary> /// <param name="input">stream input</param> /// <param name="context">Input context</param> /// <returns> /// <para> /// A <see cref="ReplaceResult"/> reference that contains the result of the operation, to check if the operation is correct, the <b>Success</b> /// property will be <b>true</b> and the <b>Value</b> property will contain the value; Otherwise, the the <b>Success</b> property /// will be false and the <b>Errors</b> property will contain the errors associated with the operation, if they have been filled in. /// </para> /// <para> /// The type of the return value is <see cref="ReplaceResultData"/>, which contains the operation result /// </para> /// </returns> public ReplaceResult Apply(Stream input, IInput context) => ReplacementObject.Apply(input, context);
/// <summary>Loads the available compatibility object database</summary> /// <param name="fileName">The database file</param> internal static void LoadCompatibilityObjects(string fileName) { if (!System.IO.File.Exists(fileName)) { return; } string d = System.IO.Path.GetDirectoryName(fileName); XmlDocument currentXML = new XmlDocument(); try { currentXML.Load(fileName); } catch { return; } //Check for null if (currentXML.DocumentElement != null) { XmlNodeList DocumentNodes = currentXML.DocumentElement.SelectNodes("/openBVE/Compatibility/Object"); //Check this file actually contains OpenBVE compatibility nodes if (DocumentNodes != null) { foreach (XmlNode n in DocumentNodes) { if (n.ChildNodes.OfType <XmlElement>().Any()) { ReplacementObject o = new ReplacementObject(); string[] names = null; foreach (XmlNode c in n.ChildNodes) { switch (c.Name.ToLowerInvariant()) { case "name": if (c.InnerText.IndexOf(';') == -1) { names = new[] { c.InnerText }; } else { names = c.InnerText.Split(';'); } break; case "path": string f = OpenBveApi.Path.CombineFile(d, c.InnerText.Trim()); if (System.IO.File.Exists(f)) { o.ReplacementPath = f; } break; case "message": o.Message = c.InnerText.Trim(); break; default: Plugin.CurrentHost.AddMessage(MessageType.Warning, false, "Unexpected entry " + c.Name + " found in compatability object XML " + fileName); break; } } if (names != null) { o.ObjectNames = names; if (o.ReplacementPath != string.Empty) { int i = AvailableReplacements.Length; Array.Resize(ref AvailableReplacements, i + 1); AvailableReplacements[i] = o; } } } } DocumentNodes = currentXML.DocumentElement.SelectNodes("/openBVE/Compatibility/Sound"); //Check this file actually contains OpenBVE compatibility nodes if (DocumentNodes != null) { foreach (XmlNode n in DocumentNodes) { if (n.ChildNodes.OfType <XmlElement>().Any()) { ReplacementObject o = new ReplacementObject(); string[] names = null; foreach (XmlNode c in n.ChildNodes) { switch (c.Name.ToLowerInvariant()) { case "name": if (c.InnerText.IndexOf(';') == -1) { names = new[] { c.InnerText }; } else { names = c.InnerText.Split(';'); } break; case "path": string f = OpenBveApi.Path.CombineFile(d, c.InnerText.Trim()); if (System.IO.File.Exists(f)) { o.ReplacementPath = f; } break; case "message": o.Message = c.InnerText.Trim(); break; default: Plugin.CurrentHost.AddMessage(MessageType.Warning, false, "Unexpected entry " + c.Name + " found in compatability object XML " + fileName); break; } } if (names != null) { o.ObjectNames = names; if (o.ReplacementPath != string.Empty) { int i = AvailableSounds.Length; Array.Resize(ref AvailableSounds, i + 1); AvailableSounds[i] = o; } } } } } //Now try and load any object list XML files this references DocumentNodes = currentXML.DocumentElement.SelectNodes("/openBVE/Compatibility/ObjectList"); if (DocumentNodes != null) { foreach (XmlNode n in DocumentNodes) { if (n.ChildNodes.OfType <XmlElement>().Any()) { foreach (XmlNode c in n.ChildNodes) { switch (c.Name.ToLowerInvariant()) { case "filename": var f = c.InnerText.Trim(); if (!System.IO.File.Exists(f)) { try { f = OpenBveApi.Path.CombineFile(d, f); } catch { //Deliberately suppress all errors } } LoadCompatibilityObjects(f); break; default: Plugin.CurrentHost.AddMessage(MessageType.Warning, false, "Unexpected entry " + c.Name + " found in compatability XML list " + fileName); break; } } } } } } } }
/// <summary> /// Gets the object used for i18n token replacements /// </summary> /// <param name="questId">The quest ID - used to get the quest type</param> /// <returns> /// A generic object in the following format: /// - person - the npc name (translated) /// - otherPerson - a second npc name (translated) /// - englishPerson - untranslated npc name (used for quest keys) /// - itemName - the translated item name you will need to get for the quest - empty string if nothing /// - cropStart - the first 4 characters of the crop name - empty string if not a crop /// - id - the id of the item you need to get - 0 if nothing /// - article - "a" or "an" - currently only used for English replacements /// - number - random number between 2 and 10; used to determine how many of an item you need to get /// - reward - the money reward for a quest - between 300 and 3000 /// </returns> private static object GetTokenObject(int questId) { ReplacementObject replacements = new ReplacementObject(); string itemName = ""; string cropStart = ""; string article = ""; int id = 0; QuestItemTypes questType = QuestIdToQuestTypeMap[questId]; switch (questType) { case QuestItemTypes.Static: switch (questId) { case 3: itemName = ItemList.GetItemName((int)ObjectIndexes.Beet); break; case 6: itemName = ItemList.GetItemName(ParsnipCropId); id = ParsnipCropId; break; case 22: itemName = ItemList.GetItemName((int)ObjectIndexes.LargemouthBass); break; default: Globals.ConsoleError($"In the static quest type for unexpected quest: {questId}"); break; } break; case QuestItemTypes.Crop: itemName = replacements.Crop.DisplayName; cropStart = Globals.GetStringStart(itemName, 4); id = replacements.Crop.Id; break; case QuestItemTypes.Dish: itemName = replacements.Dish.DisplayName; id = replacements.Crop.Id; break; case QuestItemTypes.Fish: itemName = replacements.Fish.DisplayName; id = replacements.Fish.Id; break; case QuestItemTypes.Item: itemName = replacements.Item.DisplayName; id = replacements.Item.Id; break; default: break; } article = Globals.GetArticle(itemName); return(new { person = Globals.GetTranslation($"{replacements.Person}-name"), otherPerson = Globals.GetTranslation($"{replacements.OtherPerson}-name"), englishPerson = replacements.Person, item = itemName, cropStart, id, a = article, number = replacements.Number, reward = replacements.Reward }); }