Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Minecraft Forge(#1080) Event サイクル。たぶんマイクラ1.7系共通
- - NOINIT("Uninitialized",null),
- - LOADING("Loading",null),
- - CONSTRUCTING("Constructing mods",FMLConstructionEvent.class),
- - PREINITIALIZATION("Pre-initializing mods", FMLPreInitializationEvent.class),
- - INITIALIZATION("Initializing mods", FMLInitializationEvent.class),
- - POSTINITIALIZATION("Post-initializing mods", FMLPostInitializationEvent.class),
- - AVAILABLE("Mod loading complete", FMLLoadCompleteEvent.class),
- - SERVER_ABOUT_TO_START("Server about to start", FMLServerAboutToStartEvent.class),
- - SERVER_STARTING("Server starting", FMLServerStartingEvent.class),
- - SERVER_STARTED("Server started", FMLServerStartedEvent.class),
- - SERVER_STOPPING("Server stopping", FMLServerStoppingEvent.class),
- - SERVER_STOPPED("Server stopped", FMLServerStoppedEvent.class),
- - ERRORED("Mod Loading errored",null)
- ## ゲーム本体の起動
- - NOINIT("Uninitialized",null),
- - LOADING("Loading",null),
- ## MOD群の初期化
- - CONSTRUCTING("Constructing mods",FMLConstructionEvent.class),
- - PREINITIALIZATION("Pre-initializing mods", FMLPreInitializationEvent.class),
- - INITIALIZATION("Initializing mods", FMLInitializationEvent.class),
- - POSTINITIALIZATION("Post-initializing mods", FMLPostInitializationEvent.class),
- - AVAILABLE("Mod loading complete", FMLLoadCompleteEvent.class),
- ## サーバースタート
- - SERVER_ABOUT_TO_START("Server about to start", FMLServerAboutToStartEvent.class),
- - SERVER_STARTING("Server starting", FMLServerStartingEvent.class),
- - SERVER_STARTED("Server started", FMLServerStartedEvent.class),
- ## サーバーストップ
- - SERVER_STOPPING("Server stopping", FMLServerStoppingEvent.class),
- - SERVER_STOPPED("Server stopped", FMLServerStoppedEvent.class),
- - ERRORED("Mod Loading errored",null);
- # 以下、modの初期化部分について重点的に調べる。つまりForgeModLoaderの部分
- # 各Modの情報は、ModContainerオブジェクトに格納される
- - cpw.mods.fml.common.ModContainer
- - Mod名とかMod依存関係とか
- - boolean registerBus(EventBus, LoadController)で、LoadControllerのEventBusに自身の初期化メソッドを追加
- # EventBus は、com.google.common.eventbus を使っている。
- - googleさんの提供するイベント駆動基盤
- - http://docs.guava-libraries.googlecode.com/git-history/v10.0/javadoc/com/google/common/eventbus/package-summary.html
- - 日本語で解説 http://cco.hatenablog.jp/entry/20111210/1323509513
- - ``@Subscribeアノテーションを使うことでevent handlerとなる公開メソッドを実装することができます。イベントである引数は必ず1つのみであることに注意が必要です。``
- - 日本語で解説 http://blog.kengo-toda.jp/entry/20111006/1317907239
- - ``リスナーはどのようなクラスでもよく、メソッドを@Subscribeアノテーションで修飾するだけで使えます。``
- - ``リスナーが@Subscribeで修飾されたメソッドを複数個持っている場合、これらが実行される順番は環境依存となります。``
- - 上記を読んで理解した内容
- - Busにあらかじめレシーバーを複数登録(bus.register(receiver))しておく。
- - レシーバーには@Subscribeをつけたメソッド(引数は1つのobject)を定義しておく。メソッド名は問わない。ただし複数あるとその実行順序は環境依存となるので注意
- - Busに対してメッセージをポスト(bus.post(object))すると、そのobjectのタイプに応じたレシーバーのSubscribeメソッドを実行
- - ただし、同じタイプのオブジェクトを受け取るようなレシーバーを複数登録してしまうと、その実行順序は環境依存
- - (感想)これは、シンプルで使いやすそう!
- # LoadController について
- - cpw.mods.fml.common.LoadController
- - イベント駆動部分の主体。以下private変数を一部抜粋
- - ```private masterChannel = new EventBus("FMLMainChannel");```、 LoadControllerが自身の中で@subscribeされているメソッドの駆動用チャンネル。EventBusで実装
- - ```private ImmutableMap eventChannels<modid, そのmodのEventBus>```、 各Modの初期化メソッド駆動用チャンネルを集めたもの。Mapで実装
- - LoadController内で@Subscribe定義されているもの(メッセージがポストされたらどちらかを実行)
- - ```public void buildModList(FMLLoadEvent event)```
- - 有効なModごとのEventBus作って、eventChannelに登録、各Modの状態リスト作成、リソースパックにModを登録(?)
- - ```public void propogateStateMessage(FMLEvent stateEvent)```
- - 外部からきたメッセージを振り分ける。"FMLPreInitializationEvent"なら上記のModList作成を行う。それ以外なら有効なModContainer全部にメッセージ伝達
- - 有効なModContainer全部にメッセージ伝達するメソッド ```private void sendEventToModContainer(FMLEvent stateEvent, ModContainer mc)```
- - Mod依存関係や、バージョンの解決などを行っている。
- # まとめ
- ## 階層型のイベント駆動になっており。興味深い。
- - 「LoadController」の挙動 (EventBusで実装されたイベント駆動)
- - "FMLPreInitializationEvent"受信: 有効なModリストを作ったり、有効ModごとにEventBus作ってMapに保存。そして以下を実行
- - それ以外受信: 有効なModContainer全てへメッセージを伝播(というイメージ)
- - 各「ModContainer」の挙動 (イメージ、実際はLoadControllerがeventChannelsを使って処理)
- - 何らかのメッセージを受信: 自身が依存しているModや必要バージョンなどを確認し、依存ModがまだERRORED状態なら自身もERROREDとなる
- - "Skipping event %s and marking errored mod %s since required dependency %s has errored"
- - 依存関係等問題なければ、自身の初期化用EventBusに対してメッセージを伝播。
- - 各「Modの初期化EventBus」の挙動 (EventBusで実装されたイベント駆動)
- - 何らかのメッセージを受信: メッセージにより反応するレシーバメソッドをすべて実行。つまり対応する初期化メソッドを全て実行。ただし、複数あるとき実行順は環境依存
- ## つまりわかりやすく言うと
- - 100個のModがあったら、どのように初期化作業が進むか。
- - "FMLPreInitializationEvent"メッセージ来た=> 有効なModリストなどの作成後、100個のModにメッセージ送信。それぞれのModは対応する初期化メソッド実行
- - "FMLInitializationEvent"メッセージ来た=> 100個のModにメッセージ送信。それぞれのModは対応する初期化メソッド実行
- - "FMLPostInitializationEvent"メッセージ来た=> 100個のModにメッセージ送信。それぞれのModは対応する初期化メソッド実行
- ## 未解読、謎の部分
- - 依存関係により初期化失敗したModの後処理はどうなっているのか?
- - たぶん、"FMLPreInitializationEvent"などのメッセージを発信するエンジンのコードを読まないと・・・力尽きて今日は無理です。
- ## 感想
- - どんなModが追加されるのかわからないわけで、各Modの初期化を完全に正常に行うことをある程度あきらめた設計に思える。
- - つまり、「初期化せよ」というメッセージは送るが、後はそれぞれのModのメソッドにまかせた!という設計。
- --------------------------------------------------------------------------とりあえずここまで、以下はメモ書き
- # 基本は、アノテーションを宣言しつつ既存のイベントメソッド上書き/追加
- ## @Eventhandler アノテーション
- - 例1:FMLInitializationEvent イベントに初期化メソッドを複数追加
- ```
- @EventHandler
- public hoge(FMLInitializationEvent, event);
- @EventHandler
- public init(FMLInitializationEvent, event);
- @EventHandler
- public load(FMLInitializationEvent, event);
- ```
- - "FMLInitializationEvent"メッセージが来てイベント駆動したときに、上記3つのメソッド全てが実行される。ただし、実行順序は環境依存。
- # とはいえ
- Forge的には、以下の名前のメソッドを一つずつ追加することを推奨しているようだ。
- - FMLPreInitializationEvent
- - public void preInit(FMLPreInitializationEvent event) { }
- - FMLInitializationEvent
- - public void init(FMLInitializationEvent event) {}
- - これについては、 load()を使っている人も多い模様。
- - FMLPostInitializationEvent
- - public void postInit(FMLPostInitializationEvent event) {}
- ## cpw.mods.fml.common.Mod より抜粋
- ```
- /**
- * Marks the associated method as handling an FML lifecycle event.
- * The method must have a single parameter, one of the following types. This annotation
- * replaces the multiple different annotations that previously were used.
- *
- * Current event classes. This first section is standard lifecycle events. They are dispatched
- * at various phases as the game starts. Each event should have information useful to that
- * phase of the lifecycle. They are fired in this order.
- *
- * These suggestions are mostly just suggestions on what to do in each event.
- * <ul>
- * <li> {@link FMLPreInitializationEvent} : Run before anything else. Read your config, create blocks,
- * items, etc, and register them with the {@link GameRegistry}.</li>
- * <li> {@link FMLInitializationEvent} : Do your mod setup. Build whatever data structures you care about. Register recipes,
- * send {@link FMLInterModComms} messages to other mods.</li>
- * <li> {@link FMLPostInitializationEvent} : Handle interaction with other mods, complete your setup based on this.</li>
- * </ul>
- * <p>These are the server lifecycle events. They are fired whenever a server is running, or about to run. Each time a server
- * starts they will be fired in this sequence.
- * <ul>
- * <li> {@link FMLServerAboutToStartEvent} : Use if you need to handle something before the server has even been created.</li>
- * <li> {@link FMLServerStartingEvent} : Do stuff you need to do to set up the server. register commands, tweak the server.</li>
- * <li> {@link FMLServerStartedEvent} : Do what you need to with the running server.</li>
- * <li> {@link FMLServerStoppingEvent} : Do what you need to before the server has started it's shutdown sequence.</li>
- * <li> {@link FMLServerStoppedEvent} : Do whatever cleanup you need once the server has shutdown. Generally only useful
- * on the integrated server.</li>
- * </ul>
- * The second set of events are more specialized, for receiving notification of specific
- * information.
- * <ul>
- * <li> {@link FMLFingerprintViolationEvent} : Sent just before {@link FMLPreInitializationEvent}
- * if something is wrong with your mod signature</li>
- * <li> {@link IMCEvent} : Sent just after {@link FMLInitializationEvent} if you have IMC messages waiting
- * from other mods</li>
- * </ul>
- *
- * @author cpw
- *
- */
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement