Skip to content

Commit

Permalink
Merge pull request #29 from MarvinBeym/feature/part-improvement
Browse files Browse the repository at this point in the history
Feature/part improvement
  • Loading branch information
Marvin Beym authored Aug 13, 2023
2 parents cbbed58 + 5dbcc32 commit ad3fdd6
Show file tree
Hide file tree
Showing 45 changed files with 3,257 additions and 1,164 deletions.
6 changes: 6 additions & 0 deletions Source code/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ dotnet_diagnostic.IDE0002.severity = none

# IDE0044: Modifizierer "readonly" hinzufügen
dotnet_diagnostic.IDE0044.severity = none

# IDE1006: Benennungsstile
dotnet_diagnostic.IDE1006.severity = none

# CS0618: Typ oder Element ist veraltet
dotnet_diagnostic.CS0618.severity = error
35 changes: 17 additions & 18 deletions Source code/MscModApi/Caching/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace MscModApi.Caching
{
public static class Cache
{
public static Dictionary<string, GameObject> cachedGameObjects = new Dictionary<string, GameObject>();
public static Dictionary<string, GameObject> cachedGameObjects;

private static GameObject[] globalCache;

Expand All @@ -19,17 +19,15 @@ public static GameObject Find(string name, bool findEvenIfInactive = true)
return gameObject;
}
}
catch
{
catch {
// ignored. Continues below
}

GameObject.FindObjectOfType<GameObject>();

GameObject foundObject = GameObject.Find(name);

if (!foundObject && findEvenIfInactive)
{
if (!foundObject && findEvenIfInactive) {
foundObject = FindInGlobal(name);
}

Expand All @@ -39,29 +37,25 @@ public static GameObject Find(string name, bool findEvenIfInactive = true)

private static GameObject FindInGlobal(string name)
{
if (globalCache == null)
{
if (globalCache == null) {
globalCache = Resources.FindObjectsOfTypeAll<GameObject>();
}

foreach(var gameObject in globalCache)
{
foreach (var gameObject in globalCache) {
string nameToCompareTo = gameObject.name;

if (gameObject.name.Contains("OptionsMenu"))
{

if (gameObject.name.Contains("OptionsMenu")) {
}

if (name.Contains("/"))
{
if (name.Contains("/")) {
nameToCompareTo = GetObjectPath(gameObject);
}
if (nameToCompareTo == name)
{

if (nameToCompareTo == name) {
return gameObject;
}
}

return null;
}

Expand All @@ -75,13 +69,18 @@ private static string GetObjectPath(GameObject gameObject)
Transform currentTransform = gameObject.transform;
string path = currentTransform.name;

while (currentTransform.parent != null)
{
while (currentTransform.parent != null) {
currentTransform = currentTransform.parent;
path = currentTransform.name + "/" + path;
}

return path;
}

public static void LoadCleanup()
{
cachedGameObjects = new Dictionary<string, GameObject>();
globalCache = null;
}
}
}
32 changes: 25 additions & 7 deletions Source code/MscModApi/Caching/CarH.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CarH
private static GameObject _electricity;
private static FsmString _playerCurrentVehicle;

public static bool running => drivetrain.rpm > 0;
public static bool running => drivetrain.rpm > 20;
public static bool playerInCar => playerCurrentVehicle == "Satsuma";

public static string playerCurrentVehicle
Expand All @@ -28,7 +28,8 @@ public static string playerCurrentVehicle
}
}

public static GameObject electricity {
public static GameObject electricity
{
get
{
if (_electricity != null) return _electricity;
Expand All @@ -38,7 +39,8 @@ public static GameObject electricity {
}
}

public static bool hasPower {
public static bool hasPower
{
get
{
if (_electricsOk != null) return _electricsOk.Value;
Expand All @@ -48,7 +50,8 @@ public static bool hasPower {
}
}

public static CarController carController {
public static CarController carController
{
get
{
if (_carController != null) return _carController;
Expand All @@ -58,7 +61,8 @@ public static CarController carController {
}
}

public static AxisCarController axisCarController {
public static AxisCarController axisCarController
{
get
{
if (_axisController != null) return _axisController;
Expand All @@ -68,7 +72,8 @@ public static AxisCarController axisCarController {
}
}

public static Drivetrain drivetrain {
public static Drivetrain drivetrain
{
get
{
if (_drivetrain != null) return _drivetrain;
Expand All @@ -78,7 +83,8 @@ public static Drivetrain drivetrain {
}
}

public static GameObject satsuma {
public static GameObject satsuma
{
get
{
if (_satsuma != null) return _satsuma;
Expand All @@ -87,5 +93,17 @@ public static GameObject satsuma {
return _satsuma;
}
}

public static void LoadCleanup()
{
_satsuma = null;
_satsuma = null;
_drivetrain = null;
_axisController = null;
_carController = null;
_electricsOk = null;
_electricity = null;
_playerCurrentVehicle = null;
}
}
}
7 changes: 6 additions & 1 deletion Source code/MscModApi/Caching/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static float money
{
if (_money != null) return _money.Value;
_money = PlayMakerGlobals.Instance.Variables.FindFsmFloat("PlayerMoney");
return (float) Math.Round(_money.Value, 1);
return (float)Math.Round(_money.Value, 1);
}
set
{
Expand All @@ -23,5 +23,10 @@ public static float money
_money.Value = value;
}
}

public static void LoadCleanup()
{
_money = null;
}
}
}
170 changes: 170 additions & 0 deletions Source code/MscModApi/Commands/ScrewPlacementModCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MSCLoader;
using MscModApi.Parts;
using MscModApi.Tools;

namespace MscModApi.Commands
{
class ScrewPlacementModCommand : ConsoleCommand
{
private readonly MscModApi mod;
protected List<Command> availableCommands = new List<Command>();
private readonly Dictionary<string, Dictionary<string, Part>> modsParts;

private readonly Command helpCommand;
private readonly Command enableCommand;
private readonly Command disableCommand;
private readonly Command listCommand;

protected class Command
{
public string name { get; }

public string help { get; }


public string parameterCallExample { get; }

public Command(string name, string help, string parameterCallExample = "")
{
this.name = name;
this.help = help;
this.parameterCallExample = parameterCallExample;
}
}

public ScrewPlacementModCommand(MscModApi mod, Dictionary<string, Dictionary<string, Part>> modsParts)
{
this.mod = mod;
this.modsParts = modsParts;

helpCommand = new Command(
"help",
"This help",
"Escape further command arguments with spaces, with two <color=blue>'</color>"
);

enableCommand = new Command(
"enable",
"Enable screw placement mode for a part",
"mod-api-screw enable '<your-mod-id>' '<part-id>'"
);

disableCommand = new Command(
"disable",
"Disable screw placement mode for a part",
"mod-api-screw disable '<your-mod-id>' '<part-id>'"
);

listCommand = new Command(
"list",
"Lists all available parts with their screw placement mode status",
"mod-api-screw list '<your-mod-id>'"
);

availableCommands.AddRange(new[]
{
helpCommand,
listCommand,
enableCommand,
disableCommand,
});
}

public override void Run(string[] args)
{
Command mainCommand = null;
foreach (Command command in availableCommands) {
if (command.name == args.ElementAtOrDefault(0)) {
mainCommand = command;
}
}

string partId;
string modId;
Part part;
switch (mainCommand) {
case Command cmd when cmd == helpCommand:
foreach (Command command in availableCommands) {
ModConsole.Print($"<color=orange>{command.name}</color>: {command.help}");
if (command.parameterCallExample != "") {
ModConsole.Print($"<color=orange>=></color> {command.parameterCallExample}");
}
}

break;
case Command cmd1 when cmd1 == enableCommand:
case Command cmd2 when cmd2 == disableCommand:
modId = args.ElementAtOrDefault(1);
partId = args.ElementAtOrDefault(2);

if (string.IsNullOrEmpty(modId) || string.IsNullOrEmpty(partId)) {
goto default;
}

//Removing potential ''
modId = modId.Replace("'", "");
partId = partId.Replace("'", "");

if (!ScrewPlacementAssist.IsScrewPlacementModeEnabled(modId)) {
ModConsole.Print($"ScrewPlacementMode not enabled for mod with id '{modId}'");
break;
}


if (!modsParts.ContainsKey(modId)) {
ModConsole.Error($"No mod with id <color=blue>{modId}</color> found that has added parts");
break;
}

if (!modsParts[modId].ContainsKey(partId)) {
ModConsole.Error(
$"No part with id <color=blue>{partId}</color> found for mod with id <color=blue>{modId}</color>");
break;
}

part = modsParts[modId][partId];
part.screwPlacementMode = mainCommand == enableCommand;
break;
case Command cmd2 when cmd2 == listCommand:
modId = args.ElementAtOrDefault(1);

if (string.IsNullOrEmpty(modId)) {
goto default;
}

//Removing potential ''
modId = modId.Replace("'", "");

if (!ScrewPlacementAssist.IsScrewPlacementModeEnabled(modId)) {
ModConsole.Print($"ScrewPlacementMode not enabled for mod with id '{modId}'");
break;
}

if (!modsParts.ContainsKey(modId)) {
ModConsole.Error($"No mod with id <color=blue>{modId}</color> found that has added parts");
break;
}

foreach (var partDict in modsParts[modId]) {
part = partDict.Value;
ModConsole.Print(
$"<color=orange>{part.id}</color> => {(part.screwPlacementMode ? "Enabled" : "Disabled")}");
}

break;
default:
ModConsole.Error($"Invalid command <color=blue>mod-api-screw {string.Join(" ", args)}</color>");
break;
}
}

public override string Name => "mod-api-screw";

public override string Help =>
"Run <color=blue>mod-api-screw help</color> for a list of commands and arguments";
}
}
Loading

0 comments on commit ad3fdd6

Please sign in to comment.