Job drivers

Started by Wishmaster, March 30, 2017, 09:07:37 PM

Previous topic - Next topic

Wishmaster

I just tried to make an alternative job driver for tending.

So first, why does decompiled vanilla job drivers look like this.
Guess it has something to do with the attribute.

[DebuggerHidden]
protected override IEnumerable<Toil> MakeNewToils()
{
JobDriver_LayEgg.<MakeNewToils>c__Iterator0 <MakeNewToils>c__Iterator = new JobDriver_LayEgg.<MakeNewToils>c__Iterator0();
<MakeNewToils>c__Iterator.<>f__this = this;
JobDriver_LayEgg.<MakeNewToils>c__Iterator0 expr_0E = <MakeNewToils>c__Iterator;
expr_0E.$PC = -2;
return expr_0E;
}


I am not sure if I'm not using the decompiler well (Ilspy) or of it is obfuscated on purpose.

I am not sure because some mods such as Dubs Rimkit for tending reproduce this driver behavior very well.
Did the author had to rewrite the entire code ? Or is there a way to clear something clear out of those MakeNewToils() methods ? That would really help.

RawCode

use dnspy or Zhentar's fork of ilspy


RawCode

// Token: 0x06000040 RID: 64 RVA: 0x00003DDC File Offset: 0x00001FDC
[DebuggerHidden]
protected override IEnumerable<Toil> MakeNewToils()
{
JobDriver_LayEgg.<MakeNewToils>c__Iterator0 <MakeNewToils>c__Iterator = new JobDriver_LayEgg.<MakeNewToils>c__Iterator0();
<MakeNewToils>c__Iterator.<>f__this = this;
JobDriver_LayEgg.<MakeNewToils>c__Iterator0 expr_0E = <MakeNewToils>c__Iterator;
expr_0E.$PC = -2;
return expr_0E;
}


c__Iterator0

private sealed class <MakeNewToils>c__Iterator0 : IEnumerator, IDisposable, IEnumerable<Toil>, IEnumerator<Toil>, IEnumerable

+

public bool MoveNext()
{
uint num = (uint)this.$PC;
this.$PC = -1;
switch (num)
{
case 0u:
this.$current = Toils_Goto.GotoCell(TargetIndex.A, PathEndMode.OnCell);
this.$PC = 1;
return true;
case 1u:
this.<prepare>__0 = new Toil();
this.<prepare>__0.defaultCompleteMode = ToilCompleteMode.Delay;
this.<prepare>__0.defaultDuration = 500;
this.$current = this.<prepare>__0;
this.$PC = 2;
return true;
case 2u:
this.<finalize>__1 = new Toil();
this.<finalize>__1.initAction = delegate
{
Pawn actor = this.<finalize>__1.actor;
Thing forbiddenIfOutsideHomeArea = GenSpawn.Spawn(actor.GetComp<CompEggLayer>().ProduceEgg(), actor.Position, this.<>f__this.Map);
forbiddenIfOutsideHomeArea.SetForbiddenIfOutsideHomeArea();
};
this.<finalize>__1.defaultCompleteMode = ToilCompleteMode.Instant;
this.$current = this.<finalize>__1;
this.$PC = 3;
return true;
case 3u:
this.$PC = -1;
break;
}
return false;
}


easily parsed into
yield Toils_Goto.GotoCell(TargetIndex.A, PathEndMode.OnCell);
yield new Toil(defaultCompleteMode = ToilCompleteMode.Delay, defaultDuration = 500);
yield this part you will complete on your own following same techineque described above (copypaste madskillz)

and you have missed "show compiler generated code"

Wishmaster

Found that in the settings of dnSpy (but not the fork of Ilspy).
Anyway thank you.
And that just made me understand why it is written this way instead of

yield ...
yield ...

RawCode

because there is no yield on IL layer and compiler have no other way.
please read any article about syntax sugar