Clean up
This commit is contained in:
@ -1,26 +1,33 @@
|
||||
using GTA;
|
||||
using GTA.Math;
|
||||
using GTA.Native;
|
||||
using RageCoop.Client.Menus;
|
||||
using RageCoop.Client.Scripting;
|
||||
using RageCoop.Core;
|
||||
using SHVDN;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
using GTA;
|
||||
using GTA.Math;
|
||||
using GTA.Native;
|
||||
using GTA.UI;
|
||||
using LemonUI.Elements;
|
||||
using LemonUI.Menus;
|
||||
using Lidgren.Network;
|
||||
using RageCoop.Client.Menus;
|
||||
using RageCoop.Client.Scripting;
|
||||
using RageCoop.Core;
|
||||
using SHVDN;
|
||||
using Console = GTA.Console;
|
||||
using Control = GTA.Control;
|
||||
using Screen = System.Windows.Forms.Screen;
|
||||
using Script = GTA.Script;
|
||||
|
||||
namespace RageCoop.Client
|
||||
{
|
||||
/// <summary>
|
||||
/// Don't use it!
|
||||
/// Don't use it!
|
||||
/// </summary>
|
||||
[ScriptAttributes(Author = "RageCoop", NoDefaultInstance = false, SupportURL = "https://github.com/RAGECOOP/RAGECOOP-V")]
|
||||
[ScriptAttributes(Author = "RageCoop", NoDefaultInstance = false,
|
||||
SupportURL = "https://github.com/RAGECOOP/RAGECOOP-V")]
|
||||
internal class Main : Script
|
||||
{
|
||||
private static bool _gameLoaded = false;
|
||||
@ -31,9 +38,6 @@ namespace RageCoop.Client
|
||||
internal static RelationshipGroup SyncedPedsGroup;
|
||||
|
||||
internal static new Settings Settings = null;
|
||||
|
||||
#if !NON_INTERACTIVE
|
||||
#endif
|
||||
internal static Chat MainChat = null;
|
||||
internal static Stopwatch Counter = new Stopwatch();
|
||||
internal static Logger Logger = null;
|
||||
@ -41,26 +45,34 @@ namespace RageCoop.Client
|
||||
internal static Vector3 PlayerPosition;
|
||||
internal static Resources Resources = null;
|
||||
private static readonly ConcurrentQueue<Action> TaskQueue = new ConcurrentQueue<Action>();
|
||||
public static string LogPath => $"{Settings.DataDirectory}\\RageCoop.Client.log";
|
||||
|
||||
public static Ped P;
|
||||
public static float FPS;
|
||||
private static bool _lastDead;
|
||||
|
||||
/// <summary>
|
||||
/// Don't use it!
|
||||
/// Don't use it!
|
||||
/// </summary>
|
||||
public Main()
|
||||
{
|
||||
Util.StartUpCheck();
|
||||
Console.Info($"Starting {typeof(Main).FullName}, domain: {AppDomain.CurrentDomain.Id} {AppDomain.CurrentDomain.FriendlyName}");
|
||||
Console.Info(
|
||||
$"Starting {typeof(Main).FullName}, domain: {AppDomain.CurrentDomain.Id} {AppDomain.CurrentDomain.FriendlyName}");
|
||||
try
|
||||
{
|
||||
Settings = Util.ReadSettings();
|
||||
if (Settings.DataDirectory.StartsWith("Scripts"))
|
||||
{
|
||||
var defaultDir = new Settings().DataDirectory;
|
||||
Console.Warning("Data directory must be outside scripts folder, migrating to default direcoty: " + defaultDir);
|
||||
Console.Warning("Data directory must be outside scripts folder, migrating to default direcoty: " +
|
||||
defaultDir);
|
||||
if (Directory.Exists(Settings.DataDirectory))
|
||||
{
|
||||
CoreUtils.CopyFilesRecursively(new DirectoryInfo(Settings.DataDirectory), new DirectoryInfo(defaultDir));
|
||||
CoreUtils.CopyFilesRecursively(new DirectoryInfo(Settings.DataDirectory),
|
||||
new DirectoryInfo(defaultDir));
|
||||
Directory.Delete(Settings.DataDirectory, true);
|
||||
}
|
||||
|
||||
Settings.DataDirectory = defaultDir;
|
||||
Util.SaveSettings();
|
||||
}
|
||||
@ -71,6 +83,7 @@ namespace RageCoop.Client
|
||||
Settings = new Settings();
|
||||
Util.SaveSettings();
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(Settings.DataDirectory);
|
||||
Logger = new Logger()
|
||||
{
|
||||
@ -112,17 +125,20 @@ namespace RageCoop.Client
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_gameLoaded)
|
||||
{
|
||||
GTA.UI.Notification.Show("~r~Please update your GTA5 to v1.0.1290 or newer!", true);
|
||||
Notification.Show("~r~Please update your GTA5 to v1.0.1290 or newer!", true);
|
||||
_gameLoaded = true;
|
||||
}
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
BaseScript.OnStart();
|
||||
SyncedPedsGroup = World.AddRelationshipGroup("SYNCPED");
|
||||
Game.Player.Character.RelationshipGroup.SetRelationshipBetweenGroups(SyncedPedsGroup, Relationship.Neutral, true);
|
||||
Game.Player.Character.RelationshipGroup.SetRelationshipBetweenGroups(SyncedPedsGroup, Relationship.Neutral,
|
||||
true);
|
||||
#if !NON_INTERACTIVE
|
||||
#endif
|
||||
MainChat = new Chat();
|
||||
@ -132,9 +148,10 @@ namespace RageCoop.Client
|
||||
|
||||
Util.NativeMemory();
|
||||
Counter.Restart();
|
||||
|
||||
}
|
||||
|
||||
public static string LogPath => $"{Settings.DataDirectory}\\RageCoop.Client.log";
|
||||
|
||||
private static void OnAborted(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
@ -179,20 +196,30 @@ namespace RageCoop.Client
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queue an action to main thread and wait for execution to complete, must be called from script thread.
|
||||
/// Queue an action to main thread and wait for execution to complete, must be called from script thread.
|
||||
/// </summary>
|
||||
/// <param name="task"></param>
|
||||
internal static void QueueToMainThreadAndWait(Action task)
|
||||
{
|
||||
Exception e = null;
|
||||
TaskQueue.Enqueue(() => { try { task(); } catch (Exception ex) { e = ex; } });
|
||||
TaskQueue.Enqueue(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
task();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
e = ex;
|
||||
}
|
||||
});
|
||||
Yield();
|
||||
if (e != null) { throw e; }
|
||||
if (e != null)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static Ped P;
|
||||
public static float FPS;
|
||||
private static bool _lastDead;
|
||||
private static void OnTick(object sender, EventArgs e)
|
||||
{
|
||||
P = Game.Player.Character;
|
||||
@ -205,7 +232,8 @@ namespace RageCoop.Client
|
||||
else if (!_gameLoaded && (_gameLoaded = true))
|
||||
{
|
||||
#if !NON_INTERACTIVE
|
||||
GTA.UI.Notification.Show(GTA.UI.NotificationIcon.AllPlayersConf, "RAGECOOP", "Welcome!", $"Press ~g~{Main.Settings.MenuKey}~s~ to open the menu.");
|
||||
Notification.Show(NotificationIcon.AllPlayersConf, "RAGECOOP", "Welcome!",
|
||||
$"Press ~g~{Settings.MenuKey}~s~ to open the menu.");
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -218,6 +246,7 @@ namespace RageCoop.Client
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Game.TimeScale != 1)
|
||||
{
|
||||
Game.TimeScale = 1;
|
||||
@ -225,9 +254,14 @@ namespace RageCoop.Client
|
||||
|
||||
if (Networking.ShowNetworkInfo)
|
||||
{
|
||||
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();
|
||||
new ScaledText(new PointF(Screen.PrimaryScreen.Bounds.Width / 2, 0),
|
||||
$"L: {Networking.Latency * 1000:N0}ms", 0.5f) { Alignment = Alignment.Center }.Draw();
|
||||
new ScaledText(new PointF(Screen.PrimaryScreen.Bounds.Width / 2, 30),
|
||||
$"R: {NetUtility.ToHumanReadable(Statistics.BytesDownPerSecond)}/s", 0.5f)
|
||||
{ Alignment = Alignment.Center }.Draw();
|
||||
new ScaledText(new PointF(Screen.PrimaryScreen.Bounds.Width / 2, 60),
|
||||
$"S: {NetUtility.ToHumanReadable(Statistics.BytesUpPerSecond)}/s", 0.5f)
|
||||
{ Alignment = Alignment.Center }.Draw();
|
||||
}
|
||||
|
||||
MainChat.Tick();
|
||||
@ -249,6 +283,7 @@ namespace RageCoop.Client
|
||||
Logger.Debug("Player died.");
|
||||
API.Events.InvokePlayerDied();
|
||||
}
|
||||
|
||||
GTA.UI.Screen.StopEffects();
|
||||
}
|
||||
else
|
||||
@ -264,6 +299,7 @@ namespace RageCoop.Client
|
||||
_lastDead = P.IsDead;
|
||||
Ticked++;
|
||||
}
|
||||
|
||||
private static void OnKeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (MainChat.Focused)
|
||||
@ -271,11 +307,12 @@ namespace RageCoop.Client
|
||||
MainChat.OnKeyDown(e.KeyCode);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Networking.IsOnServer)
|
||||
{
|
||||
if (Voice.WasInitialized())
|
||||
{
|
||||
if (Game.IsControlPressed(GTA.Control.PushToTalk))
|
||||
if (Game.IsControlPressed(Control.PushToTalk))
|
||||
{
|
||||
Voice.StartRecording();
|
||||
return;
|
||||
@ -287,22 +324,26 @@ namespace RageCoop.Client
|
||||
}
|
||||
}
|
||||
|
||||
if (Game.IsControlPressed(GTA.Control.FrontendPause))
|
||||
if (Game.IsControlPressed(Control.FrontendPause))
|
||||
{
|
||||
Function.Call(Hash.ACTIVATE_FRONTEND_MENU, Function.Call<int>(Hash.GET_HASH_KEY, "FE_MENU_VERSION_SP_PAUSE"), false, 0);
|
||||
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)
|
||||
|
||||
if (Game.IsControlPressed(Control.FrontendPauseAlternate) && Settings.DisableAlternatePause)
|
||||
{
|
||||
Function.Call(Hash.ACTIVATE_FRONTEND_MENU, Function.Call<int>(Hash.GET_HASH_KEY, "FE_MENU_VERSION_SP_PAUSE"), false, 0);
|
||||
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)
|
||||
{
|
||||
if (CoopMenu.MenuPool.AreAnyVisible)
|
||||
{
|
||||
CoopMenu.MenuPool.ForEach<LemonUI.Menus.NativeMenu>(x =>
|
||||
CoopMenu.MenuPool.ForEach<NativeMenu>(x =>
|
||||
{
|
||||
if (x.Visible)
|
||||
{
|
||||
@ -316,20 +357,25 @@ namespace RageCoop.Client
|
||||
CoopMenu.LastMenu.Visible = true;
|
||||
}
|
||||
}
|
||||
else if (Game.IsControlJustPressed(GTA.Control.MpTextChatAll))
|
||||
else if (Game.IsControlJustPressed(Control.MpTextChatAll))
|
||||
{
|
||||
if (Networking.IsOnServer)
|
||||
{
|
||||
MainChat.Focused = true;
|
||||
}
|
||||
}
|
||||
else if (MainChat.Focused) { return; }
|
||||
else if (Game.IsControlJustPressed(GTA.Control.MultiplayerInfo))
|
||||
else if (MainChat.Focused)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if (Game.IsControlJustPressed(Control.MultiplayerInfo))
|
||||
{
|
||||
if (Networking.IsOnServer)
|
||||
{
|
||||
ulong currentTimestamp = Util.GetTickCount64();
|
||||
PlayerList.Pressed = (currentTimestamp - PlayerList.Pressed) < 5000 ? (currentTimestamp - 6000) : currentTimestamp;
|
||||
PlayerList.Pressed = (currentTimestamp - PlayerList.Pressed) < 5000
|
||||
? (currentTimestamp - 6000)
|
||||
: currentTimestamp;
|
||||
}
|
||||
}
|
||||
else if (e.KeyCode == Settings.PassengerKey)
|
||||
@ -362,12 +408,14 @@ namespace RageCoop.Client
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
P.Task.EnterVehicle(V, seat, -1, 5, EnterVehicleFlags.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Connected()
|
||||
{
|
||||
Memory.ApplyPatches();
|
||||
@ -375,21 +423,21 @@ namespace RageCoop.Client
|
||||
{
|
||||
Voice.Init();
|
||||
}
|
||||
|
||||
API.QueueAction(() =>
|
||||
{
|
||||
WorldThread.Traffic(!Settings.DisableTraffic);
|
||||
Function.Call(Hash.SET_ENABLE_VEHICLE_SLIPSTREAMING, true);
|
||||
CoopMenu.ConnectedMenuSetting();
|
||||
MainChat.Init();
|
||||
GTA.UI.Notification.Show("~g~Connected!");
|
||||
Notification.Show("~g~Connected!");
|
||||
});
|
||||
|
||||
Logger.Info(">> Connected <<");
|
||||
}
|
||||
|
||||
public static void Disconnected(string reason)
|
||||
{
|
||||
|
||||
|
||||
Logger.Info($">> Disconnected << reason: {reason}");
|
||||
API.QueueAction(() =>
|
||||
{
|
||||
@ -397,13 +445,14 @@ namespace RageCoop.Client
|
||||
{
|
||||
MainChat.Focused = false;
|
||||
}
|
||||
|
||||
PlayerList.Cleanup();
|
||||
MainChat.Clear();
|
||||
EntityPool.Cleanup();
|
||||
WorldThread.Traffic(true);
|
||||
Function.Call(Hash.SET_ENABLE_VEHICLE_SLIPSTREAMING, false);
|
||||
CoopMenu.DisconnectedMenuSetting();
|
||||
GTA.UI.Notification.Show("~r~Disconnected: " + reason);
|
||||
Notification.Show("~r~Disconnected: " + reason);
|
||||
LocalPlayerID = default;
|
||||
Resources.Unload();
|
||||
});
|
||||
@ -412,6 +461,7 @@ namespace RageCoop.Client
|
||||
Voice.ClearAll();
|
||||
}
|
||||
|
||||
|
||||
#if !NON_INTERACTIVE
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user