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

Use C# 8 and the nullable reference type specification in the adapter #2312

Closed
wants to merge 16 commits into from

Conversation

pleroy
Copy link
Member

@pleroy pleroy commented Aug 28, 2019

No description provided.

Copy link
Member

@eggrobin eggrobin left a comment

Choose a reason for hiding this comment

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

Fix the generator to add ? as appropriate to interface.generated.cs.

ksp_plugin_adapter/class_marshalers.cs Outdated Show resolved Hide resolved
ksp_plugin_adapter/class_marshalers.cs Outdated Show resolved Hide resolved
ksp_plugin_adapter/class_marshalers.cs Show resolved Hide resolved
ksp_plugin_adapter/flight_planner.cs Outdated Show resolved Hide resolved
@@ -77,7 +77,7 @@ class FlightPlanner : SupervisedWindowRenderer {
} else if (UnityEngine.GUILayout.Button("Create flight plan")) {
plugin.FlightPlanCreate(vessel_guid,
plugin.CurrentTime() + 1000,
vessel_.GetTotalMass());
vessel_!.GetTotalMass());
Copy link
Member

Choose a reason for hiding this comment

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

Remove, see above.

ksp_plugin_adapter/flight_planner.cs Show resolved Hide resolved
ksp_plugin_adapter/flight_planner.cs Outdated Show resolved Hide resolved
ksp_plugin_adapter/flight_planner.cs Show resolved Hide resolved
ksp_plugin_adapter/flight_planner.cs Show resolved Hide resolved
ksp_plugin_adapter/flight_planner.cs Show resolved Hide resolved
ksp_plugin_adapter/flight_planner.cs Show resolved Hide resolved
ksp_plugin_adapter/flight_planner.cs Show resolved Hide resolved
ksp_plugin_adapter/flight_planner.cs Show resolved Hide resolved
@@ -21,7 +21,7 @@ internal class DisposableIteratorMarshaller : ICustomMarshaler {
if (managed_object == null) {
return IntPtr.Zero;
}
var disposable_iterator = managed_object as DisposableIterator;
var disposable_iterator = (managed_object as DisposableIterator)!;
Copy link
Member

Choose a reason for hiding this comment

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

(DisposableIterator)managed_object, which checks.

@@ -50,7 +50,7 @@ internal class DisposablePlanetariumMarshaller : ICustomMarshaler {
if (managed_object == null) {
return IntPtr.Zero;
}
var disposable_planetarium = managed_object as DisposablePlanetarium;
var disposable_planetarium = (managed_object as DisposablePlanetarium)!;
Copy link
Member

Choose a reason for hiding this comment

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

(DisposablePlanetarium)managed_object.

private KSP.UI.Screens.DebugToolbar.DebugScreen debug_screen_;
private KSP.UI.Screens.DebugToolbar.Screens.Cheats.HackGravity hack_gravity_;
private KSP.UI.Screens.DebugToolbar.DebugScreen? debug_screen_;
private KSP.UI.Screens.DebugToolbar.Screens.Cheats.HackGravity? hack_gravity_;
private KSP.UI.Screens.DebugToolbar.Screens.Cheats.HackGravity hack_gravity {
Copy link
Member

Choose a reason for hiding this comment

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

The property is also a HackGravity?, because there only is a HackGravity once someone opens the cheats menu; this is why we get the debug_screen_ and search within it.

See also line 420:

if (vessel.loaded && hack_gravity?.toggle.isOn == true) {
reasons.Add("vessel is loaded and gravity is being hacked");
}

@@ -457,7 +457,7 @@ private enum UnityLayers {
}

// Returns false and nulls |texture| if the file does not exist.
private bool LoadTextureIfExists(out UnityEngine.Texture texture,
private bool LoadTextureIfExists(out UnityEngine.Texture? texture,
Copy link
Member

Choose a reason for hiding this comment

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

@@ -789,7 +791,7 @@ private enum UnityLayers {
var normal =
(Vector3d)plugin_.VesselBinormal(active_vessel.id.ToString());

SetNavballVector(navball_.progradeVector, prograde);
SetNavballVector(navball_!.progradeVector, prograde);
Copy link
Member

Choose a reason for hiding this comment

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

Comment why this is not null (I think because RenderNavball set it above; we might want to make it a property like the Krakensbane etc. in the future for clarity).

@@ -1186,7 +1188,7 @@ private enum UnityLayers {
new Origin{reference_part_is_at_origin =
FloatingOrigin.fetch.continuous,
reference_part_is_unmoving =
krakensbane_.FrameVel != Vector3d.zero,
krakensbane!.FrameVel != Vector3d.zero,
Copy link
Member

Choose a reason for hiding this comment

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

Same.

@@ -1603,7 +1606,7 @@ private enum UnityLayers {
// Texture the ball.
navball_changed_ = false;
if (plotting_frame_selector_.target_override) {
set_navball_texture(target_navball_texture_);
set_navball_texture(target_navball_texture_!);
Copy link
Member

Choose a reason for hiding this comment

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

Comment that the textures are set in OnAwake (we could write lazy-load properties for them so that things are clearer).

ConfigNode ephemeris_parameters =
numerics_blueprint.GetAtMostOneNode("ephemeris");
ConfigNode? ephemeris_parameters =
numerics_blueprint!.GetAtMostOneNode("ephemeris");
Copy link
Member

Choose a reason for hiding this comment

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

Is this ! necessary with the null check above?

@@ -2121,7 +2124,7 @@ private enum UnityLayers {
node => node.GetUniqueValue("name"));
BodyProcessor insert_body = body => {
Log.Info("Inserting " + body.name + "...");
if (!name_to_gravity_model.TryGetValue(
if (!name_to_gravity_model!.TryGetValue(
Copy link
Member

Choose a reason for hiding this comment

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

Change lines 2111 .. 2113 from

if (name_to_gravity_model == null) {
  Log.Fatal("Cartesian config without gravity models");
}

to

if (name_to_gravity_model == null) {
  throw Log.Fatal("Cartesian config without gravity models");
}

so that control flow analysis understands that name_to_gravity_model is not null here.

if (name_to_gravity_model?.TryGetValue(
body.name,
out body_gravity_model) == true) {
Log.Info("using custom gravity model");
}
Orbit orbit = unmodified_orbits_.GetValueOrNull(body);
Orbit? orbit = unmodified_orbits_!.GetValueOrNull(body);
Copy link
Member

Choose a reason for hiding this comment

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

Comment that unmodified_orbits_ has been set in OnAwake (we should probably create the dictionary at construction so that unmodified_orbits_ is not null, and just fill it in OnAwake, but we can clean that up later).

@@ -114,7 +114,7 @@ internal abstract class BaseWindowRenderer : ScalingRenderer, IConfigNode {
var old_skin = UnityEngine.GUI.skin;
if (skin_ == null) {
UnityEngine.GUI.skin = null;
skin_ = MakeSkin(UnityEngine.GUI.skin);
skin_ = MakeSkin(UnityEngine.GUI.skin!);
Copy link
Member

Choose a reason for hiding this comment

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

This is a case where we would really need to annotate the engine declarations:
the property UnityEngine.GUI.skin is UnityEngine.GUISkin, not UnityEngine.GUISkin?, but it is [AllowNull] (set accepts null, get never returns null).

@pleroy pleroy closed this Sep 1, 2019
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.

None yet

2 participants