Skip to content

Commit

Permalink
implement removeLods and aux functions
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciuszendron committed Nov 26, 2020
1 parent c7c943d commit 9593e7c
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
Description: Tools to work with scenery creation in Microsoft Flight Simulator.
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ export(fixObjectsXML)
export(getXmlGuid)
export(isValidXML)
export(objectsXmlGuids)
export(removeLodNodesFromXML)
export(removeLods)
export(updateSceneryFiles)
25 changes: 25 additions & 0 deletions R/BinGltf.R
Original file line number Diff line number Diff line change
Expand Up @@ -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("----------------------------")
}
47 changes: 47 additions & 0 deletions R/lods.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<!-- badges: start -->
[![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)
<!-- badges: end -->

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.
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions man/removeLodNodesFromXML.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/removeLods.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9593e7c

Please sign in to comment.