Exemple #1
0
        public static Dictionary <string, string> OutputExtraResources(PSB psb, FreeMountContext context, string name,
                                                                       string dirPath, out Dictionary <string, float[]> flattenArrays, PsbExtractOption extractOption = PsbExtractOption.Original)
        {
            Dictionary <string, string> extraResDictionary = new Dictionary <string, string>();

            flattenArrays = null;

            if (!context.UseFlattenArray() && !Consts.FlattenArrayByDefault)
            {
                extractOption = PsbExtractOption.Original;
            }

            if (extractOption == PsbExtractOption.Original)
            {
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }

                for (int i = 0; i < psb.ExtraResources.Count; i++)
                {
                    var relativePath = psb.ExtraResources[i].Index == null ? $"#{Consts.ExtraResourceIdentifierChar}{i}.bin" : $"{Consts.ExtraResourceIdentifierChar}{psb.ExtraResources[i].Index}.bin";

                    File.WriteAllBytes(
                        Path.Combine(dirPath, relativePath),
                        psb.ExtraResources[i].Data);
                    extraResDictionary.Add(Path.GetFileNameWithoutExtension(relativePath), $"{name}/{Consts.ExtraResourceFolderName}/{relativePath}");
                }
            }
            else //Extract
            {
                flattenArrays = new Dictionary <string, float[]>();
                //context.Context[Consts.Context_FlattenArray] = flattenArrays;
                for (int i = 0; i < psb.ExtraResources.Count; i++)
                {
                    var relativePath = psb.ExtraResources[i].Index == null ? $"#{Consts.ExtraResourceIdentifierChar}{i}.bin" : $"{Consts.ExtraResourceIdentifierChar}{psb.ExtraResources[i].Index}.bin";
                    var data         = psb.ExtraResources[i].Data;
                    var resName      = Path.GetFileNameWithoutExtension(relativePath);
                    if (data.Length % 4 == 0)
                    {
                        var floats = MemoryMarshal.Cast <byte, float>(data.AsSpan());
                        flattenArrays.Add(resName, floats.ToArray());
                        //extraResDictionary.Add(resName, "");
                    }
                    else
                    {
                        if (!Directory.Exists(dirPath))
                        {
                            Directory.CreateDirectory(dirPath);
                        }

                        File.WriteAllBytes(
                            Path.Combine(dirPath, relativePath),
                            psb.ExtraResources[i].Data);
                        extraResDictionary.Add(resName, $"{name}/{Consts.ExtraResourceFolderName}/{relativePath}");
                    }
                }
            }

            return(extraResDictionary);
        }
Exemple #2
0
        public static void LinkExtraResources(PSB psb, FreeMountContext context, Dictionary <string, string> extraResourcesDictionary, Dictionary <string, float[]> flattenArrays = null, string baseDir = null)
        {
            foreach (var extraResource in psb.ExtraResources)
            {
                if (extraResource.Index == null)
                {
                    Console.WriteLine("[WARN] Found Extra resource without index. Skipped.");
                    continue;
                }
                var key = $"{Consts.ExtraResourceIdentifierChar}{extraResource.Index}";
                if (flattenArrays != null && (context.UseFlattenArray() || Consts.FlattenArrayByDefault))
                {
                    if (!LinkFromFlattenArray(extraResource, key))
                    {
                        if (!LinkFromFile(extraResource, key))
                        {
                            Console.WriteLine($"[WARN] Extra resource {key} cannot be linked.");
                        }
                    }
                }
                else
                {
                    if (!LinkFromFile(extraResource, key))
                    {
                        Console.WriteLine($"[WARN] Extra resource {key} cannot be linked.");
                    }
                }
            }

            bool LinkFromFile(PsbResource res, string key)
            {
                if (extraResourcesDictionary.ContainsKey(key) && !string.IsNullOrEmpty(extraResourcesDictionary[key]))
                {
                    var path     = extraResourcesDictionary[key];
                    var fullPath = Path.IsPathRooted(path)
                        ? path
                        : Path.Combine(baseDir ?? "", path.Replace('/', '\\'));
                    if (!File.Exists(fullPath))
                    {
                        return(false);
                    }
                    res.Data = File.ReadAllBytes(fullPath);
                    return(true);
                }

                return(false);
            }

            bool LinkFromFlattenArray(PsbResource res, string key)
            {
                if (flattenArrays.ContainsKey(key))
                {
                    var floats = flattenArrays[key].AsSpan();
                    var bytes  = MemoryMarshal.Cast <float, byte>(floats);
                    res.Data = bytes.ToArray();
                    return(true);
                }

                return(false);
            }
        }