#include "skincache.hpp" #include #include #include #include "../vcpkgs/arm64-v8a-android/include/spdlog/spdlog.h" const std::string& get_cache_file_full_path() { static std::string cache_file = [] { char cmdline[512] = {0}; int fd = open("/proc/self/cmdline", O_RDONLY); if (fd != -1) { read(fd, cmdline, sizeof(cmdline)); close(fd); return "/storage/emulated/0/Android/data/" + std::string(cmdline) + "/blhxskin"; } return std::string(); }(); return cache_file; } bool SkinCache::has_ship(ShipId ship_id) { return m_ships_skin.contains(ship_id); } SkinCache::SkinId SkinCache::get_skin(ShipId ship_id) { return m_ships_skin.at(ship_id); } void SkinCache::save(ShipId ship_id, SkinId skin_id) { m_ships_skin[ship_id] = skin_id; save_to_file(); } void SkinCache::save_to_file() { auto item_count = m_ships_skin.size(); auto cached_data = std::unique_ptr(new ShipSkin[item_count]); auto cached_data_raw = cached_data.get(); auto it = m_ships_skin.begin(); for(int i = 0; it != m_ships_skin.end(); ++it, ++i) { cached_data_raw[i].ship_id = it->first; cached_data_raw[i].skin_id = it->second; } auto cache_file = std::ofstream(get_cache_file_full_path(), std::ios::binary | std::ios::trunc); if (!cache_file.is_open()) { return; } cache_file.write(reinterpret_cast(cached_data_raw), sizeof(ShipSkin) * item_count); } void SkinCache::clear() { m_ships_skin.clear(); std::filesystem::remove(get_cache_file_full_path()); } SkinCache& SkinCache::get_instance() { static SkinCache instance = [] { SkinCache cache; cache.load(); return cache; }(); return instance; } void SkinCache::load() { SPDLOG_INFO("Ship skin cache file: {}", get_cache_file_full_path()); if(!std::filesystem::exists(get_cache_file_full_path())) { return; } auto file_size = std::filesystem::file_size(get_cache_file_full_path()); if (file_size == 0) { return; } auto cache_file = std::ifstream(get_cache_file_full_path(), std::ios::binary); if (!cache_file.is_open()) { return; } auto cached_data = std::unique_ptr(new ShipSkin[file_size / sizeof(ShipSkin)]); cache_file.read(reinterpret_cast(cached_data.get()), file_size); for (int i = 0; i < file_size / sizeof(ShipSkin); i++) { const auto& item = cached_data[i]; m_ships_skin[item.ship_id] = item.skin_id; } }