Browse Source

Fix path formatting

Michael Ragazzon 6 years ago
parent
commit
f190bb561e
2 changed files with 31 additions and 12 deletions
  1. 27 7
      Samples/shell/src/win32/ShellWin32.cpp
  2. 4 5
      Source/Core/URL.cpp

+ 27 - 7
Samples/shell/src/win32/ShellWin32.cpp

@@ -32,6 +32,8 @@
 #include "ShellFileInterface.h"
 #include <stdio.h>
 #include <stdarg.h>
+#include <shlwapi.h>
+#pragma comment( lib  , "Shlwapi.lib"  )
 
 static LRESULT CALLBACK WindowProcedure(HWND window_handle, UINT message, WPARAM w_param, LPARAM l_param);
 
@@ -76,11 +78,12 @@ bool Shell::Initialise()
 	cursor_unavailable = LoadCursor(nullptr, IDC_NO);
 
 	Rml::Core::String root = FindSamplesRoot();
+	bool result = !root.empty();
 	
 	file_interface = std::make_unique<ShellFileInterface>(root);
 	Rml::Core::SetFileInterface(file_interface.get());
 
-	return true;
+	return result;
 }
 
 void Shell::Shutdown()
@@ -92,9 +95,9 @@ void Shell::Shutdown()
 
 Rml::Core::String Shell::FindSamplesRoot()
 {
-	Rml::Core::String path = "../../Samples/";
+	const char* candidate_paths[] = { "", "..\\..\\Samples\\", "..\\Samples\\"  };
 	
-	// Fetch the path of the executable, append the path onto that.
+	// Fetch the path of the executable, test the candidate paths appended to that.
 	char executable_file_name[MAX_PATH];
 	if (GetModuleFileNameA(instance_handle, executable_file_name, MAX_PATH) >= MAX_PATH &&
 		GetLastError() == ERROR_INSUFFICIENT_BUFFER)
@@ -102,10 +105,27 @@ Rml::Core::String Shell::FindSamplesRoot()
 		executable_file_name[0] = 0;
 	}
 
-	Rml::Core::String executable_path = Rml::Core::String(executable_file_name);
-	executable_path = executable_path.substr(0, executable_path.rfind("\\") + 1);
-	
-	return executable_path + path;
+	Rml::Core::String executable_path(executable_file_name);
+	executable_path = executable_path.substr(0, executable_path.rfind('\\') + 1);
+
+	// We assume we have found the correct path if we can find the lookup file from it
+	const char* lookup_file = "assets\\rml.rcss";
+
+	for(const char* relative_path : candidate_paths)
+	{
+		Rml::Core::String absolute_path = executable_path + relative_path;
+
+		if (PathFileExistsA(Rml::Core::String(absolute_path + lookup_file).c_str()))
+		{
+			char canonical_path[MAX_PATH];
+			if (!PathCanonicalizeA(canonical_path, absolute_path.c_str()))
+				canonical_path[0] = 0;
+
+			return Rml::Core::String(canonical_path);
+		}
+	}
+
+	return Rml::Core::String();
 }
 
 static ShellRenderInterfaceExtensions *shell_renderer = nullptr;

+ 4 - 5
Source/Core/URL.cpp

@@ -222,13 +222,12 @@ bool URL::SetURL(const String& _url)
 
 		// Normalise the path, stripping any ../'s from it
 		size_t parent_dir_pos = String::npos;
-		while ((parent_dir_pos = path.find("/..")) != String::npos)
+		while ((parent_dir_pos = path.find("/..")) != String::npos && parent_dir_pos != 0)
 		{
-			// If we found a /.. we should be able to find the start of the parent
-			// directory, if we can't something wierd has happend, bail
-			size_t parent_dir_start_pos = path.rfind("/", parent_dir_pos - 1);
+			// Find the start of the parent directory.
+			size_t parent_dir_start_pos = path.rfind('/', parent_dir_pos - 1);
 			if (parent_dir_start_pos == String::npos)
-				break;
+				parent_dir_start_pos = 0;
 
 			// Strip out the parent dir and the /..
 			path.erase(parent_dir_start_pos, parent_dir_pos - parent_dir_start_pos + 3);