Skip to content

Commit

Permalink
P31 3 immortal spectators fix (#498)
Browse files Browse the repository at this point in the history
* - Better error handling, turrets shouldn't stuck now. Or lesser at least
- Energy fields are no longer mineable by indy turrets

* - Dead robots no more resurrecting by being unable to drop inexistent painting into loot

---------

Co-authored-by: Dolzhukov, Viktor <[email protected]>
  • Loading branch information
Sellafield and Dolzhukov, Viktor authored May 29, 2024
1 parent 220e239 commit 4109c38
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 237 deletions.
128 changes: 97 additions & 31 deletions src/Perpetuum/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public static void ThrowIfZero(this int source, ErrorCodes error)

public static void ThrowIfZero(this int source, Func<Exception> exceptionFactory)
{
source.ThrowIfEqual(0, exceptionFactory);
_ = source.ThrowIfEqual(0, exceptionFactory);
}

public static void ThrowIfZero(long source, Func<Exception> exceptionFactory)
{
source.ThrowIfEqual(0, exceptionFactory);
_ = source.ThrowIfEqual(0, exceptionFactory);
}

public static double ThrowIfZero(this double source, Func<Exception> exceptionFactory)
Expand All @@ -28,136 +28,202 @@ public static double ThrowIfZero(this double source, Func<Exception> exceptionFa
public static void ThrowIfTrue([UsedImplicitly] this bool value, Func<Exception> exceptionFactory)
{
if (value)
{
throw exceptionFactory();
}
}

public static void ThrowIfFalse([UsedImplicitly] this bool value, Func<Exception> exceptionFactory)
{
if (!value)
{
throw exceptionFactory();
}
}

public static T ThrowIfNotType<T>(this object value, ErrorCodes error)
public static T ThrowIfNotType<T>(
this object value,
ErrorCodes error,
[InstantHandle] Action<PerpetuumException> exceptionAction = null)
{
(value is T).ThrowIfFalse(error);
(value is T).ThrowIfFalse(error, exceptionAction);

return (T)value;
}

public static void ThrowIfType<T>(this object actual, ErrorCodes error)
public static void ThrowIfType<T>(
this object actual,
ErrorCodes error,
[InstantHandle] Action<PerpetuumException> exceptionAction = null)
{
(actual is T).ThrowIfTrue(error);
(actual is T).ThrowIfTrue(error, exceptionAction);
}

public static T ThrowIfGreater<T>(this T source, T comparer, ErrorCodes error, [InstantHandle] Action<PerpetuumException> exceptionAction = null) where T : IComparable<T>
public static T ThrowIfGreater<T>(
this T source,
T comparer,
ErrorCodes error,
[InstantHandle] Action<PerpetuumException> exceptionAction = null)
where T : IComparable<T>
{
(source.CompareTo(comparer) > 0).ThrowIfTrue(error, exceptionAction);

return source;
}

public static T ThrowIfGreaterOrEqual<T>(this T source, T comparer, ErrorCodes error, [InstantHandle] Action<PerpetuumException> exceptionAction = null) where T : IComparable<T>
public static T ThrowIfGreaterOrEqual<T>(
this T source,
T comparer,
ErrorCodes error,
[InstantHandle] Action<PerpetuumException> exceptionAction = null)
where T : IComparable<T>
{
(source.CompareTo(comparer) >= 0).ThrowIfTrue(error, exceptionAction);

return source;
}

public static T ThrowIfLess<T>(this T source, T comparer, ErrorCodes error, [InstantHandle] Action<PerpetuumException> exceptionAction = null) where T : IComparable<T>
public static T ThrowIfLess<T>(
this T source,
T comparer,
ErrorCodes error,
[InstantHandle] Action<PerpetuumException> exceptionAction = null)
where T : IComparable<T>
{
ThrowIfTrue(source.CompareTo(comparer) < 0, error, exceptionAction);

return source;
}

public static T ThrowIfLessOrEqual<T>(this T source, T comparer, ErrorCodes error, [InstantHandle] Action<PerpetuumException> exceptionAction = null) where T : IComparable<T>
public static T ThrowIfLessOrEqual<T>(
this T source,
T comparer,
ErrorCodes error,
[InstantHandle] Action<PerpetuumException> exceptionAction = null)
where T : IComparable<T>
{
ThrowIfTrue(source.CompareTo(comparer) <= 0, error, exceptionAction);

return source;
}

public static T ThrowIfEqual<T>(this T actual, T expected, ErrorCodes error, [InstantHandle] Action<PerpetuumException> exceptionAction = null)
public static T ThrowIfEqual<T>(
this T actual,
T expected, ErrorCodes error,
[InstantHandle] Action<PerpetuumException> exceptionAction = null)
{
ThrowIfTrue(Equals(actual, expected), error, exceptionAction);

return actual;
}

public static T ThrowIfNotEqual<T>(this T actual, T expected, ErrorCodes error, [InstantHandle]Action<PerpetuumException> exceptionAction = null)
public static T ThrowIfNotEqual<T>(
this T actual,
T expected,
ErrorCodes error,
[InstantHandle] Action<PerpetuumException> exceptionAction = null)
{
ThrowIfFalse(Equals(actual, expected), error, exceptionAction);

return actual;
}

public static ErrorCodes ThrowIfError(this ErrorCodes error, [InstantHandle] Action<PerpetuumException> exceptionAction = null)
public static ErrorCodes ThrowIfError(
this ErrorCodes error,
[InstantHandle] Action<PerpetuumException> exceptionAction = null)
{
return error.ThrowIfNotEqual(ErrorCodes.NoError, () =>
{
var gex = PerpetuumException.Create(error);
exceptionAction?.Invoke(gex);
return gex;
});
return error.ThrowIfNotEqual(
ErrorCodes.NoError,
() =>
{
PerpetuumException gex = PerpetuumException.Create(error);
exceptionAction?.Invoke(gex);

return gex;
});
}

public static void ThrowIfNotNull(this object o, ErrorCodes error, [InstantHandle] Action<PerpetuumException> exceptionAction)
public static void ThrowIfNotNull(
this object o,
ErrorCodes error,
[InstantHandle] Action<PerpetuumException> exceptionAction)
{
(o != null).ThrowIfTrue(error, exceptionAction);
}

public static void ThrowIfNotNull(this object o, ErrorCodes error)
{
o.ThrowIfNotEqual(null, error);
_ = o.ThrowIfNotEqual(null, error);
}

[NotNull]
public static T ThrowIfNull<T>(this T source, ErrorCodes error, [InstantHandle]Action<PerpetuumException> exceptionAction = null)
public static T ThrowIfNull<T>(
this T source,
ErrorCodes error,
[InstantHandle] Action<PerpetuumException> exceptionAction = null)
{
return source.ThrowIfNull(() =>
{
var gex = PerpetuumException.Create(error);
PerpetuumException gex = PerpetuumException.Create(error);
exceptionAction?.Invoke(gex);

return gex;
});
}

public static void ThrowIfFalse(this bool value, ErrorCodes error, [InstantHandle]Action<PerpetuumException> exceptionAction = null)
public static void ThrowIfFalse(
this bool value,
ErrorCodes error,
[InstantHandle] Action<PerpetuumException> exceptionAction = null)
{
value.ThrowIfFalse(() =>
{
var gex = PerpetuumException.Create(error);
PerpetuumException gex = PerpetuumException.Create(error);
exceptionAction?.Invoke(gex);

return gex;
});
}

public static void ThrowIfTrue(this bool value, ErrorCodes error, [InstantHandle]Action<PerpetuumException> exceptionAction = null)
public static void ThrowIfTrue(
this bool value,
ErrorCodes error,
[InstantHandle] Action<PerpetuumException> exceptionAction = null)
{
value.ThrowIfTrue(() =>
{
var gex = PerpetuumException.Create(error);
PerpetuumException gex = PerpetuumException.Create(error);
exceptionAction?.Invoke(gex);

return gex;
});
}

public static T ThrowIfNull<T>([CanBeNull] this T source, Func<Exception> exceptionFactory)
{
if (Equals(source, null))
throw exceptionFactory();

return source;
return Equals(source, null)
? throw exceptionFactory()
: source;
}

public static T ThrowIfNotEqual<T>(this T source, T comparer, Func<Exception> exceptionFactory)
{
Equals(source, comparer).ThrowIfFalse(exceptionFactory);

return source;
}

public static T ThrowIfEqual<T>(this T source, T comparer, Func<Exception> exceptionFactory)
{
Equals(source, comparer).ThrowIfTrue(exceptionFactory);

return source;
}

public static string ThrowIfNullOrEmpty(this string text, ErrorCodes error)
{
string.IsNullOrEmpty(text).ThrowIfTrue(error);

return text;
}
}
Expand Down
Loading

0 comments on commit 4109c38

Please sign in to comment.