Skip to content

Commit 2842b48

Browse files
kristijan-ujevickirre-bylund
authored andcommitted
Implement methods for updating player files
1 parent f84f89d commit 2842b48

File tree

5 files changed

+105
-5
lines changed

5 files changed

+105
-5
lines changed

Runtime/Client/LootLockerBaseServerAPI.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ UnityWebRequest CreateWebRequest(string url, LootLockerServerRequest request)
250250
case LootLockerHTTPMethod.UPLOAD_FILE:
251251
webRequest = UnityWebRequest.Post(url, request.form);
252252
break;
253+
case LootLockerHTTPMethod.UPDATE_FILE:
254+
// Workaround for UnityWebRequest with PUT HTTP verb not having form fields
255+
webRequest = UnityWebRequest.Post(url, request.form);
256+
webRequest.method = UnityWebRequest.kHttpVerbPUT;
257+
break;
253258
case LootLockerHTTPMethod.POST:
254259
case LootLockerHTTPMethod.PATCH:
255260
// Defaults are fine for PUT

Runtime/Client/LootLockerEndPoints.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections;
1+
using System.Collections;
22
using System.Collections.Generic;
33
using UnityEngine;
44

@@ -50,6 +50,7 @@ public class LootLockerEndPoints
5050
public static EndPointClass getPlayerFilesByPlayerId = new EndPointClass("player/{0}/files", LootLockerHTTPMethod.GET);
5151
public static EndPointClass getSingleplayerFile = new EndPointClass("player/files/{0}", LootLockerHTTPMethod.GET);
5252
public static EndPointClass uploadPlayerFile = new EndPointClass("player/files", LootLockerHTTPMethod.UPLOAD_FILE);
53+
public static EndPointClass updatePlayerFile = new EndPointClass("/player/files/{0}", LootLockerHTTPMethod.UPDATE_FILE);
5354
public static EndPointClass deletePlayerFile = new EndPointClass("/player/files/{0}", LootLockerHTTPMethod.DELETE);
5455

5556
// Player Progressions

Runtime/Client/LootLockerServerRequest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public enum LootLockerHTTPMethod
2323
CREATE = 5,
2424
OPTIONS = 6,
2525
PATCH = 7,
26-
UPLOAD_FILE = 8
26+
UPLOAD_FILE = 8,
27+
UPDATE_FILE = 9
2728
}
2829

2930
/// <summary>

Runtime/Game/LootLockerSDKManager.cs

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,8 +1284,7 @@ public static void UploadPlayerFile(string pathToFile, string filePurpose, bool
12841284
LootLockerResponse.Serialize(onComplete, serverResponse);
12851285
});
12861286
}
1287-
1288-
///
1287+
12891288
/// <summary>
12901289
/// Upload a file with the provided name and content. The file will be owned by the player with the provided playerID.
12911290
/// It will not be viewable by other players.
@@ -1376,7 +1375,7 @@ public static void UploadPlayerFile(byte[] fileBytes, string fileName, string fi
13761375
LootLockerResponse.Serialize(onComplete, serverResponse);
13771376
});
13781377
}
1379-
1378+
13801379
/// <summary>
13811380
/// Upload a file using a byte array. Can be useful if you want to upload without storing anything on disk. The file will be owned by the currently active player.
13821381
/// </summary>
@@ -1388,6 +1387,99 @@ public static void UploadPlayerFile(byte[] fileBytes, string fileName, string fi
13881387
{
13891388
UploadPlayerFile(fileBytes, fileName, filePurpose, false, onComplete);
13901389
}
1390+
1391+
///////////////////////////////////////////////////////////////////////////////
1392+
1393+
/// <summary>
1394+
/// Update an existing player file with a new file.
1395+
/// </summary>
1396+
/// <param name="fileId">Id of the file. You can get the ID of files when you upload a file, or with GetAllPlayerFiles()</param>
1397+
/// <param name="pathToFile">Path to the file, example: Application.persistentDataPath + "/" + fileName;</param>
1398+
/// <param name="onComplete">onComplete Action for handling the response of type LootLockerPlayerFile</param>
1399+
public static void UpdatePlayerFile(int fileId, string pathToFile, Action<LootLockerPlayerFile> onComplete)
1400+
{
1401+
if (!CheckInitialized())
1402+
{
1403+
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerPlayerFile>());
1404+
return;
1405+
}
1406+
1407+
var fileBytes = new byte[] { };
1408+
try
1409+
{
1410+
fileBytes = File.ReadAllBytes(pathToFile);
1411+
}
1412+
catch (Exception e)
1413+
{
1414+
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)($"File error: {e.Message}");
1415+
return;
1416+
}
1417+
1418+
var endpoint = string.Format(LootLockerEndPoints.updatePlayerFile.endPoint, fileId);
1419+
1420+
LootLockerServerRequest.UploadFile(endpoint, LootLockerEndPoints.updatePlayerFile.httpMethod, fileBytes, Path.GetFileName(pathToFile), "multipart/form-data", new Dictionary<string, string>(),
1421+
onComplete: (serverResponse) =>
1422+
{
1423+
LootLockerResponse.Serialize(onComplete, serverResponse);
1424+
});
1425+
}
1426+
1427+
/// <summary>
1428+
/// Update an existing player file with a new file using a Filestream. Can be useful if you want to upload without storing anything on disk.
1429+
/// </summary>
1430+
/// <param name="fileId">Id of the file. You can get the ID of files when you upload a file, or with GetAllPlayerFiles()</param>
1431+
/// <param name="fileStream">Filestream to upload</param>
1432+
/// <param name="onComplete">onComplete Action for handling the response of type LootLockerPlayerFile</param>
1433+
public static void UpdatePlayerFile(int fileId, FileStream fileStream, Action<LootLockerPlayerFile> onComplete)
1434+
{
1435+
if (!CheckInitialized())
1436+
{
1437+
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerPlayerFile>());
1438+
return;
1439+
}
1440+
1441+
var fileBytes = new byte[fileStream.Length];
1442+
try
1443+
{
1444+
fileStream.Read(fileBytes, 0, Convert.ToInt32(fileStream.Length));
1445+
}
1446+
catch (Exception e)
1447+
{
1448+
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)($"File error: {e.Message}");
1449+
return;
1450+
}
1451+
1452+
var endpoint = string.Format(LootLockerEndPoints.updatePlayerFile.endPoint, fileId);
1453+
1454+
LootLockerServerRequest.UploadFile(endpoint, LootLockerEndPoints.updatePlayerFile.httpMethod, fileBytes, Path.GetFileName(fileStream.Name), "multipart/form-data", new Dictionary<string, string>(),
1455+
onComplete: (serverResponse) =>
1456+
{
1457+
LootLockerResponse.Serialize(onComplete, serverResponse);
1458+
});
1459+
}
1460+
1461+
/// <summary>
1462+
/// Update an existing player file with a new file using a byte array. Can be useful if you want to upload without storing anything on disk.
1463+
/// </summary>
1464+
/// <param name="fileId">Id of the file. You can get the ID of files when you upload a file, or with GetAllPlayerFiles()</param>
1465+
/// <param name="fileBytes">Byte array to upload</param>
1466+
/// <param name="onComplete">onComplete Action for handling the response of type LootLockerPlayerFile</param>
1467+
public static void UpdatePlayerFile(int fileId, byte[] fileBytes, Action<LootLockerPlayerFile> onComplete)
1468+
{
1469+
if (!CheckInitialized())
1470+
{
1471+
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerPlayerFile>());
1472+
return;
1473+
}
1474+
1475+
var endpoint = string.Format(LootLockerEndPoints.updatePlayerFile.endPoint, fileId);
1476+
1477+
LootLockerServerRequest.UploadFile(endpoint, LootLockerEndPoints.updatePlayerFile.httpMethod, fileBytes, null, "multipart/form-data", new Dictionary<string, string>(),
1478+
onComplete: (serverResponse) =>
1479+
{
1480+
LootLockerResponse.Serialize(onComplete, serverResponse);
1481+
});
1482+
}
13911483

13921484
/// <summary>
13931485
/// The file will be deleted immediately and the action can not be reversed. You will get the ID of files when you upload a file, or with GetAllPlayerFiles().

Runtime/Game/Requests/PlayerRequest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ public class LootLockerPlayerFilesResponse : LootLockerResponse
199199
public class LootLockerPlayerFile : LootLockerResponse
200200
{
201201
public int id { get; set; }
202+
public string revision_id { get; set; }
202203
public string name { get; set; }
203204
public int size { get; set; }
204205
public string purpose { get; set; }

0 commit comments

Comments
 (0)