From 160488849f3bce5828d9d0327175df03f29628c5 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Tue, 18 Mar 2025 23:12:13 +0100 Subject: [PATCH] Implement counting files only of the latest version --- lib/mod.dart | 71 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 9 deletions(-) diff --git a/lib/mod.dart b/lib/mod.dart index f1603cc..50558b8 100644 --- a/lib/mod.dart +++ b/lib/mod.dart @@ -215,18 +215,71 @@ class Mod { int size = 0; if (!skipFileCount) { - size = + // Find all directories matching version pattern (like "1.0", "1.4", etc.) + final versionDirs = Directory(path) - .listSync(recursive: true) + .listSync(recursive: false) + .whereType() .where( - (entity) => - !entity.path - .split(Platform.pathSeparator) - .last - .startsWith('.'), + (dir) => RegExp( + r'^\d+\.\d+$', + ).hasMatch(dir.path.split(Platform.pathSeparator).last), ) - .length; - logger.info('File count in mod directory: $size'); + .toList(); + + // Find the latest version directory (if any) + Directory? latestVersionDir; + if (versionDirs.isNotEmpty) { + // Sort by version number + versionDirs.sort((a, b) { + final List vA = + a.path + .split(Platform.pathSeparator) + .last + .split('.') + .map(int.parse) + .toList(); + final List vB = + b.path + .split(Platform.pathSeparator) + .last + .split('.') + .map(int.parse) + .toList(); + return vA[0] != vB[0] + ? vA[0] - vB[0] + : vA[1] - vB[1]; // Compare major, then minor version + }); + latestVersionDir = versionDirs.last; + logger.info( + 'Latest version directory found: ${latestVersionDir.path.split(Platform.pathSeparator).last}', + ); + } + + // Count all files, excluding older version directories + size = + Directory(path).listSync(recursive: true).where((entity) { + if (entity is! File || + entity.path + .split(Platform.pathSeparator) + .any((part) => part.startsWith('.'))) { + return false; + } + + // Skip files in version directories except for the latest + for (final verDir in versionDirs) { + if (verDir != latestVersionDir && + entity.path.startsWith(verDir.path)) { + return false; + } + } + + return true; + }).length; + + logger.info( + 'File count in mod directory (with only latest version): $size', + ); } // Check if this is RimWorld base game or expansion