From 4034b8d48d3bf9ad5f347b2b46177f32b7e0a0d9 Mon Sep 17 00:00:00 2001 From: Exund Date: Sun, 17 Apr 2022 15:41:41 +0200 Subject: [PATCH] Fix dependencies version check + clearer verifyVersion parameter names --- ModLoader/Loader.cs | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/ModLoader/Loader.cs b/ModLoader/Loader.cs index 7cd6689..51ad82f 100644 --- a/ModLoader/Loader.cs +++ b/ModLoader/Loader.cs @@ -34,6 +34,8 @@ public class Loader : MonoBehaviour /// public static GameObject root; + private static readonly Regex version_regex = new Regex(@"\bv([0-9]+)(\.([0-9]+|x)){2}\b", RegexOptions.Compiled | RegexOptions.IgnoreCase); + /// /// Current Modlaoder version /// @@ -232,7 +234,7 @@ private bool loadDependencies(Dictionary dependencies) bool versionFlag = false; foreach (string version in item.Value) { - if (verifyVersion(dependency.Version, version)) + if (verifyVersion(version, dependency.Version)) { versionFlag = true; break; @@ -294,49 +296,45 @@ private void loadMod(SFSMod mod) /// /// Check the string of two versions to identify if they are the same /// - /// + /// /// Version to check /// - /// + /// /// Verison to check /// - /// - /// check if version 1 is is less than or equal to version 2 + /// + /// Check if targetVersion is less than or equal to currentVersion /// - /// - /// True if they are valid versions - /// - private bool verifyVersion(string version1, string version2, bool checkMinVersion = false) + private bool verifyVersion(string targetVersion, string currentVersion, bool lessOrEqual = false) { - Regex rx = new Regex(@"\bv([0-9]+)(\.([0-9]+|x)){2}\b", RegexOptions.Compiled | RegexOptions.IgnoreCase); // has the format v1.x.x - if (rx.IsMatch(version1) && rx.IsMatch(version2)) + if (version_regex.IsMatch(targetVersion) && version_regex.IsMatch(currentVersion)) { - string[] target1 = version1.Split('.'); - string[] target2 = version2.Split('.'); - int num1, num2; + string[] target = targetVersion.Split('.'); + string[] current = currentVersion.Split('.'); + bool minor = false; - if (target1.Length == target2.Length) + if (target.Length == current.Length) { - for (short index = 0; index < target2.Length; index++) + for (short index = 0; index < current.Length; index++) { - if (target1[index] == "x" || target1[index] == target2[index]) + if (target[index] == "x" || target[index] == current[index]) { continue; } - // check if version1 is MIN that version2 - if (checkMinVersion) + // check if targetVersion is MIN that currentVersion + if (lessOrEqual) { // the previous section was MIN, if it was min the rest are MIN too if (minor) { continue; } - if (int.TryParse(target1[index], out num1)) + if (int.TryParse(target[index], out int num1)) { - if(int.TryParse(target2[index], out num2)) + if(int.TryParse(current[index], out int num2)) { - // the version1 section is MIN that version 2 section + // the targetVersion section is MIN that version 2 section if(num1 < num2) { minor = true;