Skip to content

Commit

Permalink
Merge pull request #289 from splaplapla/refactor2
Browse files Browse the repository at this point in the history
ボタン設定の管理でシングルトンパターンをやめる
  • Loading branch information
jiikko authored Jan 7, 2024
2 parents dbc062f + 1b59f77 commit 91f870d
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 317 deletions.
15 changes: 3 additions & 12 deletions lib/procon_bypass_man.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,12 @@ def self.ephemeral_config
end

# NOTE: setting.ymlから読み込んだボタンに関する値を管理する
# @return [ProconBypassMan::SettingConfiguration]
def self.setting_config
@@setting_configuration ||= ProconBypassMan::SettingConfiguration.instance
end

# @deprecated
def self.buttons_setting_configuration
ProconBypassMan::ButtonsSettingConfiguration.instance
@@buttons_setting_configuration ||= ProconBypassMan::ButtonsSettingConfiguration.new
end

# @return [void]
def self.reset!
ProconBypassMan::Procon.reset!
buttons_setting_configuration.reset!
ProconBypassMan.ephemeral_config.reset!
def self.buttons_setting_configuration=(instance)
@@buttons_setting_configuration = instance
end

# @return [void]
Expand Down
54 changes: 11 additions & 43 deletions lib/procon_bypass_man/buttons_setting_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,17 @@
require "procon_bypass_man/buttons_setting_configuration/layer"

module ProconBypassMan
class Position < Struct.new(:x, :y); end

class ButtonsSettingConfiguration
class Position < Struct.new(:x, :y); end

module ManualMode; def self.name; :manual; end; end

attr_accessor :layers,
:setting_path,
:macro_registry,
:mode_registry,
:neutral_position

def self.instance
@@context ||= {}
@@context[current_context_key] ||= new
end

def self.current_context_key
@@current_context_key ||= :main
end

def self.instance=(val)
@@context[current_context_key] = val
end

def self.switch_new_context(new_context_key)
@@context[new_context_key] = new
previous_key = current_context_key
if block_given?
@@current_context_key = new_context_key
value = yield(@@context[new_context_key])
return value
else
@@current_context_key = new_context_key
end
ensure
@@current_context_key = previous_key
end

def initialize
reset!
end
Expand All @@ -59,11 +34,12 @@ def layer(direction, mode: ManualMode, &block)
end
end

unless ([ManualMode.name] + ProconBypassMan.buttons_setting_configuration.mode_registry.plugins.keys).include?(mode_name)
unless ([ManualMode.name] + mode_registry.plugins.keys).include?(mode_name)
# TODO: strict modeが有効なときはエラーにする
warn "#{mode_name}モードがinstallされていません"
end

layer = Layer.new(mode: mode_name)
layer = Layer.new(self, mode: mode_name)
layer.instance_eval(&block) if block_given?
self.layers[direction] = layer
self
Expand All @@ -84,14 +60,6 @@ def prefix_keys_for_changing_layer(buttons)
self
end

def macro_registry
@macro_registry
end

def mode_registry
@mode_registry
end

def set_neutral_position(x, y)
self.neutral_position = Position.new(x, y).freeze
self
Expand Down Expand Up @@ -128,10 +96,10 @@ def reset!
# self.setting_path = nil
# どこかで初期化している気がするのでコメントアウト
self.layers = {
up: Layer.new,
down: Layer.new,
left: Layer.new,
right: Layer.new,
up: Layer.new(self),
down: Layer.new(self),
left: Layer.new(self),
right: Layer.new(self),
}
@macro_registry = ProconBypassMan::Procon::MacroRegistry2.new
@mode_registry = ProconBypassMan::Procon::ModeRegistry2.new
Expand Down
7 changes: 4 additions & 3 deletions lib/procon_bypass_man/buttons_setting_configuration/layer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ class ButtonsSettingConfiguration
class Layer
attr_accessor :mode, :flips, :macros, :disable_macros, :remaps, :left_analog_stick_caps, :disables

def initialize(mode: :manual)
def initialize(context, mode: :manual)
@context = context
self.mode = mode
self.flips = {}
self.macros = {}
Expand Down Expand Up @@ -83,7 +84,7 @@ def macro(name, if_pressed: nil, if_tilted_left_stick: nil, force_neutral: nil)
end

macro_name = name.to_s.to_sym
if ProconBypassMan.buttons_setting_configuration.macro_registry.plugins[macro_name]
if @context.macro_registry.plugins[macro_name]
self.macros[macro_name] = { if_pressed: if_pressed, if_tilted_left_stick: if_tilted_left_stick, force_neutral: force_neutral }.compact
else
warn "#{macro_name}マクロがinstallされていません"
Expand Down Expand Up @@ -133,7 +134,7 @@ def open_macro(name, steps: [], if_pressed: nil, if_tilted_left_stick: nil, forc
end

macro_name = name || "OpenMacro-#{steps.join}".to_sym
ProconBypassMan.buttons_setting_configuration.macro_registry.install_plugin(macro_name, steps: steps)
@context.macro_registry.install_plugin(macro_name, steps: steps)
self.macros[macro_name] = { if_pressed: if_pressed, if_tilted_left_stick: if_tilted_left_stick, force_neutral: force_neutral }.compact
end

Expand Down
64 changes: 27 additions & 37 deletions lib/procon_bypass_man/buttons_setting_configuration/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,41 @@ module Loader

# @return [ProconBypassMan::ButtonsSettingConfiguration]
def self.load(setting_path: )
ProconBypassMan.buttons_setting_configuration.setting_path = setting_path

metadata_loader = ProconBypassMan::ButtonsSettingConfiguration::MetadataLoader.load(setting_path: setting_path)
if(Gem::Version.new(metadata_loader.required_pbm_version) >= Gem::Version.new(ProconBypassMan::VERSION))
ProconBypassMan::SendErrorCommand.execute(error: '起動中のPBMが設定ファイルのバージョンを満たしていません。設定ファイルが意図した通り動かない可能性があります。PBMのバージョンをあげてください。')
end

ProconBypassMan::ButtonsSettingConfiguration.switch_new_context(:validation) do |new_instance|
yaml = ProconBypassMan::YamlLoader.load(path: setting_path)
new_instance.instance_eval(yaml["setting"])
validator = Validator.new(new_instance)
if validator.valid?
next
else
raise ProconBypassMan::CouldNotLoadConfigError, validator.errors_to_s
end
rescue SyntaxError
fallback_setting_if_has_backup(current_setting_path: setting_path)
raise ProconBypassMan::CouldNotLoadConfigError, "Rubyスクリプトのシンタックスエラーです"
rescue NameError
fallback_setting_if_has_backup(current_setting_path: setting_path)
raise ProconBypassMan::CouldNotLoadConfigError, "Rubyスクリプトに未定義の定数・変数があります"
rescue Psych::SyntaxError
fallback_setting_if_has_backup(current_setting_path: setting_path)
raise ProconBypassMan::CouldNotLoadConfigError, "yamlのシンタックスエラーです"
end

ProconBypassMan.buttons_setting_configuration.reset!
ProconBypassMan.reset!
ProconBypassMan::Procon.reset! # TODO: ここでresetするのは微妙な気がする

yaml = ProconBypassMan::YamlLoader.load(path: setting_path)
ProconBypassMan.config.raw_setting = yaml.dup
case yaml["version"]
when 1.0, nil
ProconBypassMan.buttons_setting_configuration.instance_eval(yaml["setting"])
else
ProconBypassMan.logger.warn "不明なバージョンです。failoverします"
ProconBypassMan.buttons_setting_configuration.instance_eval(yaml["setting"])
end
new_instance, yaml =
begin
new_instance = ProconBypassMan::ButtonsSettingConfiguration.new
new_instance.setting_path = setting_path
yaml = ProconBypassMan::YamlLoader.load(path: setting_path)
new_instance.instance_eval(yaml["setting"])
validator = Validator.new(new_instance)
if validator.valid?
[new_instance, yaml]
else
# TODO: fallbackが必要では?
raise ProconBypassMan::CouldNotLoadConfigError, validator.errors_to_s
end
rescue SyntaxError
fallback_setting_if_has_backup(current_setting_path: setting_path)
raise ProconBypassMan::CouldNotLoadConfigError, "Rubyスクリプトのシンタックスエラーです"
rescue NameError
fallback_setting_if_has_backup(current_setting_path: setting_path)
raise ProconBypassMan::CouldNotLoadConfigError, "Rubyスクリプトに未定義の定数・変数があります"
rescue Psych::SyntaxError
fallback_setting_if_has_backup(current_setting_path: setting_path)
raise ProconBypassMan::CouldNotLoadConfigError, "yamlのシンタックスエラーです"
end

ProconBypassMan.config.raw_setting = yaml
ProconBypassMan.buttons_setting_configuration = new_instance
File.write(ProconBypassMan.digest_path, Digest::MD5.hexdigest(yaml["setting"]))

if File.exist?(ProconBypassMan.fallback_setting_path)
FileUtils.rm_rf(ProconBypassMan.fallback_setting_path)
end
FileUtils.rm_rf(ProconBypassMan.fallback_setting_path) # NOTE: 設定ファイルの読み込みに成功したら、バックアップを削除する

ProconBypassMan.buttons_setting_configuration
end
Expand Down
115 changes: 0 additions & 115 deletions lib/procon_bypass_man/setting_configuration.rb

This file was deleted.

Loading

0 comments on commit 91f870d

Please sign in to comment.