From 0a9f3f2281106adf61f8665198dca04f26a0c505 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Fri, 9 Jun 2017 11:46:44 +0100 Subject: [PATCH] Added common utility functions to RemoteStorage --- .../Client/RemoteStorage.cs | 22 ++++++++ Facepunch.Steamworks/Client/RemoteStorage.cs | 56 +++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/Facepunch.Steamworks.Test/Client/RemoteStorage.cs b/Facepunch.Steamworks.Test/Client/RemoteStorage.cs index 4876a8c..08f7f8b 100644 --- a/Facepunch.Steamworks.Test/Client/RemoteStorage.cs +++ b/Facepunch.Steamworks.Test/Client/RemoteStorage.cs @@ -40,6 +40,28 @@ public void WriteFile() } } + [TestMethod] + public void ReadText() + { + using ( var client = new Steamworks.Client( 252490 ) ) + { + var text = client.RemoteStorage.ReadString( "test.txt" ); + + Assert.IsNotNull( text ); + Assert.AreEqual( text, "Hello world!" ); + } + } + + [TestMethod] + public void WriteText() + { + using ( var client = new Steamworks.Client( 252490 ) ) + { + var result = client.RemoteStorage.WriteString( "test.txt", "Hello world!" ); + Assert.IsTrue( result ); + } + } + [TestMethod] public void WriteFiles() { diff --git a/Facepunch.Steamworks/Client/RemoteStorage.cs b/Facepunch.Steamworks/Client/RemoteStorage.cs index f7fe95f..1847fac 100644 --- a/Facepunch.Steamworks/Client/RemoteStorage.cs +++ b/Facepunch.Steamworks/Client/RemoteStorage.cs @@ -78,6 +78,62 @@ public RemoteFile CreateFile( string path ) return existing ?? new RemoteFile( this, path, client.SteamId, 0 ); } + /// + /// Opens the file if it exists, else returns null; + /// + public RemoteFile OpenFile( string path ) + { + path = NormalizePath( path ); + + InvalidateFiles(); + var existing = Files.FirstOrDefault( x => x.FileName == path ); + return existing; + } + + /// + /// Write all text to the file at the specified path. This + /// overwrites the contents - it does not append. + /// + public bool WriteString( string path, string text, Encoding encoding = null ) + { + var file = CreateFile( path ); + file.WriteAllText( text, encoding ); + return file.Exists; + } + + /// + /// Write all data to the file at the specified path. This + /// overwrites the contents - it does not append. + /// + public bool WriteBytes( string path, byte[] data ) + { + var file = CreateFile( path ); + file.WriteAllBytes( data ); + return file.Exists; + } + + /// + /// Read the entire contents of the file as a string. + /// Returns null if the file isn't found. + /// + public string ReadString( string path, Encoding encoding = null ) + { + var file = OpenFile( path ); + if ( file == null ) return null; + return file.ReadAllText( encoding ); + } + + /// + /// Read the entire contents of the file as raw data. + /// Returns null if the file isn't found. + /// + public byte[] ReadBytes( string path ) + { + var file = OpenFile( path ); + if ( file == null ) return null; + return file.ReadAllBytes(); + } + internal void OnWrittenNewFile( RemoteFile file ) { if ( _files.Any( x => x.FileName == file.FileName ) ) return;