diff --git a/OmsiExtensions.sln b/OmsiExtensions.sln index 84f3749..b488af9 100644 --- a/OmsiExtensions.sln +++ b/OmsiExtensions.sln @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/OmsiHook/docs/articles/examples/Basic_CLI.md b/OmsiHook/docs/articles/examples/Basic_CLI.md new file mode 100644 index 0000000..e62f9a1 --- /dev/null +++ b/OmsiHook/docs/articles/examples/Basic_CLI.md @@ -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. \ No newline at end of file diff --git a/OmsiHook/docs/articles/toc.yml b/OmsiHook/docs/articles/toc.yml index f2ba49b..de1b476 100644 --- a/OmsiHook/docs/articles/toc.yml +++ b/OmsiHook/docs/articles/toc.yml @@ -3,4 +3,8 @@ - name: Building Native Omsi Plugins href: building-native-plugins.md - name: Performance Tips - href: performance-tips.md \ No newline at end of file + href: performance-tips.md +- name: Examples + items: + - name: Basic CLI Example + href: examples\Basic_CLI.md \ No newline at end of file diff --git a/_OmsiHookExamples/Basic_CLI/Basic_CLI.csproj b/_OmsiHookExamples/Basic_CLI/Basic_CLI.csproj new file mode 100644 index 0000000..33fd140 --- /dev/null +++ b/_OmsiHookExamples/Basic_CLI/Basic_CLI.csproj @@ -0,0 +1,16 @@ + + + + Exe + net6.0-windows10.0.22621.0 + disable + enable + x86 + Basic_CLI.Program + + + + + + + diff --git a/_OmsiHookExamples/Basic_CLI/Program.cs b/_OmsiHookExamples/Basic_CLI/Program.cs new file mode 100644 index 0000000..3e36dcc --- /dev/null +++ b/_OmsiHookExamples/Basic_CLI/Program.cs @@ -0,0 +1,50 @@ +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) + { + weather ??= omsi.Globals.Weather; + 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}"); + 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 "☀️"; + } + } +}