Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/build warnings 2024 09 15 #159

Open
wants to merge 31 commits into
base: development
Choose a base branch
from

Conversation

brmassa
Copy link
Contributor

@brmassa brmassa commented Sep 16, 2024

Attacking #158

fixes for:

CS0108: 4
CS4014: 4
CS9191: 3
CS8619: 3
CS0649: 3
CS8765: 2
CS8767: 2
CS8620: 2
CS0659: 1
CS0661: 1
CS8714: 1
CS8607: 1
CS8509: 1
CS1998: 1

from 529 to 467 compilation warnings (not considering DotCast nor Veldrid)

NOTE: If one wants to focus on solving these issues, I suggest to adding
dotnet_diagnostic.<CODE>.severity = error
inside
.editorconfig.
Your IDE might immediately point to errors. It not, try compiling the code and check the log

@brmassa
Copy link
Contributor Author

brmassa commented Sep 16, 2024

CS8629 and some other fixes

@brmassa
Copy link
Contributor Author

brmassa commented Sep 16, 2024

CS8603 and some CS8601

down to 406 warnings

@brmassa
Copy link
Contributor Author

brmassa commented Sep 17, 2024

down to 357 warnings

@michaelsakharov
Copy link
Contributor

michaelsakharov commented Sep 18, 2024

It might make more sense to disable the Readonly warning.
As there's a ton of cases with the Readonly's where it doesn't make a whole lot of sense.
Including a lot of public API intended for end users to change in runtime
Or ones that frequently change via Reflection using the Inspector in the editor

For example almost all the Components/Monobehaviour shouldn't have readonly fields, since the whole point of them are settings for the components.

@brmassa
Copy link
Contributor Author

brmassa commented Sep 18, 2024

Understand the point. Some thoughts:

  • Dictionaries/Lists generally are not recreated but rather manipulated (add, remove items)
  • Fields, unlike Properties, are not meant to be public settable. f327527 only changes fields.
  • The static code analysis shows that any of these fields were changed inside the code. Of course, if there were meant to be used by the user, we could change them to properties.

In general, readonly allow the compiler to make A LOT of optimizations and also to raise aware when one tries to change them.

Paths forward:

  1. Maintain the changes and revert case-by-case (when ones code depend on them)
  2. Revert some classes
  3. Revert all the commit

…l because it will be the index of a dictionary
…bset. just check if `subAsset` is not null
@brmassa brmassa force-pushed the fix/build-warnings-2024-09-15 branch from 3e2af96 to 100488f Compare September 18, 2024 18:35
@michaelsakharov
Copy link
Contributor

The issue with switching to Properties is that moves away from the Unity API. Unity doesn't really even support properties-only fields, our serializer also doesn't handle them properly and it would be a reasonably big change for newcomers to c# who only know c# through Unity.

I think the read-only one makes sense to remove for the time being, What do you think @sinnwrig

@PaperPrototype
Copy link
Contributor

hehe yeah im one of those unity C# only people :P

@PaperPrototype
Copy link
Contributor

One thought I have is maybe not worrying about Unity only devs? We would only be perpetuating the problem.

…r reassigned (typical for collections)"

This reverts commit aca944c.
@brmassa
Copy link
Contributor Author

brmassa commented Sep 19, 2024

It's problem going to bite us in the future, but for the time being, I'm reverting it. There are still a lot (200+) other warning to attack.

@michaelsakharov
Copy link
Contributor

michaelsakharov commented Sep 19, 2024

One thought I have is maybe not worrying about Unity only devs? We would only be perpetuating the problem.

I mean a huge part of prowl is to be a unity alternative, so it's main audience by far will be unity devs. On top of the fact that it's not really a huge problem to perpetuate in the first place. Its less a problem and more a design choice in my opinion.

@brmassa
Copy link
Contributor Author

brmassa commented Sep 19, 2024

ok. Commit reverted.

I believe Unity did some choices back in old days due lack of resources. Joachim (Unity ttech chief back then) repeatedly said there are several design mistakes that now they kinda have to live with.

Their serializer is sub-par. Sirenix (Odin) serializer is much more capable.

Using public mutable fields is a very bad practice. Basically all Static Analyzers will complain: Visual Studio, Rider, Codacy, Qodana, etc.

Sicne drag-and-drop replacement is virtually impossible, I believe we could have peace of mind on taking some things in other direction. As long it's well documented and explained to users.

@sinnwrig
Copy link
Contributor

The issue with switching to Properties is that moves away from the Unity API. Unity doesn't really even support properties-only fields, our serializer also doesn't handle them properly and it would be a reasonably big change for newcomers to c# who only know c# through Unity.

I think the read-only one makes sense to remove for the time being, What do you think @sinnwrig

The readonly warnings should only be appearing because of C# style recommendations, correct? If so, it would probably be feasible to revert the warnings back to suggestions in the .editorconfig, since the compiler can't be aware of how a user and the serializer might interact with it. On top of that, the warning seems a bit like premature optimization, since the performance boost gained by setting fields to readonly is negligible in most cases unless it's in perf. critical code, in which case it can be optimized manually later down the line.

@@ -168,7 +169,7 @@ public static void Update(bool doUnload = true, bool forceCacheUpdate = false)
cacheModified = true;
bool hasMeta = assetPathToMeta.TryGetValue(file, out var meta);

if (hasMeta)
if (hasMeta && meta is not null)
Copy link
Contributor

@michaelsakharov michaelsakharov Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably throw an error instead of continuing past as Meta being null here while hasMeta is true is pretty bad and should never occur.

Debug.LogError($"No valid importer found for asset: {ToRelativePath(assetFile)}");
return false;
}
// TODO: FIXME: its always not null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they modify the meta file itself for example breaking or removing the importer, then load it into the editor, the Meta file will successfully load however Importer will be null. So this check is required for that edge case. Not sure why the compiler says it cant be null.

@@ -494,8 +496,11 @@ public static bool Reimport(FileInfo assetFile, bool disposeExisting = true)
{
var serializedAsset = SerializedAsset.FromSerializedAsset(serializedAssetPath.FullName);
serializedAsset.Guid = assetGuid;
serializedAsset.Main.AssetID = assetGuid;
serializedAsset.Main.FileID = 0;
if (serializedAsset.Main is not null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should throw an error, Main not existing is an entirely broken asset, main should always exist.

@@ -170,7 +170,7 @@ private void DrawPackageList()
}
}

private void DrawPackageDetails()
private async void DrawPackageDetails()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be mistaken, But does making this method Async mean that the methods that call it continue onwards before this method finishes it's execution?
To my understanding, that's how Async methods work when executed from a synchronous caller. This would be problematic here, as it would change when/how this UI code executes, which could break the UI in unpredictable ways.

I believe the original intentional was calls to InstallPackage are not awaited and can execute at a later time asynchronous, but DrawPackageDetails in particular needs to execute and finish immediately on call for UI.

@@ -12,9 +12,9 @@ public class Double_PropertyDrawer : PropertyDrawer
public override double MinWidth => EditorStylePrefs.Instance.ItemSize * 2;
public override bool OnValueGUI(Gui gui, string ID, Type targetType, ref object? value)
{
double val = (double)value;
double val = value is double v ? v : double.NaN;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NaN here might be a bit dangerous, just 0 or an exception would probably be ideal.
If this were to break and set things to NaN it would set users settings and values to NaN, which could spread and corrupt projects.

protected override int GetCount(Array value) => value.Length;
protected override object GetElement(Array value, int index) => value.GetValue(index);
protected override object GetElement(Array value, int index) => value.GetValue(index)!;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be able to return null, so probably saver to make these methods nullabes. since its just an array GetValue for the inspector anything inside can be null which should be supported by the editor.

@@ -308,7 +307,7 @@ public static void SaveScene()
return;
}

if (AssetDatabase.TryGetFile(scene.AssetID, out var file))
if (AssetDatabase.TryGetFile(scene.AssetID, out var file) && file is not null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably throw an exception if the file is Null, since it means the asset doesn't actually exist, yet its ID is in the database, this suggests a bigger problem with the Asset Database,

@sinnwrig
Copy link
Contributor

sinnwrig commented Sep 19, 2024

ok. Commit reverted.

I believe Unity did some choices back in old days due lack of resources. Joachim (Unity ttech chief back then) repeatedly said there are several design mistakes that now they kinda have to live with.

Their serializer is sub-par. Sirenix (Odin) serializer is much more capable.

Using public mutable fields is a very bad practice. Basically all Static Analyzers will complain: Visual Studio, Rider, Codacy, Qodana, etc.

Sicne drag-and-drop replacement is virtually impossible, I believe we could have peace of mind on taking some things in other direction. As long it's well documented and explained to users.

On the public mutable fields, it seems a bit harsh to remove them completely because of bad design practice, since for many cases, having simple, raw, exposed values without a get/set (even if it's auto-implemented) is useful. If it's possible, it would eventually be nice to support [SerializeField] on property get/set. However, for most cases, something like [field:SerializeField] attribute is good enough to let the serializer get to an auto-implemented property.

In addition, while it might be good in the context of an organizational project, the engine should not be enforcing good practice or style on users, and the decision should ideally be the developer's to follow their own style guides and practices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants