From 4ac030ab35a6c9dde5790ee3409ada04ceb31e5c Mon Sep 17 00:00:00 2001 From: Daniel Hultgren Date: Tue, 24 Dec 2019 23:11:32 +0100 Subject: [PATCH] Added unit tests --- I3DShapesTool.sln | 14 ++++ I3DShapesTool/I3DShape.cs | 2 +- I3DShapesTool/I3DShapesFile.cs | 2 +- I3DShapesTool/I3DTri.cs | 2 +- I3DShapesTool/I3DUV.cs | 2 +- I3DShapesTool/I3DVector.cs | 2 +- I3DShapesTool/WavefrontObj.cs | 2 +- I3DShapesToolTest/I3DShapesToolTest.csproj | 75 +++++++++++++++++ I3DShapesToolTest/Properties/AssemblyInfo.cs | 20 +++++ I3DShapesToolTest/SteamHelper.cs | 88 ++++++++++++++++++++ I3DShapesToolTest/UnitTest.cs | 78 +++++++++++++++++ I3DShapesToolTest/packages.config | 5 ++ 12 files changed, 286 insertions(+), 6 deletions(-) create mode 100644 I3DShapesToolTest/I3DShapesToolTest.csproj create mode 100644 I3DShapesToolTest/Properties/AssemblyInfo.cs create mode 100644 I3DShapesToolTest/SteamHelper.cs create mode 100644 I3DShapesToolTest/UnitTest.cs create mode 100644 I3DShapesToolTest/packages.config diff --git a/I3DShapesTool.sln b/I3DShapesTool.sln index 34ed0eb..168529e 100644 --- a/I3DShapesTool.sln +++ b/I3DShapesTool.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.26730.10 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "I3DShapesTool", "I3DShapesTool\I3DShapesTool.csproj", "{0E254DD0-B7E2-4009-A460-ED817130AAFD}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "I3DShapesToolTest", "I3DShapesToolTest\I3DShapesToolTest.csproj", "{9DFD6611-B506-4F53-961E-912DCE4EFD9E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,18 @@ Global {0E254DD0-B7E2-4009-A460-ED817130AAFD}.Release|x64.Build.0 = Release|x64 {0E254DD0-B7E2-4009-A460-ED817130AAFD}.Release|x86.ActiveCfg = Release|x86 {0E254DD0-B7E2-4009-A460-ED817130AAFD}.Release|x86.Build.0 = Release|x86 + {9DFD6611-B506-4F53-961E-912DCE4EFD9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9DFD6611-B506-4F53-961E-912DCE4EFD9E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9DFD6611-B506-4F53-961E-912DCE4EFD9E}.Debug|x64.ActiveCfg = Debug|Any CPU + {9DFD6611-B506-4F53-961E-912DCE4EFD9E}.Debug|x64.Build.0 = Debug|Any CPU + {9DFD6611-B506-4F53-961E-912DCE4EFD9E}.Debug|x86.ActiveCfg = Debug|Any CPU + {9DFD6611-B506-4F53-961E-912DCE4EFD9E}.Debug|x86.Build.0 = Debug|Any CPU + {9DFD6611-B506-4F53-961E-912DCE4EFD9E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9DFD6611-B506-4F53-961E-912DCE4EFD9E}.Release|Any CPU.Build.0 = Release|Any CPU + {9DFD6611-B506-4F53-961E-912DCE4EFD9E}.Release|x64.ActiveCfg = Release|Any CPU + {9DFD6611-B506-4F53-961E-912DCE4EFD9E}.Release|x64.Build.0 = Release|Any CPU + {9DFD6611-B506-4F53-961E-912DCE4EFD9E}.Release|x86.ActiveCfg = Release|Any CPU + {9DFD6611-B506-4F53-961E-912DCE4EFD9E}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/I3DShapesTool/I3DShape.cs b/I3DShapesTool/I3DShape.cs index 3566c3a..eaa8978 100644 --- a/I3DShapesTool/I3DShape.cs +++ b/I3DShapesTool/I3DShape.cs @@ -3,7 +3,7 @@ namespace I3DShapesTool { - class I3DShape + public class I3DShape { public int Type { get; } diff --git a/I3DShapesTool/I3DShapesFile.cs b/I3DShapesTool/I3DShapesFile.cs index 35a1207..40aa1f2 100644 --- a/I3DShapesTool/I3DShapesFile.cs +++ b/I3DShapesTool/I3DShapesFile.cs @@ -7,7 +7,7 @@ namespace I3DShapesTool { - class I3DShapesFile + public class I3DShapesFile { public string FilePath { get; private set; } public string FileName => Path.GetFileName(FilePath); diff --git a/I3DShapesTool/I3DTri.cs b/I3DShapesTool/I3DTri.cs index bcb32c6..f69ae65 100644 --- a/I3DShapesTool/I3DTri.cs +++ b/I3DShapesTool/I3DTri.cs @@ -2,7 +2,7 @@ namespace I3DShapesTool { - class I3DTri + public class I3DTri { public ushort P1Idx { get; set; } public ushort P2Idx { get; set; } diff --git a/I3DShapesTool/I3DUV.cs b/I3DShapesTool/I3DUV.cs index 9429059..6de6549 100644 --- a/I3DShapesTool/I3DUV.cs +++ b/I3DShapesTool/I3DUV.cs @@ -2,7 +2,7 @@ namespace I3DShapesTool { - class I3DUV + public class I3DUV { public float U { get; } public float V { get; } diff --git a/I3DShapesTool/I3DVector.cs b/I3DShapesTool/I3DVector.cs index 0841dae..52656d0 100644 --- a/I3DShapesTool/I3DVector.cs +++ b/I3DShapesTool/I3DVector.cs @@ -2,7 +2,7 @@ namespace I3DShapesTool { - class I3DVector + public class I3DVector { public float X { get; set; } public float Y { get; set; } diff --git a/I3DShapesTool/WavefrontObj.cs b/I3DShapesTool/WavefrontObj.cs index 64cc687..ca52ad2 100644 --- a/I3DShapesTool/WavefrontObj.cs +++ b/I3DShapesTool/WavefrontObj.cs @@ -3,7 +3,7 @@ namespace I3DShapesTool { - class WavefrontObj + public class WavefrontObj { public string Name { get; set; } diff --git a/I3DShapesToolTest/I3DShapesToolTest.csproj b/I3DShapesToolTest/I3DShapesToolTest.csproj new file mode 100644 index 0000000..7ef702d --- /dev/null +++ b/I3DShapesToolTest/I3DShapesToolTest.csproj @@ -0,0 +1,75 @@ + + + + + + Debug + AnyCPU + {9DFD6611-B506-4F53-961E-912DCE4EFD9E} + Library + Properties + I3DShapesToolTest + I3DShapesToolTest + v4.6.1 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + + {0e254dd0-b7e2-4009-a460-ed817130aafd} + I3DShapesTool + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/I3DShapesToolTest/Properties/AssemblyInfo.cs b/I3DShapesToolTest/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a870e98 --- /dev/null +++ b/I3DShapesToolTest/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("I3DShapesToolTest")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("I3DShapesToolTest")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("9dfd6611-b506-4f53-961e-912dce4efd9e")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/I3DShapesToolTest/SteamHelper.cs b/I3DShapesToolTest/SteamHelper.cs new file mode 100644 index 0000000..1bf15cc --- /dev/null +++ b/I3DShapesToolTest/SteamHelper.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.Win32; + +namespace I3DShapesToolTest +{ + class SteamHelper + { + private static List steamGameDirs; + + public static void LoadSteamGameDirectories() + { + steamGameDirs = new List(); + const string steam32 = "SOFTWARE\\VALVE\\"; + const string steam64 = "SOFTWARE\\Wow6432Node\\Valve\\"; + var key32 = Registry.LocalMachine.OpenSubKey(steam32); + var key64 = Registry.LocalMachine.OpenSubKey(steam64); + if (key64?.ToString() == null || key64.ToString() == "") + { + foreach (var k32SubKey in key32.GetSubKeyNames()) + { + using (var subKey = key32.OpenSubKey(k32SubKey)) + { + var steam32Path = subKey?.GetValue("InstallPath").ToString(); + var config32Path = $"{steam32Path}/steamapps/libraryfolders.vdf"; + const string driveRegex = @"[A-Z]:\\"; + if (!File.Exists(config32Path)) continue; + var configLines = File.ReadAllLines(config32Path); + foreach (var item in configLines) + { + //Console.WriteLine($"32: {item}"); + var match = Regex.Match(item, driveRegex); + if (item == string.Empty || !match.Success) continue; + var matched = match.ToString(); + var item2 = item.Substring(item.IndexOf(matched, StringComparison.Ordinal)); + item2 = item2.Replace("\\\\", "\\"); + item2 = item2.Replace("\"", "\\steamapps\\common\\"); + steamGameDirs.Add(item2); + } + steamGameDirs.Add($"{steam32Path}\\steamapps\\common\\"); + } + } + } + foreach (var k64SubKey in key64.GetSubKeyNames()) + { + using (var subKey = key64.OpenSubKey(k64SubKey)) + { + var steam64Path = subKey?.GetValue("InstallPath").ToString(); + var config64Path = $"{steam64Path}/steamapps/libraryfolders.vdf"; + const string driveRegex = @"[A-Z]:\\"; + if (!File.Exists(config64Path)) continue; + var configLines = File.ReadAllLines(config64Path); + foreach (var item in configLines) + { + //Console.WriteLine($"64: {item}"); + var match = Regex.Match(item, driveRegex); + if (item == string.Empty || !match.Success) continue; + var matched = match.ToString(); + var item2 = item.Substring(item.IndexOf(matched, StringComparison.Ordinal)); + item2 = item2.Replace("\\\\", "\\"); + item2 = item2.Replace("\"", "\\steamapps\\common\\"); + steamGameDirs.Add(item2); + } + steamGameDirs.Add($"{steam64Path}\\steamapps\\common\\"); + } + } + } + + public static string GetGameDirectory(string gameFolderName) + { + foreach (var steamGameDir in steamGameDirs) + { + if (Directory.Exists(Path.Combine(steamGameDir, gameFolderName))) + return Path.Combine(steamGameDir, gameFolderName); + } + + Assert.Inconclusive($"{gameFolderName} not found installed."); + + return null; + } + } +} diff --git a/I3DShapesToolTest/UnitTest.cs b/I3DShapesToolTest/UnitTest.cs new file mode 100644 index 0000000..fa4faa5 --- /dev/null +++ b/I3DShapesToolTest/UnitTest.cs @@ -0,0 +1,78 @@ +using System; +using System.IO; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using I3DShapesTool; + +namespace I3DShapesToolTest +{ + [TestClass] + public class UnitTest + { + [TestInitialize] + public void TestInit() + { + SteamHelper.LoadSteamGameDirectories(); + } + + private static void AssertShapesFile(I3DShapesFile file, int seed, int version, int shapeCount) + { + Assert.AreEqual(seed, file.Seed, "Unexpected seed"); + Assert.AreEqual(version, file.Version, "Unexpected version"); + Assert.AreEqual(shapeCount, file.ShapeCount, "Unexpected shape count"); + } + + private static void AssertShape(I3DShape shape, string name, uint shapeId, int vertexCount, int faceCount) + { + Assert.AreEqual(name, shape.Name); + Assert.AreEqual(shapeId, shape.ShapeId); + Assert.AreEqual(vertexCount, shape.Positions.Length); + Assert.AreEqual(vertexCount, shape.Normals.Length); + Assert.AreEqual(vertexCount, shape.UVs.Length); + Assert.AreEqual(faceCount, shape.Triangles.Length); + } + + [TestMethod] + public void TestFS19_1() + { + var gameFolder = SteamHelper.GetGameDirectory("Farming Simulator 19"); + + var file = new I3DShapesFile(); + file.Load(Path.Combine(gameFolder, @"data\vehicles\magsi\telehandlerBaleFork\telehandlerBaleFork.i3d.shapes")); + AssertShapesFile(file, 201, 5, 9); + AssertShape(file.Shapes[0], "colPartBackShape1", 4, 24, 12); + } + + [TestMethod] + public void TestFS17_1() + { + var gameFolder = SteamHelper.GetGameDirectory("Farming Simulator 17"); + + var file = new I3DShapesFile(); + file.Load(Path.Combine(gameFolder, @"data\vehicles\tools\magsi\wheelLoaderLogFork.i3d.shapes")); + AssertShapesFile(file, 49, 5, 12); + AssertShape(file.Shapes[0], "wheelLoaderLogForkShape", 1, 24, 12); + } + + [TestMethod] + public void TestFS15_1() + { + var gameFolder = SteamHelper.GetGameDirectory("Farming Simulator 15"); + + var file = new I3DShapesFile(); + file.Load(Path.Combine(gameFolder, @"data\vehicles\tools\grimme\grimmeFT300.i3d.shapes")); + AssertShapesFile(file, 188, 3, 16); + AssertShape(file.Shapes[0], "grimmeFTShape300", 1, 40, 20); + } + + [TestMethod] + public void TestFS13_1() + { + var gameFolder = SteamHelper.GetGameDirectory("Farming Simulator 2013"); + + var file = new I3DShapesFile(); + file.Load(Path.Combine(gameFolder, @"data\vehicles\tools\kuhn\kuhnGA4521GM.i3d.shapes")); + AssertShapesFile(file, 68, 2, 32); + AssertShape(file.Shapes[0], "blanketBarShape2", 26, 68, 44); + } + } +} diff --git a/I3DShapesToolTest/packages.config b/I3DShapesToolTest/packages.config new file mode 100644 index 0000000..102a45c --- /dev/null +++ b/I3DShapesToolTest/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file