Files
RconGUI.App/ServerContext.cs
T

178 lines
5.7 KiB
C#
Raw Normal View History

2025-05-13 00:55:30 +03:00
using CoreRCON;
using Microsoft.Maui.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace RconGUI {
public class ServerContext : INotifyPropertyChanged {
private RCON _rcon;
public event PropertyChangedEventHandler? PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string prop = "") {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
private bool _isConnected = false;
public bool IsConnected {
get {
return _isConnected;
}
set {
if (_isConnected != value) {
_isConnected = value;
OnPropertyChanged();
}
}
}
public ObservableCollection<RconScript> CustomActions { get; private set; } = new ObservableCollection<RconScript>();
public ObservableCollection<RconScript> MapActions { get; private set; } = new ObservableCollection<RconScript>();
public ObservableCollection<ConsoleObject> ConsoleObjects { get; private set; } = new ObservableCollection<ConsoleObject>();
public ServerContext() {
CustomActions.Add(new RconScript("sv_cheats 1", "sv_cheats true"));
CustomActions.Add(new RconScript("sv_cheats 0", "sv_cheats false"));
CustomActions.Add(new RconScript("Competitive offline", "exec gamemode_competitive_offline"));
CustomActions.Add(new RconScript("Warmup end", "mp_warmup_end"));
CustomActions.Add(new RconScript("Wingman", new string[] {
"sv_cheats 1",
"sv_gravity 230",
"weapon_accuracy_nospread 1",
"weapon_recoil_scale 2.0",
"weapon_recoil_scale_motion_controller 1.0",
"weapon_recoil_view_punch_extra 0.055",
"mp_t_default_primary weapon_ssg08",
"mp_t_default_melee weapon_knife",
"mp_t_default_secondary \"\"",
"mp_ct_default_primary weapon_ssg08",
"mp_ct_default_melee weapon_knife",
"mp_ct_default_secondary \"\"",
"mp_maxmoney 0",
"mp_restartgame 1"
}));
}
public async void ReadVariables() {
var result = await _rcon.SendCommandAsync("cvarlist");
var lines = result.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
int first = 0;
for (int i = 0; i < lines.Length; i++) {
if (lines[i].StartsWith("---")) {
first = i + 1;
break;
}
}
int last = 0;
for (int i = first + 1; i < lines.Length; i++) {
last = i - 1;
if (lines[i].StartsWith("---")) {
break;
}
}
for(int i = first; i <= last; ++i) {
ConsoleObjects.Add(ParseConsoleObject(lines[i]));
}
}
private ConsoleObject ParseConsoleObject(string line) {
var blocks = line.Split(':', StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
var value = blocks[1];
ConsoleObject result;
if (value == "false") {
result = new ConsoleObject<bool>(false);
}
else if (value == "true") {
result = new ConsoleObject<bool>(true);
}else {
double dvalue = 0;
if(double.TryParse(value, out dvalue)) {
result = new ConsoleObject<double>(dvalue);
} else {
result = new ConsoleObject<string>(value);
}
}
result.Name = blocks[0];
result.Flags = blocks[2];
result.Help = blocks[3];
return result;
}
public async Task<string> RunCommand(string cmd) {
var result = await _rcon.SendCommandAsync(cmd);
//var er = await _rcon.SendCommandAsync($"echo \"Running command: {cmd}\"");
return result;
}
public async Task RunCommands(string[] cmd) {
foreach (var command in cmd) {
await RunCommand(command);
}
}
public async Task<string[]> ListOfficialMaps() {
var list = await RunCommand("maps *");
var maps = list.Split(new char[] { '\t', '\n' }, StringSplitOptions.RemoveEmptyEntries);
return maps.Where(x=> !x.EndsWith("vanity") && (x.StartsWith("de_") || x.StartsWith("cs_") || x.StartsWith("ar_"))).ToArray();
}
public async void Connect() {
_rcon = new RCON(IPAddress.Parse("109.106.136.146"), 27015, "125325Abc");
try {
await _rcon.ConnectAsync();
IsConnected = true;
MapActions.Clear();
var maps = await ListOfficialMaps();
foreach (var map in maps) {
MapActions.Add(new RconScript(map, $"map {map}"));
}
ReadVariables();
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
Console.WriteLine();
}
public async Task<string> LoadMap(string name) {
return await _rcon.SendCommandAsync($"map {name}");
}
}
}