Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New examples #81

Merged
merged 7 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions OmsiExtensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OmsiHookInvoker", "OmsiHookInvoker\OmsiHookInvoker.vcxproj", "{CBCB99EF-DD1A-4D4D-A9A8-9BF251FDCD1B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OmsiHookRPCPlugin", "OmsiHookRPCPlugin\OmsiHookRPCPlugin.csproj", "{CDB17143-5653-48BE-AAC8-8419D5B4FD2C}"
ProjectSection(ProjectDependencies) = postProject
{CBCB99EF-DD1A-4D4D-A9A8-9BF251FDCD1B} = {CBCB99EF-DD1A-4D4D-A9A8-9BF251FDCD1B}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Basic_CLI", "_OmsiHookExamples\Basic_CLI\Basic_CLI.csproj", "{BA833C68-E8BD-4C86-9555-85542DF02015}"
ProjectSection(ProjectDependencies) = postProject
{2E750CBE-F868-4AB7-96C2-27560F53E06B} = {2E750CBE-F868-4AB7-96C2-27560F53E06B}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -47,7 +55,6 @@ Global
{28DA0165-EAA7-4171-A065-319409682BD1}.Release|x64.ActiveCfg = Release|Any CPU
{28DA0165-EAA7-4171-A065-319409682BD1}.Release|x64.Build.0 = Release|Any CPU
{28DA0165-EAA7-4171-A065-319409682BD1}.Release|x86.ActiveCfg = Release|Any CPU
{28DA0165-EAA7-4171-A065-319409682BD1}.Release|x86.Build.0 = Release|Any CPU
{FDA9A525-9722-46D3-B80C-8D2A76ABCA2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FDA9A525-9722-46D3-B80C-8D2A76ABCA2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FDA9A525-9722-46D3-B80C-8D2A76ABCA2D}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand All @@ -59,7 +66,6 @@ Global
{FDA9A525-9722-46D3-B80C-8D2A76ABCA2D}.Release|x64.ActiveCfg = Release|Any CPU
{FDA9A525-9722-46D3-B80C-8D2A76ABCA2D}.Release|x64.Build.0 = Release|Any CPU
{FDA9A525-9722-46D3-B80C-8D2A76ABCA2D}.Release|x86.ActiveCfg = Release|Any CPU
{FDA9A525-9722-46D3-B80C-8D2A76ABCA2D}.Release|x86.Build.0 = Release|Any CPU
{D5CA6EEA-D436-456E-BCA5-34C3DFD5BFC7}.Debug|Any CPU.ActiveCfg = Debug|x86
{D5CA6EEA-D436-456E-BCA5-34C3DFD5BFC7}.Debug|Any CPU.Build.0 = Debug|x86
{D5CA6EEA-D436-456E-BCA5-34C3DFD5BFC7}.Debug|x64.ActiveCfg = Debug|x86
Expand Down Expand Up @@ -93,6 +99,17 @@ Global
{CDB17143-5653-48BE-AAC8-8419D5B4FD2C}.Release|x64.ActiveCfg = Release|x86
{CDB17143-5653-48BE-AAC8-8419D5B4FD2C}.Release|x86.ActiveCfg = Release|x86
{CDB17143-5653-48BE-AAC8-8419D5B4FD2C}.Release|x86.Build.0 = Release|x86
{BA833C68-E8BD-4C86-9555-85542DF02015}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BA833C68-E8BD-4C86-9555-85542DF02015}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA833C68-E8BD-4C86-9555-85542DF02015}.Debug|x64.ActiveCfg = Debug|Any CPU
{BA833C68-E8BD-4C86-9555-85542DF02015}.Debug|x64.Build.0 = Debug|Any CPU
{BA833C68-E8BD-4C86-9555-85542DF02015}.Debug|x86.ActiveCfg = Debug|x86
{BA833C68-E8BD-4C86-9555-85542DF02015}.Debug|x86.Build.0 = Debug|x86
{BA833C68-E8BD-4C86-9555-85542DF02015}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA833C68-E8BD-4C86-9555-85542DF02015}.Release|Any CPU.Build.0 = Release|Any CPU
{BA833C68-E8BD-4C86-9555-85542DF02015}.Release|x64.ActiveCfg = Release|Any CPU
{BA833C68-E8BD-4C86-9555-85542DF02015}.Release|x64.Build.0 = Release|Any CPU
{BA833C68-E8BD-4C86-9555-85542DF02015}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
28 changes: 28 additions & 0 deletions OmsiHook/docs/articles/examples/Basic_CLI.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Basic CLI Example

This article provides a basic understanding to a basic C# .NET example leveraging the OMSIHook library. The example focuses on retrieving crucial information about the map, weather, date, and the current vehicle.

_This article is in direct relation to the Sample Project available [here](https://github.com/space928/Omsi-Extensions/tree/main/_OmsiHookExamples/Basic_CLI)._

## Initialization

Initialize an instance of the `OmsiHook` class and establish a connection to the OMSI game:

```cs
OmsiHook.OmsiHook omsi = new();
omsi.AttachToOMSI().Wait();
```

## Caching of Globals

Efficiently cache top-level global variables outside the loop for optimized performance:

```cs
// Cache global variables for faster access
var playerVehicle = omsi.Globals.PlayerVehicle;
var time = omsi.Globals.Time;
var map = omsi.Globals.Map;
var weather = omsi.Globals.Weather;
```

By caching these variables outside the loop, you significantly enhance access speed during subsequent iterations.
6 changes: 5 additions & 1 deletion OmsiHook/docs/articles/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
- name: Building Native Omsi Plugins
href: building-native-plugins.md
- name: Performance Tips
href: performance-tips.md
href: performance-tips.md
- name: Examples
items:
- name: Basic CLI Example
href: examples\Basic_CLI.md
16 changes: 16 additions & 0 deletions _OmsiHookExamples/Basic_CLI/Basic_CLI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows10.0.22621.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>x86</Platforms>
<StartupObject>Basic_CLI.Program</StartupObject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OmsiHook" Version="2.3.2" />
</ItemGroup>

</Project>
49 changes: 49 additions & 0 deletions _OmsiHookExamples/Basic_CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Threading;
using OmsiHook;

namespace Basic_CLI
{
// Most Basic example of reading various values exposed by OMSIHook
class Program
{
static void Main(string[] args)
{
Console.WriteLine("#=#=#=#=#=# OMSIHook Basic CLI Sample #=#=#=#=#=#");

OmsiHook.OmsiHook omsi = new();
omsi.AttachToOMSI().Wait();
var playerVehicle = omsi.Globals.PlayerVehicle;
var time = omsi.Globals.Time;
var map = omsi.Globals.Map;
var weather = omsi.Globals.Weather;
Console.OutputEncoding = System.Text.Encoding.UTF8;
while (true)
{
playerVehicle ??= omsi.Globals.PlayerVehicle;
var pos = playerVehicle?.Position ?? default;
var rot = playerVehicle?.Rotation ?? default;

Console.SetCursorPosition(0, 1);
Console.WriteLine($"Map: {map?.FriendlyName}".PadRight(Console.WindowWidth - 17) + $"Date: {time.Day:00}/{time.Month:00}/{time.Year:0000}");
Console.WriteLine($"Weather: {WeatherEmoji(weather)}".PadRight(Console.WindowWidth - 15) + $"Time: {time.Hour:00}:{time.Minute:00}:{time.Second:00}");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weather might be null here...

Console.WriteLine($"Bus: {playerVehicle?.RoadVehicle?.FileName}".PadRight(Console.WindowWidth - 1));
Console.WriteLine(($"Position: {pos.x:F2},{pos.y:F2},{pos.z:F2}".PadRight(Console.WindowWidth - 8) +
$"Tile: {playerVehicle?.Kachel ?? 0}"));
Console.WriteLine(($"Rotation: {rot.w:F2},{rot.x:F2},{rot.y:F2},{rot.z:F2}".PadRight(Console.WindowWidth - 1)));

Thread.Sleep(20);
}
}

// Pick an emoji to show for the weather
static string WeatherEmoji(OmsiWeather weather)
{
if (weather?.ActWeather.fogDensity < 900)
return "🌫️";
if (weather?.ActWeather.percipitation > 0)
return "🌧️";
return "☀️";
}
}
}