public PForwardedComponent GetLatestVersion(string id) { if (!remoteComponents.TryGetValue(id, out PForwardedComponent remoteComponent)) { // Attempt to resolve it object instantiated = getLatestVersion.Invoke(id); if (instantiated == null) { #if DEBUG PRegistry.LogPatchWarning("Unable to find a component matching: " + id); #endif remoteComponent = null; } else if (instantiated is PForwardedComponent inThisMod) { // Running the current version remoteComponent = inThisMod; } else { remoteComponent = new PRemoteComponent(instantiated); } remoteComponents.Add(id, remoteComponent); } return(remoteComponent); }
public PForwardedComponent GetLatestVersion(string id) { if (string.IsNullOrEmpty(id)) { throw new ArgumentNullException(nameof(id)); } if (!latestComponents.TryGetValue(id, out PForwardedComponent remoteComponent)) { #if DEBUG PRegistry.LogPatchWarning("Unable to find a component matching: " + id); #endif remoteComponent = null; } return(remoteComponent); }
internal PRegistryComponent() { if (instance == null) { instance = this; } else { #if DEBUG PRegistry.LogPatchWarning("Multiple PLocalRegistry created!"); #endif } ModData = new ConcurrentDictionary <string, object>(2, 64); forwardedComponents = new ConcurrentDictionary <string, PVersionList>(2, 32); instantiatedComponents = new ConcurrentDictionary <string, object>(2, 32); latestComponents = new ConcurrentDictionary <string, PForwardedComponent>(2, 32); PLibInstance = new Harmony(PLIB_HARMONY); }
/// <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)); } } }
/// <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); } } }