|
| 1 | +# Starscript.Net |
| 2 | + |
| 3 | +[](https://www.nuget.org/packages/Starscript.Net/) |
| 4 | +[](https://www.nuget.org/packages/Starscript.Net/) |
| 5 | + |
| 6 | +Fast text formatting language for C#. This is a .NET port of [starscript by MeteorDevelopment](https://github.com/MeteorDevelopment/starscript/), written in Java. |
| 7 | +Licensed under MIT. |
| 8 | + |
| 9 | +- Lightweight; No dependencies. Just .NET 9. |
| 10 | +- Standard operators: + - * / % ^ |
| 11 | +- Ability to call functions defined in C# |
| 12 | +- Variables can be different each time they are used |
| 13 | +- Conditional output (ternary operator) |
| 14 | +- Variables can be maps |
| 15 | + |
| 16 | +## Examples |
| 17 | +- `Hello {name}!` |
| 18 | +- `Number: {someNumber * 100}` |
| 19 | +- `FPS: {round(fps)}` |
| 20 | +- `Today is a {good ? 'good' : 'bad'} day` |
| 21 | +- `Name: {user.name}` |
| 22 | + |
| 23 | +## Usage: |
| 24 | +You can find the latest version number at the top of this README. With that: |
| 25 | + |
| 26 | +Your .csproj: |
| 27 | +```xml |
| 28 | +<PackageReference Include="Starscript.Net" Version="{version}" /> |
| 29 | +``` |
| 30 | + |
| 31 | + |
| 32 | +In your code: |
| 33 | +```csharp |
| 34 | +using Starscript; |
| 35 | + |
| 36 | +// Parse |
| 37 | +if (!Parser.TryParse(@"Hello {name}! Starscript was originally created by {originalAuthor()}", out Parser.Result result)) |
| 38 | +{ |
| 39 | + foreach (var error in result.Errors) |
| 40 | + Console.WriteLine(error); |
| 41 | + |
| 42 | + return; |
| 43 | +} |
| 44 | + |
| 45 | +// Compile |
| 46 | +Script script = Compiler.SingleCompile(result); |
| 47 | +// The compiler also has the ability to batch compile and directly compile from source (throwing a ParseException if any errors are present) |
| 48 | +
|
| 49 | +// Create Starscript hypervisor instance |
| 50 | +StarscriptHypervisor hv = StarscriptHypervisor.Create() |
| 51 | + .WithStandardLibrary(); // Adds the default functions, not required |
| 52 | +
|
| 53 | +hv.Set("name", () => "GreemDev"); // dynamic variable |
| 54 | +hv.Set("originalAuthor", _ => "MineGame159"); // function |
| 55 | +
|
| 56 | +// Run |
| 57 | +Console.WriteLine(hv.Run(script)); |
| 58 | +// or... |
| 59 | +Console.WriteLine(script.Execute(hv)); |
| 60 | +``` |
0 commit comments