{"id":24999020,"url":"https://github.com/bukowa/twdll","last_synced_at":"2025-03-29T18:12:14.213Z","repository":{"id":274893573,"uuid":"924396816","full_name":"bukowa/twdll","owner":"bukowa","description":"`twdll` is a sample project that demonstrates how to call and manipulate *Total War: Rome II* Lua stack directly from C. It shows how to inject a native DLL into the Lua environment and execute Lua functions from C, enabling seamless integration between both execution contexts.","archived":false,"fork":false,"pushed_at":"2025-02-15T16:44:30.000Z","size":1850,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-15T17:30:05.766Z","etag":null,"topics":["c","dll","library","lua","native","rome2","scripting","state","totalwar"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bukowa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-29T23:23:58.000Z","updated_at":"2025-02-15T16:44:35.000Z","dependencies_parsed_at":"2025-02-15T17:35:37.226Z","dependency_job_id":null,"html_url":"https://github.com/bukowa/twdll","commit_stats":null,"previous_names":["bukowa/twdll"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bukowa%2Ftwdll","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bukowa%2Ftwdll/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bukowa%2Ftwdll/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bukowa%2Ftwdll/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bukowa","download_url":"https://codeload.github.com/bukowa/twdll/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246223331,"owners_count":20743167,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","dll","library","lua","native","rome2","scripting","state","totalwar"],"created_at":"2025-02-04T18:52:51.497Z","updated_at":"2025-03-29T18:12:14.196Z","avatar_url":"https://github.com/bukowa.png","language":"C","readme":"# twdll\n\n`twdll` is a sample project demonstrating how to call *Total War: Rome II*'s Lua stack directly from C. The main challenge was manually determining the correct Lua version used by the game to ensure proper execution.\n\n## What This Project Does\n\n- **Directly Executes *Rome II*'s Lua State in C** \n  - – Enables calling and manipulating the game's Lua stack from native C code.\n- **Allows Native Calls with Lua State** \n  - – Ensures that C functions can execute within the Lua execution context without breaking it.\n- **Loads a DLL in Lua** \n  - – Demonstrates how to inject a native library into the game's scripting environment.\n\n### Notes\n\n- Requires **Lua 5.1.1** or **Lua 5.1.2** for proper functionality.\n- **Lua 5.1** does not return from `dostring`.\n- **Lua 5.1.3 and later** crash due to memory access issues.\n- To prevent crashes in **Lua 5.1.3**, you can replace the following files with those from **Lua 5.1.2**:  \n    ```\n  lstate.h\n    lstate.c\n    ldo.c\n  ```\n- `CA` uses this method of table insertion that doesn't work in our lua VM\n\n  ```lua\n  -- CA method\n  events[event][#events[event]+1] = func\n  \n  -- REQUIRED method\n  table.insert(events[event], #events[event], func)\n  ```\n  It happens here https://github.com/bukowa/twdll/blob/bc9b2092009a261ec7e2f0fd7171befe229b0344/include/lua-ver-5.1.2/src/ltable.c#L425\n  ```\n  /*\n  Exception 0xc0000005 encountered at address 0x23af116e: Access violation writing location 0x68801ccc\n  \n  ** inserts a new key into a hash table; first, check whether key's main\n  ** position is free. If not, check whether colliding node is in its main\n  ** position or not: if it is not, move colliding node to an empty place and\n  ** put new key in its main position; otherwise (colliding node is in its main\n  ** position), new key goes to an empty position.\n  */\n  static TValue *newkey (lua_State *L, Table *t, const TValue *key) {\n  \n  (lldb) memory read 0x23b5116e\n  0x23b5116e: 89 50 08 8b 4d fc 8b 55 10 8b 42 04 89 41 0c 8b  �P.�M��U.�B.�A.�\n  0x23b5117e: 4d 10 83 79 04 04 7c 2a 8b 55 10 8b 02 0f b6 48  M.�y..|*�U.�..�H\n  \n  (lldb) memory read 0x68801ccc -c1000\n  0x68801ccc: 00 00 00 00 00 00 00 00 00 00 00 00 69 6e 76 61  ............inva\n  0x68801cdc: 6c 69 64 20 6b 65 79 20 74 6f 20 27 6e 65 78 74  lid key to 'next\n  0x68801cec: 27 00 00 00 74 61 62 6c 65 20 6f 76 65 72 66 6c  '...table overfl\n  0x68801cfc: 6f 77 00 00 74 61 62 6c 65 20 69 6e 64 65 78 20  ow..table index\n  0x68801d0c: 69 73 20 6e 69 6c 00 00 74 61 62 6c 65 20 69 6e  is nil..table in\n  0x68801d1c: 64 65 78 20 69 73 20 4e 61 4e 00 00 25 2e 31 34  dex is NaN..%.14\n  0x68801d2c: 67 00 00 00 69 6e 64 65 78 00 00 00 6c 6f 6f 70  g...index...loop\n  0x68801d3c: 20 69 6e 20 67 65 74 74 61 62 6c 65 00 00 00 00   in gettable....\n  0x68801d4c: 6c 6f 6f 70 20 69 6e 20 73 65 74 74 61 62 6c 65  loop in settable\n  0x68801d5c: 00 00 00 00 73 74 72 69 6e 67 20 6c 65 6e 67 74  ....string lengt\n  0x68801d6c: 68 20 6f 76 65 72 66 6c 6f 77 00 00 67 65 74 20  h overflow..get\n  0x68801d7c: 6c 65 6e 67 74 68 20 6f 66 00 00 00 27 66 6f 72  length of...'for\n  0x68801d8c: 27 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20  ' initial value\n  0x68801d9c: 6d 75 73 74 20 62 65 20 61 20 6e 75 6d 62 65 72  must be a number\n\n  ```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbukowa%2Ftwdll","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbukowa%2Ftwdll","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbukowa%2Ftwdll/lists"}