User Tools

Site Tools


zh_cn:tutorial:accesswideners

This is an old revision of the document!


Access Wideners

Access wideners(访问加宽)提供了一种放宽类、方法、字段访问权限的方式。Access wideners 类似于著名的 Access Transformers.

Access wideners 应该仅在使用 Mixin 无法实现时使用。目前只有以下两种情景 Mixin 无法做到:

  • 需要访问一个(包内)的私有类,尤其是为了 @Shadow 修饰或在 Mixin 类内访问一个字段或方法。
  • 需要重写使用 final 修饰的子类。
    • 在你考虑重写使用 final 修饰的方法之前,使用注入器等方法修改。
    • 如果你想为一个包含私有构造方法的类创建子类,wideners 是个不错的选择

为了让 access widener 造成的的变化显示在反编译的源代码中,请运行 genSources gradle任务。

需求

  • Fabric-loader 0.8.0 或更高
  • Loom 0.2.7 或更高

文件格式

一个特定的文件格式被用来定义你的mod中包含的访问变化。为了帮助IDE,你应该使用“.accesswidener “的文件扩展名。

需要在你的模组里包含一个声明修改访问权限的特殊文件,为了方便,你应该在IDE中使用 .accesswidener 文件扩展

该文件首行应该遵循下述格式,命名空间 应该与你模组源码使用混淆命名空间匹配,通常是 named。 Loom 将会为你重新映射 access widener 文件,使其更改为 intermediary。如果你使用自定义的 RemapJarTask,设置 remapAccessWidenertrue 来确保上述行为发生。

accessWidener	v1	<命名空间>

再次提醒,命名空间应该在你使用 yarn 命名时设置为 named, 使用中间名命名时设置为 intermediary

Access widener 文件允许使用空行和和以 # 开头的注释。

# 这样的注释是支持的,当它被放在行尾也是这样的

任何空白字符都作为 access widener 文件的分隔符,推荐制表符 tab。

类名应该使用 / 分隔,而不是 .

对于内部类,你应该使用 $ 来代替 /

类的访问权限可以通过指定 访问权 和 类的名称 来改变,就像第一行定义命名空间那样。

<访问权>   class   <类名>
  1. 访问权可以是 accessibleextendable
方法

可以通过指定访问权限、类名称、方法名称和方法描述符来改变方法访问权,就像上面定义的映射命名空间一样。

<access>   method   <className>   <methodName>   <methodDesc>
  1. access can be accessible or extendable
  2. classname 是拥有者类
  3. methodName 是方法名称
  4. methodDesc 是方法描述符
字段

Field access can be changed by specifying the access, class name, field name and field descriptor as named the mappings namespace defined in the header.

<access>   field   <className>   <fieldName>   <fieldDesc>
  1. access can be accessible or mutable
  2. className is the owner class
  3. fieldName is the field name
  4. fieldDesc is the field descriptor

Access Changes

Extendable

Extendable should be used where you want to extend a class or override a method.

  • Classes are made public and final is removed
  • Methods are made protected and final is removed

Making a method extendable also makes the class extendable.

Accessible

Accessible should be used when you want to access a class, field or method from another class.

  • Classes are made public
  • Methods are made public and final if private
  • Fields are made public

Making a method or field accessible also makes the class accessible.

Mutable

Mutable should be used when you want to mutate a final field

  • Fields have final removed

Specifying file location

The access widener file location must be specified in your build.gradle and in your fabric.mod.json file. It should be stored in the resources as it needs to be included in the exported jar file.

Loom 0.9 or higher:

  1. loom {
  2. accessWidenerPath = file("src/main/resources/modid.accesswidener")
  3. }

Loom 0.8 or lower:

  1. loom {
  2. accessWidener = file("src/main/resources/modid.accesswidener")
  3. }

fabric.mod.json:

  1. ...
  2.  
  3. "accessWidener" : "modid.accesswidener",
  4.  
  5. ...
zh_cn/tutorial/accesswideners.1633742635.txt.gz · Last modified: 2021/10/09 01:23 by solidblock