Skip to content

Commit

Permalink
Merge pull request #13 from viniciuszendron/removeLods
Browse files Browse the repository at this point in the history
V 0.3.0
  • Loading branch information
viniciuszendron authored Dec 3, 2020
2 parents c7c943d + 1463b8a commit a402add
Show file tree
Hide file tree
Showing 13 changed files with 189 additions and 33 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
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ export(fixObjectsXML)
export(getXmlGuid)
export(isValidXML)
export(objectsXmlGuids)
export(removeBinGltfByLods)
export(removeLodNodesFromXML)
export(removeLods)
export(removeModelLibTexturesByLods)
export(updateSceneryFiles)
10 changes: 3 additions & 7 deletions R/BinGltf.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
#' Check .bin and .gltf Files
#'
#' @param xmlPath
#' @param nlods
#' @param nlods Numeric. Number of lods of the project.
#'
#' @return
#' @export
#'
#' @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"))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -116,3 +111,4 @@ fixBinGltf <- function(PackageSourcesDir, invalids, deleteTextures = TRUE) {
fixObjectsXML(xmlPath = file.path(PackageSourcesDir, "scene/objects.xml"), invalidGuids = invalidGuids)

}

10 changes: 0 additions & 10 deletions R/fixCorruptedFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
89 changes: 89 additions & 0 deletions R/lods.R
Original file line number Diff line number Diff line change
Expand Up @@ -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("----------------------------")
}
12 changes: 0 additions & 12 deletions R/objectsXml.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
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`: 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
Expand Down
2 changes: 1 addition & 1 deletion man/checkAllFilesBinGltf.Rd

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

2 changes: 1 addition & 1 deletion man/checkFilesBinGltf.Rd

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

17 changes: 17 additions & 0 deletions man/removeBinGltfByLods.Rd

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

17 changes: 17 additions & 0 deletions man/removeLodNodesFromXML.Rd

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

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

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

17 changes: 17 additions & 0 deletions man/removeModelLibTexturesByLods.Rd

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

0 comments on commit a402add

Please sign in to comment.