Skip to content

Commit

Permalink
更新文档
Browse files Browse the repository at this point in the history
  • Loading branch information
pirunxi committed Mar 8, 2024
1 parent e786300 commit 5acc0f1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
15 changes: 9 additions & 6 deletions docs/business/reload/hotreloadassembly.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@

- 支持卸载assembly,卸载100%的assembly所占用的内存
- 支持重新加载assembly,代码可以任意变化甚至完全不同(MonoBehaviour和Scriptable有一定的限制)

## 待实现,暂未支持的特性

- 支持**限定热更新assembly中能访问的函数的集合**,适合UGC游戏中创建沙盒环境,避免恶意玩家代码造成破坏。

## 不支持特性及特殊要求

- 要求业务代码不会再使用被卸载的Assembly中的对象或者函数,并且退出所有在执行的旧逻辑
- 要求重载的MonoBehaviour中的事件或消息函数如Awake、OnEable之类不发生增删(但函数体可以变化)
- 要求重载后在旧Assembly中存在同名类的MonoBehaviour类的序列化字段名不发生变化(类型可以变)
- 不能直接卸载被依赖的Assembly,必须按照逆依赖顺序先卸载依赖者,再卸载被依赖者。例如A.dll依赖B.dll,则需要先卸载A.dll,再卸载B.dll
- 由于Unity自身实现的原因,与2022的Jobs不兼容,需要自行小幅修改UnityEngine.CoreModule.dll的代码。 2020-2021仍然可以正常工作
- MonoBehaviour和ScriptableObject相关
- 要求重载的MonoBehaviour中的事件或消息函数如Awake、OnEable之类不发生增删(但函数体可以变化)
- 要求重载后在旧Assembly中存在同名脚本类的序列化字段名不发生变化(类型可以变)
- 不能继承泛型类型,例如`class MyScript : CommonScript<int>`
- 一些会缓存反射信息的库(这种行为在序列化相关的库中最为普遍,如LitJson),在热重载后需要清理掉缓存的反射信息
- 不支持析构函数,~XXX()。也不允许实例化泛型参数带本程序集类型的带析构函数的泛型类
- 与dots不兼容。由于dots大量缓存的类型信息,实现复杂,很难单独清理掉缓存信息。


## 一些不兼容的库

- 2022的Jobs会缓存类型相关信息,需要自行小幅[修改UnityEngine.CoreModule.dll](./modifydll.md)的代码。 低于2022的版本不需要修改
- LitJson之类的反序列化库会缓存反射信息,需要在热重载后清理掉库中缓存的反射信息,具体操作跟库的实现相关


Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
# Hotreload Technology
# Hot reload technology

The hot reloading technology is used to completely unload or reload an assembly, which is suitable for small game collection type games. This solution only provides **commercial version**, please refer to [Introduction to Hot Reload Version](./intro.md) for details.
Hot reload technology is used to completely uninstall or reload an assembly, which is suitable for small game collection type games. This program only provides **commercial version**.

## Supported features

- Support unloading assembly
- Support reloading assembly, the code can be changed arbitrarily or even completely different (except MonoBehaviour class)
- Unload 100% of the metadata memory
- Supports uninstalling assembly and uninstalling 100% of the memory occupied by assembly
- Supports reloading the assembly, the code can be changed arbitrarily or even completely different (MonoBehaviour and Scriptable have certain restrictions)
- Supports **limited set of functions that can be accessed in the hot update assembly**, which is suitable for creating a sandbox environment in UGC games to avoid damage caused by malicious player code.

## Does not support features and special requirements

## To be implemented, currently unsupported features
- Require business code to no longer use objects or functions in the uninstalled Assembly, and exit all old logic being executed
- The dependent Assembly cannot be uninstalled directly. The dependent Assembly must be uninstalled first, and then the dependent Assembly must be uninstalled in reverse dependency order. For example, if A.dll depends on B.dll, you need to uninstall A.dll first, and then uninstall B.dll.
- MonoBehaviour is related to ScriptableObject
- It is required that the event or message functions in the overloaded MonoBehaviour, such as Awake and OnEable, are not added or deleted (but the function body can be changed)
- It is required that the serialized field name of the script class with the same name in the old Assembly does not change after overloading (the type can be changed)
- Cannot inherit from generic types, such as `class MyScript : CommonScript<int>`
- Some libraries that cache reflection information (this behavior is most common in serialization-related libraries, such as LitJson) need to clear the cached reflection information after hot reloading.
- Destructor, ~XXX() is not supported. It is also not allowed to instantiate a generic class with a destructor that takes a generic parameter of this assembly type.
- Not compatible with dots. Due to the large amount of type information cached by dots, the implementation is complex and it is difficult to clear the cache information alone.

- Most of the metadata can be released, but Il2CppClass and MethodInfo objects have not been released yet. The follow-up will be perfected soon.
- Support **Limit the collection of functions that can be accessed in the hot update assembly**, suitable for creating a sandbox environment in UGC games to avoid damage caused by malicious player code.

## does not support features and special requirements
## Some incompatible libraries

- Cannot unload itself in the code of the unloaded assembly.
- It is required that the business code will no longer use the objects or functions in the uninstalled Assembly, and exit all the old logic that is being executed
- It is required that after overloading, in the MonoBehaviour of the same name class in the old Assembly, the functions specially processed by the Unity engine, such as Awake, will not be added or deleted (but the function body can be changed)
- The dependent Assembly cannot be uninstalled directly, the dependent must be uninstalled first and then the dependent must be uninstalled according to the reverse dependency order. For example, if A.dll depends on B.dll, you need to uninstall A.dll first, and then uninstall B.dll
- Due to Unity's own implementation, it is not compatible with 2022 Jobs, and you need to slightly modify the code of UnityEngine.CoreModule.dll by yourself. 2020-2021 will still work normally
- Destructors, ~XXX() are not supported. It is also not allowed to instantiate a generic class with a destructor with a generic parameter of this assembly type
- Jobs in 2022 will cache type-related information, and you need to slightly [modify UnityEngine.CoreModule.dll](./modifydll.md) code yourself. Versions lower than 2022 do not need to be modified
- Deserialization libraries such as LitJson will cache reflection information. It is necessary to clean up the reflection information cached in the library after hot reloading. The specific operation is related to the implementation of the library.

0 comments on commit 5acc0f1

Please sign in to comment.