Skip to content

Commit

Permalink
version 0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciuszendron committed Nov 4, 2020
1 parent 90308f1 commit e87f9ab
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 38 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
^.*\.Rproj$
^\.Rproj\.user$
dev
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.1
Version: 0.1.2
Author: Vinícius Zendron
Maintainer: Vinícius Zendron <[email protected]>
Description: Tools to work with scenery creation in Microsoft Flight Simulator.
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export(fixCorruptedFiles)
export(fixLods)
export(fixObjectsXML)
export(isValidXML)
export(objectsXmlGuids)
18 changes: 14 additions & 4 deletions R/BinGltf.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,22 @@ fixBinGltf <- function(PackageSourcesDir, invalids) {

# Delete invalid XML from modelLib
message("Removendo arquivos BIN e GLTF inválidos em modelLib")
filesToRemove <- paste0(file.path(PackageSourcesDir, "modelLib", sapply(invalids, "[[", 3)))
xmlsToRemove <- paste0(file.path(PackageSourcesDir, "modelLib", sapply(invalids, "[[", 2)))
filesToRemove <- file.path(PackageSourcesDir, "modelLib", sapply(invalids, "[[", 3))
xmlsToRemove <- file.path(PackageSourcesDir, "modelLib", sapply(invalids, "[[", 2))
status <- file.remove(c(filesToRemove, xmlsToRemove))
message(sum(status), " arquivos removidos")
message(sum(status), " arquivos BIN/GLTF/XML removidos")
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("----------------------------")


# Clean corrupted guids from objects.xml
invalidGuids <- sapply(invalids, "[[", 4)
Expand Down
27 changes: 0 additions & 27 deletions R/fixCorruptedFiles.R
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
# library(magrittr)
# dir_modelLib = "D:/FSProjects/florianopolis-megapack/florianopolis-mega/PackageSources/modelLib"
# xmls = dir(dir_modelLib) %>% .[stringr::str_detect(., "xml")]
# count_xmls = length(xmls)
# modelLibObjs = xmls %>% stringr::str_remove_all(., stringr::fixed(".xml"))
#
# dir_scene = "D:/FSProjects/florianopolis-megapack/florianopolis-mega/PackageSources/scene"
# objects.xml = xml2::read_xml(file.path(dir_scene, "objects.xml"))
# SceneryObjectNodes = xml2::xml_find_all(objects.xml, ".//SceneryObject")
# length(SceneryObjectNodes)
#
#
# dir_output = "D:/FSProjects/florianopolis-megapack/florianopolis-mega/Packages/msfsmaps-florianopolis-mega"
# filesTEXTURE = dir(file.path(dir_output, "scenery/msfsmaps/TEXTURE"))
# filesTEXTUREUnique = filesTEXTURE %>%
# stringr::str_remove_all(., stringr::fixed(".PNG.DDS")) %>%
# stringr::str_remove_all(., stringr::fixed(".json")) %>%
# stringr::str_remove_all(., "_.*") %>%
# unique()
#
# modelLibObjs[!modelLibObjs %in% filesTEXTUREUnique]


#install.packages("xml2")
#install.packages("stringr")


#' Detect and Remove Corrupted Files
#'
#' @param PackageSourcesDir
Expand Down
15 changes: 12 additions & 3 deletions R/lods.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,20 @@ fixLods <- function(PackageSourcesDir, invalids) {

# Delete invalid XML from modelLib
message("Removendo arquivos XML inválidos em modelLib")
filesToRemove <- paste0(file.path(PackageSourcesDir, "modelLib", sapply(invalids, "[[", 3)))
filesToRemove <- file.path(PackageSourcesDir, "modelLib", sapply(invalids, "[[", 3))
status <- file.remove(filesToRemove)
message(sum(status), " arquivos removidos")
message(sum(status), " arquivos XML removidos")
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("----------------------------")
#

# Clean corrupted guids from objects.xml
invalidGuids <- sapply(invalids, "[[", 2)
Expand Down
34 changes: 32 additions & 2 deletions R/objectsXml.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#' Fix objects.xml
#'
#' @param xmlPath
#' @param xmlPath Path to the objects.xml file (usually located inside PackageSources/scene).
#' @param invalidGuids
#' @param createBackup
#'
#' @return
#' @export
fixObjectsXML <- function(xmlPath, invalidGuids, createBackup = FALSE) {

Expand Down Expand Up @@ -39,3 +38,34 @@ fixObjectsXML <- function(xmlPath, invalidGuids, createBackup = FALSE) {
xml2::write_xml(obj, xmlPath)

}


#' Get guids from objects.xml nodes
#'
#' Get all guids identifiers registered in objects.xml file.
#'
#' @param xmlPath Path to the objects.xml file (usually located inside PackageSources/scene).
#'
#' @return A vector with the guids of all objects.xml nodes.
#' @export
#'
#' @examples objectsXmlGuids("D:/FSProjects/my-package/PackageSources/scene/objects.xml")
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")
message(basename(xmlPath), " inicializado com ", length(LibObjNodes), " entradas")

guids <- xml2::xml_attr(LibObjNodes, attr = "name")

guids <- stringr::str_replace_all(guids, "\\{|\\}", "")

return(guids)

}

21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# msfsSceneryTools


[![version](https://img.shields.io/badge/version-0.1.2-blue.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.

## Installation

After installing R, just run the code above to install the latest version available:

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

## Changelog

### 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).
- New function: `objectsXmlGuids` (Get all guids identifiers registered in objects.xml file).
82 changes: 82 additions & 0 deletions dev/script.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Install Package if not installed
if (!require("remotes")) install.packages("remotes")
if (!require("msfsSceneryTools")) remotes::install_github("viniciuszendron/msfsSceneryTools@main")

# Check Package Version
packageVersion("msfsSceneryTools")

# Set main variables
# Change these values to the values of your scenery!
PackageSourcesDir <- "C:/.../PackageSources"
modelLibDir <- "D:/.../PackageSources/modelLib"
nlods <- 1 # Number of lods

# Perform checks
binGltfCheck <- msfsSceneryTools::checkAllFilesBinGltf(modelLibDir, nlods = nlods)
lodsCheck <- msfsSceneryTools::checkAllLods(modelLibDir)

# Check which files are INVALID
invalidBinGltf <- Filter(function(x) isFALSE(x[[1]][1]), binGltfCheck)
sapply(invalidBinGltf, "[[", 4) # Get guids of invalidBinGltf
names(invalidBinGltf)
invalidLods <- Filter(function(x) isFALSE(x[[1]][1]), lodsCheck)
names(invalidLods)

# Check which are VALID
validBinGltf <- Filter(function(x) isTRUE(x[[1]][1]), binGltfCheck)
names(validBinGltf)
validLods <- Filter(function(x) isTRUE(x[[1]][1]), lodsCheck)
names(validLods)

# Fix both Lods and missing bin/gltf files
msfsSceneryTools::fixCorruptedFiles(PackageSourcesDir, nlods = nlods)

# Just check if there are no invalid files anymore
invisible(msfsSceneryTools::checkAllFilesBinGltf(modelLibDir, nlods = nlods))
invisible(msfsSceneryTools::checkAllLods(modelLibDir))


# Now, it is time to build the scenery package and see if everything is fine. It should not return error or failed.
# Code below is not necessary - debug only

# Check if the exported package is OK
library(magrittr)
xmls <- dir(modelLibDir) %>% .[stringr::str_detect(., "xml")]
count_xmls <- length(xmls)
modelLibObjs <- xmls %>% stringr::str_remove_all(., stringr::fixed(".xml"))

#dir_scene <- "D:/FSProjects/florianopolis-megapack/florianopolis-mega/PackageSources/scene"
#objects.xml <- xml2::read_xml(file.path(dir_scene, "objects.xml"))
#SceneryObjectNodes <- xml2::xml_find_all(objects.xml, ".//SceneryObject")
#length(SceneryObjectNodes)

outputDir <- "D:/FSProjects/maceio/Packages/flv-maceio"

filesTEXTURE <- dir(file.path(outputDir, "scenery/flv/TEXTURE"))
filesTEXTUREUnique <- filesTEXTURE %>%
stringr::str_remove_all(., stringr::fixed(".PNG.DDS")) %>%
stringr::str_remove_all(., stringr::fixed(".json")) %>%
stringr::str_remove_all(., "_.*") %>%
unique()

modelLibObjs[!modelLibObjs %in% filesTEXTUREUnique]


# Get all objects.xml nodes and compare
guids <- objectsXmlGuids("D:/FSProjects/maceio/PackageSources/scene/objects.xml")

guidsBinGltf <- sapply(binGltfCheck, "[[", 4)
guidsLods <- sapply(lodsCheck, "[[", 2)
length(lodsCheck)
length(binGltfCheck)
length(guids)

all(
length(lodsCheck) == length(binGltfCheck),
length(guids) == length(lodsCheck),
length(guids) == length(binGltfCheck)
)

guidsBinGltf[guidsBinGltf %in% guids]
guids[guids %in% guidsBinGltf]
guidsBinGltf[!guidsBinGltf %in% guids]
17 changes: 17 additions & 0 deletions man/objectsXmlGuids.Rd

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

0 comments on commit e87f9ab

Please sign in to comment.