Skip to content

Commit a2118f3

Browse files
committed
save work
1 parent ddeee47 commit a2118f3

4 files changed

Lines changed: 184 additions & 54 deletions

File tree

src/Classes/FoundList.cs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,14 @@ internal FoundList()
3030
}
3131

3232
/// <summary>
33-
/// Internal method to initialize with an existing FoundList Lua object on the stack
33+
/// Internal method to set this as a wrapper for the current FoundList Lua object
34+
/// The FoundList object should be on the Lua stack when this is called
3435
/// </summary>
35-
internal void InitializeWithLuaObject()
36+
internal void SetLuaFoundListObject()
3637
{
37-
// The FoundList Lua object should be on top of the stack
38-
// Create a reference to it so we can use it later
39-
lua.PushValue(-1); // Duplicate the object on stack
40-
41-
// Store reference in Lua registry
42-
lua.GetGlobal(FOUNDLIST_REFS_TABLE);
43-
if (lua.IsNil(-1))
44-
{
45-
lua.Pop(1);
46-
lua.CreateTable();
47-
lua.SetGlobal(FOUNDLIST_REFS_TABLE);
48-
lua.GetGlobal(FOUNDLIST_REFS_TABLE);
49-
}
50-
51-
// Generate a unique reference ID
52-
_luaObjectRef = GetHashCode();
53-
lua.PushInteger(_luaObjectRef);
54-
lua.PushValue(-3); // Push the FoundList object
55-
lua.SetTable(-3); // _FOUNDLIST_REFS[ref] = foundListObject
56-
57-
lua.Pop(2); // Pop registry table and original FoundList object
5838
_foundListObjectCreated = true;
39+
// The FoundList is created by the MemScan and should be accessible through global functions
40+
// We don't need to store references since we'll access it through the MemScan object
5941
}
6042

6143
private void EnsureFoundListObject()
@@ -65,29 +47,57 @@ private void EnsureFoundListObject()
6547
}
6648

6749
/// <summary>
68-
/// Pushes the FoundList Lua object onto the stack
50+
/// Gets the current MemScan's attached FoundList object
6951
/// </summary>
7052
private void PushFoundListObject()
7153
{
7254
EnsureFoundListObject();
7355

74-
lua.GetGlobal(FOUNDLIST_REFS_TABLE);
56+
// Get the current MemScan object
57+
lua.GetGlobal("getCurrentMemscan");
58+
if (!lua.IsFunction(-1))
59+
{
60+
lua.Pop(1);
61+
throw new FoundListException("getCurrentMemscan function not available");
62+
}
63+
64+
var result = lua.PCall(0, 1);
65+
if (result != 0)
66+
{
67+
var error = lua.ToString(-1);
68+
lua.Pop(1);
69+
throw new FoundListException($"getCurrentMemscan() failed: {error}");
70+
}
71+
7572
if (lua.IsNil(-1))
7673
{
7774
lua.Pop(1);
78-
throw new FoundListException("FoundList reference table not found");
75+
throw new FoundListException("No current MemScan object available");
7976
}
8077

81-
lua.PushInteger(_luaObjectRef);
82-
lua.GetTable(-2); // Get _FOUNDLIST_REFS[ref]
78+
// Get the attached found list from the MemScan
79+
lua.GetField(-1, "getAttachedFoundlist");
80+
if (!lua.IsFunction(-1))
81+
{
82+
lua.Pop(2);
83+
throw new FoundListException("getAttachedFoundlist method not available on MemScan object");
84+
}
85+
86+
lua.PushValue(-2); // Push MemScan object as self parameter
87+
result = lua.PCall(1, 1);
88+
if (result != 0)
89+
{
90+
lua.Pop(2);
91+
throw new FoundListException("Failed to get attached found list");
92+
}
8393

8494
if (lua.IsNil(-1))
8595
{
8696
lua.Pop(2);
87-
throw new FoundListException("FoundList object reference not found");
97+
throw new FoundListException("No found list attached to current MemScan");
8898
}
8999

90-
lua.Remove(-2); // Remove the reference table, leave FoundList object on stack
100+
lua.Remove(-2); // Remove MemScan object, keep FoundList on stack
91101
}
92102

93103
/// <summary>

src/Classes/MemScan.cs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,19 +276,51 @@ public void SaveCurrentResults(string name)
276276
/// <returns>FoundList object or null if none attached</returns>
277277
public FoundList? GetAttachedFoundList()
278278
{
279-
return CallMemScanMethod("getAttachedFoundlist", "get attached found list", () =>
279+
try
280280
{
281+
EnsureMemScanObject();
282+
283+
// Push MemScan object
284+
lua.PushValue(-1);
285+
286+
// Get getAttachedFoundlist method
287+
lua.GetField(-1, "getAttachedFoundlist");
288+
if (!lua.IsFunction(-1))
289+
{
290+
lua.Pop(2);
291+
throw new InvalidOperationException("getAttachedFoundlist method not available on MemScan object");
292+
}
293+
294+
// Push self (MemScan object)
295+
lua.PushValue(-2);
296+
297+
// Call getAttachedFoundlist()
298+
var result = lua.PCall(1, 1);
299+
if (result != 0)
300+
{
301+
var error = lua.ToString(-1);
302+
lua.Pop(2);
303+
throw new InvalidOperationException($"getAttachedFoundlist() call failed: {error}");
304+
}
305+
281306
// Check if result is nil
282307
if (lua.IsNil(-1))
283308
{
309+
lua.Pop(2); // Pop nil result and MemScan object
284310
return null;
285311
}
286312

287-
// Create FoundList wrapper and initialize it with the Lua object on the stack
313+
// Create FoundList and pass the Lua object reference
288314
var foundList = new FoundList();
289-
foundList.InitializeWithLuaObject();
315+
foundList.SetLuaFoundListObject();
316+
317+
lua.Pop(2); // Pop FoundList result and MemScan object
290318
return foundList;
291-
});
319+
}
320+
catch (Exception ex) when (ex is not InvalidOperationException)
321+
{
322+
throw new MemScanException("Failed to get attached found list", ex);
323+
}
292324
}
293325
}
294326

src/Classes/Process.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using CESDK.Lua;
34

45
namespace CESDK.Classes
@@ -60,5 +61,63 @@ public static int GetOpenedProcessID()
6061
lua.Pop(1); // Pop the result
6162
return pid;
6263
}
64+
65+
/// <summary>
66+
/// Gets a list of all running processes on the system
67+
/// </summary>
68+
/// <returns>Dictionary with process ID as key and process name as value</returns>
69+
public static Dictionary<int, string> GetProcessList()
70+
{
71+
var processList = new Dictionary<int, string>();
72+
73+
try
74+
{
75+
// Call getProcesslist() function which returns a table
76+
lua.GetGlobal("getProcesslist");
77+
if (!lua.IsFunction(-1))
78+
{
79+
lua.Pop(1);
80+
throw new InvalidOperationException("getProcesslist function not available in this CE version");
81+
}
82+
83+
// Call the function with no parameters to get the table format
84+
var result = lua.PCall(0, 1);
85+
if (result != 0)
86+
{
87+
var error = lua.ToString(-1);
88+
lua.Pop(1);
89+
throw new InvalidOperationException($"getProcesslist() call failed: {error}");
90+
}
91+
92+
// The result should be a table (pid - name format)
93+
if (!lua.IsTable(-1))
94+
{
95+
lua.Pop(1);
96+
throw new InvalidOperationException("getProcesslist() did not return a table");
97+
}
98+
99+
// Iterate through the table
100+
// According to celua.txt, the table format is (pid - name)
101+
lua.PushNil(); // First key
102+
while (lua.Next(-2) != 0) // table index is -2, lua.Next returns non-zero if more elements
103+
{
104+
// Key should be the PID (number), value should be the process name (string)
105+
if (lua.IsInteger(-2) && lua.IsString(-1))
106+
{
107+
int pid = lua.ToInteger(-2);
108+
string processName = lua.ToString(-1) ?? "";
109+
processList[pid] = processName;
110+
}
111+
lua.Pop(1); // Remove value, keep key for next iteration
112+
}
113+
114+
lua.Pop(1); // Remove the table from stack
115+
return processList;
116+
}
117+
catch (Exception ex)
118+
{
119+
throw new InvalidOperationException($"Failed to get process list: {ex.Message}", ex);
120+
}
121+
}
63122
}
64123
}

src/Classes/ThreadList.cs

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)