Skip to content

Commit

Permalink
Merge pull request #7 from viniciuszendron/updateSceneryFiles
Browse files Browse the repository at this point in the history
Update scenery files - Version 0.2.0
  • Loading branch information
viniciuszendron authored Nov 8, 2020
2 parents 1b86815 + d1aca2d commit c7c943d
Show file tree
Hide file tree
Showing 19 changed files with 258 additions and 61 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.1.2
Version: 0.2.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 @@ -8,5 +8,7 @@ export(fixBinGltf)
export(fixCorruptedFiles)
export(fixLods)
export(fixObjectsXML)
export(getXmlGuid)
export(isValidXML)
export(objectsXmlGuids)
export(updateSceneryFiles)
27 changes: 14 additions & 13 deletions R/BinGltf.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ checkAllFilesBinGltf <- function(modelLibDir, nlods) {

#' Fix missing bin/gltf
#'
#' @param PackageSourcesDir
#' @param invalids
#' @param PackageSourcesDir Path to PackageSources directory.
#' @param invalids List with invalid files from `checkAllFilesBinGltf`.
#' @param deleteTextures Whether to delete textures in modelLib/texture related to the corrupted data.
#'
#' @return
#' @export
#'
#' @examples
fixBinGltf <- function(PackageSourcesDir, invalids) {
fixBinGltf <- function(PackageSourcesDir, invalids, deleteTextures = TRUE) {

# Delete invalid XML from modelLib
message("Removendo arquivos BIN e GLTF inválidos em modelLib")
Expand All @@ -100,15 +100,16 @@ fixBinGltf <- function(PackageSourcesDir, invalids) {
message("----------------------------")

# Delete correspondent textures
ids <- stringr::str_remove(sapply(invalids, "[[", 2), ".xml$")
texToRemove <- list.files(file.path(PackageSourcesDir, "modelLib", "texture"),
paste0(ids, collapse = "|"),
all.files = TRUE,
full.names = TRUE)
statusPNG <- file.remove(texToRemove)
message(sum(statusPNG), " arquivos PNG (texture) removidos")
message("----------------------------")

if (isTRUE(deleteTextures)) {
ids <- stringr::str_remove(sapply(invalids, "[[", 2), ".xml$")
texToRemove <- list.files(file.path(PackageSourcesDir, "modelLib", "texture"),
paste0(ids, collapse = "|"),
all.files = TRUE,
full.names = TRUE)
statusPNG <- file.remove(texToRemove)
message(sum(statusPNG), " arquivos PNG (texture) removidos")
message("----------------------------")
}

# Clean corrupted guids from objects.xml
invalidGuids <- sapply(invalids, "[[", 4)
Expand Down
13 changes: 8 additions & 5 deletions R/fixCorruptedFiles.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#' Detect and Remove Corrupted Files
#'
#' @param PackageSourcesDir
#' @param PackageSourcesDir Path to PackageSources directory.
#' @param nlods Number of lods of your package.
#' Example: If you have files named \*_LOD00.\*, \*_LOD01.\* and \*_LOD02.\*, then nlods is 3.
#' If you have only files named \*_LOD00.\*, then set nlods = 1.
#' @param deleteTextures Whether to delete textures in modelLib/texture related to the corrupted data.
#'
#' @return
#' @export
#'
#' @example
fixCorruptedFiles <- function(PackageSourcesDir, nlods) {
fixCorruptedFiles <- function(PackageSourcesDir, nlods, deleteTextures = TRUE) {

# Testes
# PackageSourcesDir <- "D:/FSProjects/florianopolis-megapack/florianopolis-mega/PackageSources"
Expand Down Expand Up @@ -42,7 +45,7 @@ fixCorruptedFiles <- function(PackageSourcesDir, nlods) {
} else {
message(length(invalidBinGltf), " arquivos inválidos.")
message("----------------------------")
fixLods(PackageSourcesDir, invalidLods)
fixLods(PackageSourcesDir, invalidLods, deleteTextures = deleteTextures)
}


Expand All @@ -55,7 +58,7 @@ fixCorruptedFiles <- function(PackageSourcesDir, nlods) {
} else {
message(length(invalidBinGltf), " arquivos inválidos.")
message("----------------------------")
fixBinGltf(PackageSourcesDir, invalidBinGltf)
fixBinGltf(PackageSourcesDir, invalidBinGltf, deleteTextures = deleteTextures)
}

}
Expand Down
26 changes: 14 additions & 12 deletions R/lods.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ checkAllLods <- function(modelLibDir) {

#' Fix Lods
#'
#' @param PackageSourcesDir
#' @param invalids
#' @param PackageSourcesDir Path to PackageSources directory.
#' @param invalids List with invalid files from `checkAllLods`.
#' @param deleteTextures Whether to delete textures in modelLib/texture related to the corrupted data.
#'
#' @return
#' @export
#'
#' @examples
fixLods <- function(PackageSourcesDir, invalids) {
fixLods <- function(PackageSourcesDir, invalids, deleteTextures = TRUE) {

# Delete invalid XML from modelLib
message("Removendo arquivos XML inválidos em modelLib")
Expand All @@ -86,14 +86,16 @@ fixLods <- function(PackageSourcesDir, invalids) {
message("----------------------------")

# Delete correspondent textures
ids <- stringr::str_remove(sapply(invalids, "[[", 3), ".xml$")
texToRemove <- list.files(file.path(PackageSourcesDir, "modelLib", "texture"),
paste0(ids, collapse = "|"),
all.files = TRUE,
full.names = TRUE)
statusPNG <- file.remove(texToRemove)
message(sum(statusPNG), " arquivos PNG (texture) removidos")
message("----------------------------")
if (isTRUE(deleteTextures)) {
ids <- stringr::str_remove(sapply(invalids, "[[", 3), ".xml$")
texToRemove <- list.files(file.path(PackageSourcesDir, "modelLib", "texture"),
paste0(ids, collapse = "|"),
all.files = TRUE,
full.names = TRUE)
statusPNG <- file.remove(texToRemove)
message(sum(statusPNG), " arquivos PNG (texture) removidos")
message("----------------------------")
}

# Clean corrupted guids from objects.xml
invalidGuids <- sapply(invalids, "[[", 2)
Expand Down
9 changes: 6 additions & 3 deletions R/objectsXml.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' Fix objects.xml
#'
#' @param xmlPath Path to the objects.xml file (usually located inside PackageSources/scene).
#' @param invalidGuids
#' @param createBackup
#' @param invalidGuids Vector with invalid GUIDS to remove from objects.xml.
#' @param createBackup Create a backup of objects.xml.
#'
#' @export
fixObjectsXML <- function(xmlPath, invalidGuids, createBackup = FALSE) {
Expand Down Expand Up @@ -49,7 +49,10 @@ fixObjectsXML <- function(xmlPath, invalidGuids, createBackup = FALSE) {
#' @return A vector with the guids of all objects.xml nodes.
#' @export
#'
#' @examples objectsXmlGuids("D:/FSProjects/my-package/PackageSources/scene/objects.xml")
#' @examples
#' \donttest{
#' objectsXmlGuids("D:/FSProjects/my-package/PackageSources/scene/objects.xml")
#' }
objectsXmlGuids <- function(xmlPath){

# Tests
Expand Down
124 changes: 124 additions & 0 deletions R/updateSceneryFiles.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#' Update Scenery Files
#'
#' Delete scenery files across the project (inside of modelLib and modelLib/texture) that are no longer registered in scene/objects.xml.
#' It can also remove .PNG.DDS and .PNG.DDS.json files located inside of a TEXTURE folder of an already built project by providing the TEXTURE path in `deleteBuiltTextures` argument.
#'
#' @param xmlPath Path to objects.xml file.
#' @param modelLibDir Path to modelLib directory.
#' If NULL/not declared, will search for default location.
#' @param deleteBuiltTextures If desired, pass path of TEXTURE directory in built package to delete textures already built.
#'
#' @export
#'
#' @examples
#' \donttest{
#' updateSceneryFiles("D:/FSProjects/my-package/PackageSources/scene/objects.xml")
#' updateSceneryFiles("D:/FSProjects/my-package/PackageSources/scene/objects.xml", deleteBuiltTextures = "D:/FSProjects/my-package/Packages/my-package/scenery/my-company/TEXTURE")
#' }
updateSceneryFiles <- function(xmlPath, modelLibDir = NULL, deleteBuiltTextures) {

# Tests
# xmlPath <- "D:/FSProjects/florianopolis-part2/PackageSources/scene/objects.xml"
# modelLibDir <- "D:/FSProjects/florianopolis-part2/PackageSources/modelLib"
# xmlPath <- "C:/Users/vinic/Downloads/SimpleScenery/PackageSources/scene/scenery.xml"
# xmlPath <- "C:/Users/vinic/Downloads/Manaus/SimpleScenery/PackageSources/scene/objects.xml"
#

baseName <- basename(xmlPath)
# if (!baseName == "objects.xml") stop("Must be the objects.xml. Usually located inside PackageSources/scene.")

if (!missing(deleteBuiltTextures)) stopifnot(inherits(deleteBuiltTextures, "character"))

if (is.null(modelLibDir)) {
if (!basename(dirname(xmlPath)) == "scene") stop("Could not guess the right location of modelLib, please provide modelLibDir explicitly.")
modelLibDir <- stringr::str_replace_all(xmlPath, paste0("scene/", baseName), "modelLib")
}
textureDir <- file.path(modelLibDir, "texture")

# Get all valid GUIDS from scene/objects.xml
guidsObjectsXML <- objectsXmlGuids(xmlPath)

# Open all .xml in modelLib and match GUIDS with number IDs
modelLibXmls <- list.files(modelLibDir, pattern = ".xml$", full.names = TRUE)
guidsModelLibXMLs <- sapply(modelLibXmls, getXmlGuid, USE.NAMES = FALSE)

# Create a vector with files to delete
namesToRemoveModelLib <- guidsModelLibXMLs[!guidsModelLibXMLs %in% guidsObjectsXML]
namesToRemoveModelLib <- names(namesToRemoveModelLib)
pattern <- paste0(namesToRemoveModelLib, collapse = "|")
if (pattern == "" || is.null(pattern)) pattern <- "#--|--#"
filesToRemoveModelLib <- list.files(modelLibDir, pattern = pattern, full.names = TRUE)
filesToRemoveModelLibTex <- list.files(textureDir, pattern = pattern, full.names = TRUE)
# Find files to remove (.DDS, .json)
if (!missing(deleteBuiltTextures)) {
filesToRemoveBuiltTex <- list.files(deleteBuiltTextures, pattern = paste0(namesToRemoveModelLib, collapse = "|"), full.names = TRUE)
} else {
filesToRemoveBuiltTex <- NULL
}

if (length(filesToRemoveModelLib) == 0 && length(filesToRemoveModelLibTex) == 0 && length(filesToRemoveBuiltTex) == 0) {
message("Scenery files are up to date with objects.xml. Nothing to remove.")
return(invisible())
}

# Ask yes/no confirmation question
res <- try(utils::askYesNo(paste0("Are you sure you want to delete all objects with the following name(s)?\n", paste0(namesToRemoveModelLib, collapse = "\n"))))
if (inherits(res, "try-error")) res <- try(utils::askYesNo("Are you sure you want to delete all objects not registered in objects.xml?"))
if (!inherits(res, "try-error") & !isTRUE(res)) {
message("Operation aborted.")
return(invisible())
}

# Delete all remaining .xml, .gltf and .bin inside modelLibDir
message("Removing " , length(filesToRemoveModelLib), " .xml/.gltf/.bin files in modelLib")
statusML <- file.remove(filesToRemoveModelLib)
message(sum(statusML), " .xml/.gltf/.bin files (texture) removed")
message("----------------------------")

# Delete all textures (.png) inside textureDir
message("Removing ", length(filesToRemoveModelLibTex), " .png texture files in modelLib")
statusPNG <- file.remove(filesToRemoveModelLibTex)
message(sum(statusPNG), " .png files (texture) removed")
message("----------------------------")

if (!missing(deleteBuiltTextures)) {

# Find files to remove (.DDS, .json)
# filesToRemoveBuiltTex <- list.files(deleteBuiltTextures, pattern = paste0(namesToRemoveModelLib, collapse = "|"), full.names = TRUE)
# Delete all remaining .xml, .gltf and .bin inside modelLibDir
message("Removing ", length(filesToRemoveBuiltTex), " .PNG.DDS/.PNG.json texture files in TEXTURE directory")
statusDBT <- file.remove(filesToRemoveBuiltTex)
message(sum(statusDBT), " .PNG.DDS/.PNG.json files (TEXTURE built) removed")
message("----------------------------")

}

return(invisible())

}

#' Get GUID from .xml
#'
#' Get GUID from a modelLib .xml file.
#'
#' @param xmlPath Path to .xml in modelLib.
#'
#' @return GUID related to the .xml provided.
#' @export
#'
#' @examples
#' \donttest{
#' getXmlGuid("D:/FSProjects/my-package/PackageSources/modelLib/03726152506153634.xml")
#' }
getXmlGuid <- function(xmlPath) {
obj <- xml2::read_xml(xmlPath)
# Get guid
guid <- toupper(
stringr::str_remove_all(
xml2::xml_attr(obj, "guid"),
pattern = "\\{|\\}"
)
)
names(guid) <- stringr::str_remove(basename(xmlPath), ".xml$")
return(guid)
}
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
# msfsSceneryTools


<!-- 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.1.2-blue.svg)](https://semver.org)
[![version](https://img.shields.io/badge/version-0.2.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.

## Installation

After installing R, just run the code above to install the latest version available:
After installing R (https://www.r-project.org), just run the code below to install the latest package stable version available:

```r
if (!require("remotes")) install.packages("remotes")
remotes::install_github("viniciuszendron/msfsSceneryTools@main")
```

If a message asking to update packages appear, just press Enter key or select the option "None".


## Changelog

### Version 0.2.0

#### Update Scenery Files

The new function `updateSceneryFiles` was created to remove remaining scenery files from modelLib and modelLib/texture that are no longer registered in scene/objects.xml (e.g. deleted photogrammetry tiles from in-game scenery editor). It can also remove .PNG.DDS and .PNG.DDS.json files located inside of a TEXTURE folder of an already built project.

#### Functions

##### New Functions

- `updateSceneryFiles`: Delete scenery files across the project (modelLib and modelLib/texture) that are no longer registered in scene/objects.xml. It can also remove .PNG.DDS and .PNG.DDS.json files located inside of a TEXTURE folder of an already built project by providing the TEXTURE path in `deleteBuiltTextures` argument.
- `getXmlGuid`: Get GUID from a modelLib .xml file.

### Version 0.1.2

- Functions `fixLods` and `fixBinGltf` (both executed from `fixCorruptedFiles`) now removes all correspondent texture files (.png) from folder *texture*. Previously, only the corrupted .bin, .gltf and .xml were deleted, but the textures themselves were not. This behavior could make the final package bigger to the end user (as Flight Simulator editor does not remove them automatically).
Expand Down
3 changes: 0 additions & 3 deletions man/checkAllFilesBinGltf.Rd

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

3 changes: 0 additions & 3 deletions man/checkAllLods.Rd

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

3 changes: 0 additions & 3 deletions man/checkFilesBinGltf.Rd

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

3 changes: 0 additions & 3 deletions man/checkLods.Rd

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

9 changes: 5 additions & 4 deletions man/fixBinGltf.Rd

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

Loading

0 comments on commit c7c943d

Please sign in to comment.