提供: Minecraft Modding Wiki
この編集を取り消せます。
下記の差分を確認して、本当に取り消していいか検証してください。よろしければ変更を保存して取り消しを完了してください。
最新版 | 編集中の文章 | ||
12行目: | 12行目: | ||
===仕組み=== | ===仕組み=== | ||
− | FMLでは、Minecraftが起動した直後に処理を割り込ませ、現在のスレッドの ContextClassLoader | + | FMLでは、Minecraftが起動した直後に処理を割り込ませ、現在のスレッドの ContextClassLoader に、cpw.mods.fml.relauncher.RelaunchClassLoader を設定しています。このクラスローダーでは、クラスロード時にバイトコードのクラスを編集するポイントが設けられており、Minecraftが実行中に読み込む殆どのクラス(※)を、ロード時に動的に改変する事が可能となっています。 |
※全てのクラスの変換が行えるわけではありません。IClassTransformer を通さないよう、変換対象から除外登録されている一部のパッケージ以下のクラスは、本来のクラスローダーであるシステムクラスローダーによりロードされてしまうため、動的な変換処理を行えません。またプラグイン側で TransformerExclusions アノテーションを用い除外設定されているパッケージやクラスも変換対象外となります。 | ※全てのクラスの変換が行えるわけではありません。IClassTransformer を通さないよう、変換対象から除外登録されている一部のパッケージ以下のクラスは、本来のクラスローダーであるシステムクラスローダーによりロードされてしまうため、動的な変換処理を行えません。またプラグイン側で TransformerExclusions アノテーションを用い除外設定されているパッケージやクラスも変換対象外となります。 | ||
55行目: | 55行目: | ||
ソースコメントを参照し、必要な実装を施して書き換えましょう。 | ソースコメントを参照し、必要な実装を施して書き換えましょう。 | ||
− | === | + | ===TutorialTransformerクラス作成=== |
− | # 任意の名前のクラス( | + | # 任意の名前のクラス(ここではTutorialTransformer)を作成します。 |
− | # | + | # IClassTransformer インタフェースを実装します。 |
− | |||
− | |||
− | |||
− | |||
<source lang="java"> | <source lang="java"> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
package tutorial.asm; | package tutorial.asm; | ||
− | import java.io. | + | import java.io.IOException; |
− | import java.util. | + | import java.io.InputStream; |
+ | import java.util.List; | ||
+ | import java.util.zip.ZipEntry; | ||
+ | import java.util.zip.ZipFile; | ||
+ | |||
+ | import org.objectweb.asm.*; | ||
+ | import org.objectweb.asm.tree.*; | ||
+ | import static org.objectweb.asm.Opcodes.*; | ||
+ | import static org.objectweb.asm.tree.AbstractInsnNode.*; | ||
− | import | + | import cpw.mods.fml.relauncher.FMLRelauncher; |
− | import | + | import cpw.mods.fml.relauncher.IClassTransformer; |
− | // | + | // Opcodes : インプリメントすると、ASMによるバイトコード定数にアクセスするのに便利です。 |
− | // | + | // 必須ではありません。不用な場合は implements から削除してください。 |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | public class TutorialTransformer implements IClassTransformer, Opcodes | |
− | public class | ||
{ | { | ||
− | // | + | // 改変対象のクラスの完全修飾名です。 |
− | // | + | // 後述でMinecraft.jar内の難読化されるファイルを対象とする場合の簡易な取得方法を紹介します。 |
+ | private static final String TARGET_CLASS_NAME = "net.minecraft.src.TargetClass"; | ||
+ | |||
+ | // クラスがロードされる際に呼び出されるメソッドです。 | ||
+ | @Override | ||
+ | public byte[] transform(String name, byte[] bytes) | ||
+ | { | ||
+ | // FMLRelauncher.side() : Client/Server どちらか一方のを対象とする場合や、 | ||
+ | // 一つのMODで Client/Sever 両方に対応したMODで、この値を判定して処理を変える事ができます。 | ||
+ | // 今回は"CLIENT"と比較し、Client側のファイルを対象としている例です。 | ||
+ | // Client側専用のMODとして公開するのであれば、判定は必須ではありません。 | ||
+ | |||
+ | // name : 現在ロードされようとしているクラス名が格納されています。 | ||
+ | if (!FMLRelauncher.side().equals("CLIENT") || !name.equals(TARGET_CLASS_NAME)) | ||
+ | { | ||
+ | // 処理対象外なので何もしない | ||
+ | return bytes; | ||
+ | } | ||
− | + | try | |
+ | { | ||
+ | // -------------------------------------------------------------- | ||
+ | // クラスファイル丸ごと差し替える場合 | ||
+ | // -------------------------------------------------------------- | ||
+ | // return replaceClass(bytes); | ||
− | + | // -------------------------------------------------------------- | |
− | + | // ASMを使用し、既存のクラスファイルに改変を施す場合。 | |
− | + | // -------------------------------------------------------------- | |
+ | // return hookDoRenderLivingMethod(bytes); | ||
− | + | } | |
− | + | catch (Exception e) | |
+ | { | ||
+ | throw new RuntimeException("failed : TutorialTransformer loading", e); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // 下記の想定で実装されています。 | ||
+ | // 対象クラスの bytes を ModifiedTargetClass.class ファイルに置き換える | ||
+ | private byte[] replaceClass(byte[] bytes) throws IOException | ||
{ | { | ||
− | + | ZipFile zf = null; | |
− | + | InputStream zi = null; | |
+ | |||
+ | try | ||
+ | { | ||
+ | zf = new ZipFile(TutorialCorePlugin.location); | ||
+ | |||
+ | // 差し替え後のファイルです。coremodのjar内のパスを指定します。 | ||
+ | ZipEntry ze = zf.getEntry("ModifiedTargetClass.class"); | ||
+ | |||
+ | if (ze != null) | ||
+ | { | ||
+ | zi = zf.getInputStream(ze); | ||
+ | int len = (int) ze.getSize(); | ||
+ | bytes = new byte[len]; | ||
− | + | // ヒープサイズを超えないように、ストリームからファイルを1024ずつ読み込んで bytes に格納する | |
− | + | int MAX_READ = 1024; | |
+ | int readed = 0, readsize, ret; | ||
+ | while(readed < len) { | ||
+ | readsize = MAX_READ; | ||
+ | if (len - readed < MAX_READ ) { | ||
+ | readsize = len - readed; | ||
+ | } | ||
+ | ret = zi.read(bytes, readed, readsize); | ||
+ | if (ret == -1) break; | ||
+ | readed += ret; | ||
+ | } | ||
+ | } | ||
− | + | return bytes; | |
− | + | } | |
− | + | finally | |
− | + | { | |
+ | if (zi != null) | ||
+ | { | ||
+ | zi.close(); | ||
+ | } | ||
+ | |||
+ | if (zf != null) | ||
+ | { | ||
+ | zf.close(); | ||
+ | } | ||
+ | } | ||
} | } | ||
− | // | + | // 下記の想定で実装されています。 |
− | + | // EntityLiving.class の doRenderLiving の先頭に | |
− | + | // tutorial/test.class の passTestRender(EntityLiving, double, double, double)メソッドの呼び出しを追加する。 | |
− | + | private byte[] hookDoRenderLivingMethod(byte[] bytes) | |
{ | { | ||
− | + | // ASMで、bytesに格納されたクラスファイルを解析します。 | |
− | + | ClassNode cnode = new ClassNode(); | |
+ | ClassReader reader = new ClassReader(bytes); | ||
+ | reader.accept(cnode, 0); | ||
− | + | // 改変対象のメソッド名です | |
− | + | String targetMethodName = "doRenderLiving"; | |
− | + | // 改変対象メソッドの戻り値型および、引数型をあらわします | |
− | + | String targetMethoddesc = "(Lnet/minecraft/entity/EntityLiving;DDDFF)V"; | |
− | |||
− | |||
− | |||
− | + | // 対象のメソッドを検索取得します。 | |
− | + | MethodNode mnode = null; | |
− | + | for (MethodNode curMnode : (List<MethodNode>) cnode.methods) | |
− | + | { | |
− | + | if (targetMethodName.equals(curMnode.name) && targetMethoddesc.equals(curMnode.desc)) | |
− | + | { | |
− | + | mnode = curMnode; | |
− | + | break; | |
+ | } | ||
+ | } | ||
− | + | if (mnode != null) | |
− | |||
− | |||
− | if ( | ||
{ | { | ||
− | + | InsnList overrideList = new InsnList(); | |
+ | |||
+ | // メソッドコールを、バイトコードであらわした例です。 | ||
+ | overrideList.add(new VarInsnNode(ALOAD, 1)); | ||
+ | overrideList.add(new VarInsnNode(DLOAD, 2)); | ||
+ | overrideList.add(new VarInsnNode(DLOAD, 4)); | ||
+ | overrideList.add(new VarInsnNode(DLOAD, 6)); | ||
+ | overrideList | ||
+ | .add(new MethodInsnNode(INVOKESTATIC, "tutorial/test", "passTestRender", "(LEntityLiving;DDD)V")); | ||
+ | |||
+ | // mnode.instructions.get(1)で、対象のメソッドの先頭を取得 | ||
+ | // mnode.instructions.insertで、指定した位置にバイトコードを挿入します。 | ||
+ | mnode.instructions.insert(mnode.instructions.get(1), overrideList); | ||
+ | |||
+ | // 改変したクラスファイルをバイト列に書き出します | ||
+ | ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); | ||
+ | cnode.accept(cw); | ||
+ | bytes = cw.toByteArray(); | ||
} | } | ||
+ | |||
+ | return bytes; | ||
} | } | ||
− | + | } | |
</source> | </source> | ||
172行目: | 245行目: | ||
import com.google.common.eventbus.Subscribe; | import com.google.common.eventbus.Subscribe; | ||
− | import | + | import cpw.mods.fml.common.DummyModContainer; |
− | import | + | import cpw.mods.fml.common.LoadController; |
− | import | + | import cpw.mods.fml.common.ModMetadata; |
− | import | + | import cpw.mods.fml.common.event.FMLInitializationEvent; |
− | import | + | import cpw.mods.fml.common.versioning.ArtifactVersion; |
// 必ずしも DummyModContainer を継承している必要はありません。 | // 必ずしも DummyModContainer を継承している必要はありません。 | ||
− | // | + | // cpw.mods.fml.common.ModContainer さえ実装していれば、どんなクラスでも構いません。 |
public class TutorialModContainer extends DummyModContainer | public class TutorialModContainer extends DummyModContainer | ||
222行目: | 295行目: | ||
import java.util.zip.ZipFile; | import java.util.zip.ZipFile; | ||
+ | import org.objectweb.asm.*; | ||
+ | import org.objectweb.asm.tree.*; | ||
+ | import static org.objectweb.asm.Opcodes.*; | ||
+ | import static org.objectweb.asm.tree.AbstractInsnNode.*; | ||
+ | |||
+ | import cpw.mods.fml.relauncher.FMLRelauncher; | ||
+ | import cpw.mods.fml.relauncher.IClassTransformer; | ||
− | + | // Opcodes : インプリメントすると、ASMによるバイトコード定数にアクセスするのに便利です。 | |
− | + | // 必須ではありません。不用な場合は implements から削除してください。 | |
− | public class TutorialTransformer implements IClassTransformer | + | public class TutorialTransformer implements IClassTransformer, Opcodes |
{ | { | ||
// 改変対象のクラスの完全修飾名です。 | // 改変対象のクラスの完全修飾名です。 | ||
233行目: | 313行目: | ||
// クラスがロードされる際に呼び出されるメソッドです。 | // クラスがロードされる際に呼び出されるメソッドです。 | ||
− | public byte[] transform( | + | @Override |
− | + | public byte[] transform(String name, byte[] bytes) | |
+ | { | ||
// FMLRelauncher.side() : Client/Server どちらか一方のを対象とする場合や、 | // FMLRelauncher.side() : Client/Server どちらか一方のを対象とする場合や、 | ||
// 一つのMODで Client/Sever 両方に対応したMODで、この値を判定して処理を変える事ができます。 | // 一つのMODで Client/Sever 両方に対応したMODで、この値を判定して処理を変える事ができます。 | ||
241行目: | 322行目: | ||
// name : 現在ロードされようとしているクラス名が格納されています。 | // name : 現在ロードされようとしているクラス名が格納されています。 | ||
− | if ( | + | if (!FMLRelauncher.side().equals("CLIENT") || !name.equals(TARGET_CLASS_NAME)) |
{ | { | ||
// 処理対象外なので何もしない | // 処理対象外なので何もしない | ||
249行目: | 330行目: | ||
try | try | ||
{ | { | ||
− | // | + | // -------------------------------------------------------------- |
− | return | + | // クラスファイル丸ごと差し替える場合 |
+ | // -------------------------------------------------------------- | ||
+ | // return replaceClass(bytes); | ||
+ | |||
+ | // -------------------------------------------------------------- | ||
+ | // ASMを使用し、既存のクラスファイルに改変を施す場合。 | ||
+ | // -------------------------------------------------------------- | ||
+ | // return hookDoRenderLivingMethod(bytes); | ||
+ | |||
} | } | ||
catch (Exception e) | catch (Exception e) | ||
256行目: | 345行目: | ||
throw new RuntimeException("failed : TutorialTransformer loading", e); | throw new RuntimeException("failed : TutorialTransformer loading", e); | ||
} | } | ||
+ | } | ||
+ | |||
+ | // 下記の想定で実装されています。 | ||
+ | // 対象クラスの bytes を ModifiedTargetClass.class ファイルに置き換える | ||
+ | private byte[] replaceClass(byte[] bytes) throws IOException | ||
+ | { | ||
+ | ZipFile zf = null; | ||
+ | InputStream zi = null; | ||
+ | |||
+ | try | ||
+ | { | ||
+ | zf = new ZipFile(TutorialCorePlugin.location); | ||
+ | |||
+ | // 差し替え後のファイルです。coremodのjar内のパスを指定します。 | ||
+ | ZipEntry ze = zf.getEntry("ModifiedTargetClass.class"); | ||
+ | |||
+ | if (ze != null) | ||
+ | { | ||
+ | zi = zf.getInputStream(ze); | ||
+ | bytes = new byte[(int) ze.getSize()]; | ||
+ | zi.read(bytes); | ||
+ | } | ||
+ | |||
+ | return bytes; | ||
+ | } | ||
+ | finally | ||
+ | { | ||
+ | if (zi != null) | ||
+ | { | ||
+ | zi.close(); | ||
+ | } | ||
+ | |||
+ | if (zf != null) | ||
+ | { | ||
+ | zf.close(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // 下記の想定で実装されています。 | ||
+ | // EntityLiving.class の doRenderLiving の先頭に | ||
+ | // tutorial/test.class の passTestRender(EntityLiving, double, double, double)メソッドの呼び出しを追加する。 | ||
+ | private byte[] hookDoRenderLivingMethod(byte[] bytes) | ||
+ | { | ||
+ | // ASMで、bytesに格納されたクラスファイルを解析します。 | ||
+ | ClassNode cnode = new ClassNode(); | ||
+ | ClassReader reader = new ClassReader(bytes); | ||
+ | reader.accept(cnode, 0); | ||
+ | |||
+ | // 改変対象のメソッド名です | ||
+ | String targetMethodName = "doRenderLiving"; | ||
+ | |||
+ | // 改変対象メソッドの戻り値型および、引数型をあらわします | ||
+ | String targetMethoddesc = "(Lnet/minecraft/entity/EntityLiving;DDDFF)V"; | ||
+ | |||
+ | // 対象のメソッドを検索取得します。 | ||
+ | MethodNode mnode = null; | ||
+ | for (MethodNode curMnode : (List<MethodNode>) cnode.methods) | ||
+ | { | ||
+ | if (targetMethodName.equals(curMnode.name) && targetMethoddesc.equals(curMnode.desc)) | ||
+ | { | ||
+ | mnode = curMnode; | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if (mnode != null) | ||
+ | { | ||
+ | InsnList overrideList = new InsnList(); | ||
+ | |||
+ | // メソッドコールを、バイトコードであらわした例です。 | ||
+ | overrideList.add(new VarInsnNode(ALOAD, 1)); | ||
+ | overrideList.add(new VarInsnNode(DLOAD, 2)); | ||
+ | overrideList.add(new VarInsnNode(DLOAD, 4)); | ||
+ | overrideList.add(new VarInsnNode(DLOAD, 6)); | ||
+ | overrideList | ||
+ | .add(new MethodInsnNode(INVOKESTATIC, "tutorial/test", "passTestRender", "(LEntityLiving;DDD)V")); | ||
+ | |||
+ | // mnode.instructions.get(1)で、対象のメソッドの先頭を取得 | ||
+ | // mnode.instructions.insertで、指定した位置にバイトコードを挿入します。 | ||
+ | mnode.instructions.insert(mnode.instructions.get(1), overrideList); | ||
+ | |||
+ | // 改変したクラスファイルをバイト列に書き出します | ||
+ | ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); | ||
+ | cnode.accept(cw); | ||
+ | bytes = cw.toByteArray(); | ||
+ | } | ||
+ | |||
+ | return bytes; | ||
} | } | ||
} | } | ||
</source> | </source> | ||
− | |||
− | |||
===META-INF/MANIFEST.MFファイルの作成=== | ===META-INF/MANIFEST.MFファイルの作成=== | ||
272行目: | 448行目: | ||
上記の場合、jarに通常のFML(@Mod)クラスやmcmod.infoを含めてもロードされません。 | 上記の場合、jarに通常のFML(@Mod)クラスやmcmod.infoを含めてもロードされません。 | ||
− | + | 通常のFMLクラスを含めたい場合はFMLCorePluginContainsFMLMod節を追加し、FMLにロードさせたい @Mod.Instanceのある 完全修飾クラス名を指定します。 | |
− | + | ただし、Transformerクラス(このチュートリアルではasm)等とはpackageを別にする必要があります。 | |
<source lang="java"> | <source lang="java"> | ||
− | FMLCorePluginContainsFMLMod: | + | FMLCorePluginContainsFMLMod: tutorial.fml.TutorialFMLMod |
</source> | </source> | ||
291行目: | 467行目: | ||
**** TutorialModContainer.class | **** TutorialModContainer.class | ||
**** TutorialTransformer.class | **** TutorialTransformer.class | ||
− | ** ModifiedTargetClass.class (丸ごと差し替える場合に用いるクラス | + | ** ModifiedTargetClass.class (丸ごと差し替える場合に用いるクラス) |
通常の mod のように、zip で圧縮し、拡張子を .zip から .jar に変更すれば完成です。 | 通常の mod のように、zip で圧縮し、拡張子を .zip から .jar に変更すれば完成です。 | ||
※jarコマンドを用いて作成することも可能ですが、今回は割愛します。 | ※jarコマンドを用いて作成することも可能ですが、今回は割愛します。 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==テスト、デバッグ方法について== | ==テスト、デバッグ方法について== | ||
381行目: | 543行目: | ||
別な処理に置き変えるなど、Javaでできることはほぼなんでも出来ますが<br /> | 別な処理に置き変えるなど、Javaでできることはほぼなんでも出来ますが<br /> | ||
ASMライブラリの使用方法を別途調べる必要があります。(記述時点で、日本語解説はあまり多くありません。) | ASMライブラリの使用方法を別途調べる必要があります。(記述時点で、日本語解説はあまり多くありません。) | ||
− | |||
ASMライブラリの既知の不具合 | ASMライブラリの既知の不具合 | ||
・関数外のstaticフィールドの初期化を行うコードがあるとClassWriterがNullPointerExceptionを引き起こします。 | ・関数外のstaticフィールドの初期化を行うコードがあるとClassWriterがNullPointerExceptionを引き起こします。 | ||
− | |||
− | |||
<comments /> | <comments /> |