Skip to content
This repository has been archived by the owner on Nov 25, 2018. It is now read-only.

Commit

Permalink
Optimizations: ToInfusionDef was expensive
Browse files Browse the repository at this point in the history
  • Loading branch information
notfood committed Jul 31, 2016
1 parent 0ccdb74 commit c81417b
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 57 deletions.
2 changes: 1 addition & 1 deletion About/About.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<ModMetaData>
<name>Infused</name>
<name>Infused (local)</name>
<author>Latta &amp; NotFood</author>
<url>https://ludeon.com/forums/index.php?topic=21884.0</url>
<targetVersion>0.14.1220</targetVersion>
Expand Down
8 changes: 4 additions & 4 deletions DLL/Infused/ICommon/Gens/TextUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private static string NewInfusedThingLabel( Thing thing, bool isStuffed, bool is

if ( !inf.PassPre )
{
result.Append( inf.Prefix.ToInfusionDef().label + " " );
result.Append( inf.Prefix.label + " " );
}

string thingLabel;
Expand All @@ -124,7 +124,7 @@ private static string NewInfusedThingLabel( Thing thing, bool isStuffed, bool is
}

result.Append( !inf.PassSuf
? ResourceBank.StringInfusionOf.Translate( thingLabel, inf.Suffix.ToInfusionDef().label.CapitalizeFirst() )
? ResourceBank.StringInfusionOf.Translate( thingLabel, inf.Suffix.label.CapitalizeFirst() )
: thingLabel );

if ( !isDetailed )
Expand Down Expand Up @@ -160,7 +160,7 @@ public static string GetInfusionDesc( this Thing thing )
var result = new StringBuilder( null );
if ( !inf.PassPre )
{
var prefix = inf.Prefix.ToInfusionDef();
var prefix = inf.Prefix;
result.Append( ResourceBank.StringInfusionDescFrom.Translate( prefix.LabelCap ) )
.Append( " (" )
.Append( prefix.tier.Translate() )
Expand Down Expand Up @@ -208,7 +208,7 @@ public static string GetInfusionDesc( this Thing thing )
return result.ToString();
}

var suffix = inf.Suffix.ToInfusionDef();
var suffix = inf.Suffix;
result.Append(ResourceBank.StringInfusionDescFrom.Translate(suffix.LabelCap))
.Append(" (")
.Append(suffix.tier.Translate())
Expand Down
40 changes: 16 additions & 24 deletions DLL/Infused/ICommon/ModInitializers/ModInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,62 +34,54 @@ public override bool Inject()
//Inject every prerequisites to defs.
private void InjectVarious()
{
//Access ThingDef database with each def's defName.
String fieldName = "defsByName";
FieldInfo field = typeof(DefDatabase< ThingDef >).GetField (fieldName, BindingFlags.NonPublic | BindingFlags.Static);
if (field == null)
throw new ArgumentOutOfRangeException("fieldName", string.Format("Field {0} was not found in Type {1}", fieldName, typeof(DefDatabase< ThingDef >).FullName));
var defsByName = field.GetValue( null ) as Dictionary< string, ThingDef >;
if ( defsByName == null )
throw new Exception( "Could not access private members" );
foreach (var current in defsByName.Values.Where(current => current.IsMeleeWeapon || current.IsRangedWeapon || current.IsApparel))
foreach (var thingDef in DefDatabase< ThingDef >.AllDefs.Where ( def => def.IsMeleeWeapon || def.IsRangedWeapon || def.IsApparel ))
{
if ( AddCompInfusion( current ) )
if ( AddCompInfusion( thingDef ) )
{
AddInfusionITab( current );
AddInfusionITab( thingDef );
}
}
}

//Inject new ThingComp.
private static bool AddCompInfusion( ThingDef def )
private static bool AddCompInfusion( ThingDef thingDef )
{
if ( def.comps.Exists( s => s.compClass == typeof ( CompInfusion ) ) )
if ( thingDef.comps.Exists( s => s.compClass == typeof ( CompInfusion ) ) )
{
Log.Message ("Infused: Component exists for " + def.label);
Log.Message ("Infused: Component exists for " + thingDef.label);
return false;
}

if ( !def.comps.Exists( s => s.compClass == typeof ( CompQuality ) ) )
if ( !thingDef.comps.Exists( s => s.compClass == typeof ( CompQuality ) ) )
{
return false;
}

//As we are adding, not replacing, we need a fresh CompProperties.
//We don't need anything except compClass as CompInfusion does not take anything.
var compProperties = new CompProperties {compClass = typeof ( CompInfusion )};
def.comps.Insert( 0, compProperties );
thingDef.comps.Insert( 0, compProperties );
#if DEBUG
Log.Message ("Infused: Component added to " + def.label);
#endif
return true;
}

//Inject new ITab to given def.
private static void AddInfusionITab( ThingDef def )
private static void AddInfusionITab( ThingDef thingDef )
{
if ( def.inspectorTabs == null || def.inspectorTabs.Count == 0 )
if ( thingDef.inspectorTabs == null || thingDef.inspectorTabs.Count == 0 )
{
def.inspectorTabs = new List< Type >();
def.inspectorTabsResolved = new List< ITab >();
thingDef.inspectorTabs = new List< Type >();
thingDef.inspectorTabsResolved = new List< ITab >();
}
if ( def.inspectorTabs.Contains( typeof ( ITab_Infusion ) ) )
if ( thingDef.inspectorTabs.Contains( typeof ( ITab_Infusion ) ) )
{
Log.Message ("Infused: Tab exists for " + def.label);
Log.Message ("Infused: Tab exists for " + thingDef.label);
return;
}
def.inspectorTabs.Add( typeof ( ITab_Infusion ) );
def.inspectorTabsResolved.Add( ITabManager.GetSharedInstance( typeof ( ITab_Infusion ) ) );
thingDef.inspectorTabs.Add( typeof ( ITab_Infusion ) );
thingDef.inspectorTabsResolved.Add( ITabManager.GetSharedInstance( typeof ( ITab_Infusion ) ) );
}
}
}
Expand Down
38 changes: 25 additions & 13 deletions DLL/Infused/Infusions/Comp/CompInfusion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ namespace Infused
{
public class CompInfusion : ThingComp
{
public bool Infused {
get { return prefix != null || suffix != null; }
}

public InfusionSet Infusions {
get { return new InfusionSet (prefix, suffix); }
}
private static readonly SoundDef InfusionSound = SoundDef.Named( "Infusion_Infused" );

// Is our infusion newly created? Only for notifications.
private bool isNew;

private string prefix, suffix;
private string prefixDefName, suffixDefName;
private InfusionDef prefix, suffix;

private static readonly SoundDef InfusionSound = SoundDef.Named( "Infusion_Infused" );
public InfusionSet Infusions {
get { return new InfusionSet (prefix, suffix); }
}

public bool Infused {
get { return prefix != null || suffix != null; }
}

public void InitializeInfusionPrefix(QualityCategory qc, TechLevel tech)
{
Expand All @@ -42,7 +43,7 @@ select t
Log.Warning ("Infused: Couldn't find any prefixed InfusionDef! Tier: " + tier);
prefix = null;
} else {
prefix = preTemp.defName;
prefix = preTemp.defName.ToInfusionDef();
}

isNew = true;
Expand All @@ -68,7 +69,7 @@ select t
}
else
{
suffix = preTemp.defName;
suffix = preTemp.defName.ToInfusionDef();
}

isNew = true;
Expand Down Expand Up @@ -114,8 +115,19 @@ public override void PostExposeData()
{
base.PostExposeData();

Scribe_Values.LookValue( ref prefix, "prefix", null );
Scribe_Values.LookValue( ref suffix, "suffix", null );
// This is ugly, I need a better solution.
if (prefix != null)
prefixDefName = prefix.defName;
if (suffix != null)
suffixDefName = suffix.defName;

Scribe_Values.LookValue (ref prefixDefName, "prefix", null);
Scribe_Values.LookValue (ref suffixDefName, "suffix", null);

if (prefix == null)
prefix = prefixDefName.ToInfusionDef ();
if (suffix == null)
suffix = suffixDefName.ToInfusionDef ();

#if DEBUG
if ( (prefix != null && prefix.ToInfusionDef() == null) || (suffix != null && suffix.ToInfusionDef() == null) )
Expand Down
6 changes: 3 additions & 3 deletions DLL/Infused/Infusions/InfusionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public bool Equals(InfusionSet other)
return string.Equals(Prefix, other.Prefix) && string.Equals(Suffix, other.Suffix);
}

public string Prefix;
public string Suffix;
public InfusionDef Prefix;
public InfusionDef Suffix;

public bool PassPre {
get {return Prefix == null; }
Expand All @@ -21,7 +21,7 @@ public bool PassSuf {
get {return Suffix == null; }
}

public InfusionSet(string pre, string suf)
public InfusionSet(InfusionDef pre, InfusionDef suf)
{
Prefix = pre;
Suffix = suf;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ private static void Draw()
foreach (var current in InfusionLabelManager.Drawee)
{
var inf = current.Infusions;
var prefix = inf.Prefix.ToInfusionDef();
var suffix = inf.Suffix.ToInfusionDef();
var prefix = inf.Prefix;
var suffix = inf.Suffix;

Color color;
//When there is only suffix
Expand Down
8 changes: 4 additions & 4 deletions DLL/Infused/Infusions/StatParts/StatPart_InfusionModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public override void TransformValue( StatRequest req, ref float val )
}
//"Notifier" will notify below lines about the actual StatDef it is adjusting, via XML-written notifier string.
//InfusionSet is also string-based, so we have to find the InfusionDef behind it.
var prefix = inf.Prefix.ToInfusionDef();
var suffix = inf.Suffix.ToInfusionDef();
var prefix = inf.Prefix;
var suffix = inf.Suffix;
//Return if the worker has no stat specified
if ( !inf.PassPre && prefix.GetStatValue( stat, out mod ) )
{
Expand Down Expand Up @@ -73,11 +73,11 @@ protected string WriteExplanation( Thing thing, InfusionSet infusions )

if ( !infusions.PassPre )
{
result.Append( WriteExplanationDetail( thing, infusions.Prefix ) );
result.Append( WriteExplanationDetail( thing, infusions.Prefix.defName ) );
}
if ( !infusions.PassSuf )
{
result.Append( WriteExplanationDetail( thing, infusions.Suffix ) );
result.Append( WriteExplanationDetail( thing, infusions.Suffix.defName ) );
}
return result.ToString();
}
Expand Down
8 changes: 4 additions & 4 deletions DLL/Infused/Infusions/StatParts/StatPart_InfusionWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public override void TransformValue( StatRequest req, ref float val )
Log.ErrorOnce( "Infused: Could not find notifier's StatDef, which is " + notifier, 3388123 );
return;
}
var prefix = inf.Prefix.ToInfusionDef();
var suffix = inf.Suffix.ToInfusionDef();
var prefix = inf.Prefix;
var suffix = inf.Suffix;

if (!inf.PassPre && prefix.GetStatValue( stat, out mod ))
{
Expand Down Expand Up @@ -69,8 +69,8 @@ public override void TransformValue( StatRequest req, ref float val )
Log.ErrorOnce( "Infused: Could not find notifier's StatDef, which is " + notifier, 3388123 );
continue;
}
var prefix = inf.Prefix.ToInfusionDef();
var suffix = inf.Suffix.ToInfusionDef();
var prefix = inf.Prefix;
var suffix = inf.Suffix;

if (!inf.PassPre && prefix.GetStatValue( stat, out mod ))
{
Expand Down
4 changes: 2 additions & 2 deletions DLL/Infused/Infusions/UI/ITab_Infusion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ protected override void FillTab()
private static string GetRectLabel()
{
var infs = SelectedCompInfusion.Infusions;
var preDef = infs.Prefix.ToInfusionDef();
var sufDef = infs.Suffix.ToInfusionDef();
var preDef = infs.Prefix;
var sufDef = infs.Suffix;

var result = new StringBuilder();
if ( !infs.PassPre )
Expand Down

0 comments on commit c81417b

Please sign in to comment.