Skip to content

Commit 7ba308f

Browse files
authored
Merge pull request #90 from Backendless/BKNDLSS-28938/login_fix
stayLoggedIn fix for Unity
2 parents fe95ee6 + 0216a10 commit 7ba308f

File tree

5 files changed

+57
-21
lines changed

5 files changed

+57
-21
lines changed

Backendless/Messaging/DeviceRegistrationDto.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using System;
22
using Weborb.Service;
3+
using System.Diagnostics;
4+
using System.Text;
5+
#if !UNITY
36
using Plugin.DeviceInfo;
7+
#endif
48
using System.Collections.Generic;
59

610
namespace BackendlessAPI.Messaging
@@ -29,8 +33,11 @@ public class DeviceRegistration
2933
public String RegistrationId { get; set; }
3034

3135
[SetClientClassMemberName( "deviceId" )]
36+
#if !UNITY
3237
public String DeviceId { get; } = CrossDeviceInfo.Current.Id;
33-
38+
#else
39+
public String DeviceId { get; } = DeviceIdGenerator();
40+
#endif
3441
public void AddChannel( String channel )
3542
{
3643
if( Channels == null )
@@ -46,5 +53,13 @@ public void ClearRegistration()
4653
RegistrationId = null;
4754
DeviceToken = null;
4855
}
56+
#if UNITY
57+
public static String DeviceIdGenerator()
58+
{
59+
Guid guid = Guid.NewGuid();
60+
string str = guid.ToString();
61+
return str;
62+
}
63+
#endif
4964
}
5065
}

Backendless/Service/MessagingService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public class MessagingService
2929
private static string DEFAULT_CHANNEL_NAME = "default";
3030
private static string deviceId;
3131
private static int CHANNEL_NAME_MAX_LENGTH = 46;
32-
//private static Messaging.DeviceRegistration _deviceRegistrationDto;
33-
#if UNITY
32+
#if UNITY
33+
private static Messaging.DeviceRegistration _deviceRegistrationDto;
3434
private static AsyncCallback<string> _deviceRegisterCallback = null;
3535
private static AsyncCallback<bool> _deviceUnregisterCallback = null;
3636
public delegate void UnityRegisterDevice( string GCMSenderID, List<String> channels, DateTime? expiration );

Backendless/Utils/LoginStorage.cs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,16 @@ private bool LoadData()
5555
#else
5656
try
5757
{
58-
// Retrieve an IsolatedStorageFile for the current Domain and Assembly.
59-
IsolatedStorageFileStream isoStream = null;
60-
#if !WINDOWS_PHONE && !WINDOWS_PHONE8
58+
// Retrieve an IsolatedStorageFile for the current Domain and Assembly.
59+
IsolatedStorageFileStream isoStream = null;
60+
61+
#if NET_471 && UNITY
62+
var userData = System.IO.File.ReadAllLines("BackendlessUserInfoUnity.txt");
63+
this.UserToken = userData[ 0 ];
64+
this.ObjectId = userData[ 1 ];
65+
return true;
66+
67+
#elif !WINDOWS_PHONE && !WINDOWS_PHONE8 && !UNITY
6168
IsolatedStorageFile isoFile =
6269
IsolatedStorageFile.GetStore( IsolatedStorageScope.User |
6370
IsolatedStorageScope.Assembly |
@@ -111,10 +118,12 @@ private bool LoadData()
111118
return false;
112119
}
113120
#endif
114-
}
121+
}
115122

116123
public void SaveData()
117124
{
125+
// Retrieve an IsolatedStorageFile for the current Domain and Assembly.
126+
IsolatedStorageFileStream isoStream = null;
118127
#if UNIVERSALW8
119128
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
120129
ApplicationDataCompositeValue userInfo = new ApplicationDataCompositeValue();
@@ -125,40 +134,43 @@ public void SaveData()
125134
#else
126135
try
127136
{
128-
#if !WINDOWS_PHONE && !WINDOWS_PHONE8
137+
#if NET_471 && UNITY
138+
System.IO.File.WriteAllLines( "BackendlessUserInfoUnity.txt", new string[] { this.UserToken, this.ObjectId } );
139+
return;
140+
#elif !WINDOWS_PHONE && !WINDOWS_PHONE8 && !UNITY
129141
IsolatedStorageFile isoFile;
130142
isoFile = IsolatedStorageFile.GetUserStoreForDomain();
131143

132144
// Open or create a writable file.
133-
IsolatedStorageFileStream isoStream =
145+
isoStream =
134146
new IsolatedStorageFileStream( "BackendlessUserInfo",
135147
FileMode.OpenOrCreate,
136148
FileAccess.Write,
137149
isoFile );
138150
#else
139151
IsolatedStorageFile isoFile =
140152
IsolatedStorageFile.GetUserStoreForApplication();
141-
142153
IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream( "BackendlessUserInfo",
143154
FileMode.OpenOrCreate,
144155
FileAccess.Write,
145156
isoFile );
146157
#endif
147-
158+
#if !UNITY
148159
StreamWriter writer = new StreamWriter( isoStream );
149160
writer.WriteLine( this.UserToken );
150161
writer.WriteLine( this.ObjectId );
151162
// StreamWriter.Close implicitly closes isoStream.
152163
writer.Close();
153-
isoFile.Dispose();
164+
isoStream.Close();
165+
#endif
154166
}
155167
catch( IsolatedStorageException ex )
156168
{
157169
// Add code here to handle the exception.
158170
Console.WriteLine( ex );
159171
}
160172
#endif
161-
}
173+
}
162174

163175
public void DeleteFiles()
164176
{
@@ -168,17 +180,19 @@ public void DeleteFiles()
168180
#else
169181
try
170182
{
171-
#if !WINDOWS_PHONE && !WINDOWS_PHONE8 && !PURE_CLIENT_LIB
183+
#if NET_471 && UNITY
184+
System.IO.File.Delete("BackendlessUserInfoUnity.txt");
185+
#elif !WINDOWS_PHONE && !WINDOWS_PHONE8 && !PURE_CLIENT_LIB && !UNITY
172186
IsolatedStorageFile isoFile = IsolatedStorageFile.GetStore( IsolatedStorageScope.User |
173187
IsolatedStorageScope.Assembly |
174188
IsolatedStorageScope.Domain,
175189
typeof( System.Security.Policy.Url ),
176190
typeof( System.Security.Policy.Url ) );
177191
#else
178-
IsolatedStorageFile isoFile =
192+
IsolatedStorageFile isoFile =
179193
IsolatedStorageFile.GetUserStoreForDomain();
180194
#endif
181-
195+
#if !UNITY
182196
//String[] dirNames = isoFile.GetDirectoryNames( "*" );
183197
String[] fileNames = isoFile.GetFileNames( "*" );
184198

@@ -195,13 +209,14 @@ public void DeleteFiles()
195209
// Confirm that no files remain.
196210
fileNames = isoFile.GetFileNames( "*" );
197211
}
212+
#endif
198213
}
199214
catch( System.Exception e )
200215
{
201216
Console.WriteLine( e.ToString() );
202217
}
203218
#endif
204-
}
219+
}
205220
// This method deletes directories in the specified Isolated Storage, after first
206221
// deleting the files they contain. In this example, the Archive directory is deleted.
207222
// There should be no other directories in this Isolated Storage.

Projects/Backendless.NET.Framework4.7.1/Backendless.NET.Framework4.7.1.csproj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<DebugType>portable</DebugType>
1919
<Optimize>false</Optimize>
2020
<OutputPath>bin\Debug\</OutputPath>
21-
<DefineConstants>DEBUG;TRACE;PURE_CLIENT_LIB;WITHRT</DefineConstants>
21+
<DefineConstants>TRACE;DEBUG;PURE_CLIENT_LIB;WITHRT;NET_471</DefineConstants>
2222
<ErrorReport>prompt</ErrorReport>
2323
<WarningLevel>4</WarningLevel>
2424
</PropertyGroup>
@@ -50,6 +50,9 @@
5050
<Private>True</Private>
5151
</Reference>
5252
<Reference Include="System" />
53+
<Reference Include="System.CodeDom, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
54+
<HintPath>..\..\packages\System.CodeDom.6.0.0\lib\net461\System.CodeDom.dll</HintPath>
55+
</Reference>
5356
<Reference Include="System.Collections.Immutable, Version=1.2.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
5457
<HintPath>..\..\packages\System.Collections.Immutable.1.5.0\lib\netstandard2.0\System.Collections.Immutable.dll</HintPath>
5558
<Private>True</Private>
@@ -69,6 +72,7 @@
6972
<HintPath>packages\System.Linq.4.3.0\lib\net463\System.Linq.dll</HintPath>
7073
<Private>True</Private>
7174
</Reference>
75+
<Reference Include="System.Management" />
7276
<Reference Include="System.Net.NameResolution, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
7377
<HintPath>packages\System.Net.NameResolution.4.3.0\lib\net46\System.Net.NameResolution.dll</HintPath>
7478
<Private>True</Private>
@@ -291,7 +295,7 @@
291295
<Compile Include="..\..\Backendless\Transaction\Operations\OperationSetRelation.cs" />
292296
<Compile Include="..\..\Backendless\Transaction\Operations\OperationUpdate.cs" />
293297
<Compile Include="..\..\Backendless\Transaction\Operations\OperationUpdateBulk.cs" />
294-
<Compile Include="..\..\Backendless\Transaction\Operations\OperationUpsert.cs" />
298+
<Compile Include="..\..\Backendless\Transaction\Operations\OperationUpsert.cs" />
295299
<Compile Include="..\..\Backendless\Transaction\Operations\OperationUpsertBulk.cs" />
296300
<Compile Include="..\..\Backendless\Transaction\Operations\OperationFind.cs" />
297301
<Compile Include="..\..\Backendless\Transaction\Payload\Selector.cs" />
@@ -308,7 +312,7 @@
308312
<Compile Include="..\..\Backendless\Transaction\UnitOfWorkExecutorImpl.cs" />
309313
<Compile Include="..\..\Backendless\Transaction\UnitOfWorkFindImpl.cs" />
310314
<Compile Include="..\..\Backendless\Transaction\UnitOfWorkUpdateImpl.cs" />
311-
<Compile Include="..\..\Backendless\Transaction\UnitOfWorkUpsertImpl.cs" />
315+
<Compile Include="..\..\Backendless\Transaction\UnitOfWorkUpsertImpl.cs" />
312316
<Compile Include="..\..\Backendless\Transaction\UnitOfWorkResult.cs" />
313317
<Compile Include="..\..\Backendless\Transaction\UnitOfWorkAddRelationImpl.cs" />
314318
<Compile Include="..\..\Backendless\Transaction\UnitOfWorkSetRelationImpl.cs" />
@@ -320,7 +324,7 @@
320324
<Compile Include="..\..\Backendless\Transaction\TransactionHelper.cs" />
321325
<Compile Include="..\..\Backendless\Utils\DeviceCheck.cs" />
322326
<Compile Include="..\..\Backendless\Utils\ILoginStorage.cs" />
323-
<Compile Include="..\..\Backendless\Utils\InitAppData.cs" />
327+
<Compile Include="..\..\Backendless\Utils\InitAppData.cs" />
324328
<Compile Include="..\..\Backendless\Utils\ITimeoutManager.cs" />
325329
<Compile Include="..\..\Backendless\Utils\Json.cs" />
326330
<Compile Include="..\..\Backendless\Utils\LoginStorage.cs" />
@@ -518,8 +522,10 @@
518522
<Compile Include="..\..\Backendless\WebORB\Writer\V3ObjectSerializer.cs" />
519523
<Compile Include="..\..\Backendless\WebORB\Writer\V3ReferenceCache.cs" />
520524
<Compile Include="Properties\AssemblyInfo.cs" />
525+
<Compile Include="..\..\Backendless\Messaging\DeviceRegistrationDto.cs" />
521526
</ItemGroup>
522527
<ItemGroup>
528+
<None Include="app.config" />
523529
<None Include="packages.config" />
524530
</ItemGroup>
525531
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

0 commit comments

Comments
 (0)