public Dictionary <TKey, TValue> LoadMergedData <TKey, TValue>(params string[] paths)
        {
            var  dataMerged    = new Dictionary <TKey, TValue>();
            bool hasAnyContent = false;

            foreach (string path in paths.Reverse())
            {
                if (this.TryLoadData <TKey, TValue>(path, out var dataToMerge))
                {
                    hasAnyContent = true;
                    AssetPatchHelper.ApplyPatch(dataMerged, dataToMerge);

                    continue;
                }

                this.monitor.VerboseLog($"MergeLoad: Cannot load asset `{path}`");
            }

            if (!hasAnyContent)
            {
                this.monitor.Log($"Cannot load any of these assets: {string.Join(", ", paths)}", LogLevel.Error);
            }

            return(dataMerged);
        }
        private void ApplyTranslation <TKey, TValue>(string path, string locale, Dictionary <TKey, TValue> baseData)
        {
            if (string.IsNullOrEmpty(locale))
            {
                return;
            }

            this.monitor.VerboseLog($"Trying to load localised file `locale/{locale}/{path}.json` for `{path}`, locale `{locale}`");
            var translatedData = this.Data.ReadJsonFile <Dictionary <TKey, TValue> >($"locale/{locale}/{path}.json");

            if (translatedData == null)
            {
                this.monitor.Log($"No translations for {path} locale {locale}");
                return;
            }

            AssetPatchHelper.ApplyPatch(baseData, translatedData);
            this.monitor.Log($"Applied mod's translation to `{locale}` for `{path}`.");
        }
Exemple #3
0
        /// <summary>
        /// Patch Mod's asset contents with localised content
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="asset"></param>
        public void Edit <T>(IAssetData asset)
        {
            string locale   = asset.Locale;
            string fileName = $"{asset.AssetName.Replace(this.modName, this.modAssetDir)}.{locale}.json"; // Localised filename like Dialogue/Abigail.de-De.json for German localisation

            // Load translation for this asset if it's translated to current locale
            if (!string.IsNullOrEmpty(asset.Locale))
            {
                this.monitor.VerboseLog($"Trying to load localised file {fileName} for {asset.AssetName}, locale {locale}");
                MethodInfo method = this.GetType().GetMethod(nameof(this.ApplyLocalization), BindingFlags.Instance | BindingFlags.NonPublic);

                if (method == null)
                {
                    throw new InvalidOperationException($"Can't fetch the internal {nameof(this.ApplyLocalization)} method.");
                }

                AssetPatchHelper.MakeKeyValuePatcher <T>(method).Invoke(this, new object[] { asset, fileName });
            }

            this.ContentPacks.Edit <T>(asset); // Apply patches from content packs
        }
Exemple #4
0
        /// <summary>
        /// Patch a mod's asset with patches from content packs
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="asset"></param>
        public void Edit <T>(IAssetData asset)
        {
            var toApply = this.GetPatchesForAsset(asset, "Edit");

            if (toApply == null || toApply.Count <= 0)
            {
                return;
            }

            MethodInfo method = this.GetType().GetMethod(nameof(this.ApplyDictionary), BindingFlags.Instance | BindingFlags.NonPublic);

            if (method == null)
            {
                throw new InvalidOperationException($"Can't fetch the internal {nameof(this.ApplyDictionary)} method.");
            }

            MethodInfo patcher = AssetPatchHelper.MakeKeyValuePatcher <T>(method);

            foreach (var patch in toApply)
            {
                patcher.Invoke(this, new object[] { asset, patch });
            }
        }