<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Memory Reader</title>
<style>
html, body {
      height: 100%;
      margin: 0;
      padding: 0;
      font-family: monospace;
      background-color: #222222;
      color: #eee;
    }

    .container {
      display: flex;
      flex-direction: column;
      height: 100%;
    }

    .tab-buttons {
      display: flex;
      justify-content: space-evenly;
      background-color: #2b2b2b;
      padding: 10px;
    }

    .tab-buttons button {
      padding: 5px 10px;
      background-color: #444;
      border: none;
      color: white;
      cursor: pointer;
    }

    .tab-buttons button.active {
      background-color: #666;
    }

    .tab-content {
      display: none;
      flex: 1;
      padding: 10px;
      overflow-y: auto;
      box-sizing: border-box;
    }

    .tab-content.active {
      display: block;
    }

    input, button, select {
      font-size: 12px;
      padding: 4px;
      width: 100%;
      margin: 3px 0;
      box-sizing: border-box;
    }

    .result {
      margin-top: 10px;
      white-space: pre;
      background: #2b2b2b;
      padding: 8px;
      border-radius: 5px;
      overflow-y: auto;
      max-height: 300px;
      font-size: 12px;
    }
</style></head><body>
  <div class="container">
    <div class="tab-buttons">
      <label>Address value search</label>
    </div>
    
    <div id="valueMenu" class="tab-content active">
    
      <label>Base Address</label>
      <input id="addr" placeholder="Address">
      
      <label>Range Offset</label>
      <input id="range" placeholder="Range">

      <label>Value Type</label>
      <select id="valueType">
       <option value="F32">F32 (Float)</option>
       <option value="F64">F64 (Double)</option>
       <option value="I32">I32 (Dword)</option>
       <option value="I64" selected>I64 (Qword)</option>
      </select>

      <button onclick="readMemory()">Single Update</button>
      <button onclick="toggleAuto(this)">Auto Update: OFF</button>

      <div class="result" id="output">Results...</div>
    </div>
  </div>


<script>
   if (typeof h5gg !== 'undefined') {
  setWindowDrag(0, 0, 400, 80);
  setWindowRect(60, 60, 300, 300);
 }
 
    let timer = null;

 function toggleAuto(button) {
      if (timer) {
        clearInterval(timer);
        timer = null;
        button.textContent = "Auto Update: OFF";
      } else {
        const interval = 1000;
        timer = setInterval(readMemory, interval);
        button.textContent = "Auto Update: ON";
      }
    }

function parseHex(str) {
  str = str.trim().toLowerCase();

  let isNegative = false;

  if (str.startsWith("-")) {
    isNegative = true;
    str = str.slice(1);
  }

  if (!str.startsWith("0x")) {
    str = "0x" + str;
  }

  let value = BigInt(str);
  return isNegative ? -value : value;
}

function readMemory() {
  const addrInput = document.getElementById("addr").value;
  const rangeInput = document.getElementById("range").value;
  const output = document.getElementById("output");
  const type = document.getElementById("valueType").value;

  let base, range;
  try {
    base = parseHex(addrInput);
    range = parseHex(rangeInput);
  } catch {
    output.textContent = "Invalid address or range format.";
    return;
  }

  let result = "";
  const step = 4n;
  const startOffset = range < 0n ? range : 0n;
  const endOffset = range > 0n ? range : 0n;

  for (let offset = startOffset; offset <= endOffset; offset += step) {
    const addr = base + offset;
    const val = h5gg.getValue(addr.toString(), type)
    const offsetStr = offset >= 0n ? `+0x${offset.toString(16)}` : `-0x${(-offset).toString(16)}`;
    result += `${offsetStr.padEnd(10)} = ${val}\n`;
  }

  output.textContent = result || "No values found.";
}

</script>
</body>
</html>