-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Crops persistence #2137
base: master
Are you sure you want to change the base?
Crops persistence #2137
Conversation
tornac1234
commented
May 9, 2024
•
edited
Loading
edited
- Add crops persistence (grow progress and exact slot)
- Make sure crops sync works
- Sync trash can behaviour (related because you often need to throw items when playing with crops, ig)
- Sync deletion of giver objects (which give you some item when you click on them like melons)
- Sync fruit growing (from grown plants) and harvesting (picking up)
return new CodeMatcher(instructions).MatchEndForward(new CodeMatch(OpCodes.Call, Reflect.Method(() => Object.Destroy(default)))) | ||
.Advance(1) | ||
.InsertAndAdvance([ | ||
new CodeInstruction(OpCodes.Ldarg_0), | ||
new CodeInstruction(OpCodes.Call, Reflect.Method(() => BroadcastDeletion(default))) | ||
]) | ||
.MatchEndForward(new CodeMatch(OpCodes.Call, Reflect.Method(() => Object.Destroy(default)))) | ||
.Advance(1) | ||
.InsertAndAdvance([ | ||
new CodeInstruction(OpCodes.Ldarg_0), | ||
new CodeInstruction(OpCodes.Call, Reflect.Method(() => BroadcastDeletion(default))) | ||
]) | ||
.InstructionEnumeration(); |
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.
CodeMatcher has support for repeated operations, don't know if it's better here:
new CodeMatcher(instructions)
.MatchForward(false, // false = move at the start of the match, true = move at the end of the match
new CodeMatch(OpCodes.Stfld),
new CodeMatch(OpCodes.Ldarg_0),
new CodeMatch(i => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "test"))
.Repeat( matcher => // Do the following for each match
matcher
.Advance(2) // Move cursor to before ldfld
.InsertAndAdvance(
new CodeInstruction(OpCodes.Dup),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Foo), "Foo"))
)
)
.InstructionEnumerable(); // Finally, return the manipulated method instructions
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'll do it this time, but we should probably refrain from doing it everywhere because we could lose context in case Subnautica code gets updated. Even though tests are there to show us that some patch broke (e.g. now there are 3 matches and not 2), we might lose the exact intention of which matches exactly we wanted to patch, and which one we didn't want to patch.
public static void BroadcastDeletion(PickPrefab pickPrefab) | ||
{ | ||
if (pickPrefab.TryGetNitroxId(out NitroxId objectId) || | ||
(pickPrefab.TryGetComponent(out GrownPlant grownPlant) && grownPlant.seed && grownPlant.seed.TryGetNitroxId(out objectId))) |
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.
Don't we need to check the simulation lock here?
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.
Picking up an item from those behaviours is a task executed locally, therefore only the local player knows about it, which is why they should not require simulation lock for it.
New commit can be reviewed by itself |
@@ -227,6 +227,11 @@ private static void EntityTest(Entity entity, Entity entityAfter) | |||
break; | |||
case PlantableMetadata metadata when entityAfter.Metadata is PlantableMetadata metadataAfter: | |||
Assert.AreEqual(metadata.TimeStartGrowth, metadataAfter.TimeStartGrowth); | |||
Assert.AreEqual(metadata.SlotID, metadataAfter.SlotID); |
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.
Would add a comment here that the FruitPlantMetadata
field is not checked bc it's (hopefully) only temporary.
[DataMember(Order = 1)] | ||
public bool[] PickedStates { get; } | ||
|
||
[DataMember(Order = 1)] |
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.
💀
public override void ProcessMetadata(GameObject gameObject, FruitPlantMetadata metadata) | ||
{ | ||
// Two cases: | ||
// 1. The entity with an id |
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 think the sentence is cut off
{ | ||
ProcessMetadata(fruitPlant, metadata); | ||
} | ||
return; |
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.
Is it expected that plantable.linkedGrownPlant && plantable.linkedGrownPlant.TryGetComponent(out fruitPlant)
is false? Bc right now with the return after the if we don't log anything
{ | ||
plantable.growingPlant.timeStartGrowth = metadata.TimeStartGrowth; | ||
} | ||
else if (plantable.model.TryGetComponent(out GrowingPlant growingPlant)) |
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.
Would like a comment here explaining the state difference of the Plantable. Having the field growingPlant and getting it by hand seems like the same thing, so I guess the Plantable has a different state.
public static ReferenceHolder EnsureReferenceAttached(Component component, object reference) | ||
{ | ||
return EnsureReferenceAttached(component.gameObject, reference); | ||
} |
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.
Would move this down to it's parent method
public static void Postfix(FruitPlant __instance, (float, NitroxId, Plantable) __state) | ||
{ | ||
// If no change was made | ||
if (__state.Item1 == __instance.timeNextFruit) |
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.
Direct float equality could be problematic here, no?