371 lines
12 KiB
C#
Raw Normal View History

2022-07-20 17:50:01 +08:00
using GTA;
using GTA.Math;
using GTA.Native;
using RageCoop.Client.Menus;
using RageCoop.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics;
2022-05-22 15:55:26 +08:00
using System.IO;
2021-07-07 13:36:25 +02:00
using System.Linq;
using System.Windows.Forms;
2022-07-21 22:42:29 +08:00
using System.Threading.Tasks;
using System.Threading;
2021-07-07 13:36:25 +02:00
2022-05-22 15:55:26 +08:00
namespace RageCoop.Client
2021-07-07 13:36:25 +02:00
{
2021-12-03 20:30:00 +01:00
/// <summary>
/// Don't use it!
/// </summary>
internal class Main : Script
2021-07-07 13:36:25 +02:00
{
2022-07-20 17:50:01 +08:00
private bool _gameLoaded = false;
internal static readonly string CurrentVersion = "V0_5_0";
2021-07-07 13:36:25 +02:00
2022-07-20 17:50:01 +08:00
internal static int LocalPlayerID = 0;
internal static RelationshipGroup SyncedPedsGroup;
2021-07-07 13:36:25 +02:00
internal static new Settings Settings = null;
2022-07-20 17:50:01 +08:00
internal static Scripting.BaseScript BaseScript = new Scripting.BaseScript();
2021-08-18 11:47:59 +02:00
#if !NON_INTERACTIVE
#endif
internal static Chat MainChat = null;
internal static Stopwatch Counter = new Stopwatch();
internal static Logger Logger = null;
2022-07-20 17:50:01 +08:00
internal static ulong Ticked = 0;
internal static Vector3 PlayerPosition;
2022-07-20 17:50:01 +08:00
internal static Scripting.Resources Resources = null;
2022-05-22 15:55:26 +08:00
private static List<Func<bool>> QueuedActions = new List<Func<bool>>();
2021-12-03 20:30:00 +01:00
/// <summary>
/// Don't use it!
/// </summary>
2021-07-07 13:36:25 +02:00
public Main()
{
2022-07-14 17:59:41 +08:00
try
{
Settings = Util.ReadSettings();
}
catch
{
GTA.UI.Notification.Show("Malformed configuration, overwriting with default values...");
Settings=new Settings();
Util.SaveSettings();
}
2022-07-02 13:53:14 +08:00
Directory.CreateDirectory(Settings.DataDirectory);
Logger=new Logger()
2022-05-31 19:35:01 -08:00
{
2022-07-02 13:53:14 +08:00
LogPath=$"{Settings.DataDirectory}\\RageCoop.Client.log",
2022-05-31 19:35:01 -08:00
UseConsole=false,
#if DEBUG
LogLevel = 0,
#else
2022-06-02 08:53:01 +08:00
LogLevel=Settings.LogLevel,
2022-05-31 19:35:01 -08:00
#endif
};
2022-06-12 17:11:14 +08:00
Resources = new Scripting.Resources();
2022-06-22 14:18:20 +08:00
if (Game.Version < GameVersion.v1_0_1290_1_Steam)
{
Tick += (object sender, EventArgs e) =>
{
if (Game.IsLoading)
{
return;
}
if (!_gameLoaded)
{
GTA.UI.Notification.Show("~r~Please update your GTA5 to v1.0.1290 or newer!", true);
_gameLoaded = true;
}
};
return;
2022-06-22 14:18:20 +08:00
}
BaseScript.OnStart();
2022-05-22 15:55:26 +08:00
SyncedPedsGroup=World.AddRelationshipGroup("SYNCPED");
Game.Player.Character.RelationshipGroup.SetRelationshipBetweenGroups(SyncedPedsGroup, Relationship.Neutral, true);
#if !NON_INTERACTIVE
#endif
MainChat = new Chat();
2021-07-07 13:36:25 +02:00
Tick += OnTick;
2022-07-20 17:50:01 +08:00
Tick += (s, e) => { Scripting.API.Events.InvokeTick(); };
2021-07-07 13:36:25 +02:00
KeyDown += OnKeyDown;
KeyDown+=(s, e) => { Scripting.API.Events.InvokeKeyDown(s, e); };
KeyUp+=(s, e) => { Scripting.API.Events.InvokeKeyUp(s, e); };
2021-08-21 16:52:17 +02:00
Aborted += (object sender, EventArgs e) => CleanUp();
2022-07-20 17:50:01 +08:00
2021-07-13 16:32:45 +02:00
Util.NativeMemory();
2022-05-22 15:55:26 +08:00
Counter.Restart();
2021-07-07 13:36:25 +02:00
}
2022-07-17 18:44:16 +08:00
#if DEBUG
#endif
2022-07-20 17:50:01 +08:00
public static Ped P;
public static float FPS;
private bool _lastDead;
2021-07-07 13:36:25 +02:00
private void OnTick(object sender, EventArgs e)
{
2022-07-20 17:50:01 +08:00
P= Game.Player.Character;
PlayerPosition=P.ReadPosition();
2022-07-30 15:45:27 +08:00
/*
var V = P.CurrentVehicle;
List<int> found;
if (V!=null)
{
found = Memory.FindOffset(V.Velocity.X, V.MemoryAddress);
new LemonUI.Elements.ScaledText(new System.Drawing.PointF(50, 100), V.Velocity.ToString()).Draw();
new LemonUI.Elements.ScaledText(new System.Drawing.PointF(50, 150), V.ReadVelocity().ToString()).Draw();
}
else
{
found = Memory.FindOffset(P.Velocity.X, P.MemoryAddress);
new LemonUI.Elements.ScaledText(new System.Drawing.PointF(50, 100), P.Velocity.ToString()).Draw();
new LemonUI.Elements.ScaledText(new System.Drawing.PointF(50, 150), P.ReadVelocity().ToString()).Draw();
}
for (int i = 0; i<found.Count; i++)
{
new LemonUI.Elements.ScaledText(new System.Drawing.PointF(200*(i+1), 50), found[i].ToString()).Draw();
}
*/
PlayerPosition=P.ReadPosition();
2022-07-20 17:50:01 +08:00
FPS=Game.FPS;
PlayerPosition=P.ReadPosition();
2021-07-07 13:36:25 +02:00
if (Game.IsLoading)
{
return;
}
else if (!_gameLoaded && (_gameLoaded = true))
2021-07-07 13:36:25 +02:00
{
#if !NON_INTERACTIVE
2022-07-20 17:50:01 +08:00
GTA.UI.Notification.Show(GTA.UI.NotificationIcon.AllPlayersConf, "RAGECOOP", "Welcome!", $"Press ~g~{Main.Settings.MenuKey}~s~ to open the menu.");
#endif
2021-07-07 13:36:25 +02:00
}
#if !NON_INTERACTIVE
CoopMenu.MenuPool.Process();
#endif
2022-07-20 17:50:01 +08:00
2022-05-22 15:55:26 +08:00
DoQueuedActions();
if (!Networking.IsOnServer)
2021-07-07 13:36:25 +02:00
{
return;
}
2022-05-22 15:55:26 +08:00
if (Game.TimeScale!=1)
2022-04-11 15:10:27 +02:00
{
2022-05-22 15:55:26 +08:00
Game.TimeScale=1;
2022-04-11 15:10:27 +02:00
}
2022-05-22 15:55:26 +08:00
try
{
EntityPool.DoSync();
2022-05-22 15:55:26 +08:00
}
catch (Exception ex)
{
2022-05-31 19:35:01 -08:00
Main.Logger.Error(ex);
2022-05-22 15:55:26 +08:00
}
2022-07-20 17:50:01 +08:00
2022-05-22 15:55:26 +08:00
2021-08-18 11:47:59 +02:00
#if DEBUG
if (Networking.ShowNetworkInfo)
2021-08-18 11:47:59 +02:00
{
new LemonUI.Elements.ScaledText(new PointF(Screen.PrimaryScreen.Bounds.Width / 2, 0), $"L: {Networking.Latency * 1000:N0}ms", 0.5f) { Alignment = GTA.UI.Alignment.Center }.Draw();
new LemonUI.Elements.ScaledText(new PointF(Screen.PrimaryScreen.Bounds.Width / 2, 30), $"R: {Lidgren.Network.NetUtility.ToHumanReadable(Statistics.BytesDownPerSecond)}/s", 0.5f) { Alignment = GTA.UI.Alignment.Center }.Draw();
new LemonUI.Elements.ScaledText(new PointF(Screen.PrimaryScreen.Bounds.Width / 2, 60), $"S: {Lidgren.Network.NetUtility.ToHumanReadable(Statistics.BytesUpPerSecond)}/s", 0.5f) { Alignment = GTA.UI.Alignment.Center }.Draw();
2021-08-18 11:47:59 +02:00
}
#endif
2022-05-22 15:55:26 +08:00
2021-07-07 13:36:25 +02:00
MainChat.Tick();
PlayerList.Tick();
if (!Scripting.API.Config.EnableAutoRespawn)
{
Function.Call(Hash.PAUSE_DEATH_ARREST_RESTART, true);
2022-06-22 14:18:20 +08:00
Function.Call(Hash.IGNORE_NEXT_RESTART, true);
2022-07-20 17:50:01 +08:00
Function.Call(Hash.FORCE_GAME_STATE_PLAYING);
2022-06-22 14:18:20 +08:00
Function.Call(Hash.TERMINATE_ALL_SCRIPTS_WITH_THIS_NAME, "respawn_controller");
if (P.IsDead)
{
Function.Call(Hash.SET_FADE_OUT_AFTER_DEATH, false);
2022-07-20 17:50:01 +08:00
if (P.Health!=1)
{
P.Health=1;
Game.Player.WantedLevel=0;
Main.Logger.Debug("Player died.");
Scripting.API.Events.InvokePlayerDied();
}
GTA.UI.Screen.StopEffects();
}
else
{
2021-07-07 13:36:25 +02:00
Function.Call(Hash.DISPLAY_HUD, true);
}
2021-07-07 13:36:25 +02:00
}
else if (P.IsDead && !_lastDead)
{
Scripting.API.Events.InvokePlayerDied();
}
_lastDead=P.IsDead;
2022-05-22 15:55:26 +08:00
Ticked++;
2021-07-07 13:36:25 +02:00
}
2022-07-30 15:45:27 +08:00
Vector3 vel =new Vector3(0,0,0.123f);
2021-07-07 13:36:25 +02:00
private void OnKeyDown(object sender, KeyEventArgs e)
{
2022-07-30 15:45:27 +08:00
if(e.KeyCode == Keys.Right)
{
vel.Z+=0.1f;
}
if (e.KeyCode == Keys.Left)
{
vel.Z-=0.1f;
}
2021-07-07 13:36:25 +02:00
if (MainChat.Focused)
{
MainChat.OnKeyDown(e.KeyCode);
return;
}
if (Networking.IsOnServer)
{
if (Game.IsControlPressed(GTA.Control.FrontendPause))
{
Function.Call(Hash.ACTIVATE_FRONTEND_MENU, Function.Call<int>(Hash.GET_HASH_KEY, "FE_MENU_VERSION_SP_PAUSE"), false, 0);
return;
}
if (Game.IsControlPressed(GTA.Control.FrontendPauseAlternate)&&Settings.DisableAlternatePause)
{
Function.Call(Hash.ACTIVATE_FRONTEND_MENU, Function.Call<int>(Hash.GET_HASH_KEY, "FE_MENU_VERSION_SP_PAUSE"), false, 0);
return;
}
}
if (e.KeyCode == Settings.MenuKey)
2022-05-22 15:55:26 +08:00
{
if (CoopMenu.MenuPool.AreAnyVisible)
{
CoopMenu.MenuPool.ForEach<LemonUI.Menus.NativeMenu>(x =>
{
if (x.Visible)
{
CoopMenu.LastMenu=x;
x.Visible=false;
}
});
}
else
{
CoopMenu.LastMenu.Visible = true;
}
2022-05-22 15:55:26 +08:00
}
else if (Game.IsControlJustPressed(GTA.Control.MultiplayerInfo))
2022-05-22 15:55:26 +08:00
{
if (Networking.IsOnServer)
{
ulong currentTimestamp = Util.GetTickCount64();
PlayerList.Pressed = (currentTimestamp - PlayerList.Pressed) < 5000 ? (currentTimestamp - 6000) : currentTimestamp;
}
2022-05-22 15:55:26 +08:00
}
else if (Game.IsControlJustPressed(GTA.Control.MpTextChatAll))
2022-05-22 15:55:26 +08:00
{
if (Networking.IsOnServer)
{
MainChat.Focused = true;
}
2022-05-22 15:55:26 +08:00
}
else if (e.KeyCode==Settings.PassengerKey)
2022-05-22 15:55:26 +08:00
{
var P = Game.Player.Character;
2022-07-20 17:50:01 +08:00
if (!P.IsInVehicle())
2022-05-22 15:55:26 +08:00
{
2022-05-23 13:02:28 +08:00
if (P.IsTaskActive(TaskType.CTaskEnterVehicle))
2022-05-22 15:55:26 +08:00
{
P.Task.ClearAll();
2022-05-22 15:55:26 +08:00
}
else
2022-05-22 15:55:26 +08:00
{
var V = World.GetClosestVehicle(P.ReadPosition(), 50);
if (V!=null)
{
var seat = P.GetNearestSeat(V);
P.Task.EnterVehicle(V, seat);
}
2022-05-22 15:55:26 +08:00
}
}
}
2021-07-07 13:36:25 +02:00
}
2022-05-22 15:55:26 +08:00
public static void CleanUp()
{
2022-05-22 15:55:26 +08:00
MainChat.Clear();
EntityPool.Cleanup();
PlayerList.Cleanup();
Main.LocalPlayerID=default;
}
2022-05-22 15:55:26 +08:00
private static void DoQueuedActions()
{
lock (QueuedActions)
{
foreach (var action in QueuedActions.ToArray())
{
try
{
if (action())
{
QueuedActions.Remove(action);
}
}
2022-07-20 17:50:01 +08:00
catch (Exception ex)
2022-05-22 15:55:26 +08:00
{
2022-06-18 12:06:22 +08:00
Logger.Error(ex);
2022-05-22 15:55:26 +08:00
QueuedActions.Remove(action);
}
}
}
}
/// <summary>
2022-07-14 18:50:15 +08:00
/// Queue an action to be executed on next tick, allowing you to call scripting API from another thread.
2022-05-22 15:55:26 +08:00
/// </summary>
2022-07-14 18:50:15 +08:00
/// <param name="a"> An action to be executed with a return value indicating whether the action can be removed after execution.</param>
internal static void QueueAction(Func<bool> a)
2022-05-22 15:55:26 +08:00
{
lock (QueuedActions)
{
QueuedActions.Add(a);
}
}
internal static void QueueAction(Action a)
2022-05-22 15:55:26 +08:00
{
lock (QueuedActions)
{
2022-07-20 17:50:01 +08:00
QueuedActions.Add(() => { a(); return true; });
2022-05-22 15:55:26 +08:00
}
}
/// <summary>
/// Clears all queued actions
/// </summary>
internal static void ClearQueuedActions()
2022-05-22 15:55:26 +08:00
{
lock (QueuedActions) { QueuedActions.Clear(); }
}
2022-07-21 22:42:29 +08:00
public static void Delay(Action a, int time)
{
Task.Run(() =>
{
Thread.Sleep(time);
QueueAction(a);
});
}
2021-07-07 13:36:25 +02:00
}
}