-
Notifications
You must be signed in to change notification settings - Fork 88
C# Source Modernization initiative #430
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
b64e349
Reformat CSUtil.Commons/*
kvakvs cd692c4
Partial formatting; Debug switches now accessed via Enum
kvakvs fe75603
Modernize CustomCarAI
kvakvs 4e15a86
Modernize CargoTruckAI
kvakvs 9257f2b
DebugSettings now can be accessed via static properties = shorter code
kvakvs 4fe6ccd
Modernize CustomHumanAI
kvakvs 680f7b0
Modernize CustomPassengerCarAI
kvakvs 9914685
Modernize Custom{PoliceCar, PostVan, Resident, RoadBase, Ship, Taxi, …
kvakvs 63eab24
Modernize CustomTrainAI
kvakvs 5832f6c
Modernize CustomTramBaseAI
kvakvs 35ff979
Modernize CustomVehicleAI
kvakvs 1f02a0c
Restore base. in calls, apparently it is important
kvakvs cd62d13
Modernize CustomPathFind (v1)
kvakvs cfe7d49
Modernize CustomPathManager, StockPathFind
kvakvs ada982a
In Release logLogic variable still had its old name: debug
kvakvs 7745725
Revert and untabify StockPathFind
kvakvs 50a46fa
Modernize AdvancedParkingManager & new shorter log invocations
kvakvs e04fc4d
Log simplification
kvakvs 57f70d6
Restore performance in Debug
kvakvs deb9e3b
Refactor/rename pathfinding module; Dump other settings into editorco…
kvakvs cb59e99
Minor editorconfig changes
kvakvs cb01465
Change var to types in affected files: CSUtil.Common, TLM/Custom
kvakvs a1fd3b6
Review notes on #430; Removed DEBUGGEO now DEBUG
kvakvs 103d3b8
Removed old PF, renamed PF2 define to PF_DIJKSTRA
kvakvs f94d046
Simplify SegmentEndId Equals
kvakvs 985ea26
Rider/Resharper style settings: Use var when type is evident, otherwi…
kvakvs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,9 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace CSUtil.Commons { | ||
| public enum ArrowDirection { | ||
| None = 0, | ||
| Left = 1, | ||
| Forward = 2, | ||
| Right = 3, | ||
| Turn = 4 | ||
| } | ||
| } | ||
| namespace CSUtil.Commons { | ||
| public enum ArrowDirection { | ||
| None = 0, | ||
| Left = 1, | ||
| Forward = 2, | ||
| Right = 3, | ||
| Turn = 4 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,73 +1,14 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
|
|
||
| namespace CSUtil.Commons { | ||
| public class ArrowDirectionUtil { | ||
| public static ArrowDirection InvertLeftRight(ArrowDirection dir) { | ||
| if (dir == ArrowDirection.Left) | ||
| dir = ArrowDirection.Right; | ||
| else if (dir == ArrowDirection.Right) | ||
| dir = ArrowDirection.Left; | ||
| return dir; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Calculates the direction of <paramref name="toRelDir"/> in relation to ArrowDirection.TURN. | ||
| /// </summary> | ||
| /// <param name="fromDir">source direction</param> | ||
| /// <param name="toRelDir">target direction, relative to <paramref name="fromDir"/></param> | ||
| /// <returns></returns> | ||
| public static ArrowDirection MakeAbsolute(ArrowDirection fromDir, ArrowDirection toRelDir) { | ||
| if (fromDir == ArrowDirection.None) { | ||
| // invalid direction | ||
| return ArrowDirection.None; | ||
| } | ||
|
|
||
| if (toRelDir == ArrowDirection.None) { | ||
| // invalid direction | ||
| return ArrowDirection.None; | ||
| } | ||
|
|
||
| if (fromDir == ArrowDirection.Turn) { | ||
| // toRelDir is already relative to TURN | ||
| return toRelDir; | ||
| } | ||
|
|
||
| if (toRelDir == ArrowDirection.Turn) { | ||
| // toRelDir is fromDir | ||
| return fromDir; | ||
| } | ||
|
|
||
| int fromDirBase0 = (int)fromDir - 1; | ||
| int toRelDirBase1 = (int)toRelDir; | ||
| /* | ||
| * Direction | Base 0 | Base 1 | ||
| * ==========+========+======= | ||
| * Left | 0 | 1 | ||
| * Forward | 1 | 2 | ||
| * Right | 2 | 3 | ||
| * | ||
| * | ||
| * Direction 1 | Direction 2 | Dir. 1 B0 | Dir. 2 B1 | Sum | (Sum + 1) % 4 | Desired dir. | ||
| * ============+=============+===========+===========+=====|===============+============= | ||
| * Left | Left | 0 | 1 | 1 | 2 | (Forward, 2) | ||
| * Left | Forward | 0 | 2 | 2 | 3 | (Right, 3) | ||
| * Left | Right | 0 | 3 | 3 | 0 | (Turn, 4) | ||
| * Forward | Left | 1 | 1 | 2 | 3 | (Right, 3) | ||
| * Forward | Forward | 1 | 2 | 3 | 0 | (Turn, 4) | ||
| * Forward | Right | 1 | 3 | 4 | 1 | (Left, 1) | ||
| * Right | Left | 2 | 1 | 3 | 0 | (Turn, 4) | ||
| * Right | Forward | 2 | 2 | 4 | 1 | (Left, 1) | ||
| * Right | Right | 2 | 3 | 5 | 2 | (Forward, 2) | ||
| */ | ||
|
|
||
| int ret = (fromDirBase0 + toRelDirBase1 + 1) % 4; | ||
| if (ret == 0) { | ||
| ret = 4; | ||
| } | ||
| return (ArrowDirection)ret; | ||
| } | ||
| } | ||
| } | ||
| namespace CSUtil.Commons { | ||
| public static class ArrowDirectionUtil { | ||
| public static ArrowDirection InvertLeftRight(ArrowDirection dir) { | ||
| switch (dir) { | ||
| case ArrowDirection.Left: | ||
| return ArrowDirection.Right; | ||
| case ArrowDirection.Right: | ||
| return ArrowDirection.Left; | ||
| } | ||
|
|
||
| return dir; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,11 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| namespace CSUtil.Commons { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| namespace CSUtil.Commons { | ||
| public static class EnumUtil { | ||
| public static IEnumerable<T> GetValues<T>() { | ||
| return Enum.GetValues(typeof(T)).Cast<T>(); | ||
| } | ||
| } | ||
| } | ||
| public static class EnumUtil { | ||
| public static IEnumerable<T> GetValues<T>() { | ||
| return Enum.GetValues(typeof(T)).Cast<T>(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,73 +1,78 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using UnityEngine; | ||
| namespace CSUtil.Commons { | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using UnityEngine; | ||
|
|
||
| namespace CSUtil.Commons { | ||
| public static class Log { | ||
| private static readonly object LogLock_ = new object(); | ||
|
|
||
| public static class Log { | ||
| private enum LogLevel { | ||
| Trace, | ||
| Debug, | ||
| Info, | ||
| Warning, | ||
| Error | ||
| } | ||
| // TODO refactor log filename to configuration | ||
| private static readonly string LogFilename_ | ||
kvakvs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| = Path.Combine(Application.dataPath, "TMPE.log"); | ||
|
|
||
| private static object logLock = new object(); | ||
| private enum LogLevel { | ||
| Trace, | ||
| Debug, | ||
| Info, | ||
originalfoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Warning, | ||
| Error | ||
| } | ||
|
|
||
| private static string logFilename = Path.Combine(Application.dataPath, "TMPE.log"); // TODO refactor log filename to configuration | ||
| private static Stopwatch sw = Stopwatch.StartNew(); | ||
| private static Stopwatch sw = Stopwatch.StartNew(); | ||
|
|
||
| static Log() { | ||
| try { | ||
| if (File.Exists(logFilename)) { | ||
| File.Delete(logFilename); | ||
| } | ||
| } catch (Exception) { | ||
|
|
||
| } | ||
| } | ||
| static Log() { | ||
| try { | ||
| if (File.Exists(LogFilename_)) { | ||
| File.Delete(LogFilename_); | ||
| } | ||
| } | ||
| catch (Exception) { } | ||
| } | ||
|
|
||
| [Conditional("DEBUG")] | ||
| public static void _Debug(string s) { | ||
| LogToFile(s, LogLevel.Debug); | ||
| } | ||
| [Conditional("DEBUG")] | ||
| public static void _Debug(string s) { | ||
| LogToFile(s, LogLevel.Debug); | ||
| } | ||
|
|
||
| [Conditional("TRACE")] | ||
| public static void _Trace(string s) { | ||
| LogToFile(s, LogLevel.Trace); | ||
| } | ||
| [Conditional("TRACE")] | ||
| public static void _Trace(string s) { | ||
| LogToFile(s, LogLevel.Trace); | ||
| } | ||
|
|
||
| public static void Info(string s) { | ||
| LogToFile(s, LogLevel.Info); | ||
| } | ||
| public static void Info(string s) { | ||
| LogToFile(s, LogLevel.Info); | ||
| } | ||
|
|
||
| public static void Warning(string s) { | ||
| LogToFile(s, LogLevel.Warning); | ||
| } | ||
| [Conditional("DEBUG")] | ||
| public static void _DebugOnlyWarning(string s) { | ||
| LogToFile(s, LogLevel.Warning); | ||
| } | ||
|
|
||
| public static void Error(string s) { | ||
| LogToFile(s, LogLevel.Error); | ||
| } | ||
| public static void Warning(string s) { | ||
| LogToFile(s, LogLevel.Warning); | ||
| } | ||
|
|
||
| private static void LogToFile(string log, LogLevel level) { | ||
| try { | ||
| Monitor.Enter(logLock); | ||
|
|
||
| using (StreamWriter w = File.AppendText(logFilename)) { | ||
| w.WriteLine($"[{level.ToString()}] @ {sw.ElapsedTicks} {log}"); | ||
| if (level == LogLevel.Warning || level == LogLevel.Error) { | ||
| w.WriteLine((new System.Diagnostics.StackTrace()).ToString()); | ||
| w.WriteLine(); | ||
| } | ||
| } | ||
| } finally { | ||
| Monitor.Exit(logLock); | ||
| } | ||
| } | ||
| } | ||
| public static void Error(string s) { | ||
| LogToFile(s, LogLevel.Error); | ||
| } | ||
|
|
||
| } | ||
| private static void LogToFile(string log, LogLevel level) { | ||
| try { | ||
| Monitor.Enter(LogLock_); | ||
|
|
||
| using (StreamWriter w = File.AppendText(LogFilename_)) { | ||
| w.WriteLine($"[{level.ToString()}] @ {sw.ElapsedTicks} {log}"); | ||
| if (level == LogLevel.Warning || level == LogLevel.Error) { | ||
| w.WriteLine((new System.Diagnostics.StackTrace()).ToString()); | ||
| w.WriteLine(); | ||
| } | ||
| } | ||
| } | ||
| finally { | ||
| Monitor.Exit(LogLock_); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,12 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| namespace CSUtil.Commons { | ||
| public static class LogicUtil { | ||
| public static bool CheckFlags(uint flags, uint flagMask, uint? expectedResult = null) { | ||
| var res = flags & flagMask; | ||
| if (expectedResult == null) { | ||
| return res != 0; | ||
| } | ||
|
|
||
| namespace CSUtil.Commons { | ||
| public static class LogicUtil { | ||
| public static bool CheckFlags(uint flags, uint flagMask, uint? expectedResult=null) { | ||
| uint res = flags & flagMask; | ||
| if (expectedResult == null) { | ||
| return res != 0; | ||
| } else { | ||
| return res == expectedResult; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return res == expectedResult; | ||
kvakvs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,10 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
|
|
||
| namespace CSUtil.Commons { | ||
| public enum TernaryBool { | ||
| Undefined = 0, | ||
| False = 1, | ||
| True = 2 | ||
| } | ||
| } | ||
| namespace CSUtil.Commons { | ||
| /// <summary> | ||
| /// Tri-bool: a boolean value which can also be undefined | ||
| /// </summary> | ||
| public enum TernaryBool { | ||
| Undefined = 0, | ||
| False = 1, | ||
| True = 2 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,33 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| namespace CSUtil.Commons { | ||
| using System; | ||
|
|
||
| namespace CSUtil.Commons { | ||
| public static class TernaryBoolUtil { | ||
| public static bool ToBool(TernaryBool tb) { | ||
| if (tb == TernaryBool.Undefined) { | ||
| throw new ArgumentException("Cannot determine boolean value for undefined ternary bool"); | ||
| } | ||
| public static class TernaryBoolUtil { | ||
| public static bool ToBool(TernaryBool tb) { | ||
| if (tb == TernaryBool.Undefined) { | ||
| throw new ArgumentException("Cannot determine boolean value for undefined ternary bool"); | ||
| } | ||
|
|
||
| return tb == TernaryBool.True; | ||
| } | ||
| return tb == TernaryBool.True; | ||
| } | ||
|
|
||
| public static bool? ToOptBool(TernaryBool tb) { | ||
| if (tb == TernaryBool.Undefined) { | ||
| return null; | ||
| } | ||
| public static bool? ToOptBool(TernaryBool tb) { | ||
| if (tb == TernaryBool.Undefined) { | ||
| return null; | ||
| } | ||
|
|
||
| return tb == TernaryBool.True; | ||
| } | ||
| return tb == TernaryBool.True; | ||
| } | ||
|
|
||
| public static TernaryBool ToTernaryBool(bool? b) { | ||
| switch (b) { | ||
| case null: | ||
| default: | ||
| return TernaryBool.Undefined; | ||
| case false: | ||
| return TernaryBool.False; | ||
| case true: | ||
| return TernaryBool.True; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| public static TernaryBool ToTernaryBool(bool? b) { | ||
| switch (b) { | ||
| case null: | ||
| default: | ||
| return TernaryBool.Undefined; | ||
| case false: | ||
| return TernaryBool.False; | ||
| case true: | ||
| return TernaryBool.True; | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.