Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various changes #20

Merged
merged 13 commits into from
Sep 1, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace Fuyu.Platform.Common.Hashing
{
public static class EftHash
public static class SimpleId
{
private static readonly Random _random;
private static readonly char[] _chars;

static EftHash()
static SimpleId()
{
_random = new Random();

Expand Down
2 changes: 1 addition & 1 deletion Fuyu.Platform.Common/Http/FuyuContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private string GetPath()
return path;
}

public Dictionary<string, string> GetParameters(FuyuBehaviour behaviour)
public Dictionary<string, string> GetPathParameters(FuyuBehaviour behaviour)
{
var result = new Dictionary<string, string>();
var segments = Path.Split('/');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace Fuyu.Platform.Common.Http
{
// NOTE: Don't dispose this, keep a reference for the lifetime of the
// application.
public class FuyuClient : IDisposable
public class FuyuHttpClient : IDisposable
{
protected HttpClient Httpv;
protected string Address;
protected string Cookie;
protected int Retries;

public FuyuClient(string address, string sessionId = "", int retries = 3)
public FuyuHttpClient(string address, string sessionId = "", int retries = 3)
{
Address = address;
Cookie = $"PHPSESSID={sessionId}";
Expand Down
80 changes: 80 additions & 0 deletions Fuyu.Platform.Common/Http/FuyuWsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using WebSocketSharp;

namespace Fuyu.Platform.Common.Http
{
public class FuyuWsClient : IDisposable
{
private readonly string _url;
private WebSocket _ws;

public FuyuWsClient(string address, string path)
{
_url = address + path;
}

public void Start()
{
_ws = new WebSocket(_url)
{
EmitOnPing = true
};

_ws.OnMessage += OnMessage;
_ws.Connect();
}

public void Stop()
{
_ws.Close();
}

public void Send(string text)
{
_ws.Send(text);
}

public void Send(byte[] data)
{
_ws.Send(data);
}

private void OnMessage(object sender, MessageEventArgs e)
{
if (e.IsPing)
{
OnReceivePing();
}

if (e.IsText)
{
OnReceiveText(e.Data);
}

if (e.IsBinary)
{
OnReceiveBytes(e.RawData);
}
}

public virtual void OnReceivePing()
{
// intentionally empty
}

public virtual void OnReceiveText(string text)
{
// intentionally empty
}

public virtual void OnReceiveBytes(byte[] data)
{
// intentionally empty
}

public void Dispose()
{
Stop();
}
}
}
212 changes: 212 additions & 0 deletions Fuyu.Platform.Common/Models/EFT/Common/MongoId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
using System;
using Newtonsoft.Json;

namespace Fuyu.Platform.Common.Models.EFT.Common
{
[Serializable]
public readonly struct MongoId : IComparable<MongoId>, IEquatable<MongoId>
{
private static readonly Random _random = new Random();
private static readonly ulong _processId = Hash;
private static uint _newIdCounter;
private readonly uint _timeStamp;
private readonly ulong _counter;

public static uint UnixTimestamp
{
get
{
var startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
var currTime = DateTime.Now.ToUniversalTime() - startTime;
var result = Convert.ToUInt32(Math.Abs(currTime.TotalSeconds));
return result;
}
}

public static ulong Hash
{
get
{
return (ulong)(((long)_random.Next(0, int.MaxValue) << 8) ^ (long)_random.Next(0, int.MaxValue));
}
}

[JsonConstructor]
public MongoId([JsonProperty("$value")] string id)
{
if (id == null || id.Length != 24)
{
throw new ArgumentOutOfRangeException("Critical MongoId error: incorrect length. Id: " + id);
}

_timeStamp = GetTimestamp(id);
_counter = GetCounter(id);

GenerateNew();
}

public MongoId(bool newProcessId = true)
{
_timeStamp = 0U;

if (newProcessId)
{
_counter = Hash << 24;
}
else
{
_newIdCounter += 1U;
_counter = (_processId << 24) + (ulong)_newIdCounter;
}

_timeStamp = UnixTimestamp;
}

public MongoId(int accountId)
{
_timeStamp = UnixTimestamp;

var num = Convert.ToUInt32(accountId);
var num2 = Convert.ToUInt32(_random.Next(0, 16777215));

_counter = 4294967296UL | (ulong)num;
_counter <<= 24;
_counter |= (ulong)num2;
}

public MongoId(MongoId source, int increment, bool newTimestamp = true)
{
_timeStamp = newTimestamp
? UnixTimestamp
: source._timeStamp;

_counter = (increment > 0)
? (source._counter + (ulong)Convert.ToUInt32(increment))
: (source._counter - (ulong)Convert.ToUInt32(Math.Abs(increment)));
}

// smethod_1
public static uint GetTimestamp(string id)
{
return Convert.ToUInt32(id.Substring(0, 8), 16);
}

// smethod_2
public static ulong GetCounter(string id)
{
return Convert.ToUInt64(id.Substring(8, 16), 16);
}

// method_0
public void GenerateNew()
{
var num = Convert.ToUInt64(_counter >> 24);

if (_processId != num)
{
return;
}

var num2 = Convert.ToUInt32(_counter << 40 >> 40);
_newIdCounter = Math.Max(_newIdCounter, num2);
}

public bool Equals(MongoId other)
{
return _timeStamp == other._timeStamp
&& _counter == other._counter;
}

public int CompareTo(MongoId other)
{
if (this == other)
{
return 0;
}

if (this > other)
{
return 1;
}

return -1;
}

public override string ToString()
{
return _timeStamp.ToString("X8").ToLower()
+ _counter.ToString("X16").ToLower();
}

public override bool Equals(object obj)
{
if (obj != null)
{
if (obj is MongoId)
{
var mongoID = (MongoId)obj;
return mongoID == this;
}

string text;

if ((text = obj as string) != null)
{
return text == this.ToString();
}
}

return false;
}

public override int GetHashCode()
{
var num = Convert.ToUInt32(_counter >> 32) * 3637U;
var num2 = Convert.ToUInt32(_counter << 32 >> 32) * 5807U;

return (int)(_timeStamp ^ num ^ num2);
}

public static implicit operator string(MongoId mongoId)
{
return mongoId.ToString();
}

public static implicit operator MongoId(string id)
{
return new MongoId(id);
}

public static bool operator ==(MongoId a, MongoId b)
{
return a.Equals(b);
}

public static bool operator !=(MongoId a, MongoId b)
{
return !a.Equals(b);
}

public static bool operator >(MongoId a, MongoId b)
{
return a._timeStamp > b._timeStamp
|| (a._timeStamp == b._timeStamp && a._counter > b._counter);
}

public static bool operator <(MongoId a, MongoId b)
{
return a._timeStamp < b._timeStamp
|| (a._timeStamp == b._timeStamp && a._counter < b._counter);
}

public static bool operator >=(MongoId a, MongoId b)
{
return a == b || a > b;
}

public static bool operator <=(MongoId a, MongoId b)
{
return a == b || a < b;
}
}
}
4 changes: 2 additions & 2 deletions Fuyu.Platform.Common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class Program
}
```

### FuyuClient
### FuyuHttpClient

It's simple wrapper around `HttpClient` that's good enough for most cases. You
can make HTTP requests with it to a HTTP server. It only supports HTTP without
Expand All @@ -68,7 +68,7 @@ public class Program
static async Task Main()
{
// make a client instance
var client = new FuyuClient("http://localhost:8000");
var client = new FuyuHttpClient("http://localhost:8000");

// make request
var data = await client.GetAsync("/helloworld");
Expand Down
8 changes: 5 additions & 3 deletions Fuyu.Platform.Server/Behaviours/EFT/GameProfileCreate.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Fuyu.Platform.Common.Hashing;
using Fuyu.Platform.Common.Http;
using Fuyu.Platform.Common.IO;
using Fuyu.Platform.Common.Models.EFT.Common;
using Fuyu.Platform.Common.Models.EFT.Profiles;
using Fuyu.Platform.Common.Models.EFT.Requests;
using Fuyu.Platform.Common.Models.EFT.Responses;
Expand Down Expand Up @@ -31,15 +31,17 @@ public override void Run(FuyuContext context)

// TODO: PVP-PVE STATE DETECTION

// generate ids
var pmcId = new MongoId(accountId).ToString();
var savageId = new MongoId(pmcId, 1, false).ToString();

// create savage
var savageId = EftHash.Generate();
account.EftSave.PvE.Savage = Json.Parse<Profile>(_savageJson);

account.EftSave.PvE.Savage._id = savageId;
account.EftSave.PvE.Savage.aid = accountId;

// create pmc
var pmcId = EftHash.Generate();
var voiceTemplate = EftDatabase.Templates.GetCustomization(request.voiceId);

account.EftSave.PvE.Pmc = request.side == "bear"
Expand Down
2 changes: 1 addition & 1 deletion Fuyu.Platform.Server/Behaviours/EFT/Locale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Locale() : base("/client/locale/{languageId}")

public override void Run(FuyuContext context)
{
var arguments = context.GetParameters(this);
var arguments = context.GetPathParameters(this);

var languageId = arguments["languageId"];
var locale = EftDatabase.Locales.GetGlobalLocale(languageId);
Expand Down
Loading