mirror of
https://git.lewd.wtf/PGR/ascnet
synced 2025-12-12 17:44:43 +01:00
Add project files.
This commit is contained in:
15
AscNet.Common/AscNet.Common.csproj
Normal file
15
AscNet.Common/AscNet.Common.csproj
Normal file
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Config.Net" Version="5.1.5" />
|
||||
<PackageReference Include="Google.Protobuf" Version="3.24.3" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
25
AscNet.Common/Common.cs
Normal file
25
AscNet.Common/Common.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using MongoDB.Driver;
|
||||
using Config.Net;
|
||||
|
||||
namespace AscNet.Common
|
||||
{
|
||||
public static class Common
|
||||
{
|
||||
public static readonly IConfig config;
|
||||
public static readonly MongoClient mongoClient;
|
||||
public static readonly IMongoDatabase db;
|
||||
|
||||
static Common()
|
||||
{
|
||||
config = new ConfigurationBuilder<IConfig>().UseJsonFile("Configs/config.json").Build();
|
||||
mongoClient = new(
|
||||
new MongoClientSettings
|
||||
{
|
||||
Server = new MongoServerAddress(config.Database.Host, config.Database.Port),
|
||||
// Credential = MongoCredential.CreateCredential("admin", config.Database.Username, config.Database.Password)
|
||||
}
|
||||
);
|
||||
db = mongoClient.GetDatabase(config.Database.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
50
AscNet.Common/Config.cs
Normal file
50
AscNet.Common/Config.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Config.Net;
|
||||
|
||||
namespace AscNet.Common
|
||||
{
|
||||
public interface IConfig
|
||||
{
|
||||
[Option(DefaultValue = VerboseLevel.Normal)]
|
||||
VerboseLevel VerboseLevel { get; set; }
|
||||
|
||||
[Option]
|
||||
IGameServer GameServer { get; set; }
|
||||
|
||||
[Option]
|
||||
IDatabase Database { get; set; }
|
||||
|
||||
[Option(DefaultValue = false)]
|
||||
bool SaveClientLogs { get; set; }
|
||||
|
||||
|
||||
interface IGameServer
|
||||
{
|
||||
[Option(DefaultValue = "127.0.0.1")]
|
||||
string Host { get; set; }
|
||||
|
||||
[Option(DefaultValue = (ushort)2335)]
|
||||
ushort Port { get; set; }
|
||||
}
|
||||
|
||||
interface IDatabase
|
||||
{
|
||||
[Option(DefaultValue = "127.0.0.1")]
|
||||
string Host { get; set; }
|
||||
|
||||
[Option(DefaultValue = (ushort)27017)]
|
||||
ushort Port { get; set; }
|
||||
|
||||
[Option(DefaultValue = "sf")]
|
||||
string Name { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public enum VerboseLevel
|
||||
{
|
||||
Silent = 0,
|
||||
Normal = 1,
|
||||
Debug = 2,
|
||||
SuperDebug = 3
|
||||
}
|
||||
}
|
||||
104
AscNet.Common/Database/Account.cs
Normal file
104
AscNet.Common/Database/Account.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace AscNet.Common.Database
|
||||
{
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
public class Account
|
||||
{
|
||||
public static readonly IMongoCollection<Account> collection = Common.db.GetCollection<Account>("accounts");
|
||||
|
||||
public static Account? FromUID(long uid)
|
||||
{
|
||||
return collection.AsQueryable().FirstOrDefault(x => x.Uid == uid);
|
||||
}
|
||||
|
||||
public static Account? FromAccessToken(string token)
|
||||
{
|
||||
return collection.AsQueryable().FirstOrDefault(x => x.AccessToken == token);
|
||||
}
|
||||
|
||||
public static Account? FromPhone(string phone)
|
||||
{
|
||||
return collection.AsQueryable().FirstOrDefault(x => x.PhoneNum == phone);
|
||||
}
|
||||
|
||||
public static Account? FromPhone(string phone, string password)
|
||||
{
|
||||
return collection.AsQueryable().FirstOrDefault(x => x.PhoneNum == phone && x.Password == password);
|
||||
}
|
||||
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
public static Account Create(string phone, string password)
|
||||
{
|
||||
if (collection.AsQueryable().FirstOrDefault(x => x.PhoneNum == phone) is not null)
|
||||
throw new ArgumentException("Phone is already registered!", "phone");
|
||||
|
||||
Account account = new()
|
||||
{
|
||||
Uid = (collection.AsQueryable().OrderByDescending(x => x.Uid).FirstOrDefault()?.Uid ?? 0) + 1,
|
||||
PhoneNum = phone,
|
||||
Email = "",
|
||||
Password = password,
|
||||
AccessToken = Guid.NewGuid().ToString(),
|
||||
Age = 0,
|
||||
IsActivation = false,
|
||||
IsAdult = true,
|
||||
IsGuest = false,
|
||||
UnfreezeTime = 0,
|
||||
IsReal = true
|
||||
};
|
||||
|
||||
collection.InsertOne(account);
|
||||
return account;
|
||||
}
|
||||
|
||||
[BsonId]
|
||||
public ObjectId Id { get; set; }
|
||||
|
||||
[BsonElement("uid")]
|
||||
[BsonRequired]
|
||||
public long Uid { get; set; }
|
||||
|
||||
[BsonElement("phone_num")]
|
||||
[BsonRequired]
|
||||
public string PhoneNum { get; set; }
|
||||
|
||||
[BsonElement("email")]
|
||||
[BsonRequired]
|
||||
public string Email { get; set; }
|
||||
|
||||
[BsonElement("password")]
|
||||
[BsonRequired]
|
||||
public string Password { get; set; }
|
||||
|
||||
[BsonElement("access_token")]
|
||||
[BsonRequired]
|
||||
public string AccessToken { get; set; }
|
||||
|
||||
[BsonElement("age")]
|
||||
[BsonRequired]
|
||||
public int Age { get; set; }
|
||||
|
||||
[BsonElement("is_activation")]
|
||||
[BsonRequired]
|
||||
public bool IsActivation { get; set; }
|
||||
|
||||
[BsonElement("is_adult")]
|
||||
[BsonRequired]
|
||||
public bool IsAdult { get; set; }
|
||||
|
||||
[BsonElement("is_guest")]
|
||||
[BsonRequired]
|
||||
public bool IsGuest { get; set; }
|
||||
|
||||
[BsonElement("unfreeze_time")]
|
||||
[BsonRequired]
|
||||
public int UnfreezeTime { get; set; }
|
||||
|
||||
[BsonElement("is_real")]
|
||||
[BsonRequired]
|
||||
public bool IsReal { get; set; }
|
||||
}
|
||||
}
|
||||
15
AscNet.Common/Util/Crypto.cs
Normal file
15
AscNet.Common/Util/Crypto.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace AscNet.Common.Util
|
||||
{
|
||||
public static class Crypto
|
||||
{
|
||||
public static byte[] XORCrypt(byte[] data, byte[] key)
|
||||
{
|
||||
byte[] encryptedData = new byte[data.Length];
|
||||
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
encryptedData[i] = (byte)(data[i] ^ key[i % key.Length]);
|
||||
|
||||
return encryptedData;
|
||||
}
|
||||
}
|
||||
}
|
||||
100
AscNet.Common/Util/Logger.cs
Normal file
100
AscNet.Common/Util/Logger.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace AscNet.Common.Util
|
||||
{
|
||||
public class Logger
|
||||
{
|
||||
public static readonly Logger c = new("SF", ConsoleColor.DarkRed);
|
||||
private readonly string _name;
|
||||
private readonly bool TraceOnError;
|
||||
private readonly ConsoleColor _color;
|
||||
|
||||
public Logger(string name, ConsoleColor color = ConsoleColor.Cyan, bool traceOnError = true)
|
||||
{
|
||||
_name = name;
|
||||
_color = color;
|
||||
TraceOnError = traceOnError;
|
||||
}
|
||||
|
||||
public void Log(params string[] message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.Write(DateTime.Now.ToString("HH:mm:ss "));
|
||||
Console.ResetColor();
|
||||
Console.Write("<");
|
||||
Console.ForegroundColor = _color;
|
||||
Console.Write(_name);
|
||||
Console.ResetColor();
|
||||
Console.Write("> ");
|
||||
Console.WriteLine(string.Join("\t", message));
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
public void Warn(params string[] message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.Write(DateTime.Now.ToString("HH:mm:ss "));
|
||||
Console.ResetColor();
|
||||
Console.Write("<");
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
Console.Write(_name);
|
||||
Console.ResetColor();
|
||||
Console.Write("> ");
|
||||
Console.WriteLine(string.Join("\t", message));
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
public void Trail(params string[] msg)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.DarkGray;
|
||||
Console.WriteLine($"\t└── {string.Join(' ', msg)}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
public void Error(params string[] message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.Write(DateTime.Now.ToString("HH:mm:ss "));
|
||||
Console.ResetColor();
|
||||
Console.Write("<");
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.Write(_name);
|
||||
Console.ResetColor();
|
||||
Console.Write("> ");
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
if (TraceOnError)
|
||||
Console.BackgroundColor = ConsoleColor.DarkRed;
|
||||
Console.WriteLine(string.Join("\t", message));
|
||||
Console.ResetColor();
|
||||
#if DEBUG
|
||||
StackTrace trace = new(true);
|
||||
if (TraceOnError)
|
||||
Trail(trace.ToString());
|
||||
#endif
|
||||
}
|
||||
|
||||
public void Debug(params string[] message)
|
||||
{
|
||||
#if DEBUG
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.Write(DateTime.Now.ToString("HH:mm:ss "));
|
||||
Console.ResetColor();
|
||||
Console.Write("<");
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
Console.Write(_name);
|
||||
Console.ResetColor();
|
||||
Console.Write("> ");
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.BackgroundColor = ConsoleColor.DarkMagenta;
|
||||
Console.WriteLine(string.Join("\t", message));
|
||||
Console.ResetColor();
|
||||
Console.BackgroundColor = ConsoleColor.Black;
|
||||
|
||||
StackTrace trace = new(true);
|
||||
if (TraceOnError)
|
||||
Trail(trace.ToString());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
35
AscNet.Common/Util/Miscs.cs
Normal file
35
AscNet.Common/Util/Miscs.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
namespace AscNet.Common.Util
|
||||
{
|
||||
public static class Miscs
|
||||
{
|
||||
public static byte[] HexStringToByteArray(string hex)
|
||||
{
|
||||
if (hex.Length % 2 == 1)
|
||||
throw new Exception("The binary key cannot have an odd number of digits");
|
||||
|
||||
byte[] arr = new byte[hex.Length >> 1];
|
||||
|
||||
for (int i = 0; i < hex.Length >> 1; ++i)
|
||||
{
|
||||
arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
private static int GetHexVal(char hex)
|
||||
{
|
||||
int val = (int)hex;
|
||||
//For uppercase A-F letters:
|
||||
//return val - (val < 58 ? 48 : 55);
|
||||
//For lowercase a-f letters:
|
||||
//return val - (val < 58 ? 48 : 87);
|
||||
//Or the two combined, but a bit slower:
|
||||
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
|
||||
}
|
||||
public static byte ToByte(this bool val)
|
||||
{
|
||||
return val ? (byte)1 : (byte)0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user