From 9593e7c4c8ba7cdecd8209d6a6a79b95cbc21659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Zendron?= <53181700+viniciuszendron@users.noreply.github.com> Date: Wed, 25 Nov 2020 21:42:33 -0300 Subject: [PATCH 1/2] implement removeLods and aux functions --- DESCRIPTION | 2 +- NAMESPACE | 2 ++ R/BinGltf.R | 25 +++++++++++++++++++ R/lods.R | 47 ++++++++++++++++++++++++++++++++++++ README.md | 18 +++++++++++++- man/removeLodNodesFromXML.Rd | 14 +++++++++++ man/removeLods.Rd | 15 ++++++++++++ 7 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 man/removeLodNodesFromXML.Rd create mode 100644 man/removeLods.Rd diff --git a/DESCRIPTION b/DESCRIPTION index eb1aca2..6cfc107 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: msfsSceneryTools Type: Package Title: msfsSceneryTools -Version: 0.2.0 +Version: 0.3.0 Author: Vinícius Zendron Maintainer: Vinícius Zendron Description: Tools to work with scenery creation in Microsoft Flight Simulator. diff --git a/NAMESPACE b/NAMESPACE index 8510f26..47fd9fb 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,4 +11,6 @@ export(fixObjectsXML) export(getXmlGuid) export(isValidXML) export(objectsXmlGuids) +export(removeLodNodesFromXML) +export(removeLods) export(updateSceneryFiles) diff --git a/R/BinGltf.R b/R/BinGltf.R index 8259454..f46c73c 100644 --- a/R/BinGltf.R +++ b/R/BinGltf.R @@ -116,3 +116,28 @@ fixBinGltf <- function(PackageSourcesDir, invalids, deleteTextures = TRUE) { fixObjectsXML(xmlPath = file.path(PackageSourcesDir, "scene/objects.xml"), invalidGuids = invalidGuids) } + +removeBinGltfByLods <- function(modelLibDir, lodsToRemove) { + # modelLibDir <- "D:/FSProjects/florianopolis-megapack/florianopolis-mega - Copia/PackageSources/modelLib" + lodsToRemoveF <- paste0("_LOD", lodsToRemove) + pattern <- paste0( + c( + paste0(lodsToRemoveF, ".bin"), + paste0(lodsToRemoveF, ".gltf") + ), + collapse = "|" + ) + filesToRemove <- list.files(modelLibDir, pattern = pattern, full.names = TRUE) + status <- file.remove(filesToRemove) + message(sum(status), " arquivos BIN/GLTF removidos (LODS ", paste(lodsToRemove, collapse = ", "), ")") + message("----------------------------") +} + +removeModelLibTexturesByLods <- function(textureDir, lodsToRemove) { + # textureDir <- "D:/FSProjects/florianopolis-megapack/florianopolis-mega - Copia/PackageSources/modelLib/texture" + lodsToRemoveF <- paste0("_LOD", lodsToRemove, collapse = "|") + filesToRemove <- list.files(textureDir, pattern = lodsToRemoveF, full.names = TRUE) + status <- file.remove(filesToRemove) + message(sum(status), " texturas removidas (LODS ", paste(lodsToRemove, collapse = ", "), ")") + message("----------------------------") +} diff --git a/R/lods.R b/R/lods.R index 10d7f15..da5f375 100644 --- a/R/lods.R +++ b/R/lods.R @@ -103,3 +103,50 @@ fixLods <- function(PackageSourcesDir, invalids, deleteTextures = TRUE) { } +#' Remove lods from project +#' +#' @param PackageSourcesDir +#' @param lodsToRemove +#' +#' @export +removeLods <- function(PackageSourcesDir, lodsToRemove, removeBinGltf = TRUE, removeTextures = TRUE) { + # PackageSourcesDir <- "D:/FSProjects/florianopolis-megapack/florianopolis-mega - Copia/PackageSources" + modelLibDir <- file.path(PackageSourcesDir, "modelLib") + xmlFiles <- list.files(modelLibDir, pattern = ".xml$", full.names = TRUE) + + message("Removendo LODS ", paste0(lodsToRemove, collapse = ", "), " de .xmls em modelLib") + lapply(xmlFiles, function(xml) removeLodNodesFromXML(xml, lodsToRemove)) + message(length(xmlFiles), " arquivos .xml alterados") + + if (removeBinGltf) { + message("Removendo arquivos .bin e .gltf correspondentes em modelLib") + removeBinGltfByLods(modelLibDir = modelLibDir, lodsToRemove = lodsToRemove) + } + + if (removeTextures) { + message("Removendo texturas correspondentes em modelLib/texture") + removeModelLibTexturesByLods(textureDir = file.path(modelLibDir, "texture"), + lodsToRemove = lodsToRemove) + } +} + +#' Remove lod nodes from XML +#' +#' @param xmlPath +#' @param lodsToRemove +#' @param removeBinGltf +#' @param removeTextures +#' +#' @export +removeLodNodesFromXML <- function(xmlPath, lodsToRemove) { + # xmlPath <- "D:/FSProjects/florianopolis-megapack/florianopolis-mega - Copia/PackageSources/modelLib/03617341435360627.xml" + # lodsToRemove <- c("01", "02") + obj <- xml2::read_xml(xmlPath) + LibObjNodes <- xml2::xml_find_all(obj, "//LODS/LOD") + pattern <- paste0(paste0("_LOD", lodsToRemove, ".gltf\""), collapse = "|") + nodesToRemove <- LibObjNodes[stringr::str_detect(LibObjNodes, pattern)] + xml2::xml_remove(nodesToRemove) + # Write new file + file.remove(xmlPath) + xml2::write_xml(obj, xmlPath) +} diff --git a/README.md b/README.md index 657e0f3..addf0ca 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![R build status](https://github.com/viniciuszendron/msfsSceneryTools/workflows/R-CMD-check/badge.svg)](https://github.com/viniciuszendron/msfsSceneryTools/actions) -[![version](https://img.shields.io/badge/version-0.2.0-red.svg)](https://semver.org) +[![version](https://img.shields.io/badge/version-0.3.0-red.svg)](https://semver.org) This package provides some functions to Microsoft Flight Simulator Scenery developers, especially those who extract photogrammetry data from other sources and need to do some cleaning and removing corrupted files. @@ -22,6 +22,22 @@ If a message asking to update packages appear, just press Enter key or select th ## Changelog +### Version 0.3.0 + +#### Remove Lods + +New function `removeLods` remove specific LODS from a project. It removes LOD entries from .xml located in modelLib, its correspondent .bin/.gltf and textures in modelLib/textures. + +#### Functions + +##### New Functions + +- `removeLods`: +- `removeLodNodesFromXML`: +- `removeBinGltfByLods`: +- `removeModelLibTexturesByLods`: + + ### Version 0.2.0 #### Update Scenery Files diff --git a/man/removeLodNodesFromXML.Rd b/man/removeLodNodesFromXML.Rd new file mode 100644 index 0000000..77c8853 --- /dev/null +++ b/man/removeLodNodesFromXML.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/lods.R +\name{removeLodNodesFromXML} +\alias{removeLodNodesFromXML} +\title{Remove lod nodes from XML} +\usage{ +removeLodNodesFromXML(xmlPath, lodsToRemove) +} +\arguments{ +\item{removeTextures}{} +} +\description{ +Remove lod nodes from XML +} diff --git a/man/removeLods.Rd b/man/removeLods.Rd new file mode 100644 index 0000000..e91419b --- /dev/null +++ b/man/removeLods.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/lods.R +\name{removeLods} +\alias{removeLods} +\title{Remove lods from project} +\usage{ +removeLods(PackageSourcesDir, lodsToRemove, removeBinGltf = TRUE, + removeTextures = TRUE) +} +\arguments{ +\item{lodsToRemove}{} +} +\description{ +Remove lods from project +} From 1463b8ac596bae7dfac9ba1f9ebcbbbd47501825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Zendron?= <53181700+viniciuszendron@users.noreply.github.com> Date: Thu, 3 Dec 2020 18:49:16 -0300 Subject: [PATCH 2/2] update docs/organize code --- NAMESPACE | 2 ++ R/BinGltf.R | 33 ++--------------- R/fixCorruptedFiles.R | 10 ------ R/lods.R | 56 +++++++++++++++++++++++++---- R/objectsXml.R | 12 ------- README.md | 8 ++--- man/checkAllFilesBinGltf.Rd | 2 +- man/checkFilesBinGltf.Rd | 2 +- man/removeBinGltfByLods.Rd | 17 +++++++++ man/removeLodNodesFromXML.Rd | 5 ++- man/removeLods.Rd | 9 ++++- man/removeModelLibTexturesByLods.Rd | 17 +++++++++ 12 files changed, 105 insertions(+), 68 deletions(-) create mode 100644 man/removeBinGltfByLods.Rd create mode 100644 man/removeModelLibTexturesByLods.Rd diff --git a/NAMESPACE b/NAMESPACE index 47fd9fb..7cd3dd1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,6 +11,8 @@ export(fixObjectsXML) export(getXmlGuid) export(isValidXML) export(objectsXmlGuids) +export(removeBinGltfByLods) export(removeLodNodesFromXML) export(removeLods) +export(removeModelLibTexturesByLods) export(updateSceneryFiles) diff --git a/R/BinGltf.R b/R/BinGltf.R index f46c73c..3e90703 100644 --- a/R/BinGltf.R +++ b/R/BinGltf.R @@ -1,7 +1,7 @@ #' Check .bin and .gltf Files #' #' @param xmlPath -#' @param nlods +#' @param nlods Numeric. Number of lods of the project. #' #' @return #' @export @@ -9,11 +9,6 @@ #' @examples checkFilesBinGltf <- function(xmlPath, nlods) { - # Tests - #xmlPath = xmlsDir[[1]] - #nlods = 3 - # - id <- stringr::str_sub(basename(xmlPath), end = -5) lods <- paste0("_LOD0", 0:(nlods-1)) gltfDir <- file.path(dirname(xmlPath), paste0(id, lods, ".gltf")) @@ -46,7 +41,7 @@ checkFilesBinGltf <- function(xmlPath, nlods) { #' Check multiple .bin and .gltf files #' #' @param modelLibDir -#' @param nlods +#' @param nlods Numeric. Number of lods of the project. #' #' @return #' @export @@ -117,27 +112,3 @@ fixBinGltf <- function(PackageSourcesDir, invalids, deleteTextures = TRUE) { } -removeBinGltfByLods <- function(modelLibDir, lodsToRemove) { - # modelLibDir <- "D:/FSProjects/florianopolis-megapack/florianopolis-mega - Copia/PackageSources/modelLib" - lodsToRemoveF <- paste0("_LOD", lodsToRemove) - pattern <- paste0( - c( - paste0(lodsToRemoveF, ".bin"), - paste0(lodsToRemoveF, ".gltf") - ), - collapse = "|" - ) - filesToRemove <- list.files(modelLibDir, pattern = pattern, full.names = TRUE) - status <- file.remove(filesToRemove) - message(sum(status), " arquivos BIN/GLTF removidos (LODS ", paste(lodsToRemove, collapse = ", "), ")") - message("----------------------------") -} - -removeModelLibTexturesByLods <- function(textureDir, lodsToRemove) { - # textureDir <- "D:/FSProjects/florianopolis-megapack/florianopolis-mega - Copia/PackageSources/modelLib/texture" - lodsToRemoveF <- paste0("_LOD", lodsToRemove, collapse = "|") - filesToRemove <- list.files(textureDir, pattern = lodsToRemoveF, full.names = TRUE) - status <- file.remove(filesToRemove) - message(sum(status), " texturas removidas (LODS ", paste(lodsToRemove, collapse = ", "), ")") - message("----------------------------") -} diff --git a/R/fixCorruptedFiles.R b/R/fixCorruptedFiles.R index ff1a2ca..ed6c620 100644 --- a/R/fixCorruptedFiles.R +++ b/R/fixCorruptedFiles.R @@ -11,12 +11,6 @@ #' @example fixCorruptedFiles <- function(PackageSourcesDir, nlods, deleteTextures = TRUE) { - # Testes - # PackageSourcesDir <- "D:/FSProjects/florianopolis-megapack/florianopolis-mega/PackageSources" - # PackageSourcesDir <- "D:/FSProjects/rio-megateste/PackageSources" - # PackageSourcesDir <- "D:/FSProjects/florianopolis-megapack/florianopolis-part6/PackageSources" - # - modelLibDir <- file.path(PackageSourcesDir, "modelLib") files <- dir(modelLibDir) @@ -72,10 +66,6 @@ fixCorruptedFiles <- function(PackageSourcesDir, nlods, deleteTextures = TRUE) { #' @export isValidXML <- function(xmlPath, nlods) { - # Testes - #xmlPath = "D:/FSProjects/florianopolis-megapack/florianopolis-mega/PackageSources/modelLib/036173415260400522.xml" - # - lods <- checkLods(xmlPath) files <- checkFilesBinGltf(xmlPath, nlods) diff --git a/R/lods.R b/R/lods.R index da5f375..2566780 100644 --- a/R/lods.R +++ b/R/lods.R @@ -105,12 +105,15 @@ fixLods <- function(PackageSourcesDir, invalids, deleteTextures = TRUE) { #' Remove lods from project #' -#' @param PackageSourcesDir +#' @param PackageSourcesDir Path to PackageSources directory. #' @param lodsToRemove +#' A (character) vector with lods to remove. +#' Example: if you wan't to remove LOD01 and LOD02, provide a vector c("01", "02"). +#' @param removeBinGltf Logical. Whether to remove correspondent .bin and .gltf files. +#' @param removeTextures Logical. Whether to remove correspondent texture files. #' #' @export removeLods <- function(PackageSourcesDir, lodsToRemove, removeBinGltf = TRUE, removeTextures = TRUE) { - # PackageSourcesDir <- "D:/FSProjects/florianopolis-megapack/florianopolis-mega - Copia/PackageSources" modelLibDir <- file.path(PackageSourcesDir, "modelLib") xmlFiles <- list.files(modelLibDir, pattern = ".xml$", full.names = TRUE) @@ -132,15 +135,13 @@ removeLods <- function(PackageSourcesDir, lodsToRemove, removeBinGltf = TRUE, re #' Remove lod nodes from XML #' -#' @param xmlPath +#' @param xmlPath Path of the xml file of the object. #' @param lodsToRemove -#' @param removeBinGltf -#' @param removeTextures +#' A (character) vector with lods to remove. +#' Example: if you wan't to remove LOD01 and LOD02, provide a vector c("01", "02"). #' #' @export removeLodNodesFromXML <- function(xmlPath, lodsToRemove) { - # xmlPath <- "D:/FSProjects/florianopolis-megapack/florianopolis-mega - Copia/PackageSources/modelLib/03617341435360627.xml" - # lodsToRemove <- c("01", "02") obj <- xml2::read_xml(xmlPath) LibObjNodes <- xml2::xml_find_all(obj, "//LODS/LOD") pattern <- paste0(paste0("_LOD", lodsToRemove, ".gltf\""), collapse = "|") @@ -150,3 +151,44 @@ removeLodNodesFromXML <- function(xmlPath, lodsToRemove) { file.remove(xmlPath) xml2::write_xml(obj, xmlPath) } + +#' Remove .bin and .gltf by lods +#' +#' @param modelLibDir Path to modelLib directory. +#' @param lodsToRemove +#' A (character) vector with lods to remove. +#' Example: if you wan't to remove LOD01 and LOD02, provide a vector c("01", "02"). +#' +#' @return +#' @export +removeBinGltfByLods <- function(modelLibDir, lodsToRemove) { + lodsToRemoveF <- paste0("_LOD", lodsToRemove) + pattern <- paste0( + c( + paste0(lodsToRemoveF, ".bin"), + paste0(lodsToRemoveF, ".gltf") + ), + collapse = "|" + ) + filesToRemove <- list.files(modelLibDir, pattern = pattern, full.names = TRUE) + status <- file.remove(filesToRemove) + message(sum(status), " arquivos BIN/GLTF removidos (LODS ", paste(lodsToRemove, collapse = ", "), ")") + message("----------------------------") +} + +#' Remove textures by lods +#' +#' @param textureDir Path to modelLib/texture directory. +#' @param lodsToRemove +#' A (character) vector with lods to remove. +#' Example: if you wan't to remove LOD01 and LOD02, provide a vector c("01", "02"). +#' +#' @return +#' @export +removeModelLibTexturesByLods <- function(textureDir, lodsToRemove) { + lodsToRemoveF <- paste0("_LOD", lodsToRemove, collapse = "|") + filesToRemove <- list.files(textureDir, pattern = lodsToRemoveF, full.names = TRUE) + status <- file.remove(filesToRemove) + message(sum(status), " texturas removidas (LODS ", paste(lodsToRemove, collapse = ", "), ")") + message("----------------------------") +} diff --git a/R/objectsXml.R b/R/objectsXml.R index d01388f..3bfc52a 100644 --- a/R/objectsXml.R +++ b/R/objectsXml.R @@ -7,11 +7,6 @@ #' @export fixObjectsXML <- function(xmlPath, invalidGuids, createBackup = FALSE) { - # Testes - # xmlPath <- "D:/FSProjects/florianopolis-megapack/florianopolis-mega/PackageSources/scene/objects.xml" - # xmlPath <- "D:/FSProjects/florianopolis-megapack/florianopolis-mega - Copia/PackageSources/scene/objects.xml" - # - obj <- xml2::read_xml(xmlPath) if (isTRUE(createBackup)) { @@ -21,13 +16,10 @@ fixObjectsXML <- function(xmlPath, invalidGuids, createBackup = FALSE) { LibObjNodes <- xml2::xml_find_all(obj, "//SceneryObject/LibraryObject") message(basename(xmlPath), " inicializado com ", length(LibObjNodes), " entradas") - #guidsIndex <- stringr::str_replace_all() xml2::xml_attr(LibObjNodes, attr = "name") guidsIndex <- xml2::xml_attr(LibObjNodes, attr = "name") nodesToRemove <- which(guidsIndex %in% paste0("{", invalidGuids, "}")) - # nodesToKeep <- which(!guidsIndex %in% paste0("{", invalidGuids, "}")) - # newObj <- xml2::xml_children(obj)[nodesToKeep] message("Removendo ", length(nodesToRemove), " entradas inválidas") xml2::xml_remove(xml2::xml_children(obj)[nodesToRemove]) message(length(nodesToRemove), " entradas inválidas removidas") @@ -55,10 +47,6 @@ fixObjectsXML <- function(xmlPath, invalidGuids, createBackup = FALSE) { #' } objectsXmlGuids <- function(xmlPath){ - # Tests - # xmlPath <- "D:/FSProjects/maceio/PackageSources/scene/objects.xml" - # - obj <- xml2::read_xml(xmlPath) LibObjNodes <- xml2::xml_find_all(obj, "//SceneryObject/LibraryObject") diff --git a/README.md b/README.md index addf0ca..5357db7 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,10 @@ New function `removeLods` remove specific LODS from a project. It removes LOD en ##### New Functions -- `removeLods`: -- `removeLodNodesFromXML`: -- `removeBinGltfByLods`: -- `removeModelLibTexturesByLods`: +- `removeLods`: Remove lods from project. +- `removeLodNodesFromXML`: Remove lod nodes from XML. +- `removeBinGltfByLods`: Remove .bin and .gltf by lods. +- `removeModelLibTexturesByLods`: Remove textures by lods. ### Version 0.2.0 diff --git a/man/checkAllFilesBinGltf.Rd b/man/checkAllFilesBinGltf.Rd index 9651efd..15659fa 100644 --- a/man/checkAllFilesBinGltf.Rd +++ b/man/checkAllFilesBinGltf.Rd @@ -7,7 +7,7 @@ checkAllFilesBinGltf(modelLibDir, nlods) } \arguments{ -\item{nlods}{} +\item{nlods}{Numeric. Number of lods of the project.} } \description{ Check multiple .bin and .gltf files diff --git a/man/checkFilesBinGltf.Rd b/man/checkFilesBinGltf.Rd index 009d8d9..d4bd93d 100644 --- a/man/checkFilesBinGltf.Rd +++ b/man/checkFilesBinGltf.Rd @@ -7,7 +7,7 @@ checkFilesBinGltf(xmlPath, nlods) } \arguments{ -\item{nlods}{} +\item{nlods}{Numeric. Number of lods of the project.} } \description{ Check .bin and .gltf Files diff --git a/man/removeBinGltfByLods.Rd b/man/removeBinGltfByLods.Rd new file mode 100644 index 0000000..2dcc29c --- /dev/null +++ b/man/removeBinGltfByLods.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/lods.R +\name{removeBinGltfByLods} +\alias{removeBinGltfByLods} +\title{Remove .bin and .gltf by lods} +\usage{ +removeBinGltfByLods(modelLibDir, lodsToRemove) +} +\arguments{ +\item{modelLibDir}{Path to modelLib directory.} + +\item{lodsToRemove}{A (character) vector with lods to remove. +Example: if you wan't to remove LOD01 and LOD02, provide a vector c("01", "02").} +} +\description{ +Remove .bin and .gltf by lods +} diff --git a/man/removeLodNodesFromXML.Rd b/man/removeLodNodesFromXML.Rd index 77c8853..56a2337 100644 --- a/man/removeLodNodesFromXML.Rd +++ b/man/removeLodNodesFromXML.Rd @@ -7,7 +7,10 @@ removeLodNodesFromXML(xmlPath, lodsToRemove) } \arguments{ -\item{removeTextures}{} +\item{xmlPath}{Path of the xml file of the object.} + +\item{lodsToRemove}{A (character) vector with lods to remove. +Example: if you wan't to remove LOD01 and LOD02, provide a vector c("01", "02").} } \description{ Remove lod nodes from XML diff --git a/man/removeLods.Rd b/man/removeLods.Rd index e91419b..cc685a8 100644 --- a/man/removeLods.Rd +++ b/man/removeLods.Rd @@ -8,7 +8,14 @@ removeLods(PackageSourcesDir, lodsToRemove, removeBinGltf = TRUE, removeTextures = TRUE) } \arguments{ -\item{lodsToRemove}{} +\item{PackageSourcesDir}{Path to PackageSources directory.} + +\item{lodsToRemove}{A (character) vector with lods to remove. +Example: if you wan't to remove LOD01 and LOD02, provide a vector c("01", "02").} + +\item{removeBinGltf}{Logical. Whether to remove correspondent .bin and .gltf files.} + +\item{removeTextures}{Logical. Whether to remove correspondent texture files.} } \description{ Remove lods from project diff --git a/man/removeModelLibTexturesByLods.Rd b/man/removeModelLibTexturesByLods.Rd new file mode 100644 index 0000000..506b638 --- /dev/null +++ b/man/removeModelLibTexturesByLods.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/lods.R +\name{removeModelLibTexturesByLods} +\alias{removeModelLibTexturesByLods} +\title{Remove textures by lods} +\usage{ +removeModelLibTexturesByLods(textureDir, lodsToRemove) +} +\arguments{ +\item{textureDir}{Path to modelLib/texture directory.} + +\item{lodsToRemove}{A (character) vector with lods to remove. +Example: if you wan't to remove LOD01 and LOD02, provide a vector c("01", "02").} +} +\description{ +Remove textures by lods +}