44using Il2CppInterop . Runtime . InteropTypes . Arrays ;
55using Il2CppSystem . Collections ;
66using MoonSharp . Interpreter ;
7+ using MoonSharp . Interpreter . Debugging ;
78using MoonSharp . Interpreter . Interop ;
89using Newtonsoft . Json . Linq ;
910using PolyMod . modApi ;
1011using Polytopia . Data ;
12+ using PolytopiaBackendBase . Game ;
1113using UnityEngine ;
1214using IDisposable = Il2CppSystem . IDisposable ;
1315using Input = PolyMod . modApi . Input ;
@@ -21,24 +23,7 @@ public class LuaManager
2123 private ManualLogSource logger ;
2224 static LuaManager ( )
2325 {
24- Script . GlobalOptions . CustomConverters . SetClrToScriptCustomConversion ( typeof ( IEnumerable < > ) , ( _ , enumerable ) =>
25- {
26- IEnumerator enumerator = ( ( IEnumerable ) enumerable ) . GetEnumerator ( ) ;
27- return DynValue . NewCallback ( ( context , args ) =>
28- {
29- if ( enumerator . MoveNext ( ) )
30- {
31- // Return the current item as a single value tuple
32- return DynValue . NewTuple ( DynValue . FromObject ( context . GetScript ( ) , enumerator . Current ) ) ;
33- }
34- else
35- {
36- // Iterator is finished
37- ( ( object ) enumerator as IDisposable ) . Dispose ( ) ;
38- return DynValue . Nil ;
39- }
40- } ) ;
41- } ) ;
26+
4227 }
4328 public LuaManager ( string modName )
4429 {
@@ -71,18 +56,46 @@ void RegisterTypesAndExtensions(IEnumerable<Type> types)
7156 RegisterTypesAndExtensions ( typeof ( Enumerable ) . Assembly . GetTypes ( ) ) ; // linq
7257
7358 #region PolyMod.*
59+
60+ UserData . RegisterType ( typeof ( Registry ) ) ;
61+ lua . Globals [ "Registry" ] = typeof ( Registry ) ;
62+
7463 UserData . RegisterType ( typeof ( Patch ) ) ;
7564 lua . Globals [ "Patch" ] = typeof ( Patch ) ;
7665
7766 UserData . RegisterType ( typeof ( General ) ) ;
7867 lua . Globals [ "General" ] = typeof ( General ) ;
7968
69+ UserData . RegisterType ( typeof ( LuaEnumCache ) ) ;
70+ lua . Globals [ "EnumCache" ] = typeof ( LuaEnumCache ) ;
71+
72+ LuaEnumCache . RegisterEnum < GameMode > ( "GameMode" ) ;
73+ LuaEnumCache . RegisterEnum < TribeData . Type > ( "TribeType" ) ;
74+ LuaEnumCache . RegisterEnum < TechData . Type > ( "TechType" ) ;
75+ LuaEnumCache . RegisterEnum < UnitData . Type > ( "UnitType" ) ;
76+ LuaEnumCache . RegisterEnum < ImprovementData . Type > ( "ImprovementType" ) ;
77+ LuaEnumCache . RegisterEnum < Polytopia . Data . TerrainData . Type > ( "TerrainType" ) ;
78+ LuaEnumCache . RegisterEnum < ResourceData . Type > ( "ResourceType" ) ;
79+ LuaEnumCache . RegisterEnum < TaskData . Type > ( "TaskType" ) ;
80+ LuaEnumCache . RegisterEnum < TribeAbility . Type > ( "TribeAbilityType" ) ;
81+ LuaEnumCache . RegisterEnum < UnitAbility . Type > ( "UnitAbilityType" ) ;
82+ LuaEnumCache . RegisterEnum < ImprovementAbility . Type > ( "ImprovementAbilityType" ) ;
83+ LuaEnumCache . RegisterEnum < PlayerAbility . Type > ( "PlayerAbilityType" ) ;
84+ LuaEnumCache . RegisterEnum < UnitData . WeaponEnum > ( "WeaponType" ) ;
85+ LuaEnumCache . RegisterEnum < KeyCode > ( "KeyCode" ) ;
86+ LuaEnumCache . RegisterEnum < SkinType > ( "SkinType" ) ;
87+
88+
8089 UserData . RegisterType < LuaConfig > ( ) ;
8190 lua . Globals [ "Config" ] = new LuaConfig ( modName , Config < JsonNode > . ConfigTypes . PerMod , lua ) ;
8291 lua . Globals [ "ExposedConfig" ] = new LuaConfig ( modName , Config < JsonNode > . ConfigTypes . Exposed , lua ) ;
8392
8493 UserData . RegisterType ( typeof ( Input ) ) ;
8594 lua . Globals [ "Input" ] = typeof ( Input ) ;
95+
96+ UserData . RegisterExtensionType ( typeof ( Il2cppEnumerableExtensions ) ) ;
97+
98+ lua . Globals [ "clrTypeOf" ] = DynValue . NewCallback ( ( _ , args ) => DynValue . NewString ( args [ 0 ] . ToObject ( ) . GetType ( ) . FullName ) ) ;
8699 #endregion
87100
88101 #region Il2cppSystem.*
@@ -147,7 +160,11 @@ public void Execute(string code, string fileName)
147160 }
148161 catch ( ScriptRuntimeException e )
149162 {
150- logger . LogError ( e . DecoratedMessage ) ;
163+ logger . LogError ( e . DecoratedMessage ?? e . Message ) ;
164+ foreach ( var item in e . CallStack )
165+ {
166+ logger . LogError ( $ "AT { item . Location } ") ;
167+ }
151168 }
152169 }
153170 public void ExecuteFile ( string path )
@@ -162,3 +179,15 @@ public void ExecuteFile(string path)
162179 }
163180 }
164181}
182+
183+ public static class Il2cppEnumerableExtensions
184+ {
185+ public static System . Collections . IEnumerable ToMono < T > ( this Il2CppSystem . Collections . Generic . IEnumerable < T > enumerable )
186+ {
187+ var list = Il2CppSystem . Linq . Enumerable . ToList ( enumerable ) ;
188+ foreach ( T v in list )
189+ {
190+ yield return v ;
191+ }
192+ }
193+ }
0 commit comments