81 lines
2.2 KiB
C#
81 lines
2.2 KiB
C#
using Aki.Common;
|
|
using astealz.SmartSpawnController.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Aki.SinglePlayer.Utils
|
|
{
|
|
public class ServerConfig
|
|
{
|
|
public string BackendUrl { get; }
|
|
public string Version { get; }
|
|
|
|
public ServerConfig(string backendUrl, string version)
|
|
{
|
|
BackendUrl = backendUrl;
|
|
Version = version;
|
|
}
|
|
}
|
|
public static class RequestHandler
|
|
{
|
|
private static string _host;
|
|
private static string _session;
|
|
private static Request _request;
|
|
private static Dictionary<string, string> _headers;
|
|
|
|
static RequestHandler()
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
private static void Initialize()
|
|
{
|
|
_request = new Request();
|
|
|
|
var args = Environment.GetCommandLineArgs();
|
|
|
|
foreach (var arg in args)
|
|
{
|
|
if (arg.Contains("BackendUrl"))
|
|
{
|
|
var json = arg.Replace("-config=", string.Empty);
|
|
_host = Newtonsoft.Json.JsonConvert.DeserializeObject<ServerConfig>(json).BackendUrl;
|
|
}
|
|
|
|
if (arg.Contains("-token="))
|
|
{
|
|
_session = arg.Replace("-token=", string.Empty);
|
|
_headers = new Dictionary<string, string>()
|
|
{
|
|
{ "Cookie", $"PHPSESSID={_session}" },
|
|
{ "SessionId", _session }
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void ValidateJson(string json)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(json))
|
|
{
|
|
Logger.Error($"Request failed, body is null");
|
|
}
|
|
|
|
Logger.Info($"Request was successful");
|
|
}
|
|
|
|
public static string GetJson(string path)
|
|
{
|
|
var url = _host + path;
|
|
|
|
Logger.Info($"Request GET json: {_session}:{url}");
|
|
var data = _request.Send(url, "GET", headers: _headers);
|
|
var result = Encoding.UTF8.GetString(data);
|
|
|
|
ValidateJson(result);
|
|
return result;
|
|
}
|
|
}
|
|
}
|