MCPcopy Create free account
hub / github.com/ddnet/ddnet / UnpackAsset

Function UnpackAsset

src/android/android_main.cpp:15–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13#include <vector>
14
15static bool UnpackAsset(const char *pFilename)
16{
17 char aAssetFilename[IO_MAX_PATH_LENGTH] = "asset_integrity_files/";
18 str_append(aAssetFilename, pFilename);
19
20 // This uses SDL_RWFromFile because it can read Android assets,
21 // which are files stored in the app's APK file. All data files
22 // are stored as assets and unpacked to the external storage.
23 SDL_RWops *pAssetFile = SDL_RWFromFile(aAssetFilename, "rb");
24 if(!pAssetFile)
25 {
26 log_error("android", "Failed to open asset '%s' for reading", pFilename);
27 return false;
28 }
29
30 const long int FileLength = SDL_RWsize(pAssetFile);
31 if(FileLength < 0)
32 {
33 SDL_RWclose(pAssetFile);
34 log_error("android", "Failed to determine length of asset '%s'", pFilename);
35 return false;
36 }
37
38 char *pData = static_cast<char *>(malloc(FileLength));
39 const size_t ReadLength = SDL_RWread(pAssetFile, pData, 1, FileLength);
40 SDL_RWclose(pAssetFile);
41
42 if(ReadLength != (size_t)FileLength)
43 {
44 free(pData);
45 log_error("android", "Failed to read asset '%s' (read %" PRIzu ", wanted %ld)", pFilename, ReadLength, FileLength);
46 return false;
47 }
48
49 IOHANDLE TargetFile = io_open(pFilename, IOFLAG_WRITE);
50 if(!TargetFile)
51 {
52 free(pData);
53 log_error("android", "Failed to open '%s' for writing", pFilename);
54 return false;
55 }
56
57 const size_t WriteLength = io_write(TargetFile, pData, FileLength);
58 io_close(TargetFile);
59 free(pData);
60
61 if(WriteLength != (size_t)FileLength)
62 {
63 log_error("android", "Failed to write data to '%s' (wrote %" PRIzu ", wanted %ld)", pFilename, WriteLength, FileLength);
64 return false;
65 }
66
67 return true;
68}
69
70constexpr const char *INTEGRITY_INDEX = "integrity.txt";
71constexpr const char *INTEGRITY_INDEX_SAVE = "integrity_save.txt";

Callers 1

InitAndroidFunction · 0.85

Calls 4

io_openFunction · 0.85
io_writeFunction · 0.85
io_closeFunction · 0.85
str_appendFunction · 0.50

Tested by

no test coverage detected