2021-09-29 14:34:22 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
2021-08-20 17:28:13 +02:00
|
|
|
|
using System.ComponentModel;
|
2021-08-18 11:47:59 +02:00
|
|
|
|
using System.Timers;
|
|
|
|
|
|
2021-08-18 12:47:36 +02:00
|
|
|
|
using CoopServer;
|
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
namespace FirstGameMode
|
|
|
|
|
{
|
2021-09-29 14:34:22 +02:00
|
|
|
|
[Serializable]
|
|
|
|
|
public class SetPlayerTime
|
|
|
|
|
{
|
|
|
|
|
public int Hours { get; set; }
|
|
|
|
|
public int Minutes { get; set; }
|
|
|
|
|
public int Seconds { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public class Main : ServerScript
|
|
|
|
|
{
|
|
|
|
|
private static readonly Timer RunningSinceTimer = new() { Interval = 1000 };
|
|
|
|
|
private static int RunningSince = 0;
|
|
|
|
|
|
2021-08-18 12:47:36 +02:00
|
|
|
|
public Main()
|
2021-08-18 11:47:59 +02:00
|
|
|
|
{
|
|
|
|
|
RunningSinceTimer.Start();
|
|
|
|
|
RunningSinceTimer.Elapsed += new ElapsedEventHandler((sender, e) => RunningSince += 1);
|
|
|
|
|
|
2021-08-18 12:47:36 +02:00
|
|
|
|
API.OnPlayerConnected += OnPlayerConnected;
|
|
|
|
|
API.OnPlayerDisconnected += OnPlayerDisconnected;
|
|
|
|
|
API.OnChatMessage += OnChatMessage;
|
2021-09-29 14:34:22 +02:00
|
|
|
|
API.OnModPacketReceived += OnModPacketReceived;
|
2021-08-18 12:47:36 +02:00
|
|
|
|
|
|
|
|
|
API.RegisterCommand("running", RunningCommand);
|
|
|
|
|
API.RegisterCommands<Commands>();
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-29 14:34:22 +02:00
|
|
|
|
private void OnModPacketReceived(long from, string mod, byte customID, byte[] bytes, CancelEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (mod == "FirstScript" && customID == 1)
|
|
|
|
|
{
|
|
|
|
|
args.Cancel = true;
|
|
|
|
|
|
|
|
|
|
// Get data from bytes
|
|
|
|
|
SetPlayerTime setPlayerTime = bytes.Deserialize<SetPlayerTime>();
|
|
|
|
|
|
|
|
|
|
// Find the client by 'from' and send the time back as a nativecall
|
|
|
|
|
API.GetAllClients().Find(x => x.ID == from).SendNativeCall(0x47C3B5848C3E45D8, setPlayerTime.Hours, setPlayerTime.Minutes, setPlayerTime.Seconds);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public static void RunningCommand(CommandContext ctx)
|
|
|
|
|
{
|
2021-08-26 17:01:32 +02:00
|
|
|
|
ctx.Client.SendChatMessage("Server has been running for: " + RunningSince + " seconds!");
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-26 17:01:32 +02:00
|
|
|
|
public static void OnPlayerConnected(Client client)
|
2021-08-18 11:47:59 +02:00
|
|
|
|
{
|
2021-08-26 17:01:32 +02:00
|
|
|
|
API.SendChatMessageToAll("Player " + client.Player.Username + " connected!");
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-26 17:01:32 +02:00
|
|
|
|
public static void OnPlayerDisconnected(Client client)
|
2021-08-18 11:47:59 +02:00
|
|
|
|
{
|
2021-08-26 17:01:32 +02:00
|
|
|
|
API.SendChatMessageToAll("Player " + client.Player.Username + " disconnected!");
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 12:47:36 +02:00
|
|
|
|
public static void OnChatMessage(string username, string message, CancelEventArgs e)
|
2021-08-18 11:47:59 +02:00
|
|
|
|
{
|
2021-08-18 12:47:36 +02:00
|
|
|
|
e.Cancel = true;
|
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
if (message.StartsWith("EASTEREGG"))
|
|
|
|
|
{
|
2021-08-26 17:01:32 +02:00
|
|
|
|
Client client;
|
|
|
|
|
if ((client = API.GetClientByUsername(username)) != null)
|
|
|
|
|
{
|
|
|
|
|
client.SendChatMessage("You found the EASTEREGG! *-*");
|
|
|
|
|
}
|
2021-08-18 12:47:36 +02:00
|
|
|
|
return;
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 12:47:36 +02:00
|
|
|
|
API.SendChatMessageToAll(message, username);
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
2021-09-29 14:34:22 +02:00
|
|
|
|
}
|
2021-08-20 17:28:13 +02:00
|
|
|
|
|
2021-09-29 14:34:22 +02:00
|
|
|
|
public static class CustomSerializer
|
|
|
|
|
{
|
|
|
|
|
public static byte[] SerializeToByteArray(this object obj)
|
2021-08-20 17:28:13 +02:00
|
|
|
|
{
|
2021-09-29 14:34:22 +02:00
|
|
|
|
if (obj == null)
|
2021-08-20 17:28:13 +02:00
|
|
|
|
{
|
2021-09-29 14:34:22 +02:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BinaryFormatter bf = new BinaryFormatter();
|
|
|
|
|
using (MemoryStream ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
bf.Serialize(ms, obj);
|
|
|
|
|
return ms.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T Deserialize<T>(this byte[] byteArray) where T : class
|
|
|
|
|
{
|
|
|
|
|
if (byteArray == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (MemoryStream memStream = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
BinaryFormatter binForm = new BinaryFormatter();
|
|
|
|
|
memStream.Write(byteArray, 0, byteArray.Length);
|
|
|
|
|
memStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
T obj = (T)binForm.Deserialize(memStream);
|
|
|
|
|
return obj;
|
2021-08-20 17:28:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|