Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
baratgabor committed Oct 1, 2020
1 parent 2532cc6 commit 929a584
Show file tree
Hide file tree
Showing 13 changed files with 770 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Bootstrap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using GTA;
using GtaVehiclePersistence.Infrastructure;
using CarPersistence.Infrastructure;

namespace GtaVehiclePersistence
{
public class Bootstrap : Script
{
private readonly VehiclePersistenceController _controller;

public Bootstrap()
{
_controller = new VehiclePersistenceController(new VehicleRepository(this.BaseDirectory));
this.KeyDown += _controller.OnKeyDown;
}
}
}
64 changes: 64 additions & 0 deletions GtaVehiclePersistence.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{13B3E253-5D1B-4292-991F-86FE48247B5D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GtaVehiclePersistence</RootNamespace>
<AssemblyName>GtaVehiclePersistence</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL" />
<Reference Include="ScriptHookVDotNet3, Version=3.0.2.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>packages\ScriptHookVDotNet3.3.0.2\lib\net48\ScriptHookVDotNet3.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Bootstrap.cs" />
<Compile Include="Infrastructure\VehiclePersistenceController.cs" />
<Compile Include="Model\MyVehicle.cs" />
<Compile Include="Model\MyDoor.cs" />
<Compile Include="Model\MyVector3.cs" />
<Compile Include="Model\MyVehicleMod.cs" />
<Compile Include="Model\MyWheel.cs" />
<Compile Include="Model\MyWindow.cs" />
<Compile Include="Infrastructure\VehicleRepository.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
31 changes: 31 additions & 0 deletions GtaVehiclePersistence.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30104.148
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GtaVehiclePersistence", "GtaVehiclePersistence.csproj", "{13B3E253-5D1B-4292-991F-86FE48247B5D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleRunner", "..\ConsoleRunner\ConsoleRunner.csproj", "{80A1DB83-0F31-4C42-911F-B121DD53A641}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{13B3E253-5D1B-4292-991F-86FE48247B5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{13B3E253-5D1B-4292-991F-86FE48247B5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{13B3E253-5D1B-4292-991F-86FE48247B5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{13B3E253-5D1B-4292-991F-86FE48247B5D}.Release|Any CPU.Build.0 = Release|Any CPU
{80A1DB83-0F31-4C42-911F-B121DD53A641}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{80A1DB83-0F31-4C42-911F-B121DD53A641}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80A1DB83-0F31-4C42-911F-B121DD53A641}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80A1DB83-0F31-4C42-911F-B121DD53A641}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E9AE6FDC-AA3B-466D-9266-1552C8D582F7}
EndGlobalSection
EndGlobal
201 changes: 201 additions & 0 deletions Infrastructure/VehiclePersistenceController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
using GtaVehiclePersistence.Model;
using GtaVehiclePersistence.Infrastructure;
using GTA;
using System.Linq;
using System.Windows.Forms;

namespace CarPersistence.Infrastructure
{
class VehiclePersistenceController
{
private readonly VehicleRepository _vehicleRepository;
private int _selectedVehicle;
private const string Instructions = "\r\nAdd/Update current vehicle - NumPad*\nDelete current vehicle - Ctrl+Shift+NumPad*\nSelect previous/next vehicle - NumPad- / NumPad+\nSpawn selected vehicle - NumPad/\nDespawn selected vehicle - Ctrl+Shift+NumPad/";

private int VehicleCount => _vehicleRepository.MyVehicles.Count();

public VehiclePersistenceController(VehicleRepository vehicleRepository)
{
_vehicleRepository = vehicleRepository;

LoadVehicles();
}

public void OnKeyDown(object sender, KeyEventArgs e)
{
if (Game.IsLoading || Game.IsPaused)
{
return;
}

switch (e.KeyCode)
{
case Keys.Add:
IncrementSelectedVehicle();
break;
case Keys.Subtract:
DecrementSelectedVehicle();
break;
case Keys.Multiply:
if (e.Control && e.Shift)
{
DeleteVehicle(Game.Player.Character.CurrentVehicle.Mods.LicensePlate);
}
else
{
AddOrUpdateVehicle(Game.Player.Character.CurrentVehicle);
}
break;
case Keys.Divide:
if (e.Control && e.Shift)
{
if (e.Alt)
{
DespawnAllKnownVehicles();
}
else
{
DespawnSelectedVehicle();
}
}
else
{
SpawnSelectedVehicle();
}
break;
default:
break;
}
}

private void IncrementSelectedVehicle()
{
if (VehicleCount == 0)
{
Stdout("There are no vehicles to select from.");
return;
}

_selectedVehicle = (_selectedVehicle + 1) % VehicleCount;
DisplaySelectedVehicle();
}

private void DecrementSelectedVehicle()
{
if (VehicleCount == 0)
{
Stdout("There are no vehicles to select from.");
return;
}

_selectedVehicle = (_selectedVehicle - 1 + VehicleCount) % VehicleCount;
DisplaySelectedVehicle();
}

private void DisplaySelectedVehicle()
{
var vehicle = _vehicleRepository.MyVehicles.ElementAt(_selectedVehicle);
Stdout($"Selected Vehicle {_selectedVehicle + 1}/{VehicleCount}: {vehicle.ModelName}, Plate: {vehicle.LicensePlate}, Color: {vehicle.PrimaryColor}");
}

private void LoadVehicles()
{
_vehicleRepository.LoadVehicles();
Stdout($"Loaded {_vehicleRepository.MyVehicles.Count()} vehicles. {Instructions}", 20000);
}

private void DeleteVehicle(string licensePlate)
{
var deleted = _vehicleRepository.RemoveVehicle(licensePlate, deleteFromDisk: true);

if (deleted)
{
Stdout($"Successfully removed vehicle with license plate '{licensePlate}'.");
_selectedVehicle--; // TODO: Perhaps add logic to ascertain if selected vehicle index has to be decreased due to vehicle removal.
}
else
{
Stdout($"Vehicle with license plate '{licensePlate}' couldn't be found for removal.");
}
}

private void AddOrUpdateVehicle(Vehicle vehicle)
{
if (vehicle == null)
{
Stdout("There is no current vehicle to add or update.");
return;
}

var myVehicle = new MyVehicle(vehicle);

if (_vehicleRepository.UpdateVehicle(myVehicle, saveToDisk: true))
{
Stdout($"Successfully updated vehicle '{myVehicle.ModelName}' with license plate '{myVehicle.LicensePlate}'.");
}
else if (_vehicleRepository.AddVehicle(myVehicle, saveToDisk: true))
{
Stdout($"Successfully added vehicle '{myVehicle.ModelName}' with license plate '{myVehicle.LicensePlate}'.");
}
else
{
Stdout("Couldn't add or update vehicle.");
}
}

private void SpawnSelectedVehicle()
{
if (VehicleCount == 0)
{
Stdout("No vehicles have been loaded.");
return;
}

if (Game.Player.Character.CurrentVehicle != null)
{
Stdout("Cannot spawn vehicle when you're in another vehicle.");
return;
}

_vehicleRepository.MyVehicles.ElementAt(_selectedVehicle).Spawn(Game.Player.Character.FrontPosition + Game.Player.Character.ForwardVector * 5);
Stdout("Selected vehicle spawned.");
}

private void DespawnSelectedVehicle()
{
var selectedVehicle = _vehicleRepository.MyVehicles.ElementAt(_selectedVehicle);

var matchingNearVehicles = World.GetNearbyVehicles(Game.Player.Character.Position, 100).Where(v => v.Mods.LicensePlate == selectedVehicle.LicensePlate);
var matchingNearVehicleCount = matchingNearVehicles.Count();

foreach (var v in matchingNearVehicles)
{
v.Delete();
}

Stdout($"Despawned {matchingNearVehicleCount} instances of the selected vehicle from nearby.");
}

private void DespawnAllKnownVehicles()
{
var allNearVehicles = World.GetNearbyVehicles(Game.Player.Character.Position, 100);
var despawnedVehicleCount = 0;

foreach (var v in allNearVehicles)
{
if (_vehicleRepository.MyVehicles.Any(myV => myV.LicensePlate == v.Mods.LicensePlate))
{
v.Delete();
despawnedVehicleCount++;
}
}

Stdout($"Despawned {despawnedVehicleCount} known vehicles from nearby.");
}

private void Stdout(string message, int milliseconds = 5000)
{
GTA.UI.Screen.ShowSubtitle(message, milliseconds);
}
}
}
Loading

0 comments on commit 929a584

Please sign in to comment.