Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

tornac1234
Copy link
Collaborator

@tornac1234 tornac1234 commented May 9, 2024

  • 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)

@tornac1234 tornac1234 added the Area: bases Related to base building or interior game objects label May 9, 2024
@tornac1234 tornac1234 marked this pull request as ready for review May 10, 2024 09:21
Comment on lines 36 to 48
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();
Copy link
Member

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

Copy link
Collaborator Author

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)))
Copy link
Member

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?

Copy link
Collaborator Author

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.

@tornac1234
Copy link
Collaborator Author

New commit can be reviewed by itself

@tornac1234 tornac1234 added the Area: spawning Related to spawning and/or terrain label May 26, 2024
@@ -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);
Copy link
Member

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)]
Copy link
Member

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
Copy link
Member

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;
Copy link
Member

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))
Copy link
Member

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.

Comment on lines +9 to +12
public static ReferenceHolder EnsureReferenceAttached(Component component, object reference)
{
return EnsureReferenceAttached(component.gameObject, reference);
}
Copy link
Member

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)
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: bases Related to base building or interior game objects Area: spawning Related to spawning and/or terrain
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants