diff --git a/ReallifeGamemode.Server/Main.cs b/ReallifeGamemode.Server/Main.cs index c50c6ce4..439d20df 100644 --- a/ReallifeGamemode.Server/Main.cs +++ b/ReallifeGamemode.Server/Main.cs @@ -29,212 +29,212 @@ using System.IO; namespace ReallifeGamemode.Server { - public class Main : Script + public class Main : Script + { + public static readonly Vector3 DEFAULT_SPAWN_POSITION = new Vector3(-1033.93603515625, -2731.572998046875, 13.756634712219238); + public static readonly float DEFAULT_SPAWN_HEADING = -32.23991012573242f; + + public static readonly CultureInfo SERVER_CULTURE = new CultureInfo("de-DE"); + + private Core.Events.EventHandler eventHandler; + + private readonly Mutex logMutex = new Mutex(false, "RAGE_MAIN_LOG_MUTEX"); + + public Main() { - public static readonly Vector3 DEFAULT_SPAWN_POSITION = new Vector3(-1033.93603515625, -2731.572998046875, 13.756634712219238); - public static readonly float DEFAULT_SPAWN_HEADING = -32.23991012573242f; + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + } - public static readonly CultureInfo SERVER_CULTURE = new CultureInfo("de-DE"); - - private Core.Events.EventHandler eventHandler; - - private readonly Mutex logMutex = new Mutex(false, "RAGE_MAIN_LOG_MUTEX"); - - public Main() + [ServerEvent(Event.ResourceStart)] + public void OnResourceStart() + { + try + { + if (System.Environment.GetEnvironmentVariable("RAGEMP_UPDATE_DATABASE_ON_STARTUP", System.EnvironmentVariableTarget.User) == "true") { - AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; - } + using var dbContext = new DatabaseContext(true); + + var pendingMigrations = dbContext.Database.GetPendingMigrations(); + + if (!pendingMigrations.Any()) + { + System.Console.WriteLine("No migrations to apply"); + } + else + { + System.Console.WriteLine("Applying {0} migrations", pendingMigrations.Count()); + foreach (var migration in pendingMigrations) + { + System.Console.WriteLine("\t{0}", migration); + } - [ServerEvent(Event.ResourceStart)] - public void OnResourceStart() - { try { - if (System.Environment.GetEnvironmentVariable("RAGEMP_UPDATE_DATABASE_ON_STARTUP", System.EnvironmentVariableTarget.User) == "true") - { - using var dbContext = new DatabaseContext(true); - - var pendingMigrations = dbContext.Database.GetPendingMigrations(); - - if (!pendingMigrations.Any()) - { - System.Console.WriteLine("No migrations to apply"); - } - else - { - System.Console.WriteLine("Applying {0} migrations", pendingMigrations.Count()); - foreach (var migration in pendingMigrations) - { - System.Console.WriteLine("\t{0}", migration); - } - - try - { - dbContext.Database.Migrate(); - } - catch (System.Exception e) - { - System.Console.WriteLine("Error while updating database: {0}", e.ToString()); - System.Console.ReadLine(); - System.Environment.Exit(1); - } - - System.Console.WriteLine("Migrations successfull"); - } - } - - var methods = Assembly.GetExecutingAssembly() - .GetTypes() - .SelectMany(t => t.GetMethods()) - .Where(m => m.GetCustomAttributes(typeof(CommandAttribute), false).Length > 0) - .ToArray(); - - var cmdAttributes = methods.Select(c => c.GetCustomAttribute()).ToList(); - - var registeredCommands = cmdAttributes.Select(c => c.CommandString).ToList(); - cmdAttributes.Where(c => c.Alias?.Any() ?? false).ToList().ForEach(a => - { - registeredCommands.AddRange(a.Alias.Split(',')); - }); - - IAPI apiInstance = new RageAPI(); - eventHandler = new Core.Events.EventHandler(apiInstance); - - new Core.Main(apiInstance, eventHandler, registeredCommands.ToArray()); - - NAPI.Server.SetGlobalServerChat(false); - - NAPI.Server.SetCommandErrorMessage(""); - NAPI.Server.SetDefaultSpawnLocation(DEFAULT_SPAWN_POSITION, DEFAULT_SPAWN_HEADING); - NAPI.Server.SetAutoSpawnOnConnect(false); - NAPI.Server.SetAutoRespawnAfterDeath(false); - NAPI.Data.SetWorldData("playerCreatorDimension", 0); - - JsonConvert.DefaultSettings = () => - { - return new JsonSerializerSettings() - { - NullValueHandling = NullValueHandling.Ignore - }; - }; - - InventoryManager.LoadItems(); - ShopManager.LoadClotheShops(); - ShopManager.LoadItemShops(); - ShopManager.LoadFriseur(); - ShopManager.LoadAmmunations(); - TuningManager.LoadTuningGarages(); - - TimeManager.StartTimeManager(); - VehicleManager.CheckEnabledMods(); - - DatabaseHelper.InitDatabaseFirstTime(); - - FactionHelper.CheckFactionBankAccounts(); - BusinessManager.LoadBusinesses(); - //InteriorManager.LoadInteriors(); - DoorManager.LoadDoors(); - ATMManager.InitATMs(); - CityHallManager.LoadCityHall(); - JobManager.LoadJobs(); - //TaxiDriverJob.StartTaxiTimer(); Obselete - //HouseManager.LoadHouses(); - DrivingSchool.DrivingSchool.Setup(); - PlaneSchool.Setup(); - Gangwar.Gangwar.loadTurfs(); - Bank.Bank.Setup(); - Introduction.Setup(); - PositionManager.LoadPositionManager(); - LoadManager.LoadLoadManager(); - Rentcar.Setup(); - - World.WeatherSync.Load(); - - TempBlip tempBlip = new TempBlip() - { - Color = 1, - Name = "", - Transparency = 0, - ShortRange = true, - Sprite = 1, - Scale = 1, - }; - - NAPI.Data.SetWorldData("blipTemplate", tempBlip); - - //WantedEscapeTimer.WantedTimer(); Obselete - //Jail.JailTimer(); Obselete - //Economy.PaydayTimer(); Obselete - // WeaponDealManager.WeaponDealTimer(); Obselete - - ThreadTimers.StartAllTimers(); - - Managers.SVarManager.LoadSVars(); - - UserBankAccount.BalanceChanged += (account) => - { - using (var dbContext = new DatabaseContext()) - { - var user = dbContext.Users.Where(u => u.BankAccountId == account.Id).Select(u => u.Name).FirstOrDefault(); - if (user == null) - { - return; - } - - Player player = PlayerService.GetPlayerByNameOrId(user); - if (player == null) - { - return; - } - - player.TriggerEvent("updateMoney", account.Balance); - } - }; - - User.HandMoneyChanged += (user) => - { - user.Player.TriggerEvent("SERVER:SET_HANDMONEY", user.Handmoney); - }; - - //IPLS - NAPI.World.RequestIpl("vw_casino_garage"); - + dbContext.Database.Migrate(); } - catch (Exception e) + catch (System.Exception e) { - Log(e.ToString()); + System.Console.WriteLine("Error while updating database: {0}", e.ToString()); + System.Console.ReadLine(); + System.Environment.Exit(1); } + + System.Console.WriteLine("Migrations successfull"); + } } - private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + var methods = Assembly.GetExecutingAssembly() + .GetTypes() + .SelectMany(t => t.GetMethods()) + .Where(m => m.GetCustomAttributes(typeof(CommandAttribute), false).Length > 0) + .ToArray(); + + var cmdAttributes = methods.Select(c => c.GetCustomAttribute()).ToList(); + + var registeredCommands = cmdAttributes.Select(c => c.CommandString).ToList(); + cmdAttributes.Where(c => c.Alias?.Any() ?? false).ToList().ForEach(a => { - Log(e.ExceptionObject.ToString()); - } + registeredCommands.AddRange(a.Alias.Split(',')); + }); - private void Log(string message) + IAPI apiInstance = new RageAPI(); + eventHandler = new Core.Events.EventHandler(apiInstance); + + new Core.Main(apiInstance, eventHandler, registeredCommands.ToArray()); + + NAPI.Server.SetGlobalServerChat(false); + + NAPI.Server.SetCommandErrorMessage(""); + NAPI.Server.SetDefaultSpawnLocation(DEFAULT_SPAWN_POSITION, DEFAULT_SPAWN_HEADING); + NAPI.Server.SetAutoSpawnOnConnect(false); + NAPI.Server.SetAutoRespawnAfterDeath(false); + NAPI.Data.SetWorldData("playerCreatorDimension", 0); + + JsonConvert.DefaultSettings = () => { - string basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); - string log = Path.Combine(basePath, "log.txt"); + return new JsonSerializerSettings() + { + NullValueHandling = NullValueHandling.Ignore + }; + }; - logMutex.WaitOne(); + InventoryManager.LoadItems(); + ShopManager.LoadClotheShops(); + ShopManager.LoadItemShops(); + ShopManager.LoadFriseur(); + ShopManager.LoadAmmunations(); + TuningManager.LoadTuningGarages(); - if (!File.Exists(log)) + TimeManager.StartTimeManager(); + VehicleManager.CheckEnabledMods(); + + DatabaseHelper.InitDatabaseFirstTime(); + + FactionHelper.CheckFactionBankAccounts(); + BusinessManager.LoadBusinesses(); + //InteriorManager.LoadInteriors(); + DoorManager.LoadDoors(); + ATMManager.InitATMs(); + CityHallManager.LoadCityHall(); + JobManager.LoadJobs(); + //TaxiDriverJob.StartTaxiTimer(); Obselete + //HouseManager.LoadHouses(); + DrivingSchool.DrivingSchool.Setup(); + PlaneSchool.Setup(); + Gangwar.Gangwar.loadTurfs(); + Bank.Bank.Setup(); + Introduction.Setup(); + PositionManager.LoadPositionManager(); + LoadManager.LoadLoadManager(); + Rentcar.Setup(); + + World.WeatherSync.Load(); + + TempBlip tempBlip = new TempBlip() + { + Color = 1, + Name = "", + Transparency = 0, + ShortRange = true, + Sprite = 1, + Scale = 1, + }; + + NAPI.Data.SetWorldData("blipTemplate", tempBlip); + + //WantedEscapeTimer.WantedTimer(); Obselete + //Jail.JailTimer(); Obselete + //Economy.PaydayTimer(); Obselete + // WeaponDealManager.WeaponDealTimer(); Obselete + + ThreadTimers.StartAllTimers(); + + Managers.SVarManager.LoadSVars(); + + UserBankAccount.BalanceChanged += (account) => + { + using (var dbContext = new DatabaseContext()) + { + var user = dbContext.Users.Where(u => u.BankAccountId == account.Id).Select(u => u.Name).FirstOrDefault(); + if (user == null) { - File.Create(log); + return; } - string msg = $"[{DateTime.Now:s}] - Error: {message}{Environment.NewLine}"; + Player player = PlayerService.GetPlayerByNameOrId(user); + if (player == null) + { + return; + } - File.AppendAllText(log, msg); + player.TriggerEvent("updateMoney", account.Balance); + } + }; - logMutex.ReleaseMutex(); - } - - [RemoteEvent("CLIENT:Event")] - public void OnClientEvent(Player player, string dataStr) + User.HandMoneyChanged += (user) => { - var data = dataStr.DeserializeJson>(); - eventHandler.HandleEvent(new RagePlayer(player), data); - } + user.Player.TriggerEvent("SERVER:SET_HANDMONEY", user.Handmoney); + }; + + //IPLS + NAPI.World.RequestIpl("vw_casino_garage"); + + } + catch (Exception e) + { + Log(e.ToString()); + } } + + private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + Log(e.ExceptionObject.ToString()); + } + + private void Log(string message) + { + string basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); + string log = Path.Combine(basePath, "log.txt"); + + logMutex.WaitOne(); + + if (!File.Exists(log)) + { + File.Create(log); + } + + string msg = $"[{DateTime.Now:s}] - Error: {message}{Environment.NewLine}"; + + File.AppendAllText(log, msg); + + logMutex.ReleaseMutex(); + } + + [RemoteEvent("CLIENT:Event")] + public void OnClientEvent(Player player, string dataStr) + { + var data = dataStr.DeserializeJson>(); + eventHandler.HandleEvent(new RagePlayer(player), data); + } + } } diff --git a/ReallifeGamemode.Server/Managers/InteractionManager.cs b/ReallifeGamemode.Server/Managers/InteractionManager.cs index 4d799e80..d6d6e0ed 100644 --- a/ReallifeGamemode.Server/Managers/InteractionManager.cs +++ b/ReallifeGamemode.Server/Managers/InteractionManager.cs @@ -55,7 +55,7 @@ namespace ReallifeGamemode.Server.Managers return; } - using (var dbContext = new DatabaseContext(true)) + using (var dbContext = new DatabaseContext()) { User u = leader.GetUser(dbContext); User own = player.GetUser(dbContext); diff --git a/ReallifeGamemode.Server/WeaponDeal/WeaponDealManager.cs b/ReallifeGamemode.Server/WeaponDeal/WeaponDealManager.cs index 2d430b6a..0f26f7cf 100644 --- a/ReallifeGamemode.Server/WeaponDeal/WeaponDealManager.cs +++ b/ReallifeGamemode.Server/WeaponDeal/WeaponDealManager.cs @@ -43,7 +43,7 @@ namespace ReallifeGamemode.Server.WeaponDeal return; } - using (var context = new DatabaseContext(true)) + using (var context = new DatabaseContext()) { FactionVehicle factionVehicle = context.FactionVehicles.Where(f => f.Model == WEAPON_DEAL_GANG_VEHICLE_HASH || f.Model == WEAPON_DEAL_STAATSFRAK_VEHICLE_HASH) .ToList() @@ -130,7 +130,7 @@ namespace ReallifeGamemode.Server.WeaponDeal Faction fac = context.Factions.Where(f => f.Id == user.FactionId).FirstOrDefault(); fac.WeaponDealTime = (fac.Id == 1 | fac.Id == 3) ? 180 : 60; - context.SaveChanges(); + context.SaveChanges(); } }