Browse Source

Handle Urho3D.exe commandline as wchars on Windows.

Lasse Öörni 14 years ago
parent
commit
f3c36de839
4 changed files with 36 additions and 21 deletions
  1. 11 4
      Engine/Container/Str.h
  2. 7 11
      Engine/Core/ProcessUtils.cpp
  3. 1 1
      Engine/Core/ProcessUtils.h
  4. 17 5
      Urho3D/Urho3D.cpp

+ 11 - 4
Engine/Container/Str.h

@@ -355,7 +355,7 @@ public:
     void SetUTF8FromWChar(const wchar_t* str);
     /// Calculate number of characters in UTF8 content.
     unsigned LengthUTF8() const;
-    /// Return byte offset for UTF8 index.
+    /// Return byte offset to char in UTF8 content.
     unsigned ByteOffsetUTF8(unsigned index) const;
     /// Return next Unicode character from UTF8 content and increase byte offset.
     unsigned NextUTF8Char(unsigned& byteOffset) const;
@@ -464,7 +464,7 @@ inline String operator + (const char* lhs, const String& rhs)
     return ret;
 }
 
-/// Wide character string. Only meant for converting from UTF8 String and passing to the operating system where necessary.
+/// Wide character string. Only meant for converting from String and passing to the operating system where necessary.
 class WString
 {
 public:
@@ -475,9 +475,16 @@ public:
     /// Destruct.
     ~WString();
     
-    /// Resize string.
+    /// Return char at index.
+    wchar_t& operator [] (unsigned index) { assert(index < length_); return buffer_[index]; }
+    /// Return const char at index.
+    const wchar_t& operator [] (unsigned index) const { assert(index < length_); return buffer_[index]; }
+    /// Return char at index.
+    wchar_t& At(unsigned index) { assert(index < length_); return buffer_[index]; }
+    /// Return const char at index.
+    const wchar_t& At(unsigned index) const { assert(index < length_); return buffer_[index]; }
+    /// Resize the string.
     void Resize(unsigned newSize);
-    
     /// Return whether the string is empty.
     bool Empty() const { return length_ == 0; }
     /// Return length.

+ 7 - 11
Engine/Core/ProcessUtils.cpp

@@ -146,29 +146,25 @@ void PrintLine(const char* str)
     printf("%s\n", str);
 }
 
-const Vector<String>& ParseArguments(const char* cmdLine)
+const Vector<String>& ParseArguments(const String& cmdLine)
 {
     arguments.Clear();
     
-    if (!cmdLine)
-        return arguments;
-    
-    String cmdStr(cmdLine);
     unsigned cmdStart = 0, cmdEnd = 0;
     bool inCmd = false;
     bool inQuote = false;
     
-    for (unsigned i = 0; i < cmdStr.Length(); ++i)
+    for (unsigned i = 0; i < cmdLine.Length(); ++i)
     {
-        if (cmdStr[i] == '\"')
+        if (cmdLine[i] == '\"')
             inQuote = !inQuote;
-        if (cmdStr[i] == ' ' && !inQuote)
+        if (cmdLine[i] == ' ' && !inQuote)
         {
             if (inCmd)
             {
                 inCmd = false;
                 cmdEnd = i;
-                arguments.Push(cmdStr.Substring(cmdStart, cmdEnd - cmdStart));
+                arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
             }
         }
         else
@@ -182,8 +178,8 @@ const Vector<String>& ParseArguments(const char* cmdLine)
     }
     if (inCmd)
     {
-        cmdEnd = cmdStr.Length();
-        arguments.Push(cmdStr.Substring(cmdStart, cmdEnd - cmdStart));
+        cmdEnd = cmdLine.Length();
+        arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
     }
     
     return arguments;

+ 1 - 1
Engine/Core/ProcessUtils.h

@@ -40,7 +40,7 @@ void PrintLine(const String& str);
 /// Print to the console. A newline will be added automatically.
 void PrintLine(const char* str);
 /// Parse arguments from the command line.
-const Vector<String>& ParseArguments(const char* cmdLine);
+const Vector<String>& ParseArguments(const String& cmdLine);
 /// Return previously parsed arguments.
 const Vector<String>& GetArguments();
 /// Read input from the console window. Return empty if no input.

+ 17 - 5
Urho3D/Urho3D.cpp

@@ -38,7 +38,11 @@
 
 #include "DebugNew.h"
 
+#ifdef WIN32
+void Run(const wchar_t* cmdLineW);
+#else
 void Run(const char* cmdLine);
+#endif
 
 #ifdef WIN32
 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
@@ -50,11 +54,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, in
     #if defined(ENABLE_MINIDUMPS) && !defined(_DEBUG)
     __try
     {
-        Run(cmdLine);
+        Run(GetCommandLineW());
     }
     __except(WriteMiniDump("Urho3D", GetExceptionInformation())) {}
     #else
-    Run(cmdLine);
+    Run(GetCommandLineW());
     #endif
 
     return 0;
@@ -63,7 +67,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, in
 int main(int argc, char** argv)
 {
     String cmdLine;
-    for (int i = 1; i < argc; ++i)
+    for (int i = 0; i < argc; ++i)
     {
         if (i > 1)
             cmdLine += ' ';
@@ -75,14 +79,22 @@ int main(int argc, char** argv)
 }
 #endif
 
+#ifdef WIN32
+void Run(const wchar_t* cmdLineW)
+{
+    String cmdLineStr(cmdLineW);
+#else
 void Run(const char* cmdLine)
 {
+    String cmdLineStr(cmdLine);
+#endif
+    
     try
     {
         // Check for script file name
-        const Vector<String>& arguments = ParseArguments(cmdLine);
+        const Vector<String>& arguments = ParseArguments(cmdLineStr);
         String scriptFileName;
-        for (unsigned i = 0; i < arguments.Size(); ++i)
+        for (unsigned i = 1; i < arguments.Size(); ++i)
         {
             if (arguments[i][0] != '-')
             {