Issue with decompiled thingClass Plant

Started by WorldOfIllusion, April 11, 2014, 09:48:28 PM

Previous topic - Next topic

WorldOfIllusion

With Alpha 3 arriving I decided to have a look through how everything worked, with an intention of making some modifications.

Started by looking on the forums and found that most modders used a program ILSpy to decompile the game DLL's. When I decompiled the Plant class of the Assembly-CSharp DLL, I encountered an issue (that I assume is due to an incorrect decompile).

In the method: PrintOnto
switch (maxMeshCount)
{
case 1:
num3 = 1;
goto IL_F3;
case 2:
case 3:
IL_96:
if (maxMeshCount == 9)
{
num3 = 3;
goto IL_F3;
}
if (maxMeshCount == 16)
{
num3 = 4;
goto IL_F3;
}
if (maxMeshCount != 32)
{
Log.Error(this.def + " must have plant.MaxMeshCount that is a perfect square.");
goto IL_F3;
}
num3 = 5;
goto IL_F3;
case 4:
num3 = 2;
goto IL_F3;
}
goto IL_96;


The error that I get is due to the goto IL_96 call and the corresponding IL_96 tag which it thinks is unreferenced (in the code it is within the scope of the switch case while the goto call is outside of it).

Wondering what the actual code is? Or if this is intended (and thus how it works)?
Artistically challenged modder seeking artistically talented texturer's help. Please, please, PM me :)

Tynan

Here's the original code.

Note that this isn't great code. It's worse than most stuff, but it's old and hasn't needed maintenance so I let it lie.


public override void PrintOnto( SectionLayer layer )
{
Profiler.BeginSample("Plant.EmitMeshPieces " + this);

Vector3 trueCenter = this.TrueCenter();

Profiler.BeginSample("Meshes");

Random.seed = Position.GetHashCode();//So our random generator makes the same numbers every time

//Determine random local position variance
float positionVariance;
if( def.plant.maxMeshCount == 1 )
positionVariance = 0.05f;
else
positionVariance = 0.50f;

//Determine how many meshes to print
int meshCount = Mathf.CeilToInt( growthPercent * def.plant.maxMeshCount );
if( meshCount < 1 )
meshCount = 1;

//Grid width is the square root of max mesh count
int gridWidth = 1;
switch( def.plant.maxMeshCount )
{
case 1: gridWidth = 1; break;
case 4: gridWidth = 2; break;
case 9: gridWidth = 3; break;
case 16: gridWidth = 4; break;
case 32: gridWidth = 5; break;
default: Log.Error(def + " must have plant.MaxMeshCount that is a perfect square."); break;
}
float gridSpacing = 1f/gridWidth; //This works out to give half-spacings around the edges

//Shuffle up the position indices and place meshes at them
//We do this to get even mesh placement by placing them roughly on a grid
Vector3 adjustedCenter = Vector3.zero;
Vector2 planeSize = Vector2.zero;
int meshesYielded = 0;
int posCount = posIndexList.Count;
for(int i=0; i<posCount; i++ )
{
int posIndex = posIndexList[i];

//Determine plane size
float size = def.plant.visualSizeRange.LerpThroughRange(growthPercent);

//Determine center position
if( def.plant.maxMeshCount == 1 )
{
adjustedCenter = trueCenter + new Vector3(Random.Range(-positionVariance, positionVariance),
0,
Random.Range(-positionVariance, positionVariance) );

//Clamp bottom of plant to square bottom
//So tall plants grow upward
float squareBottom = Mathf.Floor(trueCenter.z);
if( (adjustedCenter.z - (size/2f)) <  squareBottom )
adjustedCenter.z = squareBottom + (size/2f);
}
else
{
adjustedCenter = Position.ToVector3(); //unshifted
adjustedCenter.y = def.altitude;//Set altitude

//Place this mesh at its randomized position on the submesh grid
adjustedCenter.x += 0.5f * gridSpacing;
adjustedCenter.z += 0.5f * gridSpacing;
int xInd = posIndex / gridWidth;
int zInd = posIndex % gridWidth;
adjustedCenter.x += xInd * gridSpacing;
adjustedCenter.z += zInd * gridSpacing;

//Add a random offset
float gridPosRandomness = gridSpacing * GridPosRandomnessFactor;
adjustedCenter += new Vector3(Random.Range(-gridPosRandomness, gridPosRandomness),
  0,
  Random.Range(-gridPosRandomness, gridPosRandomness) );
}

//Randomize horizontal flip
bool flipped = Random.value < 0.5f;

//Randomize material
Material mat = def.folderDrawMats.RandomListElement();

//Set wind exposure value at each vertex by setting vertex color
workingColors[1].a = workingColors[2].a = (byte)(255 * def.plant.topWindExposure);
workingColors[0].a = workingColors[3].a = 0;

if( def.overdraw )
size += 2f;
planeSize = new Vector2( size,size );
Printer_Plane.PrintPlane( layer,
  adjustedCenter,
  planeSize,
  mat,
  flipUv: flipped,
  colors:  workingColors );


meshesYielded++;
if( meshesYielded >= meshCount )
break;
}

Profiler.EndSample();

Profiler.BeginSample("Shadow");

if( def.sunShadowInfo != null)
{
//Brutal shadow positioning hack
float shadowOffsetFactor = 0.85f;
if( planeSize.y < 1 )
shadowOffsetFactor = 0.6f; //for bushes
else
shadowOffsetFactor = 0.81f; //for cacti

Vector3 sunShadowLoc = adjustedCenter;
sunShadowLoc.z -= (planeSize.y/2f) * shadowOffsetFactor;
sunShadowLoc.y -= Altitudes.AltInc;

Printer_Shadow.PrintShadow( layer, sunShadowLoc, def.sunShadowInfo );
}

Profiler.EndSample();

Profiler.EndSample();
}
Tynan Sylvester - @TynanSylvester - Tynan's Blog

WorldOfIllusion

thanks. The code is vastly better than the decompiled version, and much easier to understand, which is all i'm concerned with!
Artistically challenged modder seeking artistically talented texturer's help. Please, please, PM me :)

Kilroy232

I am having the same issue and just wondering, the code you posted above Tynan is just to fix the goto IL_96 issue correct?

Tynan

It's not a fix for anything; it's just the original code I wrote.
Tynan Sylvester - @TynanSylvester - Tynan's Blog

Kilroy232

Quote from: Tynan on April 14, 2014, 09:37:28 PM
It's not a fix for anything; it's just the original code I wrote.

sorry, poor wording on my part but that answers my question. Thank you