Skip to content

Commit dd696a3

Browse files
committed
remove unsafe use cases of First()
1 parent 052f381 commit dd696a3

12 files changed

Lines changed: 162 additions & 72 deletions

File tree

ImperialCommander2/Assets/Scenes/Campaign.unity

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ RectTransform:
672672
- {fileID: 168473805}
673673
- {fileID: 1062896580}
674674
m_Father: {fileID: 565269591}
675-
m_RootOrder: 2
675+
m_RootOrder: 3
676676
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
677677
m_AnchorMin: {x: 0, y: 0}
678678
m_AnchorMax: {x: 1, y: 0}
@@ -2489,7 +2489,7 @@ RectTransform:
24892489
m_LocalScale: {x: 1, y: 1, z: 1}
24902490
m_Children: []
24912491
m_Father: {fileID: 565269591}
2492-
m_RootOrder: 1
2492+
m_RootOrder: 2
24932493
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
24942494
m_AnchorMin: {x: 1, y: 0.5}
24952495
m_AnchorMax: {x: 1, y: 0.5}
@@ -3935,6 +3935,81 @@ CanvasRenderer:
39353935
m_PrefabAsset: {fileID: 0}
39363936
m_GameObject: {fileID: 314288885}
39373937
m_CullTransparentMesh: 1
3938+
--- !u!1 &317078553
3939+
GameObject:
3940+
m_ObjectHideFlags: 0
3941+
m_CorrespondingSourceObject: {fileID: 0}
3942+
m_PrefabInstance: {fileID: 0}
3943+
m_PrefabAsset: {fileID: 0}
3944+
serializedVersion: 6
3945+
m_Component:
3946+
- component: {fileID: 317078554}
3947+
- component: {fileID: 317078556}
3948+
- component: {fileID: 317078555}
3949+
m_Layer: 5
3950+
m_Name: burger icon
3951+
m_TagString: Untagged
3952+
m_Icon: {fileID: 0}
3953+
m_NavMeshLayer: 0
3954+
m_StaticEditorFlags: 0
3955+
m_IsActive: 1
3956+
--- !u!224 &317078554
3957+
RectTransform:
3958+
m_ObjectHideFlags: 0
3959+
m_CorrespondingSourceObject: {fileID: 0}
3960+
m_PrefabInstance: {fileID: 0}
3961+
m_PrefabAsset: {fileID: 0}
3962+
m_GameObject: {fileID: 317078553}
3963+
m_LocalRotation: {x: -0, y: -0, z: -0.7071066, w: 0.70710695}
3964+
m_LocalPosition: {x: 0, y: 0, z: 0}
3965+
m_LocalScale: {x: 1, y: 1, z: 1}
3966+
m_Children: []
3967+
m_Father: {fileID: 565269591}
3968+
m_RootOrder: 1
3969+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: -90}
3970+
m_AnchorMin: {x: 1, y: 0.5}
3971+
m_AnchorMax: {x: 1, y: 0.5}
3972+
m_AnchoredPosition: {x: -50, y: 0}
3973+
m_SizeDelta: {x: 40, y: 40}
3974+
m_Pivot: {x: 0.5, y: 0.5}
3975+
--- !u!114 &317078555
3976+
MonoBehaviour:
3977+
m_ObjectHideFlags: 0
3978+
m_CorrespondingSourceObject: {fileID: 0}
3979+
m_PrefabInstance: {fileID: 0}
3980+
m_PrefabAsset: {fileID: 0}
3981+
m_GameObject: {fileID: 317078553}
3982+
m_Enabled: 1
3983+
m_EditorHideFlags: 0
3984+
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
3985+
m_Name:
3986+
m_EditorClassIdentifier:
3987+
m_Material: {fileID: 0}
3988+
m_Color: {r: 1, g: 1, b: 1, a: 1}
3989+
m_RaycastTarget: 1
3990+
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
3991+
m_Maskable: 1
3992+
m_OnCullStateChanged:
3993+
m_PersistentCalls:
3994+
m_Calls: []
3995+
m_Sprite: {fileID: 21300000, guid: 37d28b141b602ba42b7526f30b129d35, type: 3}
3996+
m_Type: 0
3997+
m_PreserveAspect: 0
3998+
m_FillCenter: 1
3999+
m_FillMethod: 4
4000+
m_FillAmount: 1
4001+
m_FillClockwise: 1
4002+
m_FillOrigin: 0
4003+
m_UseSpriteMesh: 0
4004+
m_PixelsPerUnitMultiplier: 1
4005+
--- !u!222 &317078556
4006+
CanvasRenderer:
4007+
m_ObjectHideFlags: 0
4008+
m_CorrespondingSourceObject: {fileID: 0}
4009+
m_PrefabInstance: {fileID: 0}
4010+
m_PrefabAsset: {fileID: 0}
4011+
m_GameObject: {fileID: 317078553}
4012+
m_CullTransparentMesh: 1
39384013
--- !u!1001 &318651005
39394014
PrefabInstance:
39404015
m_ObjectHideFlags: 0
@@ -7631,6 +7706,7 @@ RectTransform:
76317706
m_LocalScale: {x: 1, y: 1, z: 1}
76327707
m_Children:
76337708
- {fileID: 1912224563}
7709+
- {fileID: 317078554}
76347710
- {fileID: 206754076}
76357711
- {fileID: 51375696}
76367712
m_Father: {fileID: 1062901723}
@@ -21667,7 +21743,7 @@ MonoBehaviour:
2166721743
m_VertexBufferAutoSizeReduction: 0
2166821744
m_useMaxVisibleDescender: 1
2166921745
m_pageToDisplay: 1
21670-
m_margin: {x: 0, y: 0, z: 0, w: 0}
21746+
m_margin: {x: 0, y: 0, z: 63.8479, w: 0}
2167121747
m_isUsingLegacyAnimationComponent: 0
2167221748
m_isVolumetricText: 0
2167321749
m_hasFontAssetChanged: 0

ImperialCommander2/Assets/Scripts/GameCore/DataStore.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ static List<DeploymentCard> GetCardsByTier( List<DeploymentCard> haystack, int t
844844
/// </summary>
845845
public static DeploymentCard GetHero( string id )
846846
{
847-
return heroCards.Concat( globalImportedCharacters.Where( x => x.deploymentCard.characterType == CharacterType.Hero ).Select( x => x.deploymentCard ) ).First( x => x.id == id ) ?? null;
847+
return heroCards.Concat( globalImportedCharacters.Where( x => x.deploymentCard.characterType == CharacterType.Hero ).Select( x => x.deploymentCard ) ).FirstOrDefault( x => x.id == id ) ?? null;
848848
}
849849

850850
/// <summary>
@@ -864,9 +864,9 @@ public static DeploymentCard GetEnemy( string id )
864864
var imports = globalImportedCharacters.Where( x => x.deploymentCard.characterType == CharacterType.Imperial || x.deploymentCard.characterType == CharacterType.Villain ).Select( x => x.deploymentCard );
865865

866866
if ( villainCards.Concat( imports ).Any( x => x.id == id ) )
867-
return villainCards.Where( x => x.id == id ).First();
867+
return villainCards.Where( x => x.id == id ).FirstOr( null );
868868
else if ( deploymentCards.Concat( imports ).Any( x => x.id == id ) )
869-
return deploymentCards.Concat( imports ).Where( x => x.id == id ).First();
869+
return deploymentCards.Concat( imports ).Where( x => x.id == id ).FirstOr( null );
870870
else
871871
return null;
872872
}

ImperialCommander2/Assets/Scripts/GameCore/FileManager.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,12 @@ public static void DeleteCampaign( Guid guid )
232232
List<string> filenames = di.GetFiles().Where( file => file.Extension == ".json" ).Select( x => x.Name ).ToList();
233233
if ( filenames.Contains( $"{guid}.json" ) )
234234
{
235-
var file = filenames.Where( x => x.Contains( $"{guid}.json" ) ).First();
235+
var file = filenames.Where( x => x.Contains( $"{guid}.json" ) ).FirstOr( null );
236+
if ( file == null )
237+
{
238+
Utils.LogWarning( $"DeleteCampaign()::Could not find campaign file with GUID: {guid}" );
239+
return;
240+
}
236241
File.Delete( Path.Combine( campaignPath, file ) );
237242
}
238243
}

ImperialCommander2/Assets/Scripts/GameCore/SagaGameVars.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public DeploymentGroupOverride CreateDeploymentOverride( string id = "" )
102102
else if ( !string.IsNullOrEmpty( id ) )
103103
{
104104
if ( dgOverrides.Any( x => x.ID == id ) )
105-
return dgOverrides.Where( x => x.ID == id ).First();
105+
return dgOverrides.Where( x => x.ID == id ).FirstOr( null );
106106
else
107107
{
108108
var ovrd = new DeploymentGroupOverride( id );
@@ -116,7 +116,7 @@ public DeploymentGroupOverride CreateDeploymentOverride( string id = "" )
116116
public DeploymentGroupOverride CreateCustomDeploymentOverride( CustomEnemyDeployment ced )
117117
{
118118
if ( dgOverrides.Any( x => x.ID == ced.enemyGroupData.cardID ) )
119-
return dgOverrides.Where( x => x.ID == ced.enemyGroupData.cardID ).First();
119+
return dgOverrides.Where( x => x.ID == ced.enemyGroupData.cardID ).FirstOr( null );
120120
else
121121
{
122122
var ovrd = new DeploymentGroupOverride( ced.enemyGroupData.cardID );

ImperialCommander2/Assets/Scripts/Saga/Managers/TileManager.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void InstantiateTiles( List<MapSection> sections )
5959
{
6060
var t = Instantiate( tilePrefab, transform );
6161
TileRenderer tileRenderer = t.GetComponent<TileRenderer>();
62-
t.GetComponent<TileRenderer>().LoadTile( mt, tileDescriptors.Where( x => x.expansion == mt.expansion.ToString() && x.id.ToString() == mt.tileID ).First() );
62+
t.GetComponent<TileRenderer>().LoadTile( mt, tileDescriptors.Where( x => x.expansion == mt.expansion.ToString() && x.id.ToString() == mt.tileID ).FirstOr( null ) );
6363
mt.tileRenderer = tileRenderer;
6464
}
6565
}
@@ -87,7 +87,10 @@ public void CamToTile( Guid tileID, bool immediate = false, Action callback = nu
8787

8888
public void CamToSection( Guid guid )
8989
{
90-
int idx = mapSections.IndexOf( mapSections.Where( x => x.GUID == guid ).First() );
90+
var selectedSection = mapSections.Where( x => x.GUID == guid ).FirstOr( null );
91+
if ( selectedSection == null )
92+
return;
93+
int idx = mapSections.IndexOf( selectedSection );
9194
CamToSection( idx );
9295
}
9396

ImperialCommander2/Assets/Scripts/Saga/MissionModels/TileRenderer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public class TileRenderer : MonoBehaviour
1919

2020
public void LoadTile( MapTile t, TileDescriptor td )
2121
{
22+
if ( t == null || td == null )
23+
{
24+
Utils.LogWarning( "LoadTile()::MapTile or TileDescriptor is null" );
25+
return;
26+
}
2227
mapTile = t;
2328
tileDescriptor = td;
2429
//Debug.Log( "LoadTile()::" + mapTile.tileID );

ImperialCommander2/Assets/Scripts/Screens/CampaignManager.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ private void InitUI()
228228
Dictionary<string, string> items = new Dictionary<string, string>();
229229
foreach ( var campaignItem in sagaCampaign.campaignItems )
230230
{
231-
items.Add( campaignItem, SagaCampaign.campaignDataItems.First( x => x.id == campaignItem ).name );
231+
if ( SagaCampaign.campaignDataItems.Any( x => x.id == campaignItem ) )
232+
items.Add( campaignItem, SagaCampaign.campaignDataItems.First( x => x.id == campaignItem ).name );
232233
}
233234
foreach ( var item in items.OrderBy( x => x.Value ) )
234235
AddItemToUI( sagaCampaign.GetItemFromID( item.Key ) );
@@ -249,7 +250,8 @@ private void InitUI()
249250
Dictionary<string, string> rewards = new Dictionary<string, string>();
250251
foreach ( var campaignReward in sagaCampaign.campaignRewards )
251252
{
252-
rewards.Add( campaignReward, SagaCampaign.campaignDataRewards.First( x => x.id == campaignReward ).name );
253+
if ( SagaCampaign.campaignDataRewards.Any( x => x.id == campaignReward ) )
254+
rewards.Add( campaignReward, SagaCampaign.campaignDataRewards.First( x => x.id == campaignReward ).name );
253255
}
254256
foreach ( var reward in rewards.OrderBy( x => x.Value ) )
255257
AddRewardToUI( sagaCampaign.GetRewardFromID( reward.Key ) );
@@ -323,9 +325,18 @@ void AddVillainToUI( DeploymentCard v )
323325

324326
void AddItemToUI( CampaignItem item, bool showCoinIcon = false )
325327
{
328+
if ( item == null )
329+
{
330+
Utils.LogWarning( $"AddItemToUI()::item is null" );
331+
return;
332+
}
333+
326334
List<string> items = new List<string>();
327335
foreach ( var campaignItem in sagaCampaign.campaignItems )
328-
items.Add( SagaCampaign.campaignDataItems.First( x => x.id == campaignItem ).name );
336+
{
337+
if ( SagaCampaign.campaignDataItems.Any( x => x.id == campaignItem ) )
338+
items.Add( SagaCampaign.campaignDataItems.First( x => x.id == campaignItem ).name );
339+
}
329340

330341
var index = items.OrderBy( x => x ).ToList().FindIndex( x => x == item.name );
331342

@@ -364,9 +375,18 @@ void AddItemToUI( CampaignItem item, bool showCoinIcon = false )
364375

365376
void AddRewardToUI( CampaignReward item )
366377
{
378+
if ( item == null )
379+
{
380+
Utils.LogWarning( $"AddRewardToUI()::item is null" );
381+
return;
382+
}
383+
367384
List<string> rewards = new List<string>();
368385
foreach ( var campaignReward in sagaCampaign.campaignRewards )
369-
rewards.Add( SagaCampaign.campaignDataRewards.First( x => x.id == campaignReward ).name );
386+
{
387+
if ( SagaCampaign.campaignDataRewards.Any( x => x.id == campaignReward ) )
388+
rewards.Add( SagaCampaign.campaignDataRewards.First( x => x.id == campaignReward ).name );
389+
}
370390

371391
var index = rewards.OrderBy( x => x ).ToList().FindIndex( x => x == item.name );
372392

ImperialCommander2/Assets/Scripts/Screens/CampaignScreen/AddCampaignItemPopup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ public void AddSkill( string heroID, Action<CampaignSkill> callback )
119119

120120
foreach ( var item in skills.Where( x => x.owner == heroID ).OrderBy( x => x.name ).OrderBy( x => x.cost ) )
121121
{
122-
if ( !sagaCampaign.campaignHeroes.Where( x => x.heroID == heroID ).First().campaignSkills.Any( x => x.id == item.id ) )
122+
var hero = sagaCampaign.campaignHeroes.FirstOrDefault( x => x.heroID == heroID );
123+
if ( hero != null && !hero.campaignSkills.Any( x => x.id == item.id ) )
123124
{
124125
var go = Instantiate( itemSkillSelectorPrefab, itemContainer );
125126
go.GetComponent<ItemSkillSelectorPrefab>().Init( item, heroID );

ImperialCommander2/Assets/Scripts/Screens/CampaignScreen/CampaignHeroPrefab.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,28 +115,34 @@ public void AddHeroToUI( CampaignHero hero )
115115
//skills
116116
var skills = SagaCampaign.campaignDataSkills.Concat( DataStore.globalImportedCharacters.Where( x => x.deploymentCard.characterType == CharacterType.Hero ).SelectMany( x => x.heroSkills ) );
117117

118-
foreach ( var skill in campaignHero.campaignSkills.OrderBy(x => x.name) )
118+
foreach ( var skill in campaignHero.campaignSkills.OrderBy( x => x.name ) )
119119
{
120120
var go = Instantiate( listItem, contentContainer );
121-
string s = skills.Where( x => x.id == skill.id ).First().name;
122-
go.GetComponent<CampaignListItemPrefab>().InitSkill( s, ( n ) =>
121+
var selectedSkill = skills.FirstOrDefault( x => x.id == skill.id );
122+
if ( selectedSkill != null )
123123
{
124-
campaignHero.xpAmount += skill.cost;
125-
mWheelHandler.ResetWheeler( hero.xpAmount );
126-
campaignHero.campaignSkills.Remove( skill );
127-
Destroy( go );
128-
} );
124+
go.GetComponent<CampaignListItemPrefab>().InitSkill( selectedSkill.name, ( n ) =>
125+
{
126+
campaignHero.xpAmount += skill.cost;
127+
mWheelHandler.ResetWheeler( hero.xpAmount );
128+
campaignHero.campaignSkills.Remove( skill );
129+
Destroy( go );
130+
} );
131+
}
129132
}
130133
//items
131-
foreach ( var item in campaignHero.campaignItems.OrderBy(x => x.name) )
134+
foreach ( var item in campaignHero.campaignItems.OrderBy( x => x.name ) )
132135
{
133136
var go = Instantiate( listItem, contentContainer );
134-
string s = SagaCampaign.campaignDataItems.Where( x => x.id == item.id ).First().name;
135-
go.GetComponent<CampaignListItemPrefab>().InitItem( s, ( n ) =>
137+
var selectedSkill = skills.FirstOrDefault( x => x.id == item.id );
138+
if ( selectedSkill != null )
136139
{
137-
campaignHero.campaignItems.Remove( item );
138-
Destroy( go );
139-
} );
140+
go.GetComponent<CampaignListItemPrefab>().InitItem( selectedSkill.name, ( n ) =>
141+
{
142+
campaignHero.campaignItems.Remove( item );
143+
Destroy( go );
144+
} );
145+
}
140146
}
141147
}
142148
}

ImperialCommander2/Assets/Scripts/Screens/CampaignScreen/Models/SagaCampaign.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ public static T LoadAsset<T>( string assetName )
240240
}
241241
}
242242

243-
public CampaignItem GetItemFromID( string id ) => campaignDataItems.First( x => x.id == id );
244-
public CampaignSkill GetSkillFromID( string id ) => campaignDataSkills.First( x => x.id == id );
245-
public CampaignReward GetRewardFromID( string id ) => campaignDataRewards.First( x => x.id == id );
243+
public CampaignItem GetItemFromID( string id ) => campaignDataItems.FirstOrDefault( x => x.id == id );
244+
public CampaignSkill GetSkillFromID( string id ) => campaignDataSkills.FirstOrDefault( x => x.id == id );
245+
public CampaignReward GetRewardFromID( string id ) => campaignDataRewards.FirstOrDefault( x => x.id == id );
246246

247247
public string GetCampaignInfo()
248248
{

0 commit comments

Comments
 (0)