Add project files.

This commit is contained in:
rfi
2023-10-06 20:29:27 +07:00
parent 43952cd917
commit 859e3fa042
18 changed files with 554 additions and 0 deletions

View 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
View 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
View 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
}
}

View 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; }
}
}

View 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;
}
}
}

View 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
}
}
}

View 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;
}
}
}