/// <summary>Extracts the localization items from the part's config.</summary>
        /// <param name="part">The part to extract items for.</param>
        /// <returns>All the localization items for the part.</returns>
        public static List <LocItem> EmitItemsForPart(AvailablePart part)
        {
            var res = new List <LocItem>();
            // The part's config in the AvailablePart doesn't have all the fields and lacks the comments.
            // We do need the comments to resolve the field tag names, so load via a custom method.
            // In case of the custom loading method fails, use the stock one without the comments support.
            var config = (ConfigStore.LoadConfigWithComments(part.configFileFullName)
                          ?? ConfigNode.Load(part.configFileFullName)).GetNode("PART");

            if (config == null)
            {
                DebugEx.Error("Failed to load part's config: partName={0}, configUrl={1}",
                              part.name, part.configFileFullName);
                return(res);
            }

            // Go through the fields we know must be localized.
            foreach (var fieldName in LocalizablePartFields)
            {
                var field = config.values.Cast <ConfigNode.Value>().FirstOrDefault(x => x.name == fieldName);
                if (field == null)
                {
                    DebugEx.Warning("Field '{0}' is not found in the part {1} config", fieldName, part.name);
                    continue;
                }
                config.values.Remove(field); // Don't handle it down the stream.
                string locTag          = null;
                string locDefaultValue = null;
                var    meta            = MetaBlock.MakeFromString(field.comment);
                if (LocalizationManager.IsLocalizationTag(meta.inlineComment, firstWordOnly: true))
                {
                    var match = Regex.Match(meta.inlineComment, @"^(#[a-zA-Z0-9_-]+)\s*=\s*(.+?)$");
                    if (match.Success)
                    {
                        locTag = match.Groups[1].Value;
                        if (field.value == locTag)
                        {
                            // Part's tag localization failed, use the default text.
                            locDefaultValue = match.Groups[2].Value;
                        }
                    }
                    else
                    {
                        DebugEx.Warning("Cannot resolve default localization tag in field {0} for part {1}: {2}",
                                        fieldName, config.GetValue("name"), meta.inlineComment);
                    }
                }
                // ReSharper disable once UseStringInterpolation
                var fieldOrderStr = string.Format(
                    "\0x00_{0:000}_{1}", // Use zero prefix to place it before anything else.
                    Controller.partFieldsSorting.IndexOf(fieldName, StringComparison.Ordinal),
                    fieldName);
                var item = new LocItem()
                {
                    groupKey        = "Part: " + part.name,
                    sortKey         = fieldOrderStr,
                    fullFilePath    = part.configFileFullName,
                    locTag          = locTag ?? MakePartFieldLocalizationTag(config.GetValue("name"), fieldName),
                    locDefaultValue = locDefaultValue ?? field.value,
                };
                res.Add(item);
            }

            res.AddRange(EmitItemsForNode(part, config));

            return(res);
        }
Example #2
0
        /// <summary>Extracts the localization items from the part's config.</summary>
        /// <param name="part">The part to extract items for.</param>
        /// <returns>All the localization items for the part.</returns>
        public static List <LocItem> EmitItemsForPart(AvailablePart part)
        {
            var res = new List <LocItem>();
            // The part's config in the AvailablePart doesn't have all the fields and lacks the comments.
            // We do need the comments to resolve the field tag names, so load via a custom method.
            // In case of the custom loading method fails, use the stock one without the comments support.
            var config = (ConfigStore.LoadConfigWithComments(part.configFileFullName)
                          ?? ConfigNode.Load(part.configFileFullName)).GetNode("PART");

            if (config == null)
            {
                Debug.LogErrorFormat("Failed to load part's config: partName={0}, configUrl={1}",
                                     part.name, part.configFileFullName);
                return(res);
            }

            // Go thru the fields we know must be localized.
            foreach (var fieldName in localizablePartFields)
            {
                var field = config.values.Cast <ConfigNode.Value>()
                            .FirstOrDefault(x => x.name == fieldName);
                if (field == null)
                {
                    Debug.LogWarningFormat("Field '{0}' is not found in the part {1} config",
                                           fieldName, part.name);
                    continue;
                }
                string locTag          = null;
                string locDefaultValue = null;
                if (!string.IsNullOrEmpty(field.comment) &&
                    field.comment.StartsWith("#", StringComparison.Ordinal))
                {
                    var match = Regex.Match(field.comment, @"^(#[a-zA-Z0-9_-]+)\s*=\s*(.+?)$");
                    if (match.Success)
                    {
                        locTag = match.Groups[1].Value;
                        if (field.value == locTag)
                        {
                            // Part's tag localization failed, use the default text.
                            locDefaultValue = match.Groups[2].Value;
                        }
                    }
                    else
                    {
                        Debug.LogWarningFormat(
                            "Cannot resolve defult localization tag in field {0} for part {1}: {2}",
                            fieldName, config.GetValue("name"), field.comment);
                    }
                }
                var item = new LocItem()
                {
                    groupKey        = "Part: " + part.name,
                    fullFilePath    = part.configFileFullName,
                    locTag          = locTag ?? MakePartFieldLocalizationTag(config.GetValue("name"), fieldName),
                    locDefaultValue = locDefaultValue ?? field.value,
                };
                res.Add(item);
            }

            res.AddRange(EmitItemsForNode(part, config));

            return(res);
        }