Skip to content

Commit

Permalink
Use LiteDB
Browse files Browse the repository at this point in the history
  • Loading branch information
oldnapalm committed May 3, 2023
1 parent 517649e commit 6090b6d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
61 changes: 43 additions & 18 deletions ActivityGhosts/ActivityGhosts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using GTA.UI;
using LemonUI;
using LemonUI.Menus;
using LiteDB;

namespace ActivityGhosts
{
Expand All @@ -30,11 +31,17 @@ public class ActivityGhosts : Script
private NativeItem loadMenuItem;
private NativeItem regroupMenuItem;
private NativeItem deleteMenuItem;
private readonly string gtaFolder;
private static LiteDatabase db;
private static ILiteCollection<ActivityFile> activityFiles;

public ActivityGhosts()
{
ghosts = new List<Ghost>();
lastTime = Environment.TickCount;
gtaFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Rockstar Games", "GTA V");
db = new LiteDatabase(Path.Combine(gtaFolder, "ModSettings", "ActivityGhosts.db"));
activityFiles = db.GetCollection<ActivityFile>();
LoadSettings();
CreateMenu();
Tick += OnTick;
Expand Down Expand Up @@ -85,29 +92,39 @@ private void RegroupGhosts()

private void LoadGhosts()
{
string activitiesPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Rockstar Games\\GTA V\\Activities";
string activitiesPath = Path.Combine(gtaFolder, "Activities");
if (Directory.Exists(activitiesPath))
{
DirectoryInfo dir = new DirectoryInfo(activitiesPath);
FileInfo[] files = dir.GetFiles("*.fit");
foreach (FileInfo file in files)
{
FitActivityDecoder fit = new FitActivityDecoder(file.FullName);
List<GeoPoint> points = fit.pointList;
if (points.Count > 1 && Game.Player.Character.Position.DistanceTo2D(new Vector2(points[0].Lat, points[0].Long)) < 50f)
foreach (var file in new DirectoryInfo(activitiesPath).GetFiles("*.fit"))
if (activityFiles.Find(x => x.Name == file.Name).FirstOrDefault() == null)
{
var points = new FitActivityDecoder(file.FullName).pointList;
if (points.Count > 1)
activityFiles.Insert(new ActivityFile() { Name = file.Name, Lat = points[0].Lat, Long = points[0].Long });
}
foreach (var file in activityFiles.FindAll())
if (Game.Player.Character.Position.DistanceTo2D(new Vector2(file.Lat, file.Long)) < 50f)
{
int offset = ghosts.Count / 2 + 1;
if (ghosts.Count % 2 == 0)
offset *= -1;
points[0].Lat += offset;
float h = Game.Player.Character.Heading;
if ((h > 90f && h < 180f) || (h > 270f && h < 360f))
points[0].Long -= offset;
string fullName = Path.Combine(activitiesPath, file.Name);
if (System.IO.File.Exists(fullName))
{
var fit = new FitActivityDecoder(fullName);
if (fit.pointList.Count > 1)
{
int offset = ghosts.Count / 2 + 1;
if (ghosts.Count % 2 == 0)
offset *= -1;
float h = Game.Player.Character.Heading;
if ((h > 45f && h < 135f) || (h > 225f && h < 315f))
fit.pointList[0].Long += offset;
else
fit.pointList[0].Lat += offset;
ghosts.Add(new Ghost(fit.pointList, fit.sport, fit.startTime));
}
}
else
points[0].Long += offset;
ghosts.Add(new Ghost(points, fit.sport, fit.startTime));
activityFiles.Delete(file.Id);
}
}
if (ghosts.Count > 0)
{
start = World.CreateBlip(Game.Player.Character.Position);
Expand Down Expand Up @@ -553,4 +570,12 @@ private double DegToRad(float angleDeg)
return angleDeg * Math.PI / 180.0f;
}
}

public class ActivityFile
{
public int Id { get; set; }
public string Name { get; set; }
public float Lat { get; set; }
public float Long { get; set; }
}
}
6 changes: 6 additions & 0 deletions ActivityGhosts/ActivityGhosts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<Reference Include="LemonUI.SHVDN3">
<HintPath>D:\Grand Theft Auto V\Scripts\LemonUI.SHVDN3.dll</HintPath>
</Reference>
<Reference Include="LiteDB, Version=5.0.16.0, Culture=neutral, PublicKeyToken=4ee40123013c9f27, processorArchitecture=MSIL">
<HintPath>..\packages\LiteDB.5.0.16\lib\net45\LiteDB.dll</HintPath>
</Reference>
<Reference Include="ScriptHookVDotNet3">
<HintPath>D:\Grand Theft Auto V\ScriptHookVDotNet3.dll</HintPath>
</Reference>
Expand All @@ -48,6 +51,9 @@
<Compile Include="ActivityGhosts.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>COPY "$(TargetPath)" "D:\Grand Theft Auto V\Scripts"</PostBuildEvent>
Expand Down
4 changes: 4 additions & 0 deletions ActivityGhosts/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="LiteDB" version="5.0.16" targetFramework="net48" />
</packages>

0 comments on commit 6090b6d

Please sign in to comment.