62 lines
1.5 KiB
C#
62 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace ESPServer
|
|
{
|
|
class RegistersContainer
|
|
{
|
|
private Dictionary<int, int> registers;
|
|
|
|
public event Action<int, float> OnRegisterWritten;
|
|
|
|
public RegistersContainer()
|
|
{
|
|
registers = new Dictionary<int, int>();
|
|
}
|
|
|
|
public int this[int address]
|
|
{
|
|
get
|
|
{
|
|
lock(registers)
|
|
{
|
|
if (registers.ContainsKey(address)) return registers[address];
|
|
return 0;
|
|
}
|
|
}
|
|
set
|
|
{
|
|
lock (registers)
|
|
{
|
|
registers[address] = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetFloat(int address, float value)
|
|
{
|
|
byte[] bytes = BitConverter.GetBytes(value);
|
|
registers[address + 1] = (int)bytes[0] + (((int)bytes[1]) << 8);
|
|
registers[address] = (int)bytes[2] + (((int)bytes[3]) << 8);
|
|
}
|
|
|
|
public void WriteRegister(int address, int value)
|
|
{
|
|
registers[address] = value;
|
|
OnRegisterWritten?.Invoke(address, value);
|
|
}
|
|
|
|
public void WriteRegisters(int address, int[] values)
|
|
{
|
|
for (int i = 0; i < values.Length; i++)
|
|
{
|
|
registers[address + i] = values[i];
|
|
}
|
|
for (int i = 0; i < values.Length; i++)
|
|
{
|
|
OnRegisterWritten?.Invoke(address + i, values[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|