Skip to content

Commit 68d6a12

Browse files
improve example scripts and surrounding system
1 parent 51f83c9 commit 68d6a12

21 files changed

Lines changed: 141 additions & 53 deletions

Code/FileSystem/FileSystem.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ public static void GenerateExamples()
108108
{
109109
var path = Path.Combine(exampleDir.FullName, $"{kvp.Key}.ser");
110110
if (File.Exists(path)) continue;
111+
112+
string? directory = Path.GetDirectoryName(path);
113+
if (!string.IsNullOrEmpty(directory))
114+
{
115+
Directory.CreateDirectory(directory);
116+
}
117+
111118
using var sw = File.CreateText(path);
112119
sw.Write(kvp.Value);
113120
}

Code/Helpers/ExampleHandler.cs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reflection;
2+
using SER.Code.Extensions;
23
using SER.Code.Helpers.ResultSystem;
34
using SER.Code.ScriptSystem;
45

@@ -7,50 +8,70 @@ namespace SER.Code.Helpers;
78
public static class ExampleHandler
89
{
910
private static Dictionary<string, string>? _cachedExamples;
11+
private const string RootFolder = "Example_Scripts";
1012

11-
[UsedImplicitly]
1213
public static Dictionary<string, string> GetAllExamples()
1314
{
1415
if (_cachedExamples != null) return _cachedExamples;
15-
16+
1617
var assembly = Assembly.GetExecutingAssembly();
18+
// Look for resources that contain our root folder and end with .ser
1719
var resourceNames = assembly.GetManifestResourceNames()
18-
.Where(n => n.EndsWith(".ser"));
20+
.Where(n => n.Contains(RootFolder) && n.EndsWith(".ser"));
1921

2022
var examples = new Dictionary<string, string>();
2123
foreach (var name in resourceNames)
2224
{
2325
using var stream = assembly.GetManifestResourceStream(name);
2426
if (stream == null) continue;
27+
2528
using var reader = new StreamReader(stream);
2629
string content = reader.ReadToEnd();
27-
string[] parts = name.Split('.');
28-
if (parts.Length < 2) continue;
29-
string fileName = parts[^2];
30-
examples[fileName] = content;
30+
31+
string relativePath = GetRelativePath(name);
32+
examples[relativePath] = content;
3133
}
3234

3335
return _cachedExamples = examples;
3436
}
3537

36-
public static string? GetExample(string name)
38+
private static string GetRelativePath(string resourceName)
39+
{
40+
// 1. Find where the "Example Scripts" folder starts in the namespace string
41+
int rootIndex = resourceName.IndexOf(RootFolder, StringComparison.InvariantCulture);
42+
if (rootIndex == -1) return resourceName;
43+
44+
// 2. Get everything after "Example Scripts."
45+
string pathWithExtension = resourceName[(rootIndex + RootFolder.Length + 1)..];
46+
47+
// 3. Remove the .ser extension
48+
int lastDot = pathWithExtension.LastIndexOf(".ser", StringComparison.InvariantCulture);
49+
string pathWithoutExtension = pathWithExtension[..lastDot];
50+
51+
// 4. Convert dots to directory separators (optional, but cleaner for "folders")
52+
// e.g., "UI.Inventory" instead of "UI/Inventory" if you prefer keeping it as a key
53+
return pathWithoutExtension.Replace('.', '/');
54+
}
55+
56+
public static string? GetExample(string path)
3757
{
38-
return GetAllExamples().TryGetValue(name, out var content) ? content : null;
58+
return GetAllExamples().TryGetValue(path, out var content) ? content : null;
3959
}
4060

4161
[UsedImplicitly]
42-
public static string Verify()
62+
public static (string?, string[]) Verify()
4363
{
4464
var examples = GetAllExamples();
4565

4666
foreach (var example in examples)
4767
{
68+
// example.Key now contains the folder path (e.g., "Combat/Fireball")
4869
if (Script.CreateAnonymous(example.Key, example.Value).Compile().HasErrored(out var error))
4970
{
50-
return new Result(false, $"in example '{example.Key}'") + error;
71+
return (new Result(false, $"in example '{example.Key}'") + error.AsError(), examples.Keys.ToArray());
5172
}
5273
}
5374

54-
return string.Empty;
75+
return (null, examples.Keys.ToArray());
5576
}
5677
}

Example Scripts/anonymousRadio.ser

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# this script makes the name into ??? when talking on the radio
2+
13
!-- OnEvent Joined
24
-- require @evPlayer
35

Example Scripts/breach.ser

Lines changed: 0 additions & 12 deletions
This file was deleted.

Example Scripts/chaosCoin.ser

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# this script adds a fully functioning coin with custom behaviors
2+
13
!-- OnEvent FlippingCoin
24

35
# formats of broadcasts, used to not repeat the same things in different broadcasts
@@ -25,10 +27,10 @@ end
2527
# select a random effect of the coin, from 9 available
2628
$effect = Random 1 9 int
2729

28-
# russian rulette with a full gun :trollface:
30+
# russian roulette with a full gun :trollface:
2931
if $effect is 1
3032
GiveItem @evPlayer GunRevolver
31-
Broadcast @evPlayer 5s "{$baseText}Let's play russian rulette!"
33+
Broadcast @evPlayer 5s "{$baseText}Let's play russian roulette!"
3234
stop
3335
end
3436

@@ -47,7 +49,7 @@ if $effect is 3
4749
*room = @evPlayer -> roomRef
4850

4951
# explode player if he isnt in a room
50-
if {*room -> isInvalid}
52+
if *room is invalid
5153
Explode @evPlayer
5254
stop
5355
end
@@ -63,11 +65,11 @@ if $effect is 3
6365
SetLightColor *room #000000
6466

6567
repeat 5
66-
TransitionLightColor *room #ff0000ff .5s
68+
SetLightColor *room #ff0000 _ .5s
6769
wait .5s
68-
TransitionLightColor *room #00ff00ff .5s
70+
SetLightColor *room #00ff00 _ .5s
6971
wait .5s
70-
TransitionLightColor *room #0000ffff .5s
72+
SetLightColor *room #0000ff _ .5s
7173
wait .5s
7274
end
7375

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# when a player with any custom role is spawned,
2+
# this will tell the player the role he has
3+
4+
!-- OnCRole spawned
5+
6+
wait 6s
7+
8+
AnimatedBroadcast @evPlayer 10s "Your custom role is:<br><br><br><size=60><b><wait=2s><charwait=300ms>{@evCRole -> displayName}"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# registers the janitor custom role
2+
# when the server is ready to accept players
3+
4+
!-- OnEvent WaitingForPlayers
5+
6+
*spawnSystem = CRole.CreateChanceSpawnSystem ClassD 20%
7+
CRole.Register janitor "LCZ Janitor" ClassD *spawnSystem
8+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
!-- OnCRole spawned
2+
-- forRoles janitor
3+
4+
TPRoom @evPlayer LczToilets
5+
GiveItem @evCRole KeycardJanitor
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# registers the senior guard custom role
2+
# when the server is ready to accept players
3+
4+
!-- OnEvent WaitingForPlayers
5+
6+
# CRole.CreateProceduralSpawnSystem arguments
7+
# 1) role to replace
8+
# 2) per-player spawn chance
9+
# 3) conversion limit
10+
# 4) minimum required players
11+
*spawnSystem = CRole.CreateProceduralSpawnSystem FacilityGuard 70% 1 3
12+
13+
CRole.Register senior_guard "Senior Guard" FacilityGuard *spawnSystem
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
!-- OnCRole spawned
2+
-- forRoles senior_guard
3+
4+
ClearInventory @evPlayer
5+
GiveItem @evPlayer KeycardMTFPrivate
6+
GiveItem @evPlayer Radio
7+
GiveItem @evPlayer Medkit
8+
GiveItem @evPlayer GrenadeHE
9+
GiveItem @evPlayer Ammo9x19 3
10+
GiveItem @evPlayer ArmorCombat
11+
GiveItem @evPlayer GunFSP9

0 commit comments

Comments
 (0)