Skip to content

Commit 6090b6d

Browse files
committed
Use LiteDB
1 parent 517649e commit 6090b6d

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

ActivityGhosts/ActivityGhosts.cs

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using GTA.UI;
1313
using LemonUI;
1414
using LemonUI.Menus;
15+
using LiteDB;
1516

1617
namespace ActivityGhosts
1718
{
@@ -30,11 +31,17 @@ public class ActivityGhosts : Script
3031
private NativeItem loadMenuItem;
3132
private NativeItem regroupMenuItem;
3233
private NativeItem deleteMenuItem;
34+
private readonly string gtaFolder;
35+
private static LiteDatabase db;
36+
private static ILiteCollection<ActivityFile> activityFiles;
3337

3438
public ActivityGhosts()
3539
{
3640
ghosts = new List<Ghost>();
3741
lastTime = Environment.TickCount;
42+
gtaFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Rockstar Games", "GTA V");
43+
db = new LiteDatabase(Path.Combine(gtaFolder, "ModSettings", "ActivityGhosts.db"));
44+
activityFiles = db.GetCollection<ActivityFile>();
3845
LoadSettings();
3946
CreateMenu();
4047
Tick += OnTick;
@@ -85,29 +92,39 @@ private void RegroupGhosts()
8592

8693
private void LoadGhosts()
8794
{
88-
string activitiesPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Rockstar Games\\GTA V\\Activities";
95+
string activitiesPath = Path.Combine(gtaFolder, "Activities");
8996
if (Directory.Exists(activitiesPath))
9097
{
91-
DirectoryInfo dir = new DirectoryInfo(activitiesPath);
92-
FileInfo[] files = dir.GetFiles("*.fit");
93-
foreach (FileInfo file in files)
94-
{
95-
FitActivityDecoder fit = new FitActivityDecoder(file.FullName);
96-
List<GeoPoint> points = fit.pointList;
97-
if (points.Count > 1 && Game.Player.Character.Position.DistanceTo2D(new Vector2(points[0].Lat, points[0].Long)) < 50f)
98+
foreach (var file in new DirectoryInfo(activitiesPath).GetFiles("*.fit"))
99+
if (activityFiles.Find(x => x.Name == file.Name).FirstOrDefault() == null)
100+
{
101+
var points = new FitActivityDecoder(file.FullName).pointList;
102+
if (points.Count > 1)
103+
activityFiles.Insert(new ActivityFile() { Name = file.Name, Lat = points[0].Lat, Long = points[0].Long });
104+
}
105+
foreach (var file in activityFiles.FindAll())
106+
if (Game.Player.Character.Position.DistanceTo2D(new Vector2(file.Lat, file.Long)) < 50f)
98107
{
99-
int offset = ghosts.Count / 2 + 1;
100-
if (ghosts.Count % 2 == 0)
101-
offset *= -1;
102-
points[0].Lat += offset;
103-
float h = Game.Player.Character.Heading;
104-
if ((h > 90f && h < 180f) || (h > 270f && h < 360f))
105-
points[0].Long -= offset;
108+
string fullName = Path.Combine(activitiesPath, file.Name);
109+
if (System.IO.File.Exists(fullName))
110+
{
111+
var fit = new FitActivityDecoder(fullName);
112+
if (fit.pointList.Count > 1)
113+
{
114+
int offset = ghosts.Count / 2 + 1;
115+
if (ghosts.Count % 2 == 0)
116+
offset *= -1;
117+
float h = Game.Player.Character.Heading;
118+
if ((h > 45f && h < 135f) || (h > 225f && h < 315f))
119+
fit.pointList[0].Long += offset;
120+
else
121+
fit.pointList[0].Lat += offset;
122+
ghosts.Add(new Ghost(fit.pointList, fit.sport, fit.startTime));
123+
}
124+
}
106125
else
107-
points[0].Long += offset;
108-
ghosts.Add(new Ghost(points, fit.sport, fit.startTime));
126+
activityFiles.Delete(file.Id);
109127
}
110-
}
111128
if (ghosts.Count > 0)
112129
{
113130
start = World.CreateBlip(Game.Player.Character.Position);
@@ -553,4 +570,12 @@ private double DegToRad(float angleDeg)
553570
return angleDeg * Math.PI / 180.0f;
554571
}
555572
}
573+
574+
public class ActivityFile
575+
{
576+
public int Id { get; set; }
577+
public string Name { get; set; }
578+
public float Lat { get; set; }
579+
public float Long { get; set; }
580+
}
556581
}

ActivityGhosts/ActivityGhosts.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
<Reference Include="LemonUI.SHVDN3">
3838
<HintPath>D:\Grand Theft Auto V\Scripts\LemonUI.SHVDN3.dll</HintPath>
3939
</Reference>
40+
<Reference Include="LiteDB, Version=5.0.16.0, Culture=neutral, PublicKeyToken=4ee40123013c9f27, processorArchitecture=MSIL">
41+
<HintPath>..\packages\LiteDB.5.0.16\lib\net45\LiteDB.dll</HintPath>
42+
</Reference>
4043
<Reference Include="ScriptHookVDotNet3">
4144
<HintPath>D:\Grand Theft Auto V\ScriptHookVDotNet3.dll</HintPath>
4245
</Reference>
@@ -48,6 +51,9 @@
4851
<Compile Include="ActivityGhosts.cs" />
4952
<Compile Include="Properties\AssemblyInfo.cs" />
5053
</ItemGroup>
54+
<ItemGroup>
55+
<None Include="packages.config" />
56+
</ItemGroup>
5157
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5258
<PropertyGroup>
5359
<PostBuildEvent>COPY "$(TargetPath)" "D:\Grand Theft Auto V\Scripts"</PostBuildEvent>

ActivityGhosts/packages.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="LiteDB" version="5.0.16" targetFramework="net48" />
4+
</packages>

0 commit comments

Comments
 (0)