11using System . Reflection ;
2+ using SER . Code . Extensions ;
23using SER . Code . Helpers . ResultSystem ;
34using SER . Code . ScriptSystem ;
45
@@ -7,50 +8,70 @@ namespace SER.Code.Helpers;
78public 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}
0 commit comments