Skip to content

Commit 9bda756

Browse files
committed
Added TRKWriter, cleaned up some code, added more comments, added example for writing track file and changing theme.
1 parent 96c1b90 commit 9bda756

16 files changed

Lines changed: 452 additions & 33 deletions

Constants.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public enum TrackTheme
3333
public enum TrackSize
3434
{
3535
Multiplayer = 0,
36-
Singleplayer = 1
36+
Singleplayer = 1,
37+
Mega = 2
3738
}
3839

3940
/// <summary>
@@ -58,22 +59,22 @@ public enum TrackTime
5859
/// Converts normalized element ID to non-normalized element ID.
5960
/// </summary>
6061
/// <param name="theme">The theme of the requested element</param>
61-
/// <param name="elementId">The normalized element ID</param>
62+
/// <param name="xid">The normalized element ID</param>
6263
/// <returns>The non-normalized element ID</returns>
63-
public static byte GetElement(TrackTheme theme, int elementId)
64+
public static byte GetElement(TrackTheme theme, int xid)
6465
{
65-
return elementTable[(int)theme][elementId];
66+
return elementTable[(int)theme][xid];
6667
}
6768

6869
/// <summary>
6970
/// Converts non-normalized element ID to normalized element ID.
7071
/// </summary>
7172
/// <param name="theme">The theme of the requested element</param>
72-
/// <param name="elementId">The non-normalized element ID</param>
73+
/// <param name="id">The non-normalized element ID</param>
7374
/// <returns>The normalized element ID</returns>
74-
public static int GetElement(TrackTheme theme, byte elementId)
75+
public static int GetElement(TrackTheme theme, byte id)
7576
{
76-
return Array.IndexOf(elementTable[(int)theme], elementId);
77+
return Array.IndexOf(elementTable[(int)theme], id);
7778
}
7879

7980
/// <summary>

Examples/ReadTrack/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ static void PrintVis()
3838
{
3939
foreach (var chr in row)
4040
{
41-
//char pr = chr != '\0' ? chr : '.';
42-
Console.Write(chr);
41+
Console.Write(chr != '\0' ? chr : '.');
4342
}
4443
Console.Write("\n");
4544
}
@@ -63,7 +62,7 @@ static void Main(string[] args)
6362
var track = reader.ReadTrack(filename);
6463

6564
track.GetInfo();
66-
//track.GetElements()[0].GetInfo();
65+
track.GetElements()[0].GetInfo();
6766
var elements = track.GetElements();
6867

6968
foreach (var element in elements)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"profiles": {
3+
"ReadTrack": {
4+
"commandName": "Project"
5+
}
6+
}
7+
}

Examples/ReadTrack/ReadTrack.csproj

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,4 @@
1212
<ProjectReference Include="..\..\LSRutil.NET.csproj" />
1313
</ItemGroup>
1414

15-
<ItemGroup>
16-
<None Include="..\..\LICENSE.md">
17-
<Pack>True</Pack>
18-
<PackagePath></PackagePath>
19-
</None>
20-
</ItemGroup>
21-
2215
</Project>

Examples/ThemeChange/Program.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using LSRutil;
2+
using static LSRutil.Constants;
3+
4+
namespace ThemeChange
5+
{
6+
class Program
7+
{
8+
static string filename = @"C:\LSR\SavedTracks\height.trk";
9+
static TrackTheme theme = TrackTheme.Ice;
10+
11+
static void ChangeTheme(TrackElement element)
12+
{
13+
element.theme = theme;
14+
}
15+
16+
static void Main(string[] args)
17+
{
18+
var writer = new TRKWriter();
19+
var reader = new TRKReader();
20+
21+
var track = reader.ReadTrack(filename);
22+
track.theme = theme;
23+
24+
var elements = track.GetElements();
25+
elements.ForEach(ChangeTheme);
26+
27+
writer.WriteTrack(track, "test2.trk");
28+
}
29+
}
30+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\LSRutil.NET.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

Examples/WriteTrack/Program.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using LSRutil;
3+
using static LSRutil.Constants;
4+
5+
namespace WriteTrack
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
var writer = new TRKWriter();
12+
var track = new Track() {
13+
size = TrackSize.Multiplayer,
14+
theme = TrackTheme.Ice,
15+
time = TrackTime.Night
16+
};
17+
18+
track.Add(new TrackElement() {
19+
theme = track.theme, // theme is set first, because that's important for the ID.
20+
xid = 0,
21+
rotation = 0,
22+
pos = new GridPosition(0,0,0)
23+
});
24+
25+
writer.WriteTrack(track, "test.trk");
26+
}
27+
}
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\LSRutil.NET.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

GridPosition.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public override bool Equals(object obj)
2222

2323
public override int GetHashCode()
2424
{
25-
return base.GetHashCode();
25+
int hash = 47;
26+
hash = (hash * 5) + X.GetHashCode();
27+
hash = (hash * 5) + Y.GetHashCode();
28+
hash = (hash * 5) + Z.GetHashCode();
29+
return hash;
2630
}
2731

2832
public override string ToString()

LSRutil.NET.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
<TargetFrameworks>netcoreapp3.0;net471</TargetFrameworks>
66
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
77
<RootNamespace>LSRutil</RootNamespace>
8-
<Version>0.8.1</Version>
8+
<Version>0.9.0</Version>
99
<Authors>Zsolt Zitting</Authors>
1010
<Copyright></Copyright>
1111
<Company></Company>
1212
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
1313
<RepositoryUrl>https://github.com/YellowberryHN/LSRutil.NET</RepositoryUrl>
14+
<Description>LSRutil.NET is a library for reading and eventually writing the LEGO Stunt Rally .trk file format. It parses tracks into a format that is easy to work with, and allows people to make converters and parsers for the format.</Description>
15+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1416
</PropertyGroup>
1517
<ItemGroup>
1618
<Compile Remove="Examples\**" />

0 commit comments

Comments
 (0)