#nullable enable using System; using System.Linq; using Crowdin.Plugin.Server_API.Helpers; namespace Crowdin.Plugin.Helpers { public static class ContextEntryIdHelper { private const string Marker = "Unity Entry ID: "; public static bool TryReadEntryIdFromContextString(string? context, out long? entryId, out string strippedContext) { entryId = null; strippedContext = context ?? string.Empty; if (!SettingsHelper.GetToggleStatus_PreserveEntryIdsOnImport()) return false; if (string.IsNullOrWhiteSpace(context)) return false; string[] lines = context!.Split("\n", StringSplitOptions.RemoveEmptyEntries); if (lines.Length < 2) return false; string entryIdLine = lines[1]; if (!entryIdLine.StartsWith(Marker)) return false; string markerData = entryIdLine[Marker.Length..]; if (!long.TryParse(markerData, out long parsedId)) return false; entryId = parsedId; strippedContext = string.Join("\n", new[] { lines[0] }.Concat(lines.Skip(2))); return true; } public static string AppendOrUpdateEntryIdInContextString(string? context, long entryId) { var newMarkerLine = $"{Marker}{entryId}"; if (string.IsNullOrEmpty(context)) { return newMarkerLine; } string[] lines = context!.Split("\n"); if (lines[0].StartsWith(Marker)) { lines[0] = newMarkerLine; } else { return newMarkerLine + "\n" + context; } return string.Join("\n", lines); } } }