From b54709c26d43cf5162e85aa84eb8925c53d63cc5 Mon Sep 17 00:00:00 2001 From: Zoey Date: Sat, 15 Jan 2022 12:31:13 -0800 Subject: [PATCH] Prevent VCS thrashing - Use platform-independent paths in OpenVRSettings `OpenVRSettings` rewrites the `Assets/XR/Settings/Open VR Settings.asset` on project startup; it continues where the original fix to #11 left off. This change avoids inconsistent serialization across platforms, preventing VCS conflicts between team-members across Linux / Windows / Mac editor targets. As the Unity built-in VCS integration does not understand the concept of path equivalence, this avoids conflicts in naive "sync" workflows and unnecessary differences in working trees. Serialized formats for cross-platform files should not use `System.IO.Path.DirectorySeparatorChar` unless they replace it during encoding and substitute the consistent variant during decoding. As all target platforms for the editor support forward-slash paths, a constant avoids the whole issue. Additionally, this PR: - Eliminates an unnecessary side-effect-free computation of a new path when not in the editor - Corrects inconsistent usage of indentation (`Awake` was indented with tabs while the rest was spaces) --- com.valve.openvr/Runtime/OpenVRSettings.cs | 31 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/com.valve.openvr/Runtime/OpenVRSettings.cs b/com.valve.openvr/Runtime/OpenVRSettings.cs index 520c0b2..d1ac545 100644 --- a/com.valve.openvr/Runtime/OpenVRSettings.cs +++ b/com.valve.openvr/Runtime/OpenVRSettings.cs @@ -18,6 +18,25 @@ namespace Unity.XR.OpenVR [System.Serializable] public class OpenVRSettings : ScriptableObject { + // Convert backslashes in relative paths to forward-slashes for cross-platform compatibility + // Windows Volume-prefixed paths and POSIX absolute paths have semantics preserved. + // Only call on output paths constructed with System.IO.Path.* on the current platform; + // For paths coming from other platforms, forward-slash must be valid or decoded; + // This is not necessary here, as forward-slash is valid in all Unity targets. + private static string SlashifyPath(string path) { + var sep = System.IO.Path.DirectorySeparatorChar; + var sepAlt = System.IO.Path.AltDirectorySeparatorChar; + // Only convert when both back slashes and forward slashes are semantically valid in the + // context of the current platform's Path handling. + // Avoids semantic alteration of questionable paths on POSIX systems where backslash is + // usually used as an escape character, and are not generally included within the path. + if ((sep == '\\' || sepAlt == '\\') && (sep == '/' || sepAlt == '/')) + { + return path.Replace('\\', '/'); + } + return path; + } + public enum StereoRenderingModes { MultiPass = 0, @@ -162,6 +181,7 @@ public static OpenVRSettings GetSettings(bool create = true) public bool InitializeActionManifestFileRelativeFilePath() { +#if UNITY_EDITOR string oldPath = ActionManifestFileRelativeFilePath; string newPath; @@ -174,13 +194,14 @@ public bool InitializeActionManifestFileRelativeFilePath() if (newPath.StartsWith("Assets")) newPath = newPath.Remove(0, "Assets".Length + 1); + + newPath = SlashifyPath(newPath); } else { newPath = null; } -#if UNITY_EDITOR if (newPath != oldPath) { ActionManifestFileRelativeFilePath = newPath; @@ -205,10 +226,10 @@ public void Awake() #else public static OpenVRSettings s_Settings; - public void Awake() - { - s_Settings = this; - } + public void Awake() + { + s_Settings = this; + } #endif } }