forked from kyx0r/FA-Binary-Patches
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adjust implementation of table functions #98
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
666951d
initial fix for `table.getsize2`
4z0t bac292a
implement in C++
4z0t 1c57c00
proper type for array
4z0t fe07019
proper include path
4z0t c6ae0b2
Merge branch 'master' into TableFunctions
4z0t f5f4c0d
refactor
4z0t 7603a9d
make const
4z0t 2e99d8e
organize fixed files
4z0t d3bce47
fix typo
4z0t 07ae1f9
fix format
4z0t 5e177d3
remove `2` versions
4z0t File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#define SECTION(index, address) ".section h"#index"; .set h"#index","#address";" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
0x0090AB1F: | ||
call @lua_openlibtable |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
#include <LuaAPI.h> | ||
namespace lua | ||
{ | ||
struct TObject | ||
{ | ||
int tt; | ||
void *value; | ||
}; | ||
|
||
struct Node | ||
{ | ||
TObject i_key; | ||
TObject i_val; | ||
Node *next; | ||
}; | ||
|
||
struct Table | ||
{ | ||
/* lua::GCObject*/ void *next; | ||
uint8_t tt; | ||
uint8_t marked; | ||
uint16_t gap; | ||
uint8_t flags; | ||
uint8_t lsizenode; | ||
// padding byte | ||
// padding byte | ||
Table *metatable; | ||
TObject *array; | ||
lua::Node *node; | ||
lua::Node *firstfree; | ||
/*lua::GCObject*/ void *gclist; | ||
int sizearray; | ||
}; | ||
|
||
} // namespace lua | ||
|
||
VALIDATE_SIZE(lua::Table, 0x24); | ||
|
||
int lua_tablesize(lua_State *L) | ||
{ | ||
int size = 0; | ||
auto base = GetField<lua::TObject *>(L, 0xC); | ||
auto top = GetField<lua::TObject *>(L, 0x8); | ||
if (base >= top || base->tt != LUA_TTABLE) | ||
{ | ||
lua_pushnumber(L, size); | ||
return 1; | ||
} | ||
|
||
auto table = (lua::Table *)base->value; | ||
uint8_t lsizenode = table->lsizenode; | ||
if (lsizenode) | ||
{ | ||
uint32_t num_nodes = 1 << lsizenode; | ||
auto node = table->node; | ||
do | ||
{ | ||
if (node->i_val.tt) | ||
++size; | ||
++node; | ||
--num_nodes; | ||
} while (num_nodes); | ||
} | ||
int sizearray = table->sizearray; | ||
if (sizearray) | ||
{ | ||
auto array = table->array; | ||
do | ||
{ | ||
if (array->tt) | ||
++size; | ||
++array; | ||
--sizearray; | ||
} while (sizearray); | ||
} | ||
|
||
lua_pushnumber(L, size); | ||
return 1; | ||
} | ||
|
||
int lua_tableempty(lua_State *L) | ||
{ | ||
auto base = GetField<lua::TObject *>(L, 0xC); | ||
auto top = GetField<lua::TObject *>(L, 0x8); | ||
if (base >= top || base->tt != LUA_TTABLE) | ||
{ | ||
lua_pushboolean(L, true); | ||
return 1; | ||
} | ||
|
||
auto table = (lua::Table *)base->value; | ||
constexpr auto has_nodes = [](lua::Table *t) | ||
{ | ||
uint8_t lsizenode = t->lsizenode; | ||
if (!lsizenode) | ||
return false; | ||
|
||
uint32_t num_nodes = 1 << lsizenode; | ||
auto node = t->node; | ||
while (node->i_val.tt == LUA_TNIL) | ||
{ | ||
++node; | ||
if (!--num_nodes) | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
constexpr auto has_array = [](lua::Table *t) | ||
{ | ||
int sizearray = t->sizearray; | ||
if (!sizearray) | ||
return false; | ||
|
||
auto array = t->array; | ||
while (array->tt == LUA_TNIL) | ||
{ | ||
++array; | ||
if (!--sizearray) | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
lua_pushboolean(L, !(has_nodes(table) || has_array(table))); | ||
return 1; | ||
} | ||
|
||
int TableClone(lua_State *L) | ||
{ | ||
LuaObject obj{L->LuaState, 1}; | ||
LuaObject cloned{}; | ||
obj.Clone(&cloned); | ||
cloned.PushStack(L); | ||
return 1; | ||
} | ||
|
||
// UI_Lua reprsl({table.unpack2({1,2,3,4},2,3)}) | ||
// UI_Lua reprsl({table.unpack2({1,2,3,4},2)}) | ||
// UI_Lua reprsl({table.unpack2({1,2,3,4})}) | ||
// UI_Lua reprsl({table.unpack2({1,2,3,4},-1)}) | ||
// UI_Lua reprsl({table.unpack2({1,2,3,4},-1000)}) | ||
// UI_Lua reprsl({table.unpack2({1,2,3,4},0, 1000000)}) //stack overflow | ||
|
||
int lua_unpack(lua_State *l) | ||
{ | ||
luaL_checktype(l, 1, LUA_TTABLE); | ||
const int n = lua_getn(l, 1); | ||
const int start_i = luaL_optnumber(l, 2, 1); | ||
const int end_i = luaL_optnumber(l, 3, n); | ||
if (start_i > end_i) | ||
return 0; | ||
|
||
const int n_stack = end_i - start_i + 1; | ||
luaL_checkstack(l, n_stack, "too many results to unpack"); | ||
for (int i = start_i; i <= end_i; i++) | ||
{ | ||
lua_rawgeti(l, 1, i); | ||
} | ||
return n_stack; | ||
} | ||
|
||
const luaL_reg RegTableFuncsDesc[] = {{"getsize2", &lua_tablesize}, | ||
{"empty2", &lua_tableempty}, | ||
{"getn2", (lua_CFunction)0x00927C20}, | ||
{"clone", &TableClone}, | ||
{"unpack", &lua_unpack}, | ||
{nullptr, nullptr}}; | ||
|
||
extern const luaL_reg original_table_funcs[] asm("0x00D47418"); | ||
|
||
int __cdecl lua_openlibtable(lua_State *L) | ||
{ | ||
luaL_openlib(L, "table", original_table_funcs, 0); | ||
luaL_openlib(L, "table", RegTableFuncsDesc, 0); | ||
return 1; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need these
2
alternatives?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can remove them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is Lua resets them in current dev branch:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then we remove that logic. As long as the assembly versions are functionally equivalent then that should be no issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also: FAForever/fa#6537
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done