diff --git a/.Rbuildignore b/.Rbuildignore index 91114bf..9665f17 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,2 +1,3 @@ ^.*\.Rproj$ ^\.Rproj\.user$ +dev diff --git a/DESCRIPTION b/DESCRIPTION index 862127d..9b0dd19 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 Description: Tools to work with scenery creation in Microsoft Flight Simulator. diff --git a/NAMESPACE b/NAMESPACE index 9b50b57..033a061 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,3 +9,4 @@ export(fixCorruptedFiles) export(fixLods) export(fixObjectsXML) export(isValidXML) +export(objectsXmlGuids) diff --git a/R/BinGltf.R b/R/BinGltf.R index 3dcf7c7..b97bfbf 100644 --- a/R/BinGltf.R +++ b/R/BinGltf.R @@ -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) diff --git a/R/fixCorruptedFiles.R b/R/fixCorruptedFiles.R index 1b2e359..9f5abe0 100644 --- a/R/fixCorruptedFiles.R +++ b/R/fixCorruptedFiles.R @@ -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 diff --git a/R/lods.R b/R/lods.R index 4415791..72b8799 100644 --- a/R/lods.R +++ b/R/lods.R @@ -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) diff --git a/R/objectsXml.R b/R/objectsXml.R index 33e91f9..e28c05b 100644 --- a/R/objectsXml.R +++ b/R/objectsXml.R @@ -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) { @@ -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) + +} + diff --git a/README.md b/README.md index 64ee53c..66617e6 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/dev/script.R b/dev/script.R new file mode 100644 index 0000000..c33ea0f --- /dev/null +++ b/dev/script.R @@ -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] diff --git a/man/objectsXmlGuids.Rd b/man/objectsXmlGuids.Rd new file mode 100644 index 0000000..922379a --- /dev/null +++ b/man/objectsXmlGuids.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/objectsXml.R +\name{objectsXmlGuids} +\alias{objectsXmlGuids} +\title{Get guids from objects.xml nodes} +\usage{ +objectsXmlGuids(xmlPath) +} +\arguments{ +\item{xmlPath}{} +} +\description{ +Get guids from objects.xml nodes +} +\examples{ +objectsXmlNodes("D:/FSProjects/my-package/PackageSources/scene/objects.xml") +}