Skip to content

Commit

Permalink
Code refactoring: Use explicit type instead of var
Browse files Browse the repository at this point in the history
Code refactoring: Use explicit type instead of var
  • Loading branch information
0x7c13 committed Sep 9, 2024
1 parent fb814e4 commit 991bb68
Show file tree
Hide file tree
Showing 133 changed files with 1,197 additions and 1,190 deletions.
10 changes: 5 additions & 5 deletions Assets/Scripts/Editor/GameVariantSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void SwitchToPal3()
#elif PAL3A
[MenuItem("PAL3A/Switch Variant/PAL3", true)]
#endif
static bool ValidateSwitchToPal3()
public static bool ValidateSwitchToPal3()
{
return !SymbolsHelper.HasSymbol("PAL3");
}
Expand All @@ -59,7 +59,7 @@ public static void SwitchToPal3A()
#elif PAL3A
[MenuItem("PAL3A/Switch Variant/PAL3A", true)]
#endif
static bool ValidateSwitchToPal3A()
public static bool ValidateSwitchToPal3A()
{
return !SymbolsHelper.HasSymbol("PAL3A");
}
Expand All @@ -75,14 +75,14 @@ private static void ApplyPlayerSettingsForVariant(string appName)
$"{GameConstants.AppIdentifierPrefix}.{appName}");
}

var gameIconPath = $"UI/game-icon-{appName}";
var gameIcon = Resources.Load<Texture2D>(gameIconPath);
string gameIconPath = $"UI/game-icon-{appName}";
Texture2D gameIcon = Resources.Load<Texture2D>(gameIconPath);
if (gameIcon == null) throw new Exception($"Game icon not found: {gameIconPath}");

foreach (NamedBuildTarget buildTarget in SymbolsHelper.GetAllSupportedNamedBuildTargets())
{
// Set app icon
var iconSizes = PlayerSettings.GetIconSizes(buildTarget, IconKind.Application);
int[] iconSizes = PlayerSettings.GetIconSizes(buildTarget, IconKind.Application);
PlayerSettings.SetIcons(buildTarget,
Enumerable.Repeat(gameIcon, iconSizes.Length).ToArray(),
IconKind.Application);
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/Editor/SourceGenerator/Base/CodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public void Write(string text)

private void WriteIndentInternal()
{
for (var i = 0; i < _indentLevel; i++)
for (int i = 0; i < _indentLevel; i++)
{
for (var j = 0; j < SpacesPerIndentLevel; j++)
for (int j = 0; j < SpacesPerIndentLevel; j++)
{
Buffer.Append(' ');
}
Expand All @@ -63,7 +63,7 @@ private void WriteIndentInternal()

public void WriteIndent()
{
for (var i = 0; i < SpacesPerIndentLevel; i++) Buffer.Append(' ');
for (int i = 0; i < SpacesPerIndentLevel; i++) Buffer.Append(' ');
}
}
}
4 changes: 2 additions & 2 deletions Assets/Scripts/Editor/SourceGenerator/Base/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class Utility
public static string GetMethodArgumentDefinitionListAsString(PropertyInfo[] properties)
{
StringBuilder argListStrBuilder = new ();
for (var i = 0; i < properties.Length; i++)
for (int i = 0; i < properties.Length; i++)
{
argListStrBuilder.Append(properties[i].PropertyType);
argListStrBuilder.Append(" ");
Expand All @@ -29,7 +29,7 @@ public static string GetMethodArgumentDefinitionListAsString(PropertyInfo[] prop
public static string GetMethodArgumentListAsString(PropertyInfo[] properties)
{
StringBuilder argListStrBuilder = new ();
for (var i = 0; i < properties.Length; i++)
for (int i = 0; i < properties.Length; i++)
{
argListStrBuilder.Append(ToLowerFirstChar(properties[i].Name));
if (i < properties.Length - 1) argListStrBuilder.Append(", ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void GenerateSourceClass(CodeWriter writer, string className, string name

Debug.Log($"[{nameof(ConsoleCommandsAutoGen<TCommand>)}] Found {commands.Length} available console commands");

for (var i = 0; i < commands.Length; i++)
for (int i = 0; i < commands.Length; i++)
{
string commandName = commands[i].Name.Replace("Command", string.Empty);
PropertyInfo[] properties = commands[i].GetProperties();
Expand Down
20 changes: 11 additions & 9 deletions Assets/Scripts/Editor/SourceGeneratorEditorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ namespace Editor

public static class SourceGeneratorEditorOptions
{
private const string NameSpace = "Pal3.Game.Command";
private const string ClassName = "ConsoleCommands";
private const string OutputFolderPath = "Assets/Scripts/Pal3.Game/Command/";

#if PAL3
private const string GAME_VARIANT_SYMBOL = "PAL3";
private static string OutputFileName = "ConsoleCommands.PAL3.g.cs";
private const string OutputFileName = "ConsoleCommands.PAL3.g.cs";
#elif PAL3A
private const string GAME_VARIANT_SYMBOL = "PAL3A";
private static string OutputFileName = "ConsoleCommands.PAL3A.g.cs";
private const string OutputFileName = "ConsoleCommands.PAL3A.g.cs";
#endif

#if PAL3
Expand All @@ -30,11 +34,9 @@ public static class SourceGeneratorEditorOptions
#endif
public static void GenerateConsoleCommands()
{
var writePath = $"Assets/Scripts/Pal3.Game/Command/{OutputFileName}";
var nameSpace = "Pal3.Game.Command";
var className = "ConsoleCommands";
string writePath = $"{OutputFolderPath}{OutputFileName}";
ISourceGenerator sourceGenerator = new ConsoleCommandsAutoGen<ICommand>();
GenerateSourceInternal(OutputFileName, writePath, className, nameSpace, sourceGenerator, true);
GenerateSourceInternal(OutputFileName, writePath, ClassName, NameSpace, sourceGenerator, true);
}

private static void GenerateSourceInternal(string fileName,
Expand All @@ -52,7 +54,7 @@ private static void GenerateSourceInternal(string fileName,

Debug.Log($"[{nameof(SourceGeneratorEditorOptions)}] Generating source file: " + writePath);

var writer = new CodeWriter
CodeWriter writer = new()
{
Buffer = new StringBuilder(),
SpacesPerIndentLevel = 4,
Expand All @@ -61,10 +63,10 @@ private static void GenerateSourceInternal(string fileName,
sourceGenerator.GenerateSourceClass(writer, className, nameSpace);

// Final output string
var output = $"#if {GAME_VARIANT_SYMBOL}\n\n{writer.Buffer}\n#endif";
string output = $"#if {GAME_VARIANT_SYMBOL}\n\n{writer.Buffer}\n#endif";

// Write to file
using StreamWriter sw = new StreamWriter(writePath);
using StreamWriter sw = new(writePath);
sw.Write(output);

Debug.Log($"[{nameof(SourceGeneratorEditorOptions)}] {fileName} generated.");
Expand Down
11 changes: 6 additions & 5 deletions Assets/Scripts/Editor/SymbolsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Editor
{
using System.Linq;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Build;

Expand Down Expand Up @@ -58,8 +59,8 @@ public static void AddSymbol(string symbolToAdd)
{
foreach (BuildTargetGroup targetGroup in GetAllSupportedTargetGroups())
{
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
var allDefines = defines.Split(';').ToList();
string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
List<string> allDefines = defines.Split(';').ToList();
if (!allDefines.Any(_ => string.Equals(_, symbolToAdd)))
{
allDefines.Add(symbolToAdd);
Expand All @@ -74,9 +75,9 @@ public static void RemoveSymbol(string symbolToDelete)
{
foreach (BuildTargetGroup targetGroup in GetAllSupportedTargetGroups())
{
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
var allDefines = defines.Split(';').ToList();
var newDefines = allDefines
string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
List<string> allDefines = defines.Split(';').ToList();
List<string> newDefines = allDefines
.Where(_ => !string.Equals(_, symbolToDelete)).ToList();
newDefines.Sort();
PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup,
Expand Down
12 changes: 6 additions & 6 deletions Assets/Scripts/Engine/Animation/CoreAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public static IEnumerator EnumerateValueAsync(float from,
yield break;
}

var timePast = 0f;
float timePast = 0f;
while (timePast < duration && !cancellationToken.IsCancellationRequested)
{
var newValue = Lerp(from, to, GetInterpolationRatio(timePast / duration, curveType));
float newValue = Lerp(from, to, GetInterpolationRatio(timePast / duration, curveType));
onValueChanged?.Invoke(newValue);
timePast += GameTimeProvider.Instance.DeltaTime;
yield return null;
Expand All @@ -95,7 +95,7 @@ public static IEnumerator MoveAsync(this ITransform target,
{
Vector3 oldPosition = target.Position;

var timePast = 0f;
float timePast = 0f;
while (timePast < duration && !target.IsNativeObjectDisposed && !cancellationToken.IsCancellationRequested)
{
target.Position = oldPosition + (toPosition - oldPosition) *
Expand Down Expand Up @@ -139,10 +139,10 @@ public static IEnumerator OrbitAroundCenterPointAsync(this ITransform target,
float distanceDelta,
CancellationToken cancellationToken = default)
{
var distance = Vector3.Distance(target.Position, centerPoint);
float distance = Vector3.Distance(target.Position, centerPoint);
Quaternion startRotation = target.Rotation;

var timePast = 0f;
float timePast = 0f;
while (timePast < duration && !target.IsNativeObjectDisposed && !cancellationToken.IsCancellationRequested)
{
Quaternion newRotation = Quaternion.Slerp(startRotation, toRotation,
Expand Down Expand Up @@ -174,7 +174,7 @@ public static IEnumerator RotateAsync(this ITransform target,
{
Quaternion startRotation = target.Rotation;

var timePast = 0f;
float timePast = 0f;
while (timePast < duration && !target.IsNativeObjectDisposed && !cancellationToken.IsCancellationRequested)
{
Quaternion rotation = Quaternion.Slerp(startRotation, toRotation,
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Engine/Core/Implementation/GameEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ public T[] GetComponentsInChildren<T>() where T : class

public T GetOrAddComponent<T>() where T : class
{
var component = GetComponent<T>();
T component = GetComponent<T>();

// Since ?? operation does not work well with UnityObject
// so I have to use the old fashion here checking if it is null.
// So I have to use the old fashion here checking if it is null.
if (component as UnityEngine.Object == null)
{
component = AddComponent<T>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ITexture2D CreateTexture(int width, int height, byte[] rgba32DataBuffer)
throw new ArgumentException("rgba32DataBuffer.Length < width * height * 4");
}

var texture = new Texture2D(width, height, TextureFormat.RGBA32, mipChain: false);
Texture2D texture = new Texture2D(width, height, TextureFormat.RGBA32, mipChain: false);
texture.LoadRawTextureData(rgba32DataBuffer);
texture.Apply(updateMipmaps: false);
return new UnityTexture2D(texture);
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Engine/DataLoader/DxtTextureLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public void Load(byte[] data, out bool hasAlphaChannel)
{
if (_rawRgbaDataBuffer != null) throw new Exception("DXT texture already loaded");

using var stream = new MemoryStream(data);
using var headerReader = new BinaryReader(stream);
using MemoryStream stream = new(data);
using BinaryReader headerReader = new(stream);

if (new string(headerReader.ReadChars(4)) != "DDS ")
{
Expand Down
28 changes: 14 additions & 14 deletions Assets/Scripts/Engine/Extensions/UnityPrimitivesConvertor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public static UnityEngine.Vector3[] ToUnityPositions(this GameBoxVector3[] gameB
float scale = GameBoxUnitToUnityUnit)
{
if (gameBoxPositions == null) return null;
var unityPositions = new UnityEngine.Vector3[gameBoxPositions.Length];
for (var i = 0; i < gameBoxPositions.Length; i++)
UnityEngine.Vector3[] unityPositions = new UnityEngine.Vector3[gameBoxPositions.Length];
for (int i = 0; i < gameBoxPositions.Length; i++)
{
unityPositions[i] = ToUnityVector3(gameBoxPositions[i], scale);
}
Expand All @@ -49,7 +49,7 @@ public static void ToUnityPositions(this GameBoxVector3[] gameBoxPositions,
throw new ArgumentException("gameBoxPositions and unityPositionsBuffer must be non-null and have the same length");
}

for (var i = 0; i < gameBoxPositions.Length; i++)
for (int i = 0; i < gameBoxPositions.Length; i++)
{
unityPositionsBuffer[i] = ToUnityVector3(gameBoxPositions[i], scale);
}
Expand Down Expand Up @@ -148,8 +148,8 @@ public static UnityEngine.Vector3 ToUnityNormal(this GameBoxVector3 gameBoxNorma
public static UnityEngine.Vector3[] ToUnityNormals(this GameBoxVector3[] gameBoxNormals)
{
if (gameBoxNormals == null) return null;
var unityNormals = new UnityEngine.Vector3[gameBoxNormals.Length];
for (var i = 0; i < gameBoxNormals.Length; i++)
UnityEngine.Vector3[] unityNormals = new UnityEngine.Vector3[gameBoxNormals.Length];
for (int i = 0; i < gameBoxNormals.Length; i++)
{
unityNormals[i] = ToUnityNormal(gameBoxNormals[i]);
}
Expand All @@ -166,7 +166,7 @@ public static void ToUnityNormals(this GameBoxVector3[] gameBoxNormals,
throw new ArgumentException("gameBoxNormals and unityNormalsBuffer must be non-null and have the same length");
}

for (var i = 0; i < gameBoxNormals.Length; i++)
for (int i = 0; i < gameBoxNormals.Length; i++)
{
unityNormalsBuffer[i] = ToUnityNormal(gameBoxNormals[i]);
}
Expand Down Expand Up @@ -248,8 +248,8 @@ private static UnityEngine.Vector3 ToUnityVector3(this GameBoxVector3 gameBoxVec
public static int[] ToUnityTriangles(this int[] gameBoxTriangles)
{
if (gameBoxTriangles == null) return null;
var unityTriangles = new int[gameBoxTriangles.Length];
for (var i = 0; i < gameBoxTriangles.Length; i++)
int[] unityTriangles = new int[gameBoxTriangles.Length];
for (int i = 0; i < gameBoxTriangles.Length; i++)
{
unityTriangles[i] = gameBoxTriangles[gameBoxTriangles.Length - 1 - i];
}
Expand All @@ -271,7 +271,7 @@ public static void ToUnityTriangles(this int[] gameBoxTriangles, int[] unityTria
throw new ArgumentException("gameBoxTriangles and unityTrianglesBuffer must be non-null and have the same length");
}

for (var i = 0; i < gameBoxTriangles.Length; i++)
for (int i = 0; i < gameBoxTriangles.Length; i++)
{
unityTrianglesBuffer[i] = gameBoxTriangles[gameBoxTriangles.Length - 1 - i];
}
Expand Down Expand Up @@ -309,8 +309,8 @@ public static GameBoxVector2Int ToGameBoxVector2Int(this UnityEngine.Vector2Int
public static UnityEngine.Vector2[] ToUnityVector2s(this GameBoxVector2[] gameBoxVector2s)
{
if (gameBoxVector2s == null) return null;
var unityVector2s = new UnityEngine.Vector2[gameBoxVector2s.Length];
for (var i = 0; i < gameBoxVector2s.Length; i++)
UnityEngine.Vector2[] unityVector2s = new UnityEngine.Vector2[gameBoxVector2s.Length];
for (int i = 0; i < gameBoxVector2s.Length; i++)
{
unityVector2s[i] = gameBoxVector2s[i].ToUnityVector2();
}
Expand All @@ -326,7 +326,7 @@ public static void ToUnityVector2s(this GameBoxVector2[] gameBoxVector2s, UnityE
throw new ArgumentException("gameBoxVector2s and unityVector2sBuffer must be non-null and have the same length");
}

for (var i = 0; i < gameBoxVector2s.Length; i++)
for (int i = 0; i < gameBoxVector2s.Length; i++)
{
unityVector2sBuffer[i] = gameBoxVector2s[i].ToUnityVector2();
}
Expand Down Expand Up @@ -358,8 +358,8 @@ public static UnityEngine.Color32 ToUnityColor32(this Color32 gameBoxColor32)
public static UnityEngine.Color32[] ToUnityColor32s(this Color32[] gameBoxColor32s)
{
if (gameBoxColor32s == null) return null;
var unityColor32s = new UnityEngine.Color32[gameBoxColor32s.Length];
for (var i = 0; i < gameBoxColor32s.Length; i++)
UnityEngine.Color32[] unityColor32s = new UnityEngine.Color32[gameBoxColor32s.Length];
for (int i = 0; i < gameBoxColor32s.Length; i++)
{
unityColor32s[i] = gameBoxColor32s[i].ToUnityColor32();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public IEnumerator PlayAnimationAsync(int loopCount)

_animationCts.Cancel();
_animationCts = new CancellationTokenSource();
var cancellationToken = _animationCts.Token;
CancellationToken cancellationToken = _animationCts.Token;

_spriteRenderer.enabled = true;

Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Engine/UI/RectTransformMaxWidthLimiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private void OnEnable()

private void Update()
{
var currentRatio = (float) Screen.width / Screen.height;
float currentRatio = (float) Screen.width / Screen.height;
_transform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, currentRatio > thresholdRatio
? _transform.rect.height * thresholdRatio - padding
: _transform.rect.height * currentRatio - padding);
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Engine/Utilities/GridOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class GridOverlay : MonoBehaviour
public Color mainColor = new Color(0f, 1f, 0f, 1f);
public Color subColor = new Color(0f, 0.5f, 0f, 1f);

void CreateLineMaterial()
private void CreateLineMaterial()
{
if (!_lineMaterial)
{
Expand All @@ -43,7 +43,7 @@ void CreateLineMaterial()
}
}

void OnPostRender()
private void OnPostRender()
{
CreateLineMaterial();

Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Engine/Utilities/Singleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static T Instance

if (_instance != null) return _instance;

var singletonObj = new GameObject
GameObject singletonObj = new()
{
name = typeof(T).ToString()
};
Expand Down
Loading

0 comments on commit 991bb68

Please sign in to comment.