Code cleanup.

This commit is contained in:
2022-11-15 17:28:42 +08:00
parent 945f9ca7d1
commit 35ac57bfba

View File

@@ -5,7 +5,6 @@ using Cryville.Crtr.Config;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Logger = Cryville.Common.Logger;
namespace Cryville.Crtr { namespace Cryville.Crtr {
public class InputProxy : IDisposable { public class InputProxy : IDisposable {
@@ -59,28 +58,29 @@ namespace Cryville.Crtr {
} }
} }
public void Set(InputProxyEntry proxy) { public void Set(InputProxyEntry proxy) {
var name = proxy.Target; var target = proxy.Target;
if (!_ruleset.inputs.ContainsKey(name)) throw new ArgumentException("Invalid input name"); if (!_ruleset.inputs.ContainsKey(target)) throw new ArgumentException("Invalid input name");
if (_use[proxy.Target] > 0) if (_tproxies.ContainsKey(target)) Remove(proxy);
if (_use[target] > 0)
throw new InvalidOperationException("Input already assigned"); throw new InvalidOperationException("Input already assigned");
if (proxy.Source != null) { if (proxy.Source != null) {
if (_judge != null) { if (_judge != null) {
proxy.Source.Value.Handler.OnInput -= OnInput; // Prevent duplicated hooks, no exception will be thrown proxy.Source.Value.Handler.OnInput -= OnInput; // Prevent duplicated hooks, no exception will be thrown
proxy.Source.Value.Handler.OnInput += OnInput; proxy.Source.Value.Handler.OnInput += OnInput;
} }
_tproxies.Add(proxy.Target, proxy); _tproxies.Add(target, proxy);
_sproxies.Add(proxy.Source.Value, proxy); _sproxies.Add(proxy.Source.Value, proxy);
IncrementUseRecursive(name); IncrementUseRecursive(target);
IncrementReversedUseRecursive(name); IncrementReversedUseRecursive(target);
} }
} }
void Remove(InputProxyEntry proxy) { void Remove(InputProxyEntry proxy) {
var name = proxy.Target; var target = proxy.Target;
if (_judge != null) _tproxies[name].Source.Value.Handler.OnInput -= OnInput; if (_judge != null) _tproxies[target].Source.Value.Handler.OnInput -= OnInput;
_sproxies.Remove(_tproxies[name].Source.Value); _sproxies.Remove(_tproxies[target].Source.Value);
_tproxies.Remove(name); _tproxies.Remove(target);
DecrementUseRecursive(name); DecrementUseRecursive(target);
DecrementReversedUseRecursive(name); DecrementReversedUseRecursive(target);
} }
public bool IsUsed(InputSource src) { public bool IsUsed(InputSource src) {
return _sproxies.ContainsKey(src); return _sproxies.ContainsKey(src);