Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaShared/Source/TextManager.cs
Joonas Rikkonen 80ab27df22 c2e39cc...409d4d9
commit 409d4d96ead69028a164274637d23e350acb73fb
Merge: 95169f539 26e89c63d
Author: EdusFF <pitkanen.eetu@gmail.com>
Date:   Mon Mar 11 15:13:27 2019 +0200

    Merge branch 'dev' of github.com:Regalis11/Barotrauma into dev

commit 95169f53937f9a7e168a884171eaa21ae7f08023
Author: EdusFF <pitkanen.eetu@gmail.com>
Date:   Mon Mar 11 15:13:11 2019 +0200

    Modified: ServerMessage structure to allow _ ; in player & submarine names

commit 26e89c63dc8da771aea9f09978a630a6cff60a6f
Merge: b7646d06d fb0b821bc
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Mar 11 14:42:44 2019 +0200

    Merge branch 'kuraiookami-logicExpantion' into dev

    # Conflicts:
    #       Barotrauma/BarotraumaShared/SharedContent.projitems

commit fb0b821bc97891cdeec8f2c740a12119696393ea
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Mar 11 14:41:21 2019 +0200

    Use invariant culture when parsing floats or converting them to strings in signal components

commit f0c8afba934b41358cf5d59a22b87caf33f98a61
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Mar 11 14:00:44 2019 +0200

    Update new signal components to use identifiers & added names and descriptions to the text file, use invariant culture in equalscomponent, memorycomponent doesn't require the signals to be floats

commit 674d9ec804fc4770b602d4b09240b08cafc8ccec
Merge: 3ea33fb54 242e2429f
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Mar 11 12:01:27 2019 +0200

    Merge branch 'logicExpantion' of https://github.com/kuraiookami/Barotrauma into kuraiookami-logicExpantion

    # Conflicts:
    #       Barotrauma/BarotraumaShared/BarotraumaShared.projitems
    #       Barotrauma/BarotraumaShared/Content/Items/Electricity/poweritems.xml
    #       Barotrauma/BarotraumaShared/Content/Items/Electricity/signalitems.xml
    #       Barotrauma/BarotraumaShared/Source/Items/Components/Power/PowerContainer.cs
    #       Barotrauma/BarotraumaShared/Source/Items/Components/Signal/AdderComponent.cs

commit b7646d06d53fb05227276e6286d0e15da5dc9080
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Mar 11 11:37:33 2019 +0200

    Re-enabled multiplayer campaign

commit cf7258f6410a5995c881ec6e95eb9def5cd90ad4
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Mar 11 11:28:48 2019 +0200

    Fixed item interfaces getting repositioned every frame when the editing HUD is open. Closes #1212

commit e8906239c779cf71de694bc65c81058e5cae16ef
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Mar 11 11:12:05 2019 +0200

    Fixed VoipCapture creating new "could not start voice capture" popups constantly if there's no suitable capture device. Closes #1262

commit a30f47fbe47fde4fccb0453c1773a76d730d226b
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Sun Mar 10 19:04:59 2019 +0200

    Disable audio instead of crashing if no audio device is found. Closes #1214

commit 242e2429fd2c3ed199ac26b55e2cbdc8636e73f9
Author: Darkwolf <Darkwolf0101@gmail.com>
Date:   Mon Jan 21 21:26:57 2019 -0600

    Expansion of Barotrauma's logic system.

    Changed:
    - AdderComponent and children can clamp their output
    - Powercontainer signals for charge,charge% and charge rate

    Added:
    - ColorComponent: Dynamic signals for light set_color inputs
    - MemoryComponent: Stores and sends a signal that is edge latched
    - DivideComponent: Standard division
    - MultiplyComponent: Standard multiplication
    - SubtractComponent: Standard subtraction
    - XorComponent: Exclusive or
    - EqualsComponent: Equals comparison
    - GreaterComponent: Greater than comparison
2019-03-18 22:50:18 +02:00

314 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
namespace Barotrauma
{
public static class TextManager
{
//only used if none of the selected content packages contain any text files
const string VanillaTextFilePath = "Content/Texts/EnglishVanilla.xml";
//key = language
private static Dictionary<string, List<TextPack>> textPacks = new Dictionary<string, List<TextPack>>();
private static string[] serverMessageCharacters = new string[] { "~", "[", "]", "=" };
public static string Language;
private static HashSet<string> availableLanguages = new HashSet<string>();
public static IEnumerable<string> AvailableLanguages
{
get { return availableLanguages; }
}
public static List<string> GetTextFiles()
{
var list = new List<string>();
GetTextFilesRecursive(Path.Combine("Content", "Texts"), ref list);
return list;
}
private static void GetTextFilesRecursive(string directory, ref List<string> list)
{
foreach (string file in Directory.GetFiles(directory))
{
list.Add(file);
}
foreach (string subDir in Directory.GetDirectories(directory))
{
GetTextFilesRecursive(subDir, ref list);
}
}
public static void LoadTextPacks(IEnumerable<ContentPackage> selectedContentPackages)
{
availableLanguages.Clear();
textPacks.Clear();
var textFiles = ContentPackage.GetFilesOfType(selectedContentPackages, ContentType.Text);
foreach (string file in textFiles)
{
try
{
var textPack = new TextPack(file);
availableLanguages.Add(textPack.Language);
if (!textPacks.ContainsKey(textPack.Language))
{
textPacks.Add(textPack.Language, new List<TextPack>());
}
textPacks[textPack.Language].Add(textPack);
}
catch (Exception e)
{
DebugConsole.ThrowError("Failed to load text file \"" + file + "\"!", e);
}
}
if (textPacks.Count == 0)
{
DebugConsole.ThrowError("No text files available in any of the selected content packages. Attempting to find a vanilla English text file...");
if (!File.Exists(VanillaTextFilePath))
{
throw new Exception("No text files found in any of the selected content packages or in the default text path!");
}
var textPack = new TextPack(VanillaTextFilePath);
availableLanguages.Add(textPack.Language);
textPacks.Add(textPack.Language, new List<TextPack>() { textPack });
}
}
public static string Get(string textTag, bool returnNull = false)
{
if (!textPacks.ContainsKey(Language))
{
DebugConsole.ThrowError("No text packs available for the selected language (" + Language + ")! Switching to English...");
Language = "English";
if (!textPacks.ContainsKey(Language))
{
throw new Exception("No text packs available in English!");
}
}
foreach (TextPack textPack in textPacks[Language])
{
string text = textPack.Get(textTag);
if (text != null) return text;
}
//if text was not found and we're using a language other than English, see if we can find an English version
//may happen, for example, if a user has selected another language and using mods that haven't been translated to that language
if (Language != "English" && textPacks.ContainsKey("English"))
{
foreach (TextPack textPack in textPacks["English"])
{
string text = textPack.Get(textTag);
if (text != null) return text;
}
}
if (returnNull)
{
return null;
}
else
{
DebugConsole.ThrowError("Text \"" + textTag + "\" not found.");
return textTag;
}
}
public static string GetFormatted(string textTag, bool returnNull = false, params object[] args)
{
string text = Get(textTag, returnNull);
if (text == null || text.Length == 0)
{
if (returnNull)
{
return null;
}
else
{
DebugConsole.ThrowError("Text \"" + textTag + "\" not found.");
return textTag;
}
}
return string.Format(text, args);
}
// Format: ServerMessage.Identifier1/ServerMessage.Indentifier2~[variable1]=value~[variable2]=value
public static string GetServerMessage(string serverMessage)
{
if (serverMessage.Contains(" ")) return serverMessage; // Spaces found, do not translate
if (!textPacks.ContainsKey(Language))
{
DebugConsole.ThrowError("No text packs available for the selected language (" + Language + ")! Switching to English...");
Language = "English";
if (!textPacks.ContainsKey(Language))
{
throw new Exception("No text packs available in English!");
}
}
string[] messages = serverMessage.Split('/');
for (int i = 0; i < messages.Length; i++)
{
if (!IsServerMessageWithVariables(messages[i])) // No variables, try to translate
{
string msg = Get(messages[i], true);
if (msg != null) // If a translation was found, otherwise use the original
{
messages[i] = msg;
}
}
else
{
string[] messageWithVariables = messages[i].Split('~');
string msg = Get(messageWithVariables[0], true);
if (msg != null) // If a translation was found, otherwise use the original
{
messages[i] = msg;
}
else
{
continue; // No translation found, probably caused by player input -> skip variable handling
}
// First index is always the message identifier -> start at 1
for (int j = 1; j < messageWithVariables.Length; j++)
{
string[] variableAndValue = messageWithVariables[j].Split('=');
messages[i] = messages[i].Replace(variableAndValue[0], variableAndValue[1]);
}
}
}
string translatedServerMessage = string.Empty;
for (int i = 0; i < messages.Length; i++)
{
translatedServerMessage += messages[i];
}
return translatedServerMessage;
}
public static bool IsServerMessageWithVariables(string message)
{
for (int i = 0; i < serverMessageCharacters.Length; i++)
{
if (!message.Contains(serverMessageCharacters[i])) return false;
}
return true;
}
public static List<string> GetAll(string textTag)
{
if (!textPacks.ContainsKey(Language))
{
DebugConsole.ThrowError("No text packs available for the selected language (" + Language + ")! Switching to English...");
Language = "English";
if (!textPacks.ContainsKey(Language))
{
throw new Exception("No text packs available in English!");
}
}
List<string> allText;
foreach (TextPack textPack in textPacks[Language])
{
allText = textPack.GetAll(textTag);
if (allText != null) return allText;
}
//if text was not found and we're using a language other than English, see if we can find an English version
//may happen, for example, if a user has selected another language and using mods that haven't been translated to that language
if (Language != "English" && textPacks.ContainsKey("English"))
{
foreach (TextPack textPack in textPacks["English"])
{
allText = textPack.GetAll(textTag);
if (allText != null) return allText;
}
}
return null;
}
public static string ReplaceGenderPronouns(string text, Gender gender)
{
if (gender == Gender.Male)
{
return text.Replace("[genderpronoun]", Get("PronounMale").ToLower())
.Replace("[genderpronounpossessive]", Get("PronounPossessiveMale").ToLower())
.Replace("[genderpronounreflexive]", Get("PronounReflexiveMale").ToLower())
.Replace("[Genderpronoun]", Capitalize(Get("PronounMale")))
.Replace("[Genderpronounpossessive]", Capitalize(Get("PronounPossessiveMale")))
.Replace("[Genderpronounreflexive]", Capitalize(Get("PronounReflexiveMale")));
}
else
{
return text.Replace("[genderpronoun]", Get("PronounFemale").ToLower())
.Replace("[genderpronounpossessive]", Get("PronounPossessiveFemale").ToLower())
.Replace("[genderpronounreflexive]", Get("PronounReflexiveFemale").ToLower())
.Replace("[Genderpronoun]", Capitalize(Get("PronounFemale")))
.Replace("[Genderpronounpossessive]", Capitalize(Get("PronounPossessiveFemale")))
.Replace("[Genderpronounreflexive]", Capitalize(Get("PronounReflexiveFemale")));
}
}
#if DEBUG
public static void CheckForDuplicates(string lang)
{
if (!textPacks.ContainsKey(lang))
{
DebugConsole.ThrowError("No text packs available for the selected language (" + lang + ")!");
return;
}
int packIndex = 0;
foreach (TextPack textPack in textPacks[lang])
{
textPack.CheckForDuplicates(packIndex);
packIndex++;
}
}
public static void WriteToCSV()
{
string lang = "English";
if (!textPacks.ContainsKey(lang))
{
DebugConsole.ThrowError("No text packs available for the selected language (" + lang + ")!");
return;
}
int packIndex = 0;
foreach (TextPack textPack in textPacks[lang])
{
textPack.WriteToCSV(packIndex);
packIndex++;
}
}
#endif
private static string Capitalize(string str)
{
if (string.IsNullOrWhiteSpace(str))
{
return str;
}
return char.ToUpper(str[0]) + str.Substring(1);
}
}
}