Sfoglia il codice sorgente

IO: Make stream size and pos i64 (#3069)

1vanK 3 anni fa
parent
commit
d171e58ec8

+ 13 - 13
Source/Urho3D/AngelScript/Generated_Members.h

@@ -1314,7 +1314,7 @@ template <class T> CScriptArray* Deserializer_StringVector_ReadStringVector_void
 // class Deserializer | File: ../IO/Deserializer.h
 template <class T> void RegisterMembers_Deserializer(asIScriptEngine* engine, const char* className)
 {
-    // virtual unsigned Deserializer::Read(void* dest, unsigned size) = 0
+    // virtual i32 Deserializer::Read(void* dest, i32 size) = 0
     // Error: type "void*" can not automatically bind
     // VariantVector Deserializer::ReadVariantVector()
     // Error: type "VariantVector" can not automatically bind
@@ -1327,13 +1327,13 @@ template <class T> void RegisterMembers_Deserializer(asIScriptEngine* engine, co
     engine->RegisterObjectMethod(className, "const String& GetName() const", AS_METHODPR(T, GetName, () const, const String&), AS_CALL_THISCALL);
     engine->RegisterObjectMethod(className, "const String& get_name() const", AS_METHODPR(T, GetName, () const, const String&), AS_CALL_THISCALL);
 
-    // unsigned Deserializer::GetPosition() const
-    engine->RegisterObjectMethod(className, "uint GetPosition() const", AS_METHODPR(T, GetPosition, () const, unsigned), AS_CALL_THISCALL);
-    engine->RegisterObjectMethod(className, "uint get_position() const", AS_METHODPR(T, GetPosition, () const, unsigned), AS_CALL_THISCALL);
+    // i64 Deserializer::GetPosition() const
+    engine->RegisterObjectMethod(className, "int64 GetPosition() const", AS_METHODPR(T, GetPosition, () const, i64), AS_CALL_THISCALL);
+    engine->RegisterObjectMethod(className, "int64 get_position() const", AS_METHODPR(T, GetPosition, () const, i64), AS_CALL_THISCALL);
 
-    // unsigned Deserializer::GetSize() const
-    engine->RegisterObjectMethod(className, "uint GetSize() const", AS_METHODPR(T, GetSize, () const, unsigned), AS_CALL_THISCALL);
-    engine->RegisterObjectMethod(className, "uint get_size() const", AS_METHODPR(T, GetSize, () const, unsigned), AS_CALL_THISCALL);
+    // i64 Deserializer::GetSize() const
+    engine->RegisterObjectMethod(className, "int64 GetSize() const", AS_METHODPR(T, GetSize, () const, i64), AS_CALL_THISCALL);
+    engine->RegisterObjectMethod(className, "int64 get_size() const", AS_METHODPR(T, GetSize, () const, i64), AS_CALL_THISCALL);
 
     // virtual bool Deserializer::IsEof() const
     engine->RegisterObjectMethod(className, "bool IsEof() const", AS_METHODPR(T, IsEof, () const, bool), AS_CALL_THISCALL);
@@ -1456,14 +1456,14 @@ template <class T> void RegisterMembers_Deserializer(asIScriptEngine* engine, co
     // unsigned Deserializer::ReadVLE()
     engine->RegisterObjectMethod(className, "uint ReadVLE()", AS_METHODPR(T, ReadVLE, (), unsigned), AS_CALL_THISCALL);
 
-    // virtual unsigned Deserializer::Seek(unsigned position) = 0
-    engine->RegisterObjectMethod(className, "uint Seek(uint)", AS_METHODPR(T, Seek, (unsigned), unsigned), AS_CALL_THISCALL);
+    // virtual i64 Deserializer::Seek(i64 position) = 0
+    engine->RegisterObjectMethod(className, "int64 Seek(int64)", AS_METHODPR(T, Seek, (i64), i64), AS_CALL_THISCALL);
 
-    // unsigned Deserializer::SeekRelative(int delta)
-    engine->RegisterObjectMethod(className, "uint SeekRelative(int)", AS_METHODPR(T, SeekRelative, (int), unsigned), AS_CALL_THISCALL);
+    // i64 Deserializer::SeekRelative(i64 delta)
+    engine->RegisterObjectMethod(className, "int64 SeekRelative(int64)", AS_METHODPR(T, SeekRelative, (i64), i64), AS_CALL_THISCALL);
 
-    // unsigned Deserializer::Tell() const
-    engine->RegisterObjectMethod(className, "uint Tell() const", AS_METHODPR(T, Tell, () const, unsigned), AS_CALL_THISCALL);
+    // i64 Deserializer::Tell() const
+    engine->RegisterObjectMethod(className, "int64 Tell() const", AS_METHODPR(T, Tell, () const, i64), AS_CALL_THISCALL);
 
     #ifdef REGISTER_MEMBERS_MANUAL_PART_Deserializer
         REGISTER_MEMBERS_MANUAL_PART_Deserializer();

+ 1 - 1
Source/Urho3D/IO/AbstractFile.h

@@ -16,7 +16,7 @@ public:
     /// Construct.
     AbstractFile() : Deserializer() { }
     /// Construct.
-    explicit AbstractFile(unsigned int size) : Deserializer(size) { }
+    explicit AbstractFile(i64 size) : Deserializer(size) { }
     /// Destruct.
     ~AbstractFile() override = default;
     /// Change the file name. Used by the resource system.

+ 3 - 2
Source/Urho3D/IO/Deserializer.cpp

@@ -18,15 +18,16 @@ Deserializer::Deserializer() :
 {
 }
 
-Deserializer::Deserializer(unsigned size) :
+Deserializer::Deserializer(i64 size) :
     position_(0),
     size_(size)
 {
+    assert(size >= 0);
 }
 
 Deserializer::~Deserializer() = default;
 
-unsigned Deserializer::SeekRelative(int delta)
+i64 Deserializer::SeekRelative(i64 delta)
 {
     return Seek(GetPosition() + delta);
 }

+ 9 - 9
Source/Urho3D/IO/Deserializer.h

@@ -18,14 +18,14 @@ public:
     /// Construct with zero size.
     Deserializer();
     /// Construct with defined size.
-    explicit Deserializer(unsigned size);
+    explicit Deserializer(i64 size);
     /// Destruct.
     virtual ~Deserializer();
 
     /// Read bytes from the stream. Return number of bytes actually read.
-    virtual unsigned Read(void* dest, unsigned size) = 0;
+    virtual i32 Read(void* dest, i32 size) = 0;
     /// Set position from the beginning of the stream. Return actual new position.
-    virtual unsigned Seek(unsigned position) = 0;
+    virtual i64 Seek(i64 position) = 0;
     /// Return name of the stream.
     /// @property
     virtual const String& GetName() const;
@@ -37,16 +37,16 @@ public:
     virtual bool IsEof() const { return position_ >= size_; }
 
     /// Set position relative to current position. Return actual new position.
-    unsigned SeekRelative(int delta);
+    i64 SeekRelative(i64 delta);
     /// Return current position.
     /// @property
-    unsigned GetPosition() const { return position_; }
+    i64 GetPosition() const { return position_; }
     /// Return current position.
-    unsigned Tell() const { return position_; }
+    i64 Tell() const { return position_; }
 
     /// Return size.
     /// @property
-    unsigned GetSize() const { return size_; }
+    i64 GetSize() const { return size_; }
 
     /// Read a 64-bit integer.
     long long ReadInt64();
@@ -131,9 +131,9 @@ public:
 
 protected:
     /// Stream position.
-    unsigned position_;
+    i64 position_;
     /// Stream size.
-    unsigned size_;
+    i64 size_;
 };
 
 }

+ 24 - 16
Source/Urho3D/IO/File.cpp

@@ -42,9 +42,9 @@ static const char* openMode[] =
 
 #ifdef __ANDROID__
 const char* APK = "/apk/";
-static const unsigned READ_BUFFER_SIZE = 32768;
+static constexpr i32 READ_BUFFER_SIZE = 32768;
 #endif
-static const unsigned SKIP_BUFFER_SIZE = 1024;
+static constexpr i32 SKIP_BUFFER_SIZE = 1024;
 
 static i32 FSeek64(FILE* stream, i64 offset, i32 origin)
 {
@@ -154,8 +154,10 @@ bool File::Open(PackageFile* package, const String& fileName)
     return true;
 }
 
-unsigned File::Read(void* dest, unsigned size)
+i32 File::Read(void* dest, i32 size)
 {
+    assert(size >= 0);
+
     if (!IsOpen())
     {
         // If file not open, do not log the error further here to prevent spamming the stderr stream
@@ -184,7 +186,7 @@ unsigned File::Read(void* dest, unsigned size)
             readBufferSize_ = 0;
         }
 
-        unsigned sizeLeft = size;
+        i32 sizeLeft = size;
         u8* destPtr = (u8*)dest;
 
         while (sizeLeft)
@@ -196,7 +198,7 @@ unsigned File::Read(void* dest, unsigned size)
                 ReadInternal(readBuffer_.Get(), readBufferSize_);
             }
 
-            unsigned copySize = Min((readBufferSize_ - readBufferOffset_), sizeLeft);
+            i32 copySize = Min((readBufferSize_ - readBufferOffset_), sizeLeft);
             memcpy(destPtr, readBuffer_.Get() + readBufferOffset_, copySize);
             destPtr += copySize;
             sizeLeft -= copySize;
@@ -210,7 +212,7 @@ unsigned File::Read(void* dest, unsigned size)
 
     if (compressed_)
     {
-        unsigned sizeLeft = size;
+        i32 sizeLeft = size;
         u8* destPtr = (u8*)dest;
 
         while (sizeLeft)
@@ -221,8 +223,8 @@ unsigned File::Read(void* dest, unsigned size)
                 ReadInternal(blockHeaderBytes, sizeof blockHeaderBytes);
 
                 MemoryBuffer blockHeader(&blockHeaderBytes[0], sizeof blockHeaderBytes);
-                unsigned unpackedSize = blockHeader.ReadUShort();
-                unsigned packedSize = blockHeader.ReadUShort();
+                i32 unpackedSize = blockHeader.ReadUShort();
+                i32 packedSize = blockHeader.ReadUShort();
 
                 if (!readBuffer_)
                 {
@@ -238,7 +240,7 @@ unsigned File::Read(void* dest, unsigned size)
                 readBufferOffset_ = 0;
             }
 
-            unsigned copySize = Min((readBufferSize_ - readBufferOffset_), sizeLeft);
+            i32 copySize = Min((readBufferSize_ - readBufferOffset_), sizeLeft);
             memcpy(destPtr, readBuffer_.Get() + readBufferOffset_, copySize);
             destPtr += copySize;
             sizeLeft -= copySize;
@@ -269,8 +271,10 @@ unsigned File::Read(void* dest, unsigned size)
     return size;
 }
 
-unsigned File::Seek(unsigned position)
+i64 File::Seek(i64 position)
 {
+    assert(position >= 0);
+
     if (!IsOpen())
     {
         // If file not open, do not log the error further here to prevent spamming the stderr stream
@@ -353,7 +357,7 @@ i32 File::Write(const void* data, i32 size)
     return size;
 }
 
-unsigned File::GetChecksum()
+hash32 File::GetChecksum()
 {
     if (offset_ || checksum_)
         return checksum_;
@@ -366,15 +370,15 @@ unsigned File::GetChecksum()
 
     URHO3D_PROFILE(CalculateFileChecksum);
 
-    unsigned oldPos = position_;
+    i64 oldPos = position_;
     checksum_ = 0;
 
     Seek(0);
     while (!IsEof())
     {
         u8 block[1024];
-        unsigned readBytes = Read(block, 1024);
-        for (unsigned i = 0; i < readBytes; ++i)
+        i32 readBytes = Read(block, 1024);
+        for (i32 i = 0; i < readBytes; ++i)
             checksum_ = SDBMHash(checksum_, block[i]);
     }
 
@@ -519,8 +523,10 @@ bool File::OpenInternal(const String& fileName, FileMode mode, bool fromPackage)
     return true;
 }
 
-bool File::ReadInternal(void* dest, unsigned size)
+bool File::ReadInternal(void* dest, i32 size)
 {
+    assert(size >= 0);
+
 #ifdef __ANDROID__
     if (assetHandle_)
     {
@@ -531,8 +537,10 @@ bool File::ReadInternal(void* dest, unsigned size)
         return fread(dest, size, 1, (FILE*)handle_) == 1;
 }
 
-void File::SeekInternal(unsigned newPosition)
+void File::SeekInternal(i64 newPosition)
 {
+    assert(newPosition >= 0);
+
 #ifdef __ANDROID__
     if (assetHandle_)
     {

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

@@ -55,9 +55,9 @@ public:
     ~File() override;
 
     /// Read bytes from the file. Return number of bytes actually read.
-    unsigned Read(void* dest, unsigned size) override;
+    i32 Read(void* dest, i32 size) override;
     /// Set position from the beginning of the file.
-    unsigned Seek(unsigned position) override;
+    i64 Seek(i64 position) override;
     /// Write bytes to the file. Return number of bytes actually written.
     i32 Write(const void* data, i32 size) override;
 
@@ -92,9 +92,9 @@ private:
     /// Open file internally using either C standard IO functions or SDL RWops for Android asset files. Return true if successful.
     bool OpenInternal(const String& fileName, FileMode mode, bool fromPackage = false);
     /// Perform the file read internally using either C standard IO functions or SDL RWops for Android asset files. Return true if successful. This does not handle compressed package file reading.
-    bool ReadInternal(void* dest, unsigned size);
+    bool ReadInternal(void* dest, i32 size);
     /// Seek in file internally using either C standard IO functions or SDL RWops for Android asset files.
-    void SeekInternal(unsigned newPosition);
+    void SeekInternal(i64 newPosition);
 
     /// Open mode.
     FileMode mode_;
@@ -109,11 +109,11 @@ private:
     /// Decompression input buffer for compressed file loading.
     SharedArrayPtr<u8> inputBuffer_;
     /// Read buffer position.
-    unsigned readBufferOffset_;
+    i32 readBufferOffset_;
     /// Bytes in the current read buffer.
-    unsigned readBufferSize_;
+    i32 readBufferSize_;
     /// Start position within a package file, 0 for regular files.
-    unsigned offset_;
+    i64 offset_;
     /// Content checksum.
     hash32 checksum_;
     /// Compression flag.

+ 6 - 2
Source/Urho3D/IO/MemoryBuffer.cpp

@@ -44,8 +44,10 @@ MemoryBuffer::MemoryBuffer(const Vector<u8>& data) :
 {
 }
 
-unsigned MemoryBuffer::Read(void* dest, unsigned size)
+i32 MemoryBuffer::Read(void* dest, i32 size)
 {
+    assert(size >= 0);
+
     if (size + position_ > size_)
         size = size_ - position_;
     if (!size)
@@ -60,8 +62,10 @@ unsigned MemoryBuffer::Read(void* dest, unsigned size)
     return size;
 }
 
-unsigned MemoryBuffer::Seek(unsigned position)
+i64 MemoryBuffer::Seek(i64 position)
 {
+    assert(position >= 0 && position <= M_MAX_INT);
+
     if (position > size_)
         position = size_;
 

+ 2 - 2
Source/Urho3D/IO/MemoryBuffer.h

@@ -23,9 +23,9 @@ public:
     explicit MemoryBuffer(const Vector<u8>& data);
 
     /// Read bytes from the memory area. Return number of bytes actually read.
-    unsigned Read(void* dest, unsigned size) override;
+    i32 Read(void* dest, i32 size) override;
     /// Set position from the beginning of the memory area. Return actual new position.
-    unsigned Seek(unsigned position) override;
+    i64 Seek(i64 position) override;
     /// Write bytes to the memory area.
     i32 Write(const void* data, i32 size) override;
 

+ 8 - 4
Source/Urho3D/IO/NamedPipe.cpp

@@ -21,7 +21,7 @@
 namespace Urho3D
 {
 
-static const unsigned PIPE_BUFFER_SIZE = 65536;
+static constexpr i32 PIPE_BUFFER_SIZE = 65536;
 
 NamedPipe::NamedPipe(Context* context) :
     Object(context),
@@ -53,7 +53,7 @@ NamedPipe::~NamedPipe()
     Close();
 }
 
-unsigned NamedPipe::Seek(unsigned position)
+i64 NamedPipe::Seek(i64 position)
 {
     return 0;
 }
@@ -121,8 +121,10 @@ bool NamedPipe::Open(const String& name, bool isServer)
     }
 }
 
-unsigned NamedPipe::Read(void* dest, unsigned size)
+i32 NamedPipe::Read(void* dest, i32 size)
 {
+    assert(size >= 0);
+
     if (handle_ != INVALID_HANDLE_VALUE)
     {
         DWORD read = 0;
@@ -254,8 +256,10 @@ bool NamedPipe::Open(const String& name, bool isServer)
 #endif
 }
 
-unsigned NamedPipe::Read(void* dest, unsigned size)
+i32 NamedPipe::Read(void* dest, i32 size)
 {
+    assert(size >= 0);
+
     // Attempt to open late if only the write handle is open yet
     if (readHandle_ == -1 && writeHandle_ != -1)
     {

+ 2 - 2
Source/Urho3D/IO/NamedPipe.h

@@ -28,9 +28,9 @@ public:
     ~NamedPipe() override;
 
     /// Read bytes from the pipe without blocking if there is less data available. Return number of bytes actually read.
-    unsigned Read(void* dest, unsigned size) override;
+    i32 Read(void* dest, i32 size) override;
     /// Set position. No-op for pipes.
-    unsigned Seek(unsigned position) override;
+    i64 Seek(i64 position) override;
     /// Write bytes to the pipe. Return number of bytes actually written.
     i32 Write(const void* data, i32 size) override;
     /// Return whether pipe has no data available.

+ 6 - 2
Source/Urho3D/IO/VectorBuffer.cpp

@@ -29,8 +29,10 @@ VectorBuffer::VectorBuffer(Deserializer& source, i32 size)
     SetData(source, size);
 }
 
-unsigned VectorBuffer::Read(void* dest, unsigned size)
+i32 VectorBuffer::Read(void* dest, i32 size)
 {
+    assert(size >= 0);
+
     if (size + position_ > size_)
         size = size_ - position_;
     if (!size)
@@ -45,8 +47,10 @@ unsigned VectorBuffer::Read(void* dest, unsigned size)
     return size;
 }
 
-unsigned VectorBuffer::Seek(unsigned position)
+i64 VectorBuffer::Seek(i64 position)
 {
+    assert(position >= 0 && position <= M_MAX_INT);
+
     if (position > size_)
         position = size_;
 

+ 2 - 2
Source/Urho3D/IO/VectorBuffer.h

@@ -22,9 +22,9 @@ public:
     VectorBuffer(Deserializer& source, i32 size);
 
     /// Read bytes from the buffer. Return number of bytes actually read.
-    unsigned Read(void* dest, unsigned size) override;
+    i32 Read(void* dest, i32 size) override;
     /// Set position from the beginning of the buffer. Return actual new position.
-    unsigned Seek(unsigned position) override;
+    i64 Seek(i64 position) override;
     /// Write bytes to the buffer. Return number of bytes actually written.
     i32 Write(const void* data, i32 size) override;
 

+ 4 - 2
Source/Urho3D/Network/HttpRequest.cpp

@@ -189,8 +189,10 @@ void HttpRequest::ThreadFunction()
     }
 }
 
-unsigned HttpRequest::Read(void* dest, unsigned size)
+i32 HttpRequest::Read(void* dest, i32 size)
 {
+    assert(size >= 0);
+
 #ifdef URHO3D_THREADING
     mutex_.Acquire();
 
@@ -250,7 +252,7 @@ unsigned HttpRequest::Read(void* dest, unsigned size)
 #endif
 }
 
-unsigned HttpRequest::Seek(unsigned position)
+i64 HttpRequest::Seek(i64 position)
 {
     return 0;
 }

+ 2 - 2
Source/Urho3D/Network/HttpRequest.h

@@ -36,9 +36,9 @@ public:
     void ThreadFunction() override;
 
     /// Read response data from the HTTP connection and return number of bytes actually read. While the connection is open, will block while trying to read the specified size. To avoid blocking, only read up to as many bytes as GetAvailableSize() returns.
-    unsigned Read(void* dest, unsigned size) override;
+    i32 Read(void* dest, i32 size) override;
     /// Set position from the beginning of the stream. Not supported.
-    unsigned Seek(unsigned position) override;
+    i64 Seek(i64 position) override;
     /// Return whether all response data has been read.
     bool IsEof() const override;