Skip to content

Commit

Permalink
reduce unneeded consecutive lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
Pathoschild committed Jun 12, 2021
1 parent 8057aa6 commit 7d05023
Show file tree
Hide file tree
Showing 55 changed files with 607 additions and 645 deletions.
15 changes: 10 additions & 5 deletions BetterShopMenu/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,20 @@ private void DrawGridLayout()
}
if (hover != null)
{
// get hover price & stock
if (itemPriceAndStock == null || !itemPriceAndStock.TryGetValue(hover, out int[] hoverPriceAndStock))
hoverPriceAndStock = null;

// render tooltip
string hoverText = hover.getDescription();
string boldTitleText = hover.DisplayName;
int hoverPrice = itemPriceAndStock == null || !itemPriceAndStock.ContainsKey(hover) ? hover.salePrice() : itemPriceAndStock[hover][0];
int hoverPrice = hoverPriceAndStock?[0] ?? hover.salePrice();
int getHoveredItemExtraItemIndex = -1;
if (itemPriceAndStock != null && hover != null && (itemPriceAndStock.ContainsKey(hover) && itemPriceAndStock[hover].Length > 2))
getHoveredItemExtraItemIndex = itemPriceAndStock[hover][2];
if (hoverPriceAndStock?.Length > 2)
getHoveredItemExtraItemIndex = hoverPriceAndStock[2];
int getHoveredItemExtraItemAmount = 5;
if (itemPriceAndStock != null && hover != null && itemPriceAndStock.ContainsKey(hover) && itemPriceAndStock[hover].Length > 3)
getHoveredItemExtraItemAmount = itemPriceAndStock[hover][3];
if (hoverPriceAndStock?.Length > 3)
getHoveredItemExtraItemAmount = hoverPriceAndStock[3];
IClickableMenu.drawToolTip(Game1.spriteBatch, hoverText, boldTitleText, hover as Item, heldItem != null, -1, currency, getHoveredItemExtraItemIndex, getHoveredItemExtraItemAmount, null, hoverPrice);
}

Expand Down
5 changes: 2 additions & 3 deletions BiggerCraftables/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ public bool IsBiggerCraftable(SObject obj)
if (!obj.bigCraftable.Value)
return false;

return Mod.Entries.FirstOrDefault(e => e.Name == obj.Name) == null ? false : true;
return Mod.Entries.FirstOrDefault(e => e.Name == obj.Name) != null;
}

public Vector2 GetBaseCraftable(GameLocation loc, Vector2 pos)
{
if (!loc.Objects.ContainsKey(pos))
if (!loc.Objects.TryGetValue(pos, out SObject obj))
return new Vector2(-1, -1);

var obj = loc.Objects[pos];
if (!this.IsBiggerCraftable(obj))
return new Vector2(-1, -1);

Expand Down
4 changes: 3 additions & 1 deletion BiggerCraftables/Framework/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ internal static class Extensions

public static int GetBiggerIndex(this StardewValley.Object obj)
{
return obj.modData.ContainsKey(Extensions.BiggerIndexKey) ? int.Parse(obj.modData[Extensions.BiggerIndexKey]) : 0;
return obj.modData.TryGetValue(Extensions.BiggerIndexKey, out string rawIndex)
? int.Parse(rawIndex)
: 0;
}

public static void SetBiggerIndex(this StardewValley.Object obj, int index)
Expand Down
5 changes: 2 additions & 3 deletions BiggerCraftables/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ private void OnObjectListChanged(object sender, ObjectListChangedEventArgs e)
for (int iy = 0; iy < entry.Length; ++iy)
{
Vector2 localPos = basePos + new Vector2(ix, iy);
if (localPos == pos || !loc.Objects.ContainsKey(localPos))
continue;
loc.Objects.Remove(localPos);
if (localPos != pos)
loc.Objects.Remove(localPos);
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions BugNet/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,30 @@ private void OnButtonPressed(object sender, ButtonPressedEventArgs e)

internal static Texture2D GetCritterTexture(string critter)
{
return Mod.CrittersData.ContainsKey(critter) ? Mod.CrittersData[critter].Texture.Texture : Game1.staminaRect;
return Mod.CrittersData.TryGetValue(critter, out CritterData critterData)
? critterData.Texture.Texture
: Game1.staminaRect;
}

internal static Rectangle GetCritterRect(string critter)
{
return Mod.CrittersData.ContainsKey(critter) ? Mod.CrittersData[critter].Texture.SourceRect : new Rectangle(0, 0, 1, 1);
return Mod.CrittersData.TryGetValue(critter, out CritterData critterData)
? critterData.Texture.SourceRect
: new Rectangle(0, 0, 1, 1);
}

internal static string GetCritterName(string critter)
{
return Mod.CrittersData.ContainsKey(critter) ? Mod.CrittersData[critter].Name() : "???";
return Mod.CrittersData.TryGetValue(critter, out CritterData critterData)
? critterData.Name()
: "???";
}

internal static Func<int, int, Critter> GetCritterMaker(string critter)
{
return Mod.CrittersData.ContainsKey(critter) ? Mod.CrittersData[critter].MakeFunction : ((x, y) => null);
return Mod.CrittersData.TryGetValue(critter, out CritterData critterData)
? critterData.MakeFunction
: ((x, y) => null);
}

internal static string GetCritterIdFrom(Critter critter)
Expand Down
6 changes: 3 additions & 3 deletions BuildableLocationsFramework/Patches/UtilityPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ private static bool Before_GetAnimal(long id, ref FarmAnimal __result)
var locs = Mod.GetAllLocations();
foreach (var loc in locs)
{
if (loc is IAnimalLocation aloc)
if (loc is IAnimalLocation animalLocation)
{
if (aloc.Animals.ContainsKey(id))
if (animalLocation.Animals.TryGetValue(id, out FarmAnimal animal))
{
__result = aloc.Animals[id];
__result = animal;
return false;
}
}
Expand Down
4 changes: 1 addition & 3 deletions CaveFarm/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ private void WallsCommand(string cmd, string[] args)
if (Math.Abs(Game1.player.getTileX() - ix) < 3 && Math.Abs(Game1.player.getTileY() - iy) < 3)
continue;
var key = new Vector2(ix, iy);
if (Game1.currentLocation.terrainFeatures.ContainsKey(key))
Game1.currentLocation.terrainFeatures.Remove(key);
Game1.currentLocation.terrainFeatures.Add(key, new CaveWall());
Game1.currentLocation.terrainFeatures[key] = new CaveWall();
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions CookingSkill/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ public static bool OnCook(CraftingRecipe recipe, Item item, List<Chest> addition
{
if (recipe.isCookingRecipe && item is SObject obj)
{
int amtCrafted = 0;
if (Game1.player.recipesCooked.ContainsKey(obj.ParentSheetIndex))
{
amtCrafted = Game1.player.recipesCooked[obj.ParentSheetIndex];
}
Random rand = new Random((int)(Game1.stats.daysPlayed + Game1.uniqueIDForThisGame + (uint)obj.ParentSheetIndex + (uint)amtCrafted));
if (!Game1.player.recipesCooked.TryGetValue(obj.ParentSheetIndex, out int timesCooked))
timesCooked = 0;

Random rand = new Random((int)(Game1.stats.daysPlayed + Game1.uniqueIDForThisGame + (uint)obj.ParentSheetIndex + (uint)timesCooked));

obj.Edibility = (int)(obj.Edibility * Mod.GetEdibilityMultiplier());

Expand Down
20 changes: 8 additions & 12 deletions CustomBuildings/Framework/BuildingData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,18 @@ public class Ingredient

public string LocalizedName()
{
var currLang = LocalizedContentManager.CurrentLanguageCode;
if (currLang == LocalizedContentManager.LanguageCode.en)
return this.Name;
if (this.NameLocalization == null || !this.NameLocalization.ContainsKey(currLang.ToString()))
return this.Name;
return this.NameLocalization[currLang.ToString()];
var lang = LocalizedContentManager.CurrentLanguageCode;
return lang != LocalizedContentManager.LanguageCode.en && this.NameLocalization != null && this.NameLocalization.TryGetValue(lang.ToString(), out string localization)
? localization
: this.Name;
}

public string LocalizedDescription()
{
var currLang = LocalizedContentManager.CurrentLanguageCode;
if (currLang == LocalizedContentManager.LanguageCode.en)
return this.Description;
if (this.DescriptionLocalization == null || !this.DescriptionLocalization.ContainsKey(currLang.ToString()))
return this.Description;
return this.DescriptionLocalization[currLang.ToString()];
var lang = LocalizedContentManager.CurrentLanguageCode;
return lang != LocalizedContentManager.LanguageCode.en && this.DescriptionLocalization != null && this.DescriptionLocalization.TryGetValue(lang.ToString(), out string localization)
? localization
: this.Description;
}

public string BlueprintString()
Expand Down
4 changes: 2 additions & 2 deletions CustomBuildings/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ private void OnWarped(object sender, WarpedEventArgs e)
var b = farm.buildings[i];

// This is probably a new building if it hasn't been converted yet.
if (this.Buildings.ContainsKey(b.buildingType.Value) && !(b is Coop))
if (this.Buildings.TryGetValue(b.buildingType.Value, out BuildingData buildingData) && !(b is Coop))
{
farm.buildings[i] = new Coop(new BluePrint(b.buildingType), new Vector2(b.tileX, b.tileY));
farm.buildings[i].indoors.Value = b.indoors.Value;
farm.buildings[i].load();
(farm.buildings[i].indoors.Value as AnimalHouse).animalLimit.Value = this.Buildings[b.buildingType.Value].MaxOccupants;
(farm.buildings[i].indoors.Value as AnimalHouse).animalLimit.Value = buildingData.MaxOccupants;
}
}
}
Expand Down
Loading

0 comments on commit 7d05023

Please sign in to comment.