Untitled

 avatar
unknown
plain_text
a month ago
3.7 kB
31
Indexable
import tkinter as tk
from tkinter import ttk
import pymem
import pymem.process
import time
from flask import Flask, jsonify, render_template
from threading import Thread

app = Flask(__name__)

value_1 = (None)

def read_memory():
    global value_1
    try:
        pm = pymem.Pymem("SimRail.exe")
        module = pymem.process.module_from_name(pm.process_handle, "GameAssembly.dll")
        module_base = module.lpBaseOfDll

        addresses_config = {
            "value_1": [
                {"base_offset": 0x068662B0, "offsets": [0xB8, 0x0, 0x40, 0x80, 0x248, 0x20, 0xC0], "type": float},
                {"base_offset": 0x067F1D70, "offsets": [0xB8, 0x50, 0x1F0, 0x50, 0x50, 0x60, 0xC0], "type": float},
                {"base_offset": 0x067DC4D8, "offsets": [0xB8, 0x10, 0x60, 0x410, 0x118, 0x20, 0xC0], "type": float},
                {"base_offset": 0x068662B0, "offsets": [0xB8, 0x0, 0x60, 0x418, 0x208, 0x20, 0xC0], "type": float},
                {"base_offset": 0x067EC8D0, "offsets": [0xB8, 0x10, 0x78, 0x918, 0x80, 0x20, 0xC0], "type": float},
                {"base_offset": 0x067F1D70, "offsets": [0xB8, 0x50, 0x80, 0x248, 0x20, 0xC0], "type": float},
                {"base_offset": 0x067CBAA0, "offsets": [0x70, 0xB8, 0x20, 0x78, 0x248, 0x20, 0xC0], "type": float},

            ],
        }


        def read_value(config_list):
            for config in config_list:
                try:
                    address = module_base + config["base_offset"]
                    for offset in config["offsets"]:
                        address = pm.read_longlong(address) + offset
                    value = pm.read_float(address) if config["type"] == float else pm.read_int(address)
                    return value
                except Exception:
                    continue
            return None  # Jeśli żaden adres nie działa, zwraca None

        # Aktualizacja wartości
        value_1 = read_value(addresses_config["value_1"])

    except Exception as e:
        print(f"Błąd odczytu pamięci: {e}")

# Funkcja do odczytu pamięci co 150 ms
def continuous_read():
    while True:
        read_memory()
        time.sleep(0.15)


@app.route('/api/receive_data', methods=['GET'])
def get_data():

    return jsonify({
        "value_1": value_1,
    })


def create_gui():
    root = tk.Tk()
    root.title("TEST API SimRail")
    root.geometry("500x300")


    fields = [tk.StringVar(value="---") for _ in range(6)]
    labels = []

    names = ["Amperomierz WN2 "]

    for i, (field, name) in enumerate(zip(fields, names)):
        label = ttk.Label(root, text=name, font=("Arial", 10))
        label.grid(row=i, column=0, sticky="w", padx=10, pady=5)
        labels.append(label)

        value_label = ttk.Label(root, textvariable=field, font=("Arial", 10), width=30, relief="sunken")
        value_label.grid(row=i, column=1, padx=10, pady=5)


    def format_value(value, is_float):
        if value is None:
            return "Brak"
        if is_float:
            return f"{value:.2f}"
        return str(value)


    def update_gui():
        while True:
            fields[0].set(f"{'Połączono' if value_1 is not None else 'Brak połączenia'}: {format_value(value_1, True)}")

    Thread(target=update_gui, daemon=True).start()

    root.mainloop()


def run_flask():
    app.run(host="0.0.0.0", port=5000, debug=False)

if __name__ == "__main__":

    read_thread = Thread(target=continuous_read, daemon=True)
    read_thread.start()


    flask_thread = Thread(target=run_flask, daemon=True)
    flask_thread.start()


    create_gui()

Leave a Comment