Skip to content

Commit

Permalink
Fix pyhm (#392)
Browse files Browse the repository at this point in the history
IMO, we've had too many people asking about PyHM problems recently, so I
automated the fix.

I also added an extra function to make life easier for us, and hopefully
for mod authors too, once it becomes widely available.
  • Loading branch information
shpaass authored Jan 27, 2025
2 parents a3b9127 + 469b2f1 commit b941edc
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 19 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,13 @@ You can run `build.sh` with [Git Bash](https://git-scm.com/downloads) to build f

## Possible incompatibilities

- For Py Hard Mode, please manually mark copper-ore-mining as accessible in Dependency Explorer.
- For Seablock, please check [this](https://github.com/ShadowTheAge/yafc/issues/31) issue that has a list of things to enable at first.
- For other mods with scripted progression, YAFC might also think that items are inaccessible. There is no silver bullet against that, but you can open Dependency Explorer and manually mark a bunch of items or technologies as accessible.

For mod authors: You can detect YAFC by checking the `data.data_crawler` variable during the data stage. It will be equal to `yafc a.b.c.d` where `a.b.c.d` is yafc version. For instance, `yafc 0.5.4.0`.
For mod authors: You can add a mod-specific fix to Yafc. Here is a [guide](/Yafc/Data/Mod-fixes/README.md) about it. You can also detect YAFC by checking the `data.data_crawler` variable during the data stage. It will be equal to `yafc a.b.c.d` where `a.b.c.d` is yafc version. For instance, `yafc 2.5.6.0`.

YAFC loads mods in environment that is not completely compatible with Factorio. If you notice any bugs, please report them in the [issues](https://github.com/have-fun-was-taken/yafc-ce/issues).


## Contributing

Expand Down
4 changes: 2 additions & 2 deletions Yafc.Parser/LuaContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ public void SetGlobal(string name, object value) {
ObjectDisposedException.ThrowIf(L == IntPtr.Zero, this);
GetReg(refId); // 1
_ = lua_pushstring(L, idx); // 2
lua_rawget(L, -2); // 3
return PopManagedValue(3);
lua_rawget(L, -2); // 2 (pop & push)
return PopManagedValue(2);
}

private object? PopManagedValue(int popc) {
Expand Down
13 changes: 0 additions & 13 deletions Yafc/Data/Mod-fixes/README

This file was deleted.

36 changes: 36 additions & 0 deletions Yafc/Data/Mod-fixes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Script execution

Mod-specific fixes go in this folder.
The file name format is _modname.path.to.modfile_.lua

Scripts are executed after `require("__modname__.path.to.modfile")`.
`...` (varargs) will contain the return from the normal call to `require("")`.
The script is expected to return a patched require result.

It is not possible to create a script that explicitly executes after data.lua, data-updates.lua, or data-final-fixes.lua.

## YAFC-specific data
### Script-generated objects

`data.script_enabled` is initially-empty sequence of tables that can be modified by these mod-fix scripts, or by mods themselves.
Each table in `data.script_enabled` should contain `type` and `name` keys with string values.
The object identified by the type and name (e.g. `{ type = "item", name = "carbonic-asteroid-chunk" }`) will be treated as an accessibility root, similar to nauvis or the character.
The primary use for this table is to specify entities that are placed on the map via scripts, and starting items.

`data.script_enabled:insert` accepts any number of array-or-entry arguments, and will insert all the supplied entries into `script_enabled`.
For example, the first three calls all insert three items, but the last will insert coal and one invalid (ignored) entry:
```lua
data.script_enabled:insert({ type = "item", name = "iron-ore" }, { type = "item", name = "copper-ore" }, { type = "item", name = "coal" })
data.script_enabled:insert({{ type = "item", name = "iron-ore" }, { type = "item", name = "copper-ore" }}, { type = "item", name = "coal" })
data.script_enabled:insert({{ type = "item", name = "iron-ore" }, { type = "item", name = "copper-ore" }, { type = "item", name = "coal" }})
-- Don't do this, though:
data.script_enabled:insert({{{ type = "item", name = "iron-ore" }, { type = "item", name = "copper-ore" }}, { type = "item", name = "coal" }})
```

## Example of a valid mod fix
<filename: core.lualib.util.lua>
```lua
local util = ...;
log("Captured it!");
return util;
```
10 changes: 10 additions & 0 deletions Yafc/Data/Mod-fixes/pyhardmode.prototypes.mining.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
data.script_enabled:insert({
type = "item",
name = "burner-mining-drill",
},
{
type = "item",
name = "offshore-pump",
})

return ...
18 changes: 17 additions & 1 deletion Yafc/Data/Sandbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ if data then
-- If data isn't set, we couldn't load __core__/lualib/dataloader, which means we're running tests. They replace the entire data table.
data.data_crawler = "yafc "..yafc_version;
log(data.data_crawler);
data.script_enabled = {}
function data.script_enabled:insert(entry, ...)
if entry then
if entry[1] then
-- unpack an array argument
for _, e in pairs(entry) do
table.insert(self, e)
end
else
-- insert a non-array argument
table.insert(self, entry)
end
-- continue to the next argument
self:insert(...)
end
end
end

size=32;
size=32;
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Date: January 20th 2025
Fixes:
- Building emission/absorption (pollution and spores) are displayed in tooltips again.
- Fix a parsing error when using the deprecated data.extend API.
- Support PyHM without extra user interaction.
----------------------------------------------------------------------------------------------------------------------
Version: 2.5.0
Date: December 30th 2024
Expand Down

0 comments on commit b941edc

Please sign in to comment.