Esempio n. 1
0
        public static StringsFolderLookupOverlay?TypicalFactory(string referenceModPath, StringsReadParameters?instructions, ModKey modKey)
        {
            var ret = new StringsFolderLookupOverlay();
            var stringsFolderPath = instructions?.StringsFolderOverride;
            var dir = Path.GetDirectoryName(referenceModPath);

            if (stringsFolderPath == null)
            {
                stringsFolderPath = Path.Combine(dir, "Strings");
            }
            if (stringsFolderPath.Value.Exists)
            {
                foreach (var file in stringsFolderPath.Value.Info.EnumerateFiles($"{modKey.Name}*{StringsUtility.StringsFileExtension}"))
                {
                    if (!StringsUtility.TryRetrieveInfoFromString(file.Name, out var type, out var lang, out _))
                    {
                        continue;
                    }
                    var dict = ret.Get(type);
                    dict[lang] = new Lazy <IStringsLookup>(() => new StringsLookupOverlay(file.FullName, type), LazyThreadSafetyMode.ExecutionAndPublication);
                }
            }
            foreach (var bsaFile in Directory.EnumerateFiles(dir, "*.bsa"))
            {
                var bsaReader = BSAReader.Load(new AbsolutePath(bsaFile, skipValidation: true));
                foreach (var item in bsaReader.Files)
                {
                    if (!StringsUtility.TryRetrieveInfoFromString(Path.GetFileName(item.Path.ToString()), out var type, out var lang, out var modName))
                    {
                        continue;
                    }
                    if (!MemoryExtensions.Equals(modKey.Name, modName, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }
                    var dict = ret.Get(type);
                    if (dict.ContainsKey(lang))
                    {
                        continue;
                    }
                    dict[lang] = new Lazy <IStringsLookup>(() =>
                    {
                        byte[] bytes     = new byte[item.Size];
                        using var stream = new MemoryStream(bytes);
                        item.CopyDataTo(stream).AsTask().Wait();
                        return(new StringsLookupOverlay(bytes, type));
                    }, LazyThreadSafetyMode.ExecutionAndPublication);
                }
            }
            return(ret);
        }