#include "static_mem.h"
#include <spdlog/spdlog.h>
#include <string_view>

unsigned char gh_static_mem[GH_DATA_SIZE];
void gh_init_data_segment() {
  SPDLOG_INFO("Initializing data segment");
  FILE *f = fopen(R3_DATA_SEGMENT_FILE, "rb");
  if (!f) {
    throw std::runtime_error("Failed to open gh_datasegment.bin");
  }
  size_t actual = fread(gh_static_mem, 1, gh_data_size, f);
  if (actual != gh_data_size) {
    throw std::runtime_error("Failed to read gh_datasegment.bin");
  }

  fclose(f);
}

void gh_static_mem_check() {
  // Quick check
  const char *testValue = (const char *)&GH_MEM(0x5b68f8);
  if (std::string_view(testValue) != "Directory") {
    throw std::runtime_error("gh_datasegment.bin is corrupted");
  }

  const char *otherValue = (const char *)&GH_MEM(0x005b683c);
  if (std::string_view(otherValue) != "%s") {
    throw std::runtime_error("gh_datasegment.bin is corrupted");
  }
}
