@@ -34,40 +34,69 @@ private void LoadThreadList()
3434 {
3535 threadIds . Clear ( ) ;
3636
37- // Create a Lua table to hold the thread list
38- lua . CreateTable ( ) ;
39- int tableRef = lua . GetTop ( ) ; // Keep reference to our table
37+ // Create a StringList object for getThreadlist
38+ lua . GetGlobal ( "createStringlist" ) ;
39+ if ( ! lua . IsFunction ( - 1 ) )
40+ {
41+ lua . Pop ( 1 ) ;
42+ throw new ThreadListException ( "createStringlist function not available in this CE version" ) ;
43+ }
44+
45+ // Call createStringlist() to create a proper StringList object
46+ var result = lua . PCall ( 0 , 1 ) ;
47+ if ( result != 0 )
48+ {
49+ var error = lua . ToString ( - 1 ) ;
50+ lua . Pop ( 1 ) ;
51+ throw new ThreadListException ( $ "createStringlist() call failed: { error } ") ;
52+ }
53+
54+ // The StringList object should now be on the stack
55+ int stringListRef = lua . GetTop ( ) ; // Keep reference to our StringList
4056
4157 // Get getThreadlist function
4258 lua . GetGlobal ( "getThreadlist" ) ;
4359 if ( ! lua . IsFunction ( - 1 ) )
4460 {
45- lua . Pop ( 1 ) ;
61+ lua . Pop ( 2 ) ; // Pop both function and StringList
4662 throw new ThreadListException ( "getThreadlist function not available in this CE version" ) ;
4763 }
4864
49- // Push the table as parameter
50- lua . PushValue ( tableRef ) ;
65+ // Push the StringList as parameter
66+ lua . PushValue ( stringListRef ) ;
5167
52- // Call getThreadlist(table )
53- var result = lua . PCall ( 1 , 0 ) ;
68+ // Call getThreadlist(StringList )
69+ result = lua . PCall ( 1 , 0 ) ;
5470 if ( result != 0 )
5571 {
5672 var error = lua . ToString ( - 1 ) ;
57- lua . Pop ( 1 ) ;
73+ lua . Pop ( 2 ) ; // Pop error and StringList
5874 throw new ThreadListException ( $ "getThreadlist() call failed: { error } ") ;
5975 }
6076
61- // Now read the filled table
62- // The table should now contain thread IDs in hex format
63- // Iterate through the table to get all values
64- lua . PushValue ( tableRef ) ; // Push table for iteration
65- lua . PushNil ( ) ; // First key for lua_next
77+ // Now read the filled StringList
78+ // According to celua.txt, getThreadlist fills the StringList with format %x (hex)
79+ // We need to access the StringList contents using its Count property and indexer
6680
67- while ( lua . Next ( - 2 ) != 0 )
81+ // Get Count property of the StringList
82+ lua . PushValue ( stringListRef ) ; // Push StringList object
83+ lua . GetField ( - 1 , "Count" ) ;
84+ if ( ! lua . IsInteger ( - 1 ) )
6885 {
69- // Key is at -2, value is at -1
70- // According to celua.txt, format is %x (hex), so values should be strings
86+ lua . Pop ( 2 ) ; // Pop Count result and StringList
87+ throw new ThreadListException ( "Could not get Count property from StringList" ) ;
88+ }
89+
90+ int count = lua . ToInteger ( - 1 ) ;
91+ lua . Pop ( 1 ) ; // Pop Count result, keep StringList
92+
93+ // Read each string from the StringList using indexer (0-based)
94+ for ( int i = 0 ; i < count ; i ++ )
95+ {
96+ // Access StringList[i]
97+ lua . PushInteger ( i ) ;
98+ lua . GetTable ( - 2 ) ; // StringList[i]
99+
71100 if ( lua . IsString ( - 1 ) )
72101 {
73102 var threadId = lua . ToString ( - 1 ) ;
@@ -76,10 +105,10 @@ private void LoadThreadList()
76105 threadIds . Add ( threadId ) ;
77106 }
78107 }
79- lua . Pop ( 1 ) ; // Remove value, keep key for next iteration
108+ lua . Pop ( 1 ) ; // Pop the string value
80109 }
81110
82- lua . Pop ( 1 ) ; // Remove table
111+ lua . Pop ( 1 ) ; // Pop StringList object
83112 _loaded = true ;
84113 }
85114 catch ( Exception ex ) when ( ex is not ThreadListException )
@@ -173,7 +202,7 @@ public ThreadInfo[] GetAllThreads()
173202 /// <returns>Array of thread ID hex strings</returns>
174203 public string [ ] GetAllThreadIds ( )
175204 {
176- return threadIds . ToArray ( ) ;
205+ return [ .. threadIds ] ;
177206 }
178207
179208 /// <summary>
0 commit comments