chore: split checkFileNames into subfunctions

This commit is contained in:
yairm210 2025-01-07 20:48:31 +02:00
parent 6f068d5fd3
commit b1da26c20c

View File

@ -1,6 +1,7 @@
package com.unciv.models.ruleset.validation
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.graphics.Color
import com.unciv.Constants
import com.unciv.UncivGame
@ -111,62 +112,94 @@ class RulesetValidator(val ruleset: Ruleset) {
}
private fun getPossibleMisspellings(originalText: String, possibleMisspellings: List<String>): List<String> =
possibleMisspellings.filter {
getRelativeTextDistance(
it,
originalText
) <= RulesetCache.uniqueMisspellingThreshold
}
private fun checkFileNames(lines: RulesetErrorList) {
val folder = ruleset.folderLocation ?: return
for (child in folder.list()){
checkMisplacedJsonFiles(folder, lines)
checkMisspelledFolders(folder, lines)
checkImagesFolders(folder, lines)
checkUnknownJsonFilenames(folder, lines)
}
private fun checkMisspelledFolders(
folder: FileHandle,
lines: RulesetErrorList
) {
val knownFolderNames = listOf("jsons", "maps", "sounds", "Images", "fonts")
for (child in folder.list()) {
if (!child.isDirectory || child.name() in knownFolderNames) continue
val possibleMisspellings = getPossibleMisspellings(child.name(), knownFolderNames)
if (possibleMisspellings.isNotEmpty())
lines.add(
"Folder \"${child.name()}\" is probably a misspelling of " + possibleMisspellings.joinToString("/"),
RulesetErrorSeverity.OK
)
}
}
private fun checkMisplacedJsonFiles(
folder: FileHandle,
lines: RulesetErrorList
) {
for (child in folder.list()) {
if (child.name().endsWith("json") && !child.name().startsWith("Atlas"))
lines.add("File ${child.name()} is located in the root folder - it should be moved to a 'jsons' folder")
}
}
fun getPossibleMisspellings(originalText: String, possibleMisspellings: List<String>): List<String> {
return possibleMisspellings.filter {
getRelativeTextDistance(
it,
originalText
) <= RulesetCache.uniqueMisspellingThreshold
}
}
private fun checkImagesFolders(
folder: FileHandle,
lines: RulesetErrorList
) {
val knownImageFolders =
Portrait.Type.entries.map { it.directory }.flatMap { listOf(it + "Icons", it + "Portraits") } +
listOf("CityStateIcons", "PolicyBranchIcons", "PolicyIcons", "OtherIcons", "EmojiIcons", "StatIcons", "TileIcons", "TileSets") // Not portrait-able (yet?)
val knownFolderNames = listOf("jsons", "maps", "sounds", "Images", "fonts")
for (child in folder.list()){
if (child.isDirectory && child.name() !in knownFolderNames){
val possibleMisspellings = getPossibleMisspellings(child.name(), knownFolderNames)
if (possibleMisspellings.isNotEmpty())
lines.add("Folder \"${child.name()}\" is probably a misspelling of "+possibleMisspellings.joinToString("/"),
RulesetErrorSeverity.OK)
}
}
val knownImageFolders = Portrait.Type.entries.map { it.directory }.flatMap { listOf(it+"Icons", it+"Portraits") } +
// Not portrait-able (yet?)
listOf("CityStateIcons", "PolicyBranchIcons", "PolicyIcons", "OtherIcons", "EmojiIcons", "StatIcons", "TileIcons", "TileSets")
val imageFolders = folder.list().filter { it.name().startsWith("Images") }
for (imageFolder in imageFolders){
for (child in imageFolder.list()){
for (imageFolder in imageFolders) {
for (child in imageFolder.list()) {
if (!child.isDirectory) {
lines.add("File \"$imageFolder/${child.name()}\" is misplaced - Images folders should not contain any files directly - only subfolders",
RulesetErrorSeverity.OK)
} else if (child.name() !in knownImageFolders){
lines.add(
"File \"$imageFolder/${child.name()}\" is misplaced - Images folders should not contain any files directly - only subfolders",
RulesetErrorSeverity.OK
)
} else if (child.name() !in knownImageFolders) {
val possibleMisspellings = getPossibleMisspellings(child.name(), knownImageFolders)
if (possibleMisspellings.isNotEmpty())
lines.add("Folder \"$imageFolder/${child.name()}\" is probably a misspelling of "+
possibleMisspellings.joinToString("/"),
RulesetErrorSeverity.OK)
lines.add(
"Folder \"$imageFolder/${child.name()}\" is probably a misspelling of " +
possibleMisspellings.joinToString("/"),
RulesetErrorSeverity.OK
)
}
}
}
}
private fun checkUnknownJsonFilenames(
folder: FileHandle,
lines: RulesetErrorList
) {
val jsonFolder = folder.child("jsons")
if (jsonFolder.exists()) {
for (file in jsonFolder.list("json")) {
if (file.name() !in RulesetFile.entries.map { it.filename }) {
var text = "File ${file.name()} is in the jsons folder but is not a recognized ruleset file"
val possibleMisspellings = getPossibleMisspellings(file.name(), RulesetFile.entries.map { it.filename })
if (possibleMisspellings.isNotEmpty()) text += "\nPossible misspelling of: "+possibleMisspellings.joinToString("/")
lines.add(text, RulesetErrorSeverity.OK)
}
}
if (!jsonFolder.exists()) return
for (file in jsonFolder.list("json")) {
if (file.name() in RulesetFile.entries.map { it.filename }) continue
var text = "File ${file.name()} is in the jsons folder but is not a recognized ruleset file"
val possibleMisspellings = getPossibleMisspellings(file.name(), RulesetFile.entries.map { it.filename })
if (possibleMisspellings.isNotEmpty())
text += "\nPossible misspelling of: " + possibleMisspellings.joinToString("/")
lines.add(text, RulesetErrorSeverity.OK)
}
}
@ -255,6 +288,7 @@ class RulesetValidator(val ruleset: Ruleset) {
"Victory type ${victoryType.name} requires adding the non-existant unit $requiredUnit to the capital to win!",
RulesetErrorSeverity.Warning, sourceObject = null
)
for (milestone in victoryType.milestoneObjects) {
if (milestone.type == null)
lines.add(
@ -268,6 +302,7 @@ class RulesetValidator(val ruleset: Ruleset) {
RulesetErrorSeverity.Error,
)
}
for (victory in ruleset.victories.values)
if (victory.name != victoryType.name && victory.milestones == victoryType.milestones)
lines.add(
@ -330,10 +365,9 @@ class RulesetValidator(val ruleset: Ruleset) {
tryFixUnknownUniques: Boolean
) {
for (policy in ruleset.policies.values) {
if (policy.requires != null)
for (prereq in policy.requires!!)
if (!ruleset.policies.containsKey(prereq))
lines.add("${policy.name} requires policy $prereq which does not exist!", sourceObject = policy)
for (prereq in policy.requires ?: emptyList())
if (!ruleset.policies.containsKey(prereq))
lines.add("${policy.name} requires policy $prereq which does not exist!", sourceObject = policy)
uniqueValidator.checkUniques(policy, lines, true, tryFixUnknownUniques)
}