Browse Source

support loading shaders from memory (#2719)

* support loading shaders from memory

* code style fix

* GetName, SetName moved to AbstractFile class

* named pipe updated, abstract file tweaked

* minor code style tweaks

* obsolete variable fixed

* filename setter updated
Arnis Lielturks 5 years ago
parent
commit
9a68d9533d

+ 8 - 0
Source/Urho3D/IO/AbstractFile.h

@@ -39,6 +39,14 @@ public:
     explicit AbstractFile(unsigned int size) : Deserializer(size) { }
     /// Destruct.
     ~AbstractFile() override = default;
+    /// Change the file name. Used by the resource system.
+    virtual void SetName(const String& name) { name_ = name; }
+    /// Return the file name.
+    const String& GetName() const override { return name_; }
+
+protected:
+    /// File name.
+    String name_;
 };
 
 }

+ 3 - 8
Source/Urho3D/IO/File.cpp

@@ -144,7 +144,7 @@ bool File::Open(PackageFile* package, const String& fileName)
         return false;
     }
 
-    fileName_ = fileName;
+    name_ = fileName;
     offset_ = entry->offset_;
     checksum_ = entry->checksum_;
     size_ = entry->size_;
@@ -411,11 +411,6 @@ void File::Flush()
         fflush((FILE*)handle_);
 }
 
-void File::SetName(const String& name)
-{
-    fileName_ = name;
-}
-
 bool File::IsOpen() const
 {
 #ifdef __ANDROID__
@@ -463,7 +458,7 @@ bool File::OpenInternal(const String& fileName, FileMode mode, bool fromPackage)
         }
         else
         {
-            fileName_ = fileName;
+            name_ = fileName;
             mode_ = mode;
             position_ = 0;
             if (!fromPackage)
@@ -515,7 +510,7 @@ bool File::OpenInternal(const String& fileName, FileMode mode, bool fromPackage)
         offset_ = 0;
     }
 
-    fileName_ = fileName;
+    name_ = fileName;
     mode_ = mode;
     position_ = 0;
     checksum_ = 0;

+ 0 - 7
Source/Urho3D/IO/File.h

@@ -80,9 +80,6 @@ public:
     /// Write bytes to the file. Return number of bytes actually written.
     unsigned Write(const void* data, unsigned size) override;
 
-    /// Return the file name.
-    const String& GetName() const override { return fileName_; }
-
     /// Return a checksum of the file contents using the SDBM hash algorithm.
     unsigned GetChecksum() override;
 
@@ -94,8 +91,6 @@ public:
     void Close();
     /// Flush any buffered output to the file.
     void Flush();
-    /// Change the file name. Used by the resource system.
-    void SetName(const String& name);
 
     /// Return the open mode.
     /// @property
@@ -120,8 +115,6 @@ private:
     /// Seek in file internally using either C standard IO functions or SDL RWops for Android asset files.
     void SeekInternal(unsigned newPosition);
 
-    /// File name.
-    String fileName_;
     /// Open mode.
     FileMode mode_;
     /// File handle.

+ 37 - 31
Source/Urho3D/IO/NamedPipe.cpp

@@ -54,7 +54,7 @@ NamedPipe::NamedPipe(Context* context) :
 {
 }
 
-NamedPipe::NamedPipe(Context* context, const String& pipeName, bool isServer) :
+NamedPipe::NamedPipe(Context* context, const String& name, bool isServer) :
     Object(context),
     isServer_(false),
 #ifdef _WIN32
@@ -64,7 +64,7 @@ NamedPipe::NamedPipe(Context* context, const String& pipeName, bool isServer) :
     writeHandle_(-1)
 #endif
 {
-    Open(pipeName, isServer);
+    Open(name, isServer);
 }
 
 NamedPipe::~NamedPipe()
@@ -81,7 +81,7 @@ unsigned NamedPipe::Seek(unsigned position)
 
 static const char* pipePath = "\\\\.\\pipe\\";
 
-bool NamedPipe::Open(const String& pipeName, bool isServer)
+bool NamedPipe::Open(const String& name, bool isServer)
 {
     URHO3D_PROFILE(OpenNamedPipe);
 
@@ -91,7 +91,7 @@ bool NamedPipe::Open(const String& pipeName, bool isServer)
 
     if (isServer)
     {
-        handle_ = CreateNamedPipeW(WString(pipePath + pipeName).CString(),
+        handle_ = CreateNamedPipeW(WString(pipePath + name).CString(),
             PIPE_ACCESS_DUPLEX,
             PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT,
             1,
@@ -103,13 +103,13 @@ bool NamedPipe::Open(const String& pipeName, bool isServer)
 
         if (handle_ == INVALID_HANDLE_VALUE)
         {
-            URHO3D_LOGERROR("Failed to create named pipe " + pipeName);
+            URHO3D_LOGERROR("Failed to create named pipe " + name);
             return false;
         }
         else
         {
-            URHO3D_LOGDEBUG("Created named pipe " + pipeName);
-            pipeName_ = pipeName;
+            URHO3D_LOGDEBUG("Created named pipe " + name);
+            name_ = name;
             isServer_ = true;
             return true;
         }
@@ -117,7 +117,7 @@ bool NamedPipe::Open(const String& pipeName, bool isServer)
     else
     {
         handle_ = CreateFileW(
-            WString(pipePath + pipeName).CString(),
+            WString(pipePath + name).CString(),
             GENERIC_READ | GENERIC_WRITE,
             0,
             nullptr,
@@ -128,13 +128,13 @@ bool NamedPipe::Open(const String& pipeName, bool isServer)
 
         if (handle_ == INVALID_HANDLE_VALUE)
         {
-            URHO3D_LOGERROR("Failed to connect to named pipe " + pipeName);
+            URHO3D_LOGERROR("Failed to connect to named pipe " + name);
             return false;
         }
         else
         {
-            URHO3D_LOGDEBUG("Connected to named pipe " + pipeName);
-            pipeName_ = pipeName;
+            URHO3D_LOGDEBUG("Connected to named pipe " + name);
+            name_ = name;
             return true;
         }
     }
@@ -178,9 +178,9 @@ void NamedPipe::Close()
 
         CloseHandle(handle_);
         handle_ = INVALID_HANDLE_VALUE;
-        pipeName_.Clear();
+        name_.Clear();
 
-        URHO3D_LOGDEBUG("Closed named pipe " + pipeName_);
+        URHO3D_LOGDEBUG("Closed named pipe " + name_);
     }
 }
 
@@ -207,7 +207,7 @@ static const char* pipePath = "/tmp/";
 
 #define SAFE_CLOSE(handle) if ((handle) != -1) { close(handle); (handle) = -1; }
 
-bool NamedPipe::Open(const String& pipeName, bool isServer)
+bool NamedPipe::Open(const String& name, bool isServer)
 {
 #ifdef __EMSCRIPTEN__
     URHO3D_LOGERROR("Opening a named pipe not supported on Web platform");
@@ -219,8 +219,8 @@ bool NamedPipe::Open(const String& pipeName, bool isServer)
 
     isServer_ = false;
 
-    String serverReadName = pipePath + pipeName + "SR";
-    String clientReadName = pipePath + pipeName + "CR";
+    String serverReadName = pipePath + name + "SR";
+    String clientReadName = pipePath + name + "CR";
 
     // Make sure SIGPIPE is ignored and will not lead to process termination
     signal(SIGPIPE, SIG_IGN);
@@ -235,7 +235,7 @@ bool NamedPipe::Open(const String& pipeName, bool isServer)
 
         if (readHandle_ == -1 && writeHandle_ == -1)
         {
-            URHO3D_LOGERROR("Failed to create named pipe " + pipeName);
+            URHO3D_LOGERROR("Failed to create named pipe " + name);
             SAFE_CLOSE(readHandle_);
             SAFE_CLOSE(writeHandle_);
             unlink(serverReadName.CString());
@@ -244,8 +244,8 @@ bool NamedPipe::Open(const String& pipeName, bool isServer)
         }
         else
         {
-            URHO3D_LOGDEBUG("Created named pipe " + pipeName);
-            pipeName_ = pipeName;
+            URHO3D_LOGDEBUG("Created named pipe " + name);
+            name_ = name;
             isServer_ = true;
             return true;
         }
@@ -256,15 +256,15 @@ bool NamedPipe::Open(const String& pipeName, bool isServer)
         writeHandle_ = open(serverReadName.CString(), O_WRONLY | O_NDELAY);
         if (readHandle_ == -1 && writeHandle_ == -1)
         {
-            URHO3D_LOGERROR("Failed to connect to named pipe " + pipeName);
+            URHO3D_LOGERROR("Failed to connect to named pipe " + name);
             SAFE_CLOSE(readHandle_);
             SAFE_CLOSE(writeHandle_);
             return false;
         }
         else
         {
-            URHO3D_LOGDEBUG("Connected to named pipe " + pipeName);
-            pipeName_ = pipeName;
+            URHO3D_LOGDEBUG("Connected to named pipe " + name);
+            name_ = name;
             return true;
         }
     }
@@ -277,9 +277,9 @@ unsigned NamedPipe::Read(void* dest, unsigned size)
     if (readHandle_ == -1 && writeHandle_ != -1)
     {
         if (isServer_)
-            readHandle_ = open((pipePath + pipeName_ + "SR").CString(), O_RDONLY | O_NDELAY);
+            readHandle_ = open((pipePath + name_ + "SR").CString(), O_RDONLY | O_NDELAY);
         else
-            readHandle_ = open((pipePath + pipeName_ + "CR").CString(), O_RDONLY | O_NDELAY);
+            readHandle_ = open((pipePath + name_ + "CR").CString(), O_RDONLY | O_NDELAY);
     }
 
     if (readHandle_ != -1)
@@ -297,9 +297,9 @@ unsigned NamedPipe::Write(const void* data, unsigned size)
     if (writeHandle_ == -1 && readHandle_ != -1)
     {
         if (isServer_)
-            writeHandle_ = open((pipePath + pipeName_ + "CR").CString(), O_WRONLY | O_NDELAY);
+            writeHandle_ = open((pipePath + name_ + "CR").CString(), O_WRONLY | O_NDELAY);
         else
-            writeHandle_ = open((pipePath + pipeName_ + "SR").CString(), O_WRONLY | O_NDELAY);
+            writeHandle_ = open((pipePath + name_ + "SR").CString(), O_WRONLY | O_NDELAY);
     }
 
     // Loop until all bytes written in case of partial write
@@ -330,14 +330,14 @@ void NamedPipe::Close()
 
         if (isServer_)
         {
-            String serverReadName = pipePath + pipeName_ + "SR";
-            String clientReadName = pipePath + pipeName_ + "CR";
+            String serverReadName = pipePath + name_ + "SR";
+            String clientReadName = pipePath + name_ + "CR";
             unlink(serverReadName.CString());
             unlink(clientReadName.CString());
             isServer_ = false;
         }
 
-        pipeName_.Clear();
+        name_.Clear();
     }
 }
 
@@ -355,9 +355,9 @@ bool NamedPipe::IsEof() const
     if (readHandle_ == -1 && writeHandle_ != -1)
     {
         if (isServer_)
-            readHandle_ = open((pipePath + pipeName_ + "SR").CString(), O_RDONLY | O_NDELAY);
+            readHandle_ = open((pipePath + name_ + "SR").CString(), O_RDONLY | O_NDELAY);
         else
-            readHandle_ = open((pipePath + pipeName_ + "CR").CString(), O_RDONLY | O_NDELAY);
+            readHandle_ = open((pipePath + name_ + "CR").CString(), O_RDONLY | O_NDELAY);
     }
 
     if (readHandle_ != -1)
@@ -376,4 +376,10 @@ bool NamedPipe::IsEof() const
 }
 #endif
 
+void NamedPipe::SetName(const String &name)
+{
+    URHO3D_LOGERROR("Cannot change name of the NamedPipe!");
+    assert(0);
+}
+
 }

+ 4 - 6
Source/Urho3D/IO/NamedPipe.h

@@ -42,7 +42,7 @@ public:
     /// Construct.
     explicit NamedPipe(Context* context);
     /// Construct and open in either server or client mode.
-    NamedPipe(Context* context, const String& pipeName, bool isServer);
+    NamedPipe(Context* context, const String& name, bool isServer);
     /// Destruct and close.
     ~NamedPipe() override;
 
@@ -54,11 +54,11 @@ public:
     unsigned Write(const void* data, unsigned size) override;
     /// Return whether pipe has no data available.
     bool IsEof() const override;
-    /// Return the pipe name.
-    const String& GetName() const override { return pipeName_; }
+    /// Not supported.
+    void SetName(const String& name) override;
 
     /// Open the pipe in either server or client mode. If already open, the existing pipe is closed. For a client end to open successfully the server end must already to be open. Return true if successful.
-    bool Open(const String& pipeName, bool isServer);
+    bool Open(const String& name, bool isServer);
     /// Close the pipe. Note that once a client has disconnected, the server needs to close and reopen the pipe so that another client can connect. At least on Windows this is not possible to detect automatically, so the communication protocol should include a "bye" message to handle this situation.
     void Close();
 
@@ -70,8 +70,6 @@ public:
     bool IsServer() const { return isServer_; }
 
 private:
-    /// Pipe name.
-    String pipeName_;
     /// Server mode flag.
     bool isServer_;
     /// Pipe handle.