Example #1
0
        public void Receive(Message msg)
        {
            log.Info($"Received message {msg.Type}");
            try {
                switch (msg.Type)
                {
                case Message.NATIVE_INIT:
                    // Reset resources
                    Program.LoadInitialState();
                    Program.channel.SendMessage(new HelloMessage());
                    break;

                case Message.NATIVE_CLOSE:
                    // Release other resources..
                    Program.channel.Dispose();
                    return;

                case Message.FOLDER_GET:
                    var folder  = msg["path"].ToString();
                    var entries = Program.explorer.GetFolderEntries(folder);
                    Program.channel.SendMessage(new FolderEntriesMessage(folder, 0, 1000, entries));
                    break;

                case Message.FILE_GET:
                    var fileBlock = Program.explorer.GetFileBlock(msg["path"].ToString());
                    var type      = BdFileDecoder.Export(fileBlock,
                                                         msg["extractTextures"]?.ToObject <bool>() ?? false,
                                                         Program.explorer.GetFileBlockForName, out string path);
                    Program.channel.SendMessage(new FileMessage(path, type));
                    break;

                case Message.FILE_UPSCALE:
                    var textureType = msg["textureType"].ToString();
                    var upscaled    = DDSUtility.Upscale(msg["path"].ToString());
                    Program.channel.SendMessage(new FileUpscaledMessage(upscaled, textureType));
                    break;
                }
            } catch (Exception e) {
                log.Warn("Error while processing message", e);
                Program.channel.SendMessage(new FailedToProcessMessage(msg.Type));
            }
        }
Example #2
0
        public static FileType Export(FileBlock fb, bool exportTextures, FileBlockResolver textureResolver,
                                      out string path)
        {
            FileType ft = FileType.Unknown;

            switch (Path.GetExtension(fb.FileName)?.ToLowerInvariant())
            {
            case ".pac":
                ft = FileType.PAC;
                break;

            case ".dds":
                ft = FileType.DDS;
                break;

            case ".txt":
            case ".xml":
                ft = FileType.Text;
                break;
            }

            path = Path.Combine(BdDiscovery.DataDir, fb.FolderName, fb.FileName);
            var upscaled = DDSUtility.GetUpscaledName(path);

            if (ft == FileType.PAC)
            {
                var file = new FileInfo(path + ".json");
                path = file.FullName;
                var data = Pac2Json.ReadPAC(fb);

                if (exportTextures)
                {
                    foreach (var mesh in data.Meshes)
                    {
                        var texture = textureResolver($"{mesh.TextureName}.dds");
                        if (texture != null)
                        {
                            Export(texture, false, null, out mesh.TexturePath);
                        }
                        var normalTexture = textureResolver(GetNormalName(mesh.TextureName));
                        if (normalTexture != null)
                        {
                            Export(normalTexture, false, null, out mesh.NormTexturePath);
                        }
                        var specularTexture = textureResolver(GetSpecularName(mesh.TextureName));
                        if (specularTexture != null)
                        {
                            Export(specularTexture, false, null, out mesh.SpecTexturePath);
                        }
                    }
                }

                using (var writer = new StreamWriter(file.FullCreate())) {
                    serializer.Serialize(writer, data);
                }
            }
            else
            {
                var hash = path + ".hash";

                if (!File.Exists(hash) || BitConverter.ToInt32(File.ReadAllBytes(hash), 0) != fb.Hash)
                {
                    fb.Extract(BdDiscovery.DataDir);
                    if (File.Exists(upscaled))
                    {
                        File.Delete(upscaled);
                    }

                    File.WriteAllBytes(hash, BitConverter.GetBytes(fb.Hash));
                }
                else
                {
                    if (File.Exists(upscaled))
                    {
                        path = upscaled;
                    }
                }
            }

            return(ft);
        }