Read a whole file. Prefer the engine's FileServer when available (so PBO / search-path resolution works); fall back to std::ifstream in unit-test contexts where the server isn't initialised.
| 53 | // search-path resolution works); fall back to std::ifstream in unit-test |
| 54 | // contexts where the server isn't initialised. |
| 55 | bool SlurpFile(const char* name, std::vector<uint8_t>& out) |
| 56 | { |
| 57 | if (GFileServer) |
| 58 | { |
| 59 | QIFStream in; |
| 60 | GFileServer->Open(in, name); |
| 61 | if (!in.fail() && in.rest() > 0) |
| 62 | { |
| 63 | int rest = in.rest(); |
| 64 | const auto* bytes = reinterpret_cast<const uint8_t*>(in.act()); |
| 65 | out.assign(bytes, bytes + rest); |
| 66 | return true; |
| 67 | } |
| 68 | } |
| 69 | std::ifstream file(name, std::ios::binary | std::ios::ate); |
| 70 | if (!file) |
| 71 | return false; |
| 72 | auto size = file.tellg(); |
| 73 | if (size <= 0) |
| 74 | return false; |
| 75 | out.resize(static_cast<size_t>(size)); |
| 76 | file.seekg(0); |
| 77 | file.read(reinterpret_cast<char*>(out.data()), size); |
| 78 | return file.good(); |
| 79 | } |
| 80 | |
| 81 | // Box-average color of an RGBA8 buffer. Used as ITextureSource::GetAverageColor(). |
| 82 | PackedColor ComputeAverage(const uint8_t* rgba, int w, int h) |