Skip to content

Commit

Permalink
Replaced a lot of ContainsKey calls with TryGetValue calls (OrderOfTh…
Browse files Browse the repository at this point in the history
…ePorcupine#250)

* Replaced a lot of ContainsKey calls with TryGetValue calls

* Stylecop and a little more tweaking

* More stylecop :)

* <3 u stylecop

* Oh how I love you stylecop (But what's with the hungarian?)
  • Loading branch information
NogginBops authored and kd7uiy committed Feb 27, 2019
1 parent b1e96cd commit 4078db1
Show file tree
Hide file tree
Showing 34 changed files with 342 additions and 281 deletions.
6 changes: 3 additions & 3 deletions Assets/Editor/UnitTests/Models/Functions/LuaFunctionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public void Test_LoadScript_BadLua_NoEnd()
{
// This makes sure that the channel lua is activated since we require to to be
// we set it back to the old value afterwards as to not annoy the 'dev'
bool oldVal = true;
if (UnityDebugger.Debugger.Channels.ContainsKey("Lua"))
bool oldVal;
if (UnityDebugger.Debugger.Channels.TryGetValue("Lua", out oldVal) == false)
{
oldVal = UnityDebugger.Debugger.Channels["Lua"];
oldVal = true;
}

UnityDebugger.Debugger.Channels["Lua"] = true;
Expand Down
19 changes: 10 additions & 9 deletions Assets/Scripts/Controllers/InputOutput/SoundController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,12 @@ public void PlaySoundAt(SoundClip clip, Tile tile, string chanGroup = "master",
}

clip.SetCooldown(cooldownTime);
if (!AudioManager.channelGroups.ContainsKey(chanGroup))
ChannelGroup channelGroup;
if (AudioManager.channelGroups.TryGetValue(chanGroup, out channelGroup) == false)
{
chanGroup = "master";
channelGroup = AudioManager.channelGroups["master"];
}

ChannelGroup channelGroup = AudioManager.channelGroups[chanGroup];


FMOD.System soundSystem = AudioManager.SoundSystem;
Channel channel;
soundSystem.playSound(clip.Get(), channelGroup, true, out channel);
Expand Down Expand Up @@ -268,9 +267,10 @@ public void SetVolume(AudioChannel channel, float volume)

public void SetVolume(string channel, float volume)
{
if (AudioManager.channelGroups.ContainsKey(channel))
ChannelGroup group;
if (AudioManager.channelGroups.TryGetValue(channel, out group))
{
AudioManager.channelGroups[channel].setVolume(volume);
group.setVolume(volume);
}
else
{
Expand All @@ -285,10 +285,11 @@ public float GetVolume(AudioChannel channel)

public float GetVolume(string channel)
{
if (AudioManager.channelGroups.ContainsKey(channel))
ChannelGroup group;
if (AudioManager.channelGroups.TryGetValue(channel, out group))
{
float volume = 0;
AudioManager.channelGroups[channel].getVolume(out volume);
group.getVolume(out volume);
return volume;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,13 @@ protected override void OnChanged(Character character)
}
}

if (objectGameObjectMap.ContainsKey(character) == false)
GameObject char_go;
if (objectGameObjectMap.TryGetValue(character, out char_go) == false)
{
UnityDebugger.Debugger.LogError("CharacterSpriteController", "OnCharacterChanged -- trying to change visuals for character not in our map.");
return;
}

GameObject char_go = objectGameObjectMap[character];


char_go.transform.position = new Vector3(character.X, character.Y, character.Z);

if (character.IsSelected)
Expand Down
25 changes: 10 additions & 15 deletions Assets/Scripts/Controllers/Sprites/FurnitureSpriteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,13 @@ protected override void OnCreated(Furniture furniture)
protected override void OnChanged(Furniture furn)
{
// Make sure the furniture's graphics are correct.
if (objectGameObjectMap.ContainsKey(furn) == false)
GameObject furn_go;
if (objectGameObjectMap.TryGetValue(furn, out furn_go) == false)
{
UnityDebugger.Debugger.LogError("FurnitureSpriteController", "OnFurnitureChanged -- trying to change visuals for furniture not in our map.");
return;
}

GameObject furn_go = objectGameObjectMap[furn];


if (furn.HasTypeTag("Door"))
{
// Check to see if we actually have a wall north/south, and if so
Expand Down Expand Up @@ -254,7 +253,8 @@ protected override void OnChanged(Furniture furn)

protected override void OnRemoved(Furniture furn)
{
if (objectGameObjectMap.ContainsKey(furn) == false)
GameObject furn_go;
if (objectGameObjectMap.TryGetValue(furn, out furn_go) == false)
{
UnityDebugger.Debugger.LogError("FurnitureSpriteController", "OnFurnitureRemoved -- trying to change visuals for furniture not in our map.");
return;
Expand All @@ -263,15 +263,9 @@ protected override void OnRemoved(Furniture furn)
furn.Changed -= OnChanged;
furn.Removed -= OnRemoved;
furn.IsOperatingChanged -= OnIsOperatingChanged;
GameObject furn_go = objectGameObjectMap[furn];
objectGameObjectMap.Remove(furn);
GameObject.Destroy(furn_go);

if (childObjectMap.ContainsKey(furn) == false)
{
return;
}


childObjectMap.Remove(furn);
}

Expand All @@ -282,12 +276,13 @@ private void OnIsOperatingChanged(Furniture furniture)
return;
}

if (childObjectMap.ContainsKey(furniture) == false)
FurnitureChildObjects childObjects;
if (childObjectMap.TryGetValue(furniture, out childObjects) == false)
{
return;
}

UpdateIconObjectsVisibility(furniture, childObjectMap[furniture]);
UpdateIconObjectsVisibility(furniture, childObjects);
}

private void UpdateIconObjectsVisibility(Furniture furniture, FurnitureChildObjects statuses)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ protected override void OnCreated(Inventory inventory)
protected override void OnChanged(Inventory inventory)
{
// Make sure the furniture's graphics are correct.
if (objectGameObjectMap.ContainsKey(inventory) == false)
GameObject inventoryGameObject;
if (objectGameObjectMap.TryGetValue(inventory, out inventoryGameObject) == false)
{
UnityDebugger.Debugger.LogError("InventorySpriteController", "OnCharacterChanged -- trying to change visuals for inventory not in our map.");
return;
}

GameObject inventoryGameObject = objectGameObjectMap[inventory];

if (inventory.StackSize > 0)
{
Text text = inventoryGameObject.GetComponentInChildren<Text>();
Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/Controllers/Sprites/JobSpriteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ protected override void OnCreated(Job job)
return;
}

// This is weird why do we have this here?
// OnCreated should not be called twice for a given job?
// This seems like there is a bug hiding somewhere
if (objectGameObjectMap.ContainsKey(job))
{
return;
Expand Down
11 changes: 5 additions & 6 deletions Assets/Scripts/Controllers/Sprites/UtilitySpriteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,28 @@ protected override void OnCreated(Utility utility)
protected override void OnChanged(Utility util)
{
// Make sure the utility's graphics are correct.
if (objectGameObjectMap.ContainsKey(util) == false)
GameObject util_go;
if (objectGameObjectMap.TryGetValue(util, out util_go) == false)
{
UnityDebugger.Debugger.LogError("UtilitySpriteController", "OnUtilityChanged -- trying to change visuals for utility not in our map.");
return;
}

GameObject util_go = objectGameObjectMap[util];


util_go.GetComponent<SpriteRenderer>().sprite = GetSpriteForUtility(util);
util_go.GetComponent<SpriteRenderer>().color = util.Tint;
}

protected override void OnRemoved(Utility util)
{
if (objectGameObjectMap.ContainsKey(util) == false)
GameObject util_go;
if (objectGameObjectMap.TryGetValue(util, out util_go) == false)
{
UnityDebugger.Debugger.LogError("UtilitySpriteController", "OnUtilityRemoved -- trying to change visuals for utility not in our map.");
return;
}

util.Changed -= OnChanged;
util.Removed -= OnRemoved;
GameObject util_go = objectGameObjectMap[util];
objectGameObjectMap.Remove(util);
GameObject.Destroy(util_go);
}
Expand Down
28 changes: 17 additions & 11 deletions Assets/Scripts/Localization/LocalizationTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public static string GetLocalization(string key, FallbackMode fallbackMode, stri
}
}

if (localizationTable.ContainsKey(language) && localizationTable[language].TryGetValue(key, out value))
Dictionary<string, string> languageTable;
if (localizationTable.TryGetValue(language, out languageTable) && languageTable.TryGetValue(key, out value))
{
try
{
Expand All @@ -108,9 +109,9 @@ public static string GetLocalization(string key, FallbackMode fallbackMode, stri
}
}

if (!missingKeysLogged.Contains(key) && key != string.Empty && IsNumber(key))
// If add returns true the key was not present in the HashSet (i.e we just added it and wasn't there before)
if (key != string.Empty && IsNumber(key) && missingKeysLogged.Add(key))
{
missingKeysLogged.Add(key);
UnityDebugger.Debugger.LogWarning("LocalizationTable", string.Format("Translation for {0} in {1} language failed: Key not in dictionary.", key, language));
}

Expand All @@ -127,13 +128,14 @@ public static string GetLocalization(string key, FallbackMode fallbackMode, stri

public static string GetLocalizaitonCodeLocalization(string code)
{
if (localizationConfigurations.ContainsKey(code) == false)
LocalizationData localizationData;
if (localizationConfigurations.TryGetValue(code, out localizationData) == false)
{
UnityDebugger.Debugger.Log("LocalizationTable", "name of " + code + " is not present in config file.");
return code;
}

return localizationConfigurations[code].LocalName;
return localizationData.LocalName;
}

public static void SetLocalization(int lang)
Expand Down Expand Up @@ -310,28 +312,32 @@ private static void LoadLocalizationFile(string path, string localizationCode)
{
try
{
if (localizationTable.ContainsKey(localizationCode) == false)
Dictionary<string, string> localeTable;
if (localizationTable.TryGetValue(localizationCode, out localeTable) == false)
{
localizationTable[localizationCode] = new Dictionary<string, string>();
localeTable = new Dictionary<string, string>();
localizationTable[localizationCode] = localeTable;
}

if (configExists && localizationConfigurations.ContainsKey(localizationCode) == false)
LocalizationData localizationConfig = null;
if (configExists && localizationConfigurations.TryGetValue(localizationCode, out localizationConfig) == false)
{
localizationConfig = null;
UnityDebugger.Debugger.LogError("LocalizationTable", "Language: " + localizationCode + " not defined in localization/config.json");
}

// Only the current and default languages translations will be loaded in memory.
if (localizationCode == DefaultLanguage || localizationCode == currentLanguage)
{
bool rightToLeftLanguage;
if (localizationConfigurations.ContainsKey(localizationCode) == false)
if (localizationConfig == null)
{
UnityDebugger.Debugger.LogWarning("LocalizationTable", "Assuming " + localizationCode + " is LTR");
rightToLeftLanguage = false;
}
else
{
rightToLeftLanguage = localizationConfigurations[localizationCode].isRightToLeft;
rightToLeftLanguage = localizationConfig.isRightToLeft;
}

string[] lines = File.ReadAllLines(path);
Expand All @@ -357,7 +363,7 @@ private static void LoadLocalizationFile(string path, string localizationCode)
keyValuePair[1] = ReverseString(keyValuePair[1]);
}

localizationTable[localizationCode][keyValuePair[0]] = keyValuePair[1];
localeTable[keyValuePair[0]] = keyValuePair[1];
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions Assets/Scripts/Models/Animation/FurnitureAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,19 @@ public void SetFrameIndex(int frameIndex)
/// </summary>
public void SetState(string stateName)
{
if (animations.ContainsKey(stateName) == false)
if (stateName == currentAnimationState)
{
return;
}

if (stateName == currentAnimationState)
SpritenameAnimation animation;
if (animations.TryGetValue(stateName, out animation) == false)
{
return;
}

currentAnimationState = stateName;
currentAnimation = animations[currentAnimationState];
currentAnimation = animation;
currentAnimation.Play();
ShowSprite(currentAnimation.CurrentFrameName);
}
Expand Down
30 changes: 20 additions & 10 deletions Assets/Scripts/Models/Area/AtmosphereComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ public float ThermalEnergy
/// <param name="gasName">Gas you want the pressure of.</param>
public float GetGasAmount(string gasName)
{
return gasses.ContainsKey(gasName) ? gasses[gasName] : 0;
float amount;

if (gasses.TryGetValue(gasName, out amount) == false)
{
amount = 0;
}

return amount;
}

/// <summary>
Expand Down Expand Up @@ -137,18 +144,21 @@ public void SetGas(float newValue)
/// <param name="amount"> The amount to update by. </param>
public void ChangeGas(string gasName, float amount)
{
if (gasses.ContainsKey(gasName) == false)
{
gasses[gasName] = 0;
}

if (gasses[gasName] <= -amount)
float gasAmount;
if (gasses.TryGetValue(gasName, out gasAmount))
{
gasses.Remove(gasName);
if (gasAmount <= -amount)
{
gasses.Remove(gasName);
}
else
{
gasses[gasName] = gasAmount + amount;
}
}
else
else if (amount > 0)
{
gasses[gasName] += amount;
gasses[gasName] = amount;
}

UpdateTotalGas();
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Models/Area/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ public HashSet<Tile> GetBoundaryTiles()
return boundaryTiles;
}

// FIXME: 'Has' methods are generally a bad idea, should be 'TryGet' instead
public bool HasRoomBehavior(string behaviorKey)
{
return RoomBehaviors.ContainsKey(behaviorKey);
Expand Down
15 changes: 7 additions & 8 deletions Assets/Scripts/Models/Buildable/Components/BuildableComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ public static BuildableComponent Deserialize(JToken jtoken)
componentTypes = FindComponentsInAssembly();
}

string componentTypeName = jtoken["Component"]["Type"].ToString();
JToken jcomponent = jtoken["Component"];
string componentTypeName = jcomponent["Type"].ToString();

if (componentTypes.ContainsKey(componentTypeName))
Type t;
if (componentTypes.TryGetValue(componentTypeName, out t))
{
Type t = componentTypes[componentTypeName];
BuildableComponent component = (BuildableComponent)jtoken["Component"].ToObject(t);
BuildableComponent component = (BuildableComponent)jcomponent.ToObject(t);

// need to set name explicitly (not part of deserialization as it's passed in)
component.Type = componentTypeName;
Expand All @@ -133,11 +134,9 @@ public static BuildableComponent FromJson(JToken componentToken)

JProperty componentProperty = (JProperty)componentToken;
string componentTypeName = componentProperty.Name;
if (componentTypes.ContainsKey(componentTypeName))
Type t;
if (componentTypes.TryGetValue(componentTypeName, out t))
{
Type t = componentTypes[componentTypeName];
t.GetType();

BuildableComponent component = (BuildableComponent)componentProperty.Value.ToObject(t);

// need to set name explicitly (not part of deserialization as it's passed in)
Expand Down
Loading

0 comments on commit 4078db1

Please sign in to comment.