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..7cd3dd1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,4 +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 8259454..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 @@ -116,3 +111,4 @@ fixBinGltf <- function(PackageSourcesDir, invalids, deleteTextures = TRUE) { fixObjectsXML(xmlPath = file.path(PackageSourcesDir, "scene/objects.xml"), invalidGuids = invalidGuids) } + 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 10d7f15..2566780 100644 --- a/R/lods.R +++ b/R/lods.R @@ -103,3 +103,92 @@ fixLods <- function(PackageSourcesDir, invalids, deleteTextures = TRUE) { } +#' Remove lods from project +#' +#' @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) { + 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 Path of the xml file of the object. +#' @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"). +#' +#' @export +removeLodNodesFromXML <- function(xmlPath, lodsToRemove) { + 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) +} + +#' 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 657e0f3..5357db7 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`: 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 #### Update Scenery Files 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 new file mode 100644 index 0000000..56a2337 --- /dev/null +++ b/man/removeLodNodesFromXML.Rd @@ -0,0 +1,17 @@ +% 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{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 new file mode 100644 index 0000000..cc685a8 --- /dev/null +++ b/man/removeLods.Rd @@ -0,0 +1,22 @@ +% 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{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 +}