using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using BepInEx;
using DiskCardGame;
using InscryptionAPI.Card;
using InscryptionAPI.Localizing;
using UnityEngine;
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace NevernamedsBestiaryRU;
[BepInPlugin("ru.localization.nevernamedsbestiary", "Nevernamed's Bestiary RU", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
public const string PluginGuid = "ru.localization.nevernamedsbestiary";
public const string PluginName = "Nevernamed's Bestiary RU";
public const string PluginVersion = "1.0.0";
private const string ApiGuid = "cyantist.inscryption.api";
private const string BestiaryGuid = "nevernamedscustominscryptioncards";
private const string TranslationFileName = "nevernameds_bestiary_ru.tsv";
private const string ReportFileName = "untranslated_ru.txt";
private static readonly string[] BestiaryCardPrefixes = new string[5] { "BeastNevernamed ", "TechNevernamed ", "DeadNevernamed ", "WizardNevernamed ", "Nevernamed " };
private readonly HashSet<string> knownKeys = new HashSet<string>();
private string pluginDir;
private bool reportWritten;
private void Awake()
{
pluginDir = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
string text = Path.Combine(pluginDir, "nevernameds_bestiary_ru.tsv");
if (!File.Exists(text))
{
((BaseUnityPlugin)this).Logger.LogError((object)("Translation file not found: " + text));
return;
}
int num = 0;
string[] array = File.ReadAllLines(text, Encoding.UTF8);
foreach (string text2 in array)
{
if (text2.Length == 0 || text2[0] == '#')
{
continue;
}
int num2 = text2.IndexOf('\t');
if (num2 > 0)
{
string text3 = Unescape(text2.Substring(0, num2));
string text4 = Unescape(text2.Substring(num2 + 1)).Trim();
if (text3.Trim().Length != 0 && text4.Length != 0)
{
LocalizationManager.Translate("ru.localization.nevernamedsbestiary", text3, text3, text4, (Language)7);
knownKeys.Add(FormatKey(text3));
num++;
}
}
}
((BaseUnityPlugin)this).Logger.LogInfo((object)("Loaded " + num + " Russian strings for Nevernamed's Bestiary."));
CardManager.ModifyCardList += ReportUntranslated;
}
private List<CardInfo> ReportUntranslated(List<CardInfo> cards)
{
if (reportWritten || cards == null)
{
return cards;
}
reportWritten = true;
try
{
List<string> list = new List<string>();
HashSet<string> seen = new HashSet<string>();
foreach (CardInfo card in cards)
{
if (!((Object)(object)card == (Object)null) && IsBestiaryCard(((Object)card).name))
{
CollectMissing(((Object)card).name, "name", card.DisplayedNameEnglish, list, seen);
CollectMissing(((Object)card).name, "description", card.description, list, seen);
}
}
string text = Path.Combine(pluginDir, "untranslated_ru.txt");
if (list.Count == 0)
{
if (File.Exists(text))
{
File.Delete(text);
}
((BaseUnityPlugin)this).Logger.LogInfo((object)"All Bestiary card names and descriptions have a translation.");
}
else
{
File.WriteAllLines(text, list.ToArray(), new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
((BaseUnityPlugin)this).Logger.LogWarning((object)(list.Count + " Bestiary strings have no translation, see " + text));
}
}
catch (Exception ex)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not check translation coverage: " + ex.Message));
}
return cards;
}
private void CollectMissing(string cardName, string field, string text, List<string> missing, HashSet<string> seen)
{
if (!string.IsNullOrEmpty(text) && HasLetter(text))
{
string item = FormatKey(text);
if (!knownKeys.Contains(item) && seen.Add(item))
{
missing.Add(cardName + "\t" + field + "\t" + text.Replace("\n", "\\n"));
}
}
}
private static bool IsBestiaryCard(string name)
{
if (string.IsNullOrEmpty(name))
{
return false;
}
for (int i = 0; i < BestiaryCardPrefixes.Length; i++)
{
if (name.StartsWith(BestiaryCardPrefixes[i], StringComparison.Ordinal))
{
return true;
}
}
return false;
}
private static bool HasLetter(string text)
{
for (int i = 0; i < text.Length; i++)
{
if (char.IsLetter(text[i]))
{
return true;
}
}
return false;
}
private static string FormatKey(string text)
{
return text.Replace("\n", "").Replace(" ", "").Replace("\"", "")
.ToLowerInvariant();
}
private static string Unescape(string text)
{
return text.Replace("\\n", "\n").Replace("\\t", "\t");
}
}