AppLovinDownloadHandler.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // AppLovinDownloadHandler.cs
  3. // AppLovin MAX Unity Plugin
  4. //
  5. // Created by Santosh Bagadi on 7/26/19.
  6. // Copyright © 2019 AppLovin. All rights reserved.
  7. //
  8. #if !UNITY_2017_2_OR_NEWER
  9. using System;
  10. using System.IO;
  11. using UnityEngine.Networking;
  12. namespace AppLovinMax.Scripts.IntegrationManager.Editor
  13. {
  14. public class AppLovinDownloadHandler : DownloadHandlerScript
  15. {
  16. // Required by DownloadHandler base class. Called when you address the 'bytes' property.
  17. protected override byte[] GetData()
  18. {
  19. return null;
  20. }
  21. private FileStream fileStream;
  22. public AppLovinDownloadHandler(string path) : base(new byte[2048])
  23. {
  24. var downloadDirectory = Path.GetDirectoryName(path);
  25. if (!Directory.Exists(downloadDirectory))
  26. {
  27. Directory.CreateDirectory(downloadDirectory);
  28. }
  29. try
  30. {
  31. //Open the current file to write to
  32. fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  33. }
  34. catch (Exception exception)
  35. {
  36. MaxSdkLogger.UserError(string.Format("Failed to create file at {0}\n{1}", path, exception.Message));
  37. }
  38. }
  39. protected override bool ReceiveData(byte[] byteFromServer, int dataLength)
  40. {
  41. if (byteFromServer == null || byteFromServer.Length < 1 || fileStream == null)
  42. {
  43. return false;
  44. }
  45. try
  46. {
  47. //Write the current data to the file
  48. fileStream.Write(byteFromServer, 0, dataLength);
  49. }
  50. catch (Exception exception)
  51. {
  52. fileStream.Close();
  53. fileStream = null;
  54. MaxSdkLogger.UserError(string.Format("Failed to download file{0}", exception.Message));
  55. }
  56. return true;
  57. }
  58. protected override void CompleteContent()
  59. {
  60. fileStream.Close();
  61. }
  62. }
  63. }
  64. #endif