Exemplo n.º 1
0
        /// <summary>
        /// Goes through the forwarded components, and picks the latest version of each to
        /// instantiate.
        /// </summary>
        public void Instantiate()
        {
            foreach (var pair in forwardedComponents)
            {
                // Sort value by version
                var versions = pair.Value.Components;
                int n        = versions.Count;
                if (n > 0)
                {
                    string id = pair.Key;
                    versions.Sort();
                    var component = versions[n - 1];
                    latestComponents.GetOrAdd(id, component);
#if DEBUG
                    PRegistry.LogPatchDebug("Instantiating component {0} using version {1} from assembly {2}".F(
                                                id, component.Version, component.GetOwningAssembly().FullName));
#endif
                    try {
                        instantiatedComponents.GetOrAdd(id, component?.DoInitialize(
                                                            PLibInstance));
                    } catch (Exception e) {
                        PRegistry.LogPatchWarning("Error when instantiating component " + id +
                                                  ":");
                        PUtil.LogException(e);
                    }
                }
            }
            // Post initialize for component compatibility
            foreach (var pair in latestComponents)
            {
                try {
                    pair.Value.PostInitialize(PLibInstance);
                } catch (Exception e) {
                    PRegistry.LogPatchWarning("Error when instantiating component " +
                                              pair.Key + ":");
                    PUtil.LogException(e);
                }
            }
        }
        /// <summary>
        /// Localizes all mods to the current locale.
        /// </summary>
        private static void Initialize_Postfix()
        {
            var locale = Localization.GetLocale();

            if (locale != null)
            {
                int    n       = 0;
                string locCode = locale.Code;
                if (string.IsNullOrEmpty(locCode))
                {
                    locCode = Localization.GetCurrentLanguageCode();
                }
                var libLocal = PRegistry.Instance.GetAllComponents(typeof(PLibCorePatches).
                                                                   FullName);
                if (libLocal != null)
                {
                    foreach (var component in libLocal)
                    {
                        component.Process(0, locale);
                    }
                }
                var modLocal = PRegistry.Instance.GetAllComponents(
                    "PeterHan.PLib.Database.PLocalization");
                if (modLocal != null)
                {
                    foreach (var component in modLocal)
                    {
                        component.Process(0, locale);
                        n++;
                    }
                }
                if (n > 0)
                {
                    PRegistry.LogPatchDebug("Localized {0:D} mod(s) to locale {1}".F(
                                                n, locCode));
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds a remote or local forwarded component by ID.
        /// </summary>
        /// <param name="id">The real ID of the component.</param>
        /// <param name="instance">The candidate instance to add.</param>
        private void AddCandidateVersion(string id, PForwardedComponent instance)
        {
            var versions = forwardedComponents.GetOrAdd(id, (_) => new PVersionList());

            if (versions == null)
            {
                PRegistry.LogPatchWarning("Missing version info for component type " + id);
            }
            else
            {
                var  list  = versions.Components;
                bool first = list.Count < 1;
                list.Add(instance);
#if DEBUG
                PRegistry.LogPatchDebug("Candidate version of {0} from {1}".F(id, instance.
                                                                              GetOwningAssembly()));
#endif
                if (first)
                {
                    instance.Bootstrap(PLibInstance);
                }
            }
        }