Esempio n. 1
0
        /// <summary>
        /// Loads codex entries from the specified directory.
        /// </summary>
        /// <param name="entries">The location where the data will be placed.</param>
        /// <param name="dir">The directory to load.</param>
        /// <param name="category">The category to assign to each entry thus loaded.</param>
        private static void LoadFromDirectory(ICollection <CodexEntry> entries, string dir,
                                              string category)
        {
            string[] codexFiles = new string[0];
            try {
                // List codex data files in the codex directory
                codexFiles = Directory.GetFiles(dir, CODEX_FILES);
            } catch (UnauthorizedAccessException ex) {
                PUtil.LogExcWarn(ex);
            } catch (IOException ex) {
                PUtil.LogExcWarn(ex);
            }
            var widgetTagMappings = WIDGET_TAG_MAPPINGS?.GetValue(null) as WidgetMappingList;

            if (widgetTagMappings == null)
            {
                PDatabaseUtils.LogDatabaseWarning("Unable to load codex files: no tag mappings found");
            }
            foreach (string str in codexFiles)
            {
                try {
                    string filename   = str;
                    var    codexEntry = YamlIO.LoadFile <CodexEntry>(filename, YamlParseErrorCB,
                                                                     widgetTagMappings);
                    if (codexEntry != null)
                    {
                        codexEntry.category = category;
                        entries.Add(codexEntry);
                    }
                } catch (IOException ex) {
                    PDatabaseUtils.LogDatabaseWarning("Unable to load codex files from {0}:".
                                                      F(dir));
                    PUtil.LogExcWarn(ex);
                }
            }
Esempio n. 2
0
        /// <summary>
        /// Localizes the specified mod assembly.
        /// </summary>
        /// <param name="modAssembly">The assembly to localize.</param>
        /// <param name="locale">The locale file name to be used.</param>
        private static void Localize(Assembly modAssembly, Localization.Locale locale)
        {
            string path    = PUtil.GetModPath(modAssembly);
            string locCode = locale.Code;

            if (string.IsNullOrEmpty(locCode))
            {
                locCode = Localization.GetCurrentLanguageCode();
            }
            var poFile = Path.Combine(Path.Combine(path, TRANSLATIONS_DIR), locCode +
                                      PLibLocalization.TRANSLATIONS_EXT);

            try {
                Localization.OverloadStrings(Localization.LoadStringsFile(poFile, false));
                RewriteStrings(modAssembly);
            } catch (FileNotFoundException) {
                // No localization available for this locale
#if DEBUG
                PDatabaseUtils.LogDatabaseDebug("No {0} localization available for mod {1}".F(
                                                    locCode, modAssembly.GetNameSafe() ?? "?"));
#endif
            } catch (DirectoryNotFoundException) {
            } catch (IOException e) {
                PDatabaseUtils.LogDatabaseWarning("Failed to load {0} localization for mod {1}:".
                                                  F(locCode, modAssembly.GetNameSafe() ?? "?"));
                PUtil.LogExcWarn(e);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Registers the specified assembly for automatic PLib localization. If the argument
        /// is omitted, the calling assembly is registered.
        /// </summary>
        /// <param name="assembly">The assembly to register for PLib localization.</param>
        public void Register(Assembly assembly = null)
        {
            if (assembly == null)
            {
                assembly = Assembly.GetCallingAssembly();
            }
            var types = assembly.GetTypes();

            if (types == null || types.Length == 0)
            {
                PDatabaseUtils.LogDatabaseWarning("Registered assembly " + assembly.
                                                  GetNameSafe() + " that had no types for localization!");
            }
            else
            {
                RegisterForForwarding();
                toLocalize.Add(assembly);
                // This call searches all types in the assembly implicitly
                Localization.RegisterForTranslation(types[0]);
#if DEBUG
                PDatabaseUtils.LogDatabaseDebug("Localizing assembly {0} using base namespace {1}".
                                                F(assembly.GetNameSafe(), types[0].Namespace));
#endif
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Creates and adds the achievement to the database. As platform achievements cannot
 /// be added using mods, the platform achievement ID will always be empty.
 /// </summary>
 public void AddAchievement()
 {
     if (Requirements == null)
     {
         throw new ArgumentNullException("No colony achievement requirements specified");
     }
     PDatabaseUtils.AddColonyAchievement(NEW_COLONY_ACHIEVEMENT.Invoke(ID, "", Name,
                                                                       Description, IsVictory, Requirements, VictoryTitle, VictoryMessage,
                                                                       VictoryVideoData, VictoryVideoLoop, OnVictory, VictoryAudioSnapshot, Icon));
 }
Esempio n. 5
0
        /// <summary>
        /// Searches types in the assembly (no worries, Localization did this anyways, so they
        /// all either loaded or failed to load) for fields that already had loc string keys
        /// created, and fixes them if so.
        /// </summary>
        /// <param name="assembly">The assembly to check for strings.</param>
        internal static void RewriteStrings(Assembly assembly)
        {
            foreach (var type in assembly.GetTypes())
            {
                foreach (var field in type.GetFields(PPatchTools.BASE_FLAGS | BindingFlags.
                                                     FlattenHierarchy | BindingFlags.Static))
                {
                    // Only use fields of type LocString
                    if (field.FieldType == typeof(LocString) && field.GetValue(null) is
                        LocString ls)
                    {
#if DEBUG
                        PDatabaseUtils.LogDatabaseDebug("Rewrote string {0}: {1} to {2}".F(ls.
                                                                                           key.String, Strings.Get(ls.key.String), ls.text));
#endif
                        Strings.Add(ls.key.String, ls.text);
                    }
                }
            }
        }